Solved

How to convert date to ISO 1806 format ?

Posted on 2014-02-06
3
569 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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 goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

705 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