2

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"
4

1 回答 1

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"
于 2013-10-05T09:16:12.193 回答