gmtime() oddities for large values
Edwin Groothuis
edwin at mavetju.org
Wed May 12 03:54:50 UTC 2010
Hello!
With my hat of the tzdata/tzcode maintainer on the FreeBSD system, I sometimes
get the more interesting bugs reported.
For example this piece of code, on 64 bit machines it returns the same values
for values bigger than the initial one:
#include <errno.h>
#include <stdint.h>
#include <stdio.h>
#include <time.h>
void t_inspect(time_t t)
{
struct tm *tp;
errno = 0;
tp = gmtime(&t);
printf("t: %ld, tp: %p errno: %d\n", t, tp, errno);
if (tp)
printf("sec: %d, min: %d, hour: %d, mday: %d, mon: %d, "
"year: %d, wday: %d, yday: %d, isdst: %d, gmtoff: %ld, "
"zone: %s\n", tp->tm_sec, tp->tm_min, tp->tm_hour,
tp->tm_mday, tp->tm_mon, tp->tm_year, tp->tm_wday,
tp->tm_yday, tp->tm_isdst, tp->tm_gmtoff, tp->tm_zone);
}
int main(void)
{
// time_t t = INT32_MAX;
time_t t = 67767976233532799;
t_inspect(t-1);
t_inspect(t);
t_inspect(t+1);
t_inspect(t+2);
return 0;
}
The output on a 64 bit machine is (the interesting part is the sec=59)
t: 67767976233532798, tp: 0x80085b9a0 errno: 2
sec: 58, min: 59, hour: 23, mday: 31, mon: 11, year: 2147481747, wday: 2, yday:
364, isdst: 0, gmtoff: 0, zone: UTC
t: 67767976233532799, tp: 0x80085b9a0 errno: 0
sec: 59, min: 59, hour: 23, mday: 31, mon: 11, year: 2147481747, wday: 2, yday:
364, isdst: 0, gmtoff: 0, zone: UTC
t: 67767976233532800, tp: 0x80085b9a0 errno: 0
sec: 59, min: 59, hour: 23, mday: 31, mon: 11, year: 2147481747, wday: 2, yday:
364, isdst: 0, gmtoff: 0, zone: UTC
t: 67767976233532801, tp: 0x80085b9a0 errno: 0
sec: 59, min: 59, hour: 23, mday: 31, mon: 11, year: 2147481747, wday: 2, yday:
364, isdst: 0, gmtoff: 0, zone: UTC
Right now, I am looking for confirmation from people on the list if this
behaviour is also happening on other 64 bit operating systems or that it is a
FreeBSD specific quirk.
Edwin
More information about the tz
mailing list