This is the mail archive of the
glibc-bugs@sources.redhat.com
mailing list for the glibc project.
[Bug libc/601] New: mktime(3) unreliable after calling tzset(3) when TZ is NULL or bogus
- From: "kreckel at ginac dot de" <sourceware-bugzilla at sources dot redhat dot com>
- To: glibc-bugs at sources dot redhat dot com
- Date: 6 Dec 2004 21:19:46 -0000
- Subject: [Bug libc/601] New: mktime(3) unreliable after calling tzset(3) when TZ is NULL or bogus
- Reply-to: sourceware-bugzilla at sources dot redhat dot com
#include <time.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
int main()
{
unsigned sec;
struct tm t;
time_t t1, t2;
const char* currTz;
t.tm_year = 100; // 2000
t.tm_mon = 0; // Jan
t.tm_mday = 1;
t.tm_hour = 0;
t.tm_min = 0;
t.tm_sec = 0;
t.tm_isdst = 0;
t1 = mktime( &t );
for ( sec = 0; sec < 24*60*60; ++sec ) {
t.tm_hour = sec / ( 60 * 60 );
t.tm_min = ( sec / ( 60 ) ) % 60;
t.tm_sec = sec % ( 60 );
printf("%02d:%02d:%02d\t",t.tm_hour,t.tm_min,t.tm_sec);
currTz = getenv( "TZ" );
//setenv( "TZ", "#!@+*%&
NULLCannotBeUsedBecauseOfABugWhenLinkingWithLibPthreadStringCannotBeUsedEither",
1 ); // leads to errors
setenv( "TZ", NULL, 1 ); // leads to errors
//setenv( "TZ", "UTC", 1 ); // seems to work
tzset();
t2 = mktime( &t );
if ( currTz )
setenv( "TZ", currTz, 1 );
else
unsetenv( "TZ" );
printf("%d\n",t2-t1);
t1 = t2;
}
}
The above program should print:
00:00:00 3600
00:00:01 1
00:00:02 1
00:00:03 1
The left column should always be the time difference to the previous line or, in
the case of the first line, the difference of the local time to UTC. However,
it sometimes prints:
00:00:00 3601
00:00:01 0
00:00:02 1
00:00:03 1
[...]
or, even stranger things like
[...]
00:01:15 1
00:01:16 7201
00:01:17 -7199
00:01:18 1
[...]
This happens only if I set TZ to NULL or to some bogus (see code) value *inside*
the loop. However, according to tzset(3): "If the TZ variable does appear in the
environment but its value is NULL or its value cannot be interpreted using any
of the formats specified below, Coordinated Universal Time (UTC) is used."
I have reproduced this bug with the glibc shipped with SuSE Professional Linux
8.1, SuSE Linux Linux 9.2 and Debian 3.0, so I assume it must be from upstream.
--
Summary: mktime(3) unreliable after calling tzset(3) when TZ is
NULL or bogus
Product: glibc
Version: unspecified
Status: NEW
Severity: normal
Priority: P2
Component: libc
AssignedTo: gotom at debian dot or dot jp
ReportedBy: kreckel at ginac dot de
CC: glibc-bugs at sources dot redhat dot com
http://sources.redhat.com/bugzilla/show_bug.cgi?id=601
------- You are receiving this mail because: -------
You are on the CC list for the bug, or are watching someone who is.