Solved

How to convert date to ISO 1806 format ?

Posted on 2014-02-06
3
554 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
 
LVL 1

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

685 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