2016-07-07 58 views
0

我使用Google時區API獲取TimeZoneID,並將其傳遞給紅寶石寶石TZInfo以獲取當地時間。如何從此信息獲取Timezones簡稱(PST,PDT,EST等)。我試過strftime(%Z),但是返回'UTC'。這是一個Ruby應用程序的紅寶石。時區標識爲短時區(pst,pdt,cst等)

回答

1

TZInfo::Timezone#strftime應該工作:

require "tzinfo" 

tz = TZInfo::Timezone.get("America/New_York") 
puts tz.strftime("%Z") 

如果你嘗試過,並沒有工作,這是神祕的。

或者,你可以使用TZInfo::TimezonePeriod#abbreviation

tz = TZInfo::Timezone.get('America/New_York') 
period = tz.period_for_utc(Time.now.utc) 
puts period.abbreviation 
+0

我是想 'tz.now.strftime( 「%Z」)',這是返回 'UTC'。你的解決方案'tz.strftime(「%Z」)'工作! –

相關問題