This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
mktime + UTC problem
- From: Daniel Drake <ddrake at brontes3d dot com>
- To: libc-alpha at sourceware dot org
- Date: Mon, 06 Nov 2006 09:00:47 -0500
- Subject: mktime + UTC problem
Hi,
I'm trying to diagnose a problem where hwclock is restoring an incorrect
time from my hardware clock. I have traced this down to mktime() not
behaving as described.
In this context, hwclock is dealing with a UTC time value in a tm
structure. It then tries to convert this to UTC seconds from the epoch
using setenv("TZ", "", 1); tzset(); then mktime(). However, the
seconds-from-epoch value being returned here is wrong.
I have reproduced this in a small stand-alone test program:
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
int main(void)
{
struct tm tm;
setenv("TZ", "", 1);
tzset();
memset(&tm, 0, sizeof(tm));
tm.tm_sec = 0;
tm.tm_min = 0;
tm.tm_hour = 18;
tm.tm_mday = 3;
tm.tm_mon = 10;
tm.tm_year = 106;
tm.tm_yday = -1;
tm.tm_isdst = 0;
time_t ret = mktime(&tm);
printf("ret %ld\n", ret);
ret = timegm(&tm);
printf("ret %ld\n", ret);
}
The above program attempts to interpret the tm time as UTC using
mktime(), then does the same with timegm().
On a normal system, this should output:
1162576800
1162576800
On this one system, I get:
1162594800
1162576800
timegm() is doing as it should but mktime() is not. I note that the
timegm man page has a code snippet for mktime very similar to what I am
using above.
Interestingly enough if I change:
setenv("TZ", "", 1);
to:
setenv("TZ", "GMT0", 1);
then the test program produces the expected results.
Am I missing something, or does this look like a bug?
Thanks!
Daniel