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?
+1
A:
Export your TZ environment variable and print date with %z for timezone offset.
#!/bin/sh
export TZ=":Pacific/Auckland"
date +%z
Misk
2010-05-13 01:59:44
Or if you don't want to mangle the current environment: `TZ=":Pacific/Auckland" date +%z`
Ignacio Vazquez-Abrams
2010-05-13 02:16:29
A:
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.
Trey
2010-05-13 02:10:50