Skip to content

Instantly share code, notes, and snippets.

@jamesstout
Created April 24, 2013 21:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamesstout/5455571 to your computer and use it in GitHub Desktop.
Save jamesstout/5455571 to your computer and use it in GitHub Desktop.
Get the current time in all the timezones known to your machine
#!/bin/bash
# Error logging
e_error() {
printf "$(tput setaf 1)x %s$(tput sgr0)\n" "$@"
}
# only do this if you know how to set it back
# set date/time to 10:00 AM PDT
# export TZ=PST8PDT
# sudo date 042510002013.00
# OpenSolaris
# /usr/share/lib/zoneinfo
# OS X and Linux
# /usr/share/zoneinfo
# default
TZ_DIR="/usr/share/zoneinfo"
if [ -n "$OSTYPE" ]; then
if [[ "$OSTYPE" =~ ^darwin ]] || [[ "$OSTYPE" =~ ^linux ]]; then
TZ_DIR="/usr/share/zoneinfo"
elif [[ "$OSTYPE" =~ ^solaris ]]; then
TZ_DIR="/usr/share/lib/zoneinfo"
fi
fi
if [ ! -d "$TZ_DIR" ]; then
e_error "$TZ_DIR does not exist"
exit 1
fi
cd "$TZ_DIR"
# get all the timezones, trim off first 2 chars: "./"
# store in array for repeat use
declare -a all_tz=(`find . | cut -c 3-`)
# get max len for output formatting
m=-1
for x in ${all_tz[@]}
do
if [ ${#x} -gt $m ]
then
m=${#x}
fi
done
for index in ${!all_tz[*]}
do
export TZ=${all_tz[$index]}
xx=`date '+%d %b %I:%M %p'`
printf "%-*s %s\n" $m "$TZ" "$xx"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment