【问题标题】:How to get the time zone abbreviation from Time.now?如何从 Time.now 获取时区缩写?
【发布时间】:2013-10-12 07:26:44
【问题描述】:

Time.now.strftime('%Z') 为我返回“山地夏令时”。我想获得 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"

【问题讨论】:

  • 真的吗?我在使用Time.now.strftime('%Z') 时得到CEST - 即缩写和docs 表示以下内容:%Z - Abbreviated time zone name or similar information.
  • Time#strftime monkey 是在代码中修补还是在使用的 gem 之一?
  • 是否有任何方法可以在 Rails 中列出特定方法是否为“猴子补丁”?
  • 是的,dax——真的。我知道文档说什么,但如果我认为它们可能已经过时了,因为我没有得到预期的结果。
  • 我其实也有同样的问题。您使用哪个系统?我将 Win7 与 ruby​​installer 的 ruby​​ 1.9.3 一起使用(没有导轨,只有 ruby​​)。如果你也使用Win7,你可以关闭问题,有一个重复的答案:stackoverflow.com/questions/22092281/…

标签: ruby-on-rails ruby timezone


【解决方案1】:

跑了一些东西,我会说尝试使用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"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-20
    • 2017-02-20
    • 1970-01-01
    • 2013-10-15
    • 2013-11-26
    • 2012-04-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多