Categories
tools

Time zones: How time changes on Linux

During the time period when we change time from the winter one to the summer one (or the other way around), millions of Linux devices do the switch automatically. How is it possible, when you know that the change happens in different places in the world at different dates? Learn how the time zone database works on Linux and how to check how the time changes anywhere in the world.

The source of the solution is the timezone database (with tools), which you can download from ftp://ftp.iana.org/tz/releases/ The database describes how much time at each location is offset from the GMT time, and when it changes. Then the Linux system uses this data to adjust the displayed time.

You can check your current time zone by looking into /etc/timezone, which contains the user-readable name. For example:
$ cat /etc/timezone
Europe/Paris

The machine-readable data is somewhere else. On Debian, you can find it in /etc/localtime, which is a symbolic link to the correct file. For example:
$ ls -l /etc/localtime
lrwxrwxrwx 1 root root 32 Feb 12 09:29 /etc/localtime ->
/usr/share/zoneinfo/Europe/Paris

You can dump the dates of the time change using the zdump tool (filter the year, otherwise the output will be long):
$ zdump -v /etc/localtime|grep 2021
/etc/localtime Sun Mar 28 00:59:59 2021 UT = Sun Mar 28 01:59:59 2021
CET isdst=0 gmtoff=3600
/etc/localtime Sun Mar 28 01:00:00 2021 UT = Sun Mar 28 03:00:00 2021
CEST isdst=1 gmtoff=7200
/etc/localtime Sun Oct 31 00:59:59 2021 UT = Sun Oct 31 02:59:59 2021
CEST isdst=1 gmtoff=7200
/etc/localtime Sun Oct 31 01:00:00 2021 UT = Sun Oct 31 02:00:00 2021
CET isdst=0 gmtoff=3600

The output contains the moment the time changes, the binary flag if this time
is DST (daylight savings time), and the offset to GMT (in seconds).

With the same tool you can also check the time changes for any other place,
for example:

$ zdump -v /usr/share/zoneinfo/Antarctica/South_Pole |grep 2021
/usr/share/zoneinfo/Antarctica/South_Pole Sat Apr 3 13:59:59 2021 UT
= Sun Apr 4 02:59:59 2021 NZDT isdst=1 gmtoff=46800
/usr/share/zoneinfo/Antarctica/South_Pole Sat Apr 3 14:00:00 2021 UT
= Sun Apr 4 02:00:00 2021 NZST isdst=0 gmtoff=43200
/usr/share/zoneinfo/Antarctica/South_Pole Sat Sep 25 13:59:59 2021 UT
= Sun Sep 26 01:59:59 2021 NZST isdst=0 gmtoff=43200
/usr/share/zoneinfo/Antarctica/South_Pole Sat Sep 25 14:00:00 2021 UT
= Sun Sep 26 03:00:00 2021 NZDT isdst=1 gmtoff=46800

If you want to display time in your program, the system libraries do the conversion automatically. For example, in C the function to use is localtime(). You can learn more from the function’s manual page or from https://man7.org/linux/man-pages/man3/localtime.3.html