I'm experiencing a bizarre issue where my system clock knows that it's daylight savings time, but glibc seems not to. This is an up-to-date Ubuntu installation, and I have checked /etc/localtime and it has the correct changeover time for last week's switch to DST.
The current correct timezone for me is Pacific Daylight Time (UTC-7). When I ask my system what time zone I'm in, it tells me correctly:
$ date +%z
-0700
But when I run the following program:
#include <time.h>
#include <stdio.h>
int main() {
tzset();
printf("%lu\n", timezone);
return 0;
}
The output is, incorrectly:
28800
Which corresponds to UTC-8, or Pacific Standard Time. (And no, TZ is not set in my environment)
I thought glibc and the date program would get their time zone information from the same source, but apparently either they don't or I'm misunderstanding how the glibc timezone global works.
The basic questions are then:
I don't think "timezone" changes with daylight time. Try the "daylight" variable. On my system:
The external variable timezone contains the difference, in seconds, between UTC and local standard time (for example, in the U.S. Eastern time zone (EST), timezone is 5*60*60). The external variable daylight is non-zero only if a summer time zone adjustment is specified in the TZ environment variable.
Look at tm.tm_isdst field after doing this:
time_t current_time;
struct tm tm;
current_time = time(NULL);
localtime_r(¤t_time, &tm);
According to the localtime_r(3) manpage, this does actually indicate whether DST is in effect at the time specified. I think you then need to assume that DST adds one hour to the timezone(3) variable you're already using, or do the diff trick against GMT.
Works for me in Australian AEST, hope it works for you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With