在linux shell腳本中從GMT偏移給定時區

[英]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?

有沒有辦法在linux shell腳本中從GMT獲取給定時區(如EDT或America / New_York這樣的標識符)的偏移量?

2 个解决方案

#1


12  

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

導出TZ環境變量並使用%z打印日期以用於時區偏移。

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

#2


4  

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,使其可執行,並像這樣運行:

tzoffset PST

This script in its current form only handles abbreviated timezones.

當前形式的此腳本僅處理縮寫的時區。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2010/05/13/72eff9fecefe965d20efe503c95c9ef2.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com