• Mastering timezone date conversions with bash one-liners

    by  • September 23, 2011 • Tips and Trick • 0 Comments

    John Harrison's famous chronometer

    Ever wondered what time is it in Los Angeles given your local time? Or maybe you’re just too busy with your late night coding and stumbled upon an epoch time timestamp  and quickly want to know what’s the equivalent in Dublin timezone?
    I give you a hint, no need to open the holy google, you can just use your old good friend, shell:

    Get current time in a different timezone:

    $ env TZ=America/Los_Angeles date -d "`date`"

    Convert date to a different timezone:

    $ env TZ="America/Chicago" date -d "Fri Sep 23 00:00:14 PDT 2011"

    Convert epoch time to a date in a specified timezone:

    $ env TZ=Europe/Dublin date -d @1316041200

    Convert a date to epoch time:

    $ date -d "Fri Sep 23 00:00:14 PDT 2011" +%s

    Notes:

    1. Check /usr/share/zoneinfo for all available timezones you can use when setting TZ env var
    2. You can always use -u switch to get UTC time

    About

    Software surveyor

    Leave a Reply

    Your email address will not be published. Required fields are marked *