2016-07-07 165 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”)'工作! –