This is the mail archive of the
libc-help@sourceware.org
mailing list for the glibc project.
Daylight Savings Time Madness
- From: Stefan Raspl <raspl at linux dot vnet dot ibm dot com>
- To: libc-help at sourceware dot org
- Date: Tue, 07 Apr 2009 15:37:07 +0200
- Subject: Daylight Savings Time Madness
- Reply-to: raspl at linux dot vnet dot ibm dot com
Debugging a problem related to daylight savings time, I encountered
a very odd behavior. The oddity was reproduced with both, glibc 2.5
and current CVS, and reproduced consistently on i386, PowerPC and
s390. I figure it is essential that you set your timezone to
something other than UTC.
Here's the code:
#include <stdio.h>
#include <time.h>
void process(const char *str) {
struct tm tm, *p_tm;
char buf[255];
time_t t;
printf("starting out with : %s\n", str);
strptime(str, "%Y-%m-%d %H:%M:%S", &tm);
t = mktime(&tm);
p_tm = localtime(&t);
strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", p_tm);
printf("after reconverting : %s\n", buf);
}
int main() {
process("2009-04-02 13:35:00");
sleep(1); // [1]
process("2009-02-02 13:35:00");
return 0;
}
When run as is, I get the following output:
starting out with : 2009-04-02 13:35:00
after reconverting : 2009-04-02 13:35:00
starting out with : 2009-02-02 13:35:00
after reconverting : 2009-02-02 13:35:00
Now remove the sleep(1) at [1] and run again:
starting out with : 2009-04-02 13:35:00
after reconverting : 2009-04-02 13:35:00
starting out with : 2009-02-02 13:35:00
after reconverting : 2009-02-02 12:35:00
Note that the final timestamp is 12:35 instead of 13:35.
Any ideas why a sleep(1) makes a difference?
Ciao,
Stefan
--
Linux on System z
IBM Deutschland Research & Development GmbH
Vorsitzender des Aufsichtsrats: Martin Jetter
Geschäftsführer: Erich Baier
Sitz der Gesellschaft: Böblingen
Registergericht: Amtsgericht Stuttgart, HRB 243294