Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Offset of a given timezone from GMT in linux shell script

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?

like image 871
dutch Avatar asked May 13 '10 01:05

dutch


2 Answers

Export your TZ environment variable and print date with %z for timezone offset.

#!/bin/sh
export TZ=":Pacific/Auckland"
date +%z
like image 130
Missaka Wijekoon Avatar answered Oct 05 '22 08:10

Missaka Wijekoon


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.

like image 23
Trey Hunner Avatar answered Oct 05 '22 08:10

Trey Hunner