Rails 5 截至 Rails 4 一年中计算错误的秒数

Number of seconds in a year time being miscalculated in Rails 5 as of Rails 4

我在 1 年中使用 1.year.to_i 秒数。但是对于 rails 4 和 5,它有 648 秒或大约 10 分钟的显着差异。这种变化是有意的吗,如果是,背后的原因是什么。

这是在 ActiveSupport::Duration 中定义的常量,称为 SECONDS_PER_YEAR,其值为 31556952。

这里是我在 pry 中遵循的步骤,可以帮助您或其他人将来阅读。

[8] pry(main)> show-source 1.year
...
def years
  ActiveSupport::Duration.years(self)
end

[9] pry(main)> show-source ActiveSupport::Duration.years
...
def years(value) #:nodoc:
  new(value * SECONDS_PER_YEAR, [[:years, value]])
end

[11] pry(main)> cd ActiveSupport::Duration
[12] pry(ActiveSupport::Duration):1> SECONDS_PER_YEAR
=> 31556952

我在 GitHub 上创建并发布了一个完美的答案。

1.year is now provided by ActiveSupport::Duration and it follows the Gregorian calendar. Length of a Gregorian year => 365.2425 days which amounts to 31556952 seconds. What you are talking about is Julian year which is almost 11 minutes longer.

所以这不是什么奇怪的行为。

https://github.com/rails/rails/issues/33978#issuecomment-424331247