这里是:
Time.zone.now => "Eastern Time (US & Canada)"
Time.zone.now => Wed, 15 Aug 2012 06:05:37 EDT -04:00
Time.zone.now + 39.years => Tue, 15 Aug 2051 06:06:03 EST -05:00
因此,Ruby on Rails 预言我们传说中的东部夏令时将在 2051 年结束。
也适用于任何其他时区更改区域。
Time.zone
=> "Pacific Time (US & Canada)"
1.9.2p180 :003 > Time.zone.now
=> Wed, 15 Aug 2012 03:08:57 PDT -07:00
1.9.2p180 :004 > Time.zone.now + 39.years
=> Tue, 15 Aug 2051 03:08:57 PST -08:00
这存在于 Rails 3.0 和 Rails 3.2.6 中
最佳答案
是的,它看起来像一个错误。它不是 Rails,而是 Ruby Time 类。它在 2038 年之后出现问题。
例如,对于 Ruby 1.8.7:
> Time.local(2037,8,16,9,30,15)
=> Sun Aug 16 09:30:15 -0400 2037
>
> Time.local(2038,8,16,9,30,15)
=> Mon Aug 16 09:30:15 -0500 2038
JRuby 1.6.7.2 - 例如 - 没有这个问题:
> Time.local(2038,8,16,9,30,15)
=> Mon Aug 16 09:30:15 -0400 2038
请注意,在 64 位系统的 MRI Ruby 上,支持添加持续时间的 ActiveSupport 时间扩展最终通过 active_support/core_ext/time/calculations.rb 中的此方法调用 Time.local 或 Time.utc:
# Returns a new Time if requested year can be accommodated by Ruby's Time class
# (i.e., if year is within either 1970..2038 or 1902..2038, depending on system architecture);
# otherwise returns a DateTime
def time_with_datetime_fallback(utc_or_local, year, month=1, day=1, hour=0, min=0, sec=0, usec=0)
::Time.send(utc_or_local, year, month, day, hour, min, sec, usec)
rescue
offset = utc_or_local.to_sym == :local ? ::DateTime.local_offset : 0
::DateTime.civil(year, month, day, hour, min, sec, offset)
end
我猜问题是对于 >= 2038 年,他们期望出现溢出异常并改用 DateTime。在 64 位系统上,不会发生这种情况。
更新:此分析对于 Ruby 1.9.2+ 是不正确的。 Time.local 正常运行,但仍然出现原来的问题。
关于ruby-on-rails - Rails Time 类有一个 TimeZone Bug。这可以证实吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11967485/