A world clock in your terminal

Posted in offlining bash linux

Zoning in

Timezones in Linux isn't a particularly intuitive issue.

On my distro, archlinux, time-zone files are located in /usr/share/zoneinfo, and there are several different categories of them.

I've given up referencing time in the usual three- and four-letter acronyms like CEST and EST long ago. Not only are they ambiguous in themselves, but not all of them even have records in the zoneinfo tree.

The best approach I have found so far is to look at the world continent directories, and look for nearby cities. Your OS alone may not be enough in all cases to figure out the zone for obscure locations, but at least it gets you half the way there.

... as in, you ask your friend or associate to name some big cities close to them, and hope you find a match in your filesystem.

Environmental zones

Turns out that the date command of GNU/Linux uses the environment variable TZ, which lets you define the time-zone you want the output to be translated to.

In my case at time of writing:

$ TZ=America/El_Salvador date
Sun Jun  9 05:00:16 PM CST 2024

# sigh, see what I mean?
$ TZ=CST date
Sun Jun  9 11:04:12 PM CST 2024

Ugly, yes. Luckily date also lets you format the output.

Times, tabled

Now, let's bring those two together.

z=("US/Hawaii" "America/El_Salvador" "US/Eastern" "Europe/Lisbon" "Europe/Berlin" "Africa/Nairobi" "Asia/Taipei")
for z in ${z[@]}; do
           d=$(TZ=$z date +'%H:%M:%S (%z)')
           s=$(printf %-16s $z)
           echo -e "$s\t$d"
           done

This script iterates a list of valid time-zone strings from /usr/share/zoneinfo, outputting the zone along with the time and offet in a readable, tabulated format.

This results in:

$ z
US/Hawaii               13:02:12 (-1000)
America/El_Salvador     17:02:12 (-0600)
US/Eastern              19:02:12 (-0400)
Europe/Lisbon           00:02:12 (+0100)
Europe/Berlin           01:02:12 (+0200)
Africa/Nairobi          02:02:12 (+0300)
Asia/Taipei             07:02:12 (+0800)

The z here is merely an alias mapping added to .bashrc [1]:

alias z=`bash $HOME/scripts/timezones.sh`

Voila, now you have a world clock in your terminal at any time.

And all it takes is two mere keystrokes.

[1]Of course, be a bit careful with those aliases. There are even commands in GNU/Linux with only one character, like w. If you override them in aliases, your override takes precedence.