Background: I have an old Siemens cpu that will send me a total of 8 bytes as the date_and_time format. This format is constructed so the 4 first byte contains the time and the last 4 bytes contains the date since 1 january 1992.
Problem: I started to extract the last 4 dates to calculate the day by using
long long int dt = 0x60592102FC29000;
char a, b, c, d;
a = (dt & 0xFF);
b = ((dt >> 8) & 0xFF);
c = ((dt >> 16) & 0xFF);
d = ((dt >> 24) & 0xFF);
long int date = int((unsigned char)a << 24 |
(unsigned char)b << 16 |
(unsigned char)c << 8 |
(unsigned char)d);
But after this part i got stuck because i cant figure out any nice way to calculate the day without looping trough all days.
Examples:
0x60592102FC29000 is equal to 2021-06-04 09:55:40.000
0x7A0DC6021C07000 is equal to 1996-12-24 12:55:34.010
References:
T#0d_0h_0m_0s_0ms to T#49d_17h_2m_47s_295ms Maximum of two digits for the values day, hour, minute, second and a maximum of three digits for milliseconds Initialization with T#0d_0h_0m_0s_0ms
D#1992-01-01 to D#2200-12-31 Leap years are taken into account, year has four digits, month and day are two digits each Initialization with D#0001-01-01
Link to siemens way of calculating
My final solutions inspired by posts below:
//1996-12-24 12:55:34.010
long long int dt = 0x7A0DC6021C070000;
const int dateOffset = 8034;
time_t date_t = 0;
int date =
((dt & 0xFF) << 24) |
(((dt >> 8) & 0xFF) << 16) |
(((dt >> 16) & 0xFF) << 8) |
(((dt >> 24) & 0xFF));
int time =
((dt >> 32 & 0xFF) << 24) |
(((dt >> 40) & 0xFF) << 16) |
(((dt >> 48) & 0xFF) << 8) |
(((dt >> 56) & 0xFF));
tm _dt = *localtime(&date_t);
_dt.tm_mday += dateOffset + date;
_dt.tm_sec += time/1000;
mktime(&_dt);