Solved

How to convert date to ISO 1806 format ?

Posted on 2014-02-06
3
550 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Focus not getting shifted out of  editbox 2 66
basic hardware to learn oop advanced design patterns 3 101
Indy 10 not Receiving UDP broadcast 3 22
C++ Code Issue 4 25
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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 technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

829 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