views:

648

answers:

5

Is there a script to display a simple world clock (time in various places around the world) on a *nix terminal?

I was thinking of writing a quick Python script, but I have a feeling that's gonna be more work than I think (e.g. due to config and output format) - not to mention reinventing the wheel...

+1  A: 

If you do still want to write it in Python, consider Pytz:

http://pytz.sourceforge.net/

Their front page shows you many simple ways to accomplish what you're looking for.

That said, I'm sure if you spent a few minutes on Google you'd find tons of scripts, some that even launch graphical programs for *nix. The first list of results for "python world clock" seem to suggest this alone.

Chris Cameron
+9  A: 

I have this bourne shell script:

#!/bin/sh

PT=`env TZ=US/Pacific date`
CT=`env TZ=US/Central date`
AT=`env TZ=Australia/Melbourne date`

echo "Santa Clara    $PT"
echo "Central        $CT"
echo "Melbourne      $AT"
Dustin
+4  A: 

Never thought of it, but not dreadfully hard to do.

#!/bin/sh
# Command-line world clock

: ${WORLDCLOCK_ZONES:=$HOME/etc/worldclock.zones}
: ${WORLDCLOCK_FORMAT:='+%Y-%m-%d %H:%M:%S %Z'}

while read zone
do echo $zone '!' $(TZ=$zone date "$WORLDCLOCK_FORMAT")
done < $WORLDCLOCK_ZONES |
awk -F '!' '{ printf "%-20s  %s\n", $1, $2;}'

Given the input file:

US/Pacific
Europe/London
Europe/Paris
Asia/Kolkatta
Africa/Johannesburg
Asia/Tokyo
Asia/Shanghai

I got the output:

US/Pacific             2008-12-15 15:58:57 PST
Europe/London          2008-12-15 23:58:57 GMT
Europe/Paris           2008-12-16 00:58:57 CET
Asia/Kolkatta          2008-12-15 23:58:57 GMT
Africa/Johannesburg    2008-12-16 01:58:57 SAST
Asia/Tokyo             2008-12-16 08:58:57 JST
Asia/Shanghai          2008-12-16 07:58:57 CST

I was fortunate that this took less than a second to run and didn't cross a 1-second boundary.

(I didn't notice that Kolkatta failed and defaulted to GMT. My system still has Asia/Calcutta as the entry for India.)

Jonathan Leffler
A: 

Many thanks for this! I wish I could vote (or comment) without registering.

That said, I'm sure if you spent a few minutes on Google you'd find tons of scripts, some that even launch graphical programs for *nix.

FWIW, I did google - but I specifically wanted something without a GUI, which turned out to be hard to find.

A GUI on the command line?
Joachim Sauer
A: 

I use this, which is basically the same as the other suggestions, except it filters on specific zones you want to see:

#!/bin/sh

# Show date and time in other time zones

search=$1

zoneinfo=/usr/share/zoneinfo/posix/
format='%a %F %T'

find $zoneinfo -type f \
    | grep -i "$search" \
    | while read z
      do
          d=$(TZ=$z date +"$format")
          printf "%-34s %23s\n" ${z#$zoneinfo} "$d"
      done

Sample output:

$ /usr/local/bin/wdate bang
Africa/Bangui                      Fri 2009-03-06 11:04:24
Asia/Bangkok                       Fri 2009-03-06 17:04:24
mivk