tags:

views:

443

answers:

2

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:

  1. Why are these two outputs different
  2. How can I reliably detect the system UTC offset from a C program?
+2  A: 

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.
Randy Proctor
My reading of the manpage for tzset indicates that the dayllight variable only indicates if the local zone uses daylight savings at some point, and doesn't necessarily indicate if it's currently in effect.
Tyler McHenry
I think I agree with you. I'd suggest { time_t t = time(NULL); printf("%d\n",(int)difftime(mktime(gmtime( } but that gives me the same result as "timezone".
Randy Proctor
+1  A: 

Hi,

I have the exact same issue (also running Ubuntu). The problem with daylight variable, seems to be related to the fact that it only tells if the current timezone has a day light saving policy or not. It seems it does not tell if the day light saving is currently applied or not (behaviour deduced from testing)... Any idea is welcome !

Joel