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.

Revisions

  1. chunkhang created this gist Jun 29, 2022.
    24 changes: 24 additions & 0 deletions convert_timezone.zsh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    #!/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
    }