Enumerating, getting, and setting the timezone on a Linux system

By David Pipkin
Lead Software Developer, code whisperer

If you ever find yourself looking for a way to enumerate, get, or set timezone information on a Linux installation, here is a quick rundown to get you started.

Most shells for Linux come with simple GUI interfaces to do all of that for you, but if you need to script or write an app that interfaces with that data then you'll need to know where to look. Luckily, it's pretty simple - the list of timezone regions as well as the currently set region are all in the file structure.

Enumerating Timezone Regions

Most Linux distributions keep their timezone information in the directory /usr/share/zoneinfo/. If you list the contents of this directory, you will see a bunch of region directories, such as "Americas" or "Africa". The root directory also contains some actual timezones such as "EST", "EDT", etc. Inside of each of the region directories are region files and subregion directories. The content of the region files themselves doesn't matter, the system will handle all of the timezone details of the region selected. The only thing that matters is the actual region path, such as "/usr/share/zoneinfo/Europe/Amsterdam". Setting the current timezone using one of these paths will be explained in the following section.

That being said, you can see how the various timezone regions can be easily enumerated in a script or program simply by enumerating all of the different paths in /usr/share/zoneinfo/. For instance, you could first display all of the regions to the user, then once that's selected, display a list of subregions - once they select an endpoint (an actual file), then set it as the active region.

Setting the Current Timezone

Now that you know where to find the timezone information, setting the timezone is a peice of cake, whether it's from the shell, a script, or C. The current timezone is actually just a symbolic link that points to one of the region files mentioned in the section above. This file is at /etc/localtime. In order to update this symbolic link to point to a new timezone region, issue the following command:

ln -sf /usr/share/zoneinfo/Europe/Amsterdam /etc/localtime

This will set the timezone to Amsterdam. You need to make sure you have administrative privileges to issue this command. If not, just add "sudo " in front of "ln" and you will be prompted for a password. Likewise, if you use this in a script or program, it will either have to be run under a super user or prompt for access at the time it needs to set the timezone.

Getting the Current Timezone

There are two ways to find the current timezone. If you want to see the actual timezone that is being used to calculate the time on the system, you can issue the command date. This will output something like:

Mon Jul 9 13:09:10 EST 2012

From this output you can tell that the timezone is set to "EST". However, if you want to see the region that is currently active which is being used to apply a timezone, then issue the command:

file -b /etc/localtime

This will output something like:

symbolic link to '/usr/share/zoneinfo/Europe/Amsterdam'

Now you can see the region info that was selected. This is useful in your program if you need to pre-fill some user input with the currently selected timezone region. Of course, in something like a C application, you'd want to use readlink instead of a shell command.

Article last updated on 4/21/2018