Skip to content

Instantly share code, notes, and snippets.

@rkratky
Forked from chmouel/tz.sh
Created April 8, 2018 08:32
Show Gist options
  • Select an option

  • Save rkratky/1bff1452f6b3dacf5b3d43e83a8ffd72 to your computer and use it in GitHub Desktop.

Select an option

Save rkratky/1bff1452f6b3dacf5b3d43e83a8ffd72 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