1. Overview
In this tutorial, we’ll be looking at the different ways we can obtain the system time zone in Linux.
2. Time Zone Representation
There are several ways the time zones are represented. For one, they can be simply represented as an abbreviation of their name. For example, Eastern Standard Time (North America) is usually abbreviated as EST. Besides that, they could also be represented as an offset from UTC. For instance, the EST time zone is UTC-4. Finally, each time zone can also be represented with its designated name in the IANA time zone database such as America/New_York.
3. TZ Environment Variable
In Linux, the TZ environment variable specifies the time zone of the system. If set, it takes precedence as the time zone of the system. To check its value, simply use the echo command to print the content to the console:
$ echo $TZ
Europe/Madrid
4. date Command
The date command is a command-line tool for dealing with the date and time of the system. Using the date command, we can obtain the time zone abbreviated name or its equivalent UTC offset. The date command accepts a format argument which allows us to output the time zone information in offset or abbreviated name.
For example, to get the abbreviated name of the time zone, we can use the date command with the %Z format:
$ date +%Z
CEST
Besides that, we can also print the time zone in its UTC offset representation with the %z format:
$ date +%z
+0200
The offset tells us that the current system time zone is 2 hours ahead of the UTC.
5. timedatectl
For all of the Linux distro that comes with systemd, the timedatectl command gives us the detailed time information of the system. This information includes the time at UTC, the local time, as well as the time zone of the system.
$ timedatectl
Local time: Tue 2022-05-03 08:01:41 CEST
Universal time: Tue 2022-05-03 06:01:41 UTC
RTC time: Tue 2022-05-03 06:01:41
Time zone: Europe/Madrid (CEST, +0200)
System clock synchronized: yes
NTP service: active
RTC in local TZ: no
From the output, we can see that the different representation of time zone is being printed along with the current date and time.
Additionally, we can pass in the show argument to print the information in a machine-readable format:
$ timedatectl show
Timezone=Europe/Madrid
LocalRTC=no
CanNTP=yes
NTP=yes
NTPSynchronized=yes
TimeUSec=Fri 2022-05-03 06:01:47 CEST
RTCTimeUSec=Fri 2022-05-03 06:01:46 CEST
6. The /etc/localtime File
We can also obtain the time zone information from the /etc/localtime file. As a matter of fact, the timedatectl command actually reads from this file under the hood.
The /etc/localtime file is a symlink to the actual time zone binary file. To obtain the value, we can check the actual time zone file it’s pointing to by running ls -l:
$ ls -l /etc/localtime
lrwxrwxrwx 1 user user 33 Dec 29 09:44 /etc/localtime -> /usr/share/zoneinfo/Europe/Madrid
From the symlink, we can directly identify that the system’s time zone is set to Europe/Madrid.
Alternatively, we can utilize the zdump command to read the content of the binary file right away:
$ zdump /etc/localtime
/etc/localtime Tue May 3 08:16:16 2022 CEST
In addition to the time zone, the zdump command also prints the current date and time of the system at the given time zone.
7. The /etc/timezone File
Finally, there’s a /etc/timezone file that stores the time zone of the system in plain text format. We can simply read the time zone using the cat command:
$ cat /etc/timezone
Europe/Madrid
8. Summary
In this tutorial, we’ve seen the different ways we can obtain the time zone of the Linux system. For example, we’ve learned that the TZ environment could specify the time zone and has the highest precedence. Then, we’ve explored the date and timedatectl command-line tools as the convenient ways to obtain the time zone of the system. Finally, we’ve inspected the /etc/localtime and /etc/timezone file to get the time zone information as well.