0

I am writing a C program on Linux with CAN functionality. I need to get the absolute time since midnight Jan. 1, 1984, in the following TIME Object format. What function should I use?

Code

typedef struct
{
    UNSIGNED32     ms; // upper 4 bits are reserved
    UNSIGNED16     days;
} TIME_OF_DAY;
Lundin
  • 195,001
  • 40
  • 254
  • 396
akashagrawal
  • 3
  • 1
  • 7
  • I'm surprised I could not find a C duplicate... Here's something similar in Python: [Conversion from UNIX time to timestamp starting in January 1, 2000](https://stackoverflow.com/q/35763357/608639). – jww Aug 03 '19 at 18:17

1 Answers1

2

The usual Unix/Linux time epoch is 1 January 1970, so you can use any of the usual functions, most likely clock_gettime() with CLOCK_REALTIME. Then simply subtract the amount of time between 1970 and 1984 (a constant you can embed in your code).

pmg
  • 106,608
  • 13
  • 126
  • 198
John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • `#define days 5113`, It is calculated [with](https://planetcalc.com/273/) – EsmaeelE Aug 03 '19 at 13:03
  • Thanks @john-zwinck for the help. This works for me. However, since the conversion from seconds to milliseconds is not so straightforward using the timespec struct, I am facing difficulty in subtracting milliseconds difference between two dates. – akashagrawal Aug 03 '19 at 14:02
  • 1
    @akashagrawal I think something along: `struct timespec s;` `TIME_OF_DAY v = { .days = s.tv_sec / (60ull * 60 * 24) - 5113, .ms = s.tv_sec % (60ull * 60 * 24) * 1000 + s.tv_nsec / (1000ull * 1000ull) }` ? – KamilCuk Aug 03 '19 at 14:27