Link to home
Start Free TrialLog in
Avatar of naseeam
naseeamFlag for United States of America

asked on

How to convert Date to Unix format ?

What is Unix Date ?

Present date is stored in 16-bit modbus register as follows:

Bits 0 - 4       Day of Month
Bits 5 - 8       Month of Year
Bits 9 - 15     (Year  -  2000)  (0 to 128  ==  2000 to 2128 )


How to conver this date to unix format ?

Thank you!
Avatar of jkr
jkr
Flag of Germany image

You could use 'mktime()' (http://www.cplusplus.com/reference/ctime/mktime/) for that, e.g.

#include <time.h>

uint16_t regtime; // from modbus

struct tm t;

t.tm_mday = regtime & 0x1f;
t.tm_mon = (regtime >> 5) & 0x07 -1;
t.tm_year = (regtime >> 9) & 0x3f + 100; // UNIX date is from 1900

time_t ut = mktime(&t);

Open in new window

Avatar of naseeam

ASKER

Thanks for great and concise response.

For tm_mon, I believe bitwise and with 0x0f instead of 0x07.
For tm_year, I believe bitwise and with 0x7f instead of 0x3f.

In our system, time_t is declared as follows:
typedef    unsigned int    time_t;

time_t is only two bytes.  Is that sufficent to store unix time ?
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial