views:

232

answers:

2

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
Or if you don't want to mangle the current environment: `TZ=":Pacific/Auckland" date +%z`
Ignacio Vazquez-Abrams
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