如何从Time.now获取时区缩写?

时间:2013-10-05 08:43:31

标签: ruby-on-rails ruby timezone

Time.now.strftime('%Z')为我返回'Mountain Daylight Time'。我想获得MDT。

获取时区缩写的正确方法是什么?

如果有人怀疑我误导了他们:

irb(main):001:0> Time.now.strftime('%Z')
=> "Mountain Daylight Time"
irb(main):002:0> Time.now.zone
=> "Mountain Daylight Time"
irb(main):003:0> Time.now.in_time_zone('Mountain Time (US & Canada)').zone
=> "MDT"

1 个答案:

答案 0 :(得分:0)

尝试一些事情,我会尝试使用Time.now.zone

#This is working for me, but not for you.  I'm not sure why (although others might!)
$ > Time.now.strftime('%Z')
 => "CEST"

#This gives UTC time which is useful, but not what you're after
$ > Time.zone
 => #<ActiveSupport::TimeZone:0x00000103049918 @name="UTC", @utc_offset=nil, @tzinfo=#<TZInfo::TimezoneProxy: Etc/UTC>, @current_period=nil>
$ > Time.zone.now
 => Sat, 05 Oct 2013 09:11:01 UTC +00:00
$ > Time.zone.now.zone
 => "UTC"

#This will (perhaps) return what you want.
#It's possible that you'll still just get "Mountain Daylight Time"
$ > Time.now
 => 2013-10-05 11:12:09 +0200
$ > Time.now.zone
 => "CEST"
相关问题