Solved

How to convert date to ISO 1806 format ?

Posted on 2014-02-06
3
533 Views
Last Modified: 2014-02-06
/* A 16-bit date is read over modbus.  Date fields are as follows:
Bits 0 - 4      Day of Month
Bits 5 - 8      Month of Year
Bits 9 - 15    Year   ( 0 to 128 == 2000 to 2128 )  */
unsigned short responseBuffer[128];

/* responseBuffer[0] contains the 16-bit date read over modbus */
dateFunc ( (char *) &responseBuffer[0], 2 );

Result  dateFunc (const char * val, unsigned short size)
{
    /* convert date read over modbus to ISO 8601 format */
   /* enter code here */


} 

Open in new window

0
Comment
Question by:naseeam
  • 2
3 Comments
 

Author Comment

by:naseeam
ID: 39839875
ISO 8601 date format is:   "2014-02-05"  or more generically "YYYY-MM-DD"
0
 
LVL 86

Expert Comment

by:jkr
ID: 39839935
You could do that using bitwise operators, e.g.

bool dateFunc (const char val, char* iso8601_buf, unsigned short size) // pass 'val' as a single byte
{
    /* convert date read over modbus to ISO 8601 format */
/* A 16-bit date is read over modbus.  Date fields are as follows:
Bits 0 - 4      Day of Month
Bits 5 - 8      Month of Year
Bits 9 - 15    Year   ( 0 to 128 == 2000 to 2128 )  */

  unsigned short usDay =   val & 0x0F; // isolate bits 0-4
  unsigned short usMon = (val >> 4) & 0x07; // isolate bits 5-8
  unsigned short usYear = (val >> 9) & 0x3F; // isolate bits 9-15 (bitwise AND is not strictly necessary

  usYear += 2000; // add offset

  if ( size < 11) return false; // buffer not large enough;

  sprintf(iso8601_buf,"%4.4d-%2.2d-%2.2d",usYear,usMon,usDay);

  return true;
} 

Open in new window

0
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 39839941
Ooops, correction (missed that it's two bytes):

bool dateFunc (const unsigned short val, char* iso8601_buf, unsigned short size) // pass 'val' as a single byte
{
    /* convert date read over modbus to ISO 8601 format */
/* A 16-bit date is read over modbus.  Date fields are as follows:
Bits 0 - 4      Day of Month
Bits 5 - 8      Month of Year
Bits 9 - 15    Year   ( 0 to 128 == 2000 to 2128 )  */

  unsigned short usDay =   val & 0x0F; // isolate bits 0-4
  unsigned short usMon = (val >> 4) & 0x07; // isolate bits 5-8
  unsigned short usYear = (val >> 9) & 0x3F; // isolate bits 9-15 (bitwise AND is not strictly necessary

  usYear += 2000; // add offset

  if ( size < 11) return false; // buffer not large enough;

  sprintf(iso8601_buf,"%4.4d-%2.2d-%2.2d",usYear,usMon,usDay);

  return true;
} 
                          
unsigned short responseBuffer[128];

/* responseBuffer[0] contains the 16-bit date read over modbus */
char dateBuf[12];
dateFunc ( responseBuffer[0], dateBuf, sizeof(dateBuf) );

Open in new window

0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now