dutch
dutch

Reputation: 105

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?

Upvotes: 9

Views: 20887

Answers (2)

Trey Hunner
Trey Hunner

Reputation: 11814

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.

Upvotes: 4

Missaka Wijekoon
Missaka Wijekoon

Reputation: 899

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

#!/bin/sh
export TZ=":Pacific/Auckland"
date +%z

Upvotes: 19

Related Questions