If the struct timeval
fields are in long
integer format (which can be or cannot) in 32bit format, is because you need it to be that size. Let's do some analysis: I took this timestamp in NTP format, and converted it to struct timeval
format (for a now timestamp) on my system:
ntpts.c:120:process: NTP(dec): 3771731346.612550000
ntpts.c:121:process: NTP(hex): 0xe0d00d92.9cd013a9
ntpts.c:123:process: UNIX: 1562742546/0x5d258f12
ntpts.c:135:process: gmtime: 10/jul/2019, 07:09:06.612550000
ntpts.c:136:process: localtime: 10/jul/2019, 10:09:06.612550000
tv_sec ==> 1562742546
tv_usec ==> 612550
so to get a single timestamp with that resolution (number of seconds since 00:00h jan/1/1970, and microseconds) you need at least 52 bits (well, normally tv_sec
is a long
or long
long
64bit field, because of the overflow that will happen in feb, 2038, and some systems have already patched this) and the tv_usec
could be fit in a 24bit variable (usec go from 0 to 999999, so they need at least 20bit), but compiler padding would make it 32bit size anyway, at a minimum.
In case you want to conserve full usec resolution with 32bit, you'll need at least 20 bits to represent the fraction of a second and you'll have only 12 bit to represent the integer part. In that case, you'll be able to express only seconds from 0 to 4095, which is some more than one hour (01h8m16s). This is possible to handle, but to make that timestamp absolute, you'll need to reference which absolute hour in history this timestamp belongs to.
On the other side, if you reduce your granularity to msec, you can get more than 1000 hours of reference window, you need to consider this to finally decide what to do next.
AMENDMENT
By the way, the gettimeofday(2)
system call is actually deprecated. Nearly all systems in use today use the clock_gettime(2)
system call, which has nanosecond resolution, and allows you to select the proper system clock to get the timestamp.