Skip to content

Instantly share code, notes, and snippets.

@chunkhang
Created June 29, 2022 08:51
Show Gist options
  • Save chunkhang/e0958962dcee1b0180e4251b5ec375e1 to your computer and use it in GitHub Desktop.
Save chunkhang/e0958962dcee1b0180e4251b5ec375e1 to your computer and use it in GitHub Desktop.
Convert time from one timezone to another
#!/usr/bin/env zsh
# Usage:
# convert_timezone <source timezone> <target timezone> <date string...>
#
# Examples:
# Asia/Singapore (now) -> Europe/Zurich
# $ convert_timezone Asia/Singapore Europe/Zurich
#
# Asia/Singapore (4 pm) -> Europe/Zurich
# $ convert_timezone Asia/Singapore Europe/Zurich 4pm
#
# Asia/Singapore (4 pm tomorrow) -> Europe/Zurich
# $ convert_timezone Asia/Singapore Europe/Zurich 4pm tomorrow
function convert_timezone() {
timezone_from="$1"; shift
timezone_to="$1"; shift
if [[ "$#" = 0 ]]; then
TZ="$timezone_to" date
else
date_string="TZ=\"${timezone_from}\" ${@}"
TZ="$timezone_to" date --date "$date_string"
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment