Skip to content

Instantly share code, notes, and snippets.

@xyntrix
Forked from chmouel/tz.sh
Created April 6, 2018 21:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xyntrix/0b08b0ff5f1812404963bbb825a565fc to your computer and use it in GitHub Desktop.
Save xyntrix/0b08b0ff5f1812404963bbb825a565fc to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# you can do things like this :
# % tz
# % tz 10h30
# % tz 10h30 next week
# % tz 11:00 next thursday
#
# and so on,
#
# This needs gnu date, on MacOSX just install gnuutils from brew
#
# This needs bash v4 too, you need to install it from brew as well
# on MacOSX
#
set -eo pipefail
declare -A tzone
## Change this
tzone=(
["Bengalore"]="Asia/Calcutta"
["Brisbane"]="Australia/Brisbane"
["Paris"]="Europe/Paris"
)
# If that fails (old distros used to do a hardlink for /etc/localtime)
# you may want to specify your tz lie America/Chicago etc...
currenttz=$(/bin/ls -l /etc/localtime|awk -F/ '{print $(NF-1)"/"$NF}')
date=date
type -p gdate >/dev/null 2>/dev/null && date=gdate
athour=
args=($@)
if [[ -n ${1} ]];then
[[ $1 != [0-9]*(:|h)[0-9]* ]] && { echo "Invalid date format: $1 you need to specify a time first like tz 10h00 tomorrow!"; exit 1; }
athour="${1/h/:} ${args[@]:1}"
fi
for i in ${!tzone[@]};do
echo -n "$i: "
# bug in gnu date? 'now' doesn't take in consideration TZ :(
[[ -n ${athour} ]] && TZ="${tzone[$i]}" ${date} --date="TZ=\"$currenttz\" ${athour}" || \
TZ=${tzone[$i]} ${date}
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment