Created
June 29, 2022 08:51
-
-
Save chunkhang/e0958962dcee1b0180e4251b5ec375e1 to your computer and use it in GitHub Desktop.
Convert time from one timezone to another
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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