Is there a way to get the offset of a given timezone (identifier like EDT or America/New_York) from GMT in linux shell script?
Export your TZ environment variable and print date with %z for timezone offset.
#!/bin/sh
export TZ=":Pacific/Auckland"
date +%z
This is a roundabout way to do it but it works (loosely based on this):
#!/bin/bash
ZONE=$1
TIME=$(date +%s --utc -d "12:00:00 $ZONE")
UTC_TIME=$(date +%s --utc -d "12:00:00")
((DIFF=UTC_TIME-TIME))
echo - | awk -v SECS=$DIFF '{printf "%d",SECS/(60*60)}'
Save that as tzoffset
, make it executable, and run it like this:
tzoffset PST
This script in its current form only handles abbreviated timezones.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With