hubertf's NetBSD Blog
Send interesting links to hubert at feyrer dot de!
 
[20091024] Fun around daylight saving time (DST) and Unix timezone handling in general
It's that time of the year again when I and my friends never know when daylight saving time is switched from summer- to winter time. Do you know? And if you've heared in the news today... do you know when the next change is?

There's an easy way to find out, using the zdump(8) utility that comes with about every Unix machine (at least with NetBSD and Mac OS X - is there anything else relevant? :), and the timezone information stored in the /usr/share/zoneinfo directory.

To find out when DST is switched on/off, run the following command:

% zdump -v /etc/localtime
...
/etc/localtime  Sun Mar 29 00:59:59 2009 UTC = Sun Mar 29 01:59:59 2009 CET isdst=0
/etc/localtime  Sun Mar 29 01:00:00 2009 UTC = Sun Mar 29 03:00:00 2009 CEST isdst=1
/etc/localtime  Sun Oct 25 00:59:59 2009 UTC = Sun Oct 25 02:59:59 2009 CEST isdst=1
/etc/localtime  Sun Oct 25 01:00:00 2009 UTC = Sun Oct 25 02:00:00 2009 CET isdst=0
/etc/localtime  Sun Mar 28 00:59:59 2010 UTC = Sun Mar 28 01:59:59 2010 CET isdst=0
/etc/localtime  Sun Mar 28 01:00:00 2010 UTC = Sun Mar 28 03:00:00 2010 CEST isdst=1
/etc/localtime  Sun Oct 31 00:59:59 2010 UTC = Sun Oct 31 02:59:59 2010 CEST isdst=1
/etc/localtime  Sun Oct 31 01:00:00 2010 UTC = Sun Oct 31 02:00:00 2010 CET isdst=0
/etc/localtime  Sun Mar 27 00:59:59 2011 UTC = Sun Mar 27 01:59:59 2011 CET isdst=0
/etc/localtime  Sun Mar 27 01:00:00 2011 UTC = Sun Mar 27 03:00:00 2011 CEST isdst=1
/etc/localtime  Sun Oct 30 00:59:59 2011 UTC = Sun Oct 30 02:59:59 2011 CEST isdst=1
/etc/localtime  Sun Oct 30 01:00:00 2011 UTC = Sun Oct 30 02:00:00 2011 CET isdst=0
...  
The data is given relative to Universal Time Coordinated, which you can determine on a NetBSD system by running "date -u":
% date
Sat 24 Oct 14:30:24 CEST 2009
% date -u
Sat 24 Oct 12:30:26 UTC 2009
And if you wonder what timezone you're actually in, you probably knokw that /etc/localtime is a symbolic link to a file that fits to your exact timezone, with all related information:
% ls -la /etc/localtime 
lrwxr-xr-x  1 root  wheel  33 May 26  2007 /etc/localtime -> /usr/share/zoneinfo/Europe/Berlin
If you always wanted to know what timezones there are, have a look at /usr/share/zoneinfo:
% ls /usr/share/zoneinfo/
Africa       Chile        GB-Eire      Israel       NZ-CHAT      UCT
America      Cuba         GMT          Jamaica      Navajo       US
Antarctica   EET          GMT+0        Japan        PRC          UTC
Arctic       EST          GMT-0        Kwajalein    PST8PDT      Universal
Asia         EST5EDT      GMT0         Libya        Pacific      W-SU
Atlantic     Egypt        Greenwich    MET          Poland       WET
Australia    Eire         HST          MST          Portugal     Zulu
Brazil       Etc          Hongkong     MST7MDT      ROC          iso3166.tab
CET          Europe       Iceland      Mexico       ROK          posixrules
CST6CDT      Factory      Indian       Mideast      Singapore    zone.tab
Canada       GB           Iran         NZ           Turkey
% ls /usr/share/zoneinfo/Europe/
Amsterdam    Chisinau     Kiev         Moscow       Sarajevo     Vatican
Andorra      Copenhagen   Lisbon       Nicosia      Simferopol   Vienna
Athens       Dublin       Ljubljana    Oslo         Skopje       Vilnius
Belfast      Gibraltar    London       Paris        Sofia        Volgograd
Belgrade     Guernsey     Luxembourg   Podgorica    Stockholm    Warsaw
Berlin       Helsinki     Madrid       Prague       Tallinn      Zagreb
Bratislava   Isle_of_Man  Malta        Riga         Tirane       Zaporozhye
Brussels     Istanbul     Mariehamn    Rome         Tiraspol     Zurich
Bucharest    Jersey       Minsk        Samara       Uzhgorod
Budapest     Kaliningrad  Monaco       San_Marino   Vaduz 
So now that we all that, here's a last cutie: suppose you want to log into a machine on the other end of the world, and still run an application that should use your local timezone, not that that the machine is in. Changing /etc/localtime is not an option, but you can do it on a per-process base by setting the "TZ" environment variable:
% date
Sat Oct 24 14:35:06 CEST 2009
% ssh remote.example.org date
Sat Oct 24 12:35:45 UTC 2009
% ssh remote.example.org env TZ=Europe/Berlin date
Sat Oct 24 14:35:58 CEST 2009 
Enjoy!

[Tags: , , , , ]


Disclaimer: All opinion expressed here is purely my own. No responsibility is taken for anything.

Access count: 36171860
Copyright (c) Hubert Feyrer