manbytesgnu_site

Source files for manbytesgnu.org
git clone git://holbrook.no/manbytesgnu_site.git
Info | Log | Files | Refs

20240609_timezones.rst (3037B)


      1 A world clock in your terminal
      2 ##############################
      3 
      4 :date: 2024-06-09 23:22:35
      5 :category: Offlining
      6 :author: Louis Holbrook
      7 :tags: bash,linux
      8 :slug: world-clock-terminal
      9 :summary: A single character command to display the current time of your favorite places in the world
     10 :lang: en
     11 :status: published
     12 
     13 
     14 Zoning in
     15 =========
     16 
     17 Timezones in Linux isn't a particularly intuitive issue.
     18 
     19 On my distro, archlinux, time-zone files are located in ``/usr/share/zoneinfo``, and there are several different categories of them.
     20 
     21 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.
     22 
     23 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.
     24 
     25 ... 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.
     26 
     27 
     28 Environmental zones
     29 ===================
     30 
     31 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.
     32 
     33 In my case at time of writing:
     34 
     35 .. code-block:: bash
     36 
     37         $ TZ=America/El_Salvador date
     38         Sun Jun  9 05:00:16 PM CST 2024
     39 
     40         # sigh, see what I mean?
     41         $ TZ=CST date
     42         Sun Jun  9 11:04:12 PM CST 2024
     43 
     44 
     45 Ugly, yes. Luckily ``date`` also lets you format the output.
     46 
     47 
     48 Times, tabled
     49 =============
     50 
     51 Now, let's bring those two together.
     52 
     53 .. code-block:: bash
     54 
     55         z=("US/Hawaii" "America/El_Salvador" "US/Eastern" "Europe/Lisbon" "Europe/Berlin" "Africa/Nairobi" "Asia/Taipei")
     56         for z in ${z[@]}; do
     57                    d=$(TZ=$z date +'%H:%M:%S (%z)')
     58                    s=$(printf %-16s $z)
     59                    echo -e "$s\t$d"
     60                    done
     61 
     62 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.
     63 
     64 This results in:
     65 
     66 .. code-block:: bash
     67 
     68         $ z
     69         US/Hawaii               13:02:12 (-1000)
     70         America/El_Salvador     17:02:12 (-0600)
     71         US/Eastern              19:02:12 (-0400)
     72         Europe/Lisbon           00:02:12 (+0100)
     73         Europe/Berlin           01:02:12 (+0200)
     74         Africa/Nairobi          02:02:12 (+0300)
     75         Asia/Taipei             07:02:12 (+0800)
     76 
     77 The ``z`` here is merely an alias mapping added to ``.bashrc`` [1]_:
     78 
     79 .. code-block:: bash
     80 
     81         alias z=`bash $HOME/scripts/timezones.sh`
     82 
     83 Voila, now you have a world clock in your terminal at any time.
     84 
     85 And all it takes is two mere keystrokes.
     86 
     87 ..
     88 
     89         .. [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.