On the two systems I've tested (a 32-bit Ubuntu 12.04 server and a 64-bit Ubuntu 13.10 VM), the seconds since the epoch given by time() may differ from gettimeofday()'s.
Specifically, though I call time()
after calling gettimeofday()
, the value returned by time()
is sometimes less than the tv_sec
value returned by gettimeofday()
.
This apparently occurs just after the clock rolls over to a new second.
This caused bugs in some of my code that expected time()'s and gettimeofday()'s seconds to be interchangeable.
Sample code demonstrating this problem:
#include <stdio.h>
#include <time.h>
#include <sys/time.h>
int main()
{
time_t start = time(NULL);
int same = 0;
int different = 0;
int max_usec = 0;
while (1) {
time_t t;
struct timeval tv;
gettimeofday(&tv, NULL);
t = time(NULL);
if (t < tv.tv_sec) {
different++;
if (tv.tv_usec > max_usec) {
max_usec = tv.tv_usec;
}
} else {
same++;
}
if (t > start + 5) {
break;
}
}
printf("Same: %i\n", same);
printf("Different: %i\n", different);
printf("Largest difference seen at %i\n", max_usec);
}
Note that I'm calling time() second and only complaining if its value is less than gettimeofday()'s.
Sample output:
Same: 33282836
Different: 177076
Largest difference seen at 5844
I.e., the two values were the same 33 million times, they were different 177k times, and they were always different within 5844 microseconds of a new second.
Is this a known issue? What causes this?
time()
value is fairly consistently updated between 7000 and 7003 microseconds after thegettimeofday()
value is updated. On my x86_64 Debian system (running an older kernel), it shows no inconsistencies. – Transpose