在Rails中,为什么1.year.from_now和1.year.to_i.seconds.from_now不一样?
In Rails, why is 1.year.from_now not the same as 1.year.to_i.seconds.from_now?
我能否让 Rails 将相同的逻辑应用到我在几秒钟内的计算,就像它在我多年的计算中一样?
puts "#{1.year.from_now} | #{1.year.to_i.seconds.from_now}"
2017-03-23 18:48:06 UTC | 2017-03-24 00:48:06 UTC
不明白6小时的时差是从哪里来的
相差6 小时。这是因为 1 年以秒为单位(由 to_i
方法转换)在 Rails 核心扩展的 Ruby 中定义为 365.25 天 :
>> 1.year.to_i / 60 / 60 / 24.0
=> 365.25
这 0.25 天是实际的 6 小时差异。通过这样做,RoR 试图计算闰年(按 basic approximation) occur once in 4 years. The same is also evident from the years
definition in Rails source code.
另一方面,1.year.from_now
改为转换具体的日历日期时间。就像您在挂历上翻页一样。
我能否让 Rails 将相同的逻辑应用到我在几秒钟内的计算,就像它在我多年的计算中一样?
puts "#{1.year.from_now} | #{1.year.to_i.seconds.from_now}"
2017-03-23 18:48:06 UTC | 2017-03-24 00:48:06 UTC
不明白6小时的时差是从哪里来的
相差6 小时。这是因为 1 年以秒为单位(由 to_i
方法转换)在 Rails 核心扩展的 Ruby 中定义为 365.25 天 :
>> 1.year.to_i / 60 / 60 / 24.0
=> 365.25
这 0.25 天是实际的 6 小时差异。通过这样做,RoR 试图计算闰年(按 basic approximation) occur once in 4 years. The same is also evident from the years
definition in Rails source code.
另一方面,1.year.from_now
改为转换具体的日历日期时间。就像您在挂历上翻页一样。