Active Supportメモ

Active Supportのn.monthn.yearの挙動が5.0系、5.1系以上で異なっていました。 バージョン上げるときにバグをうまないようメモしておきます。

https://github.com/rails/rails/blob/5-1-stable/activesupport/CHANGELOG.md

# 5.0.7.2
https://github.com/rails/rails/blob/5-0-stable/activesupport/lib/active_support/duration.rb#L14-L15
SECONDS_PER_MONTH  = 2592000  # 30 days
SECONDS_PER_YEAR   = 31557600 # length of a julian year (365.2425 days)

> 1.month.to_i
=> 2592000

> 1.year.to_i
=> 31557600

> 12.months == 1.year
=> false

> 30.days == 1.month
=> true
# 5.1.7
https://github.com/rails/rails/blob/5-1-stable/activesupport/lib/active_support/duration.rb#L111-L112
SECONDS_PER_MONTH  = 2629746  # 1/12 of a gregorian year
SECONDS_PER_YEAR   = 31556952 # length of a gregorian year (365.2425 days)

> 1.month.to_i
=> 2629746

> 1.year.to_i
=> 31556952

> 12.months == 1.year
=> true

> 30.days == 1.month
=> false

CHANGELOG.mdに次のように書いてありますね。

The value of 365.2425 days in Gregorian year is more accurate as it accounts for every 400th non-leap year.

グレゴリオ暦 - Wikipedia

ユリウス暦 - Wikipedia

面白い〜