(Time.now.next_year) == (Time.now + 1.year) が false な件 (365.25日ってナンダ?)
ActiveSupport 1.4.2 の話です。
え?そうなの?
(Time.now.next_year) == (Time.now + 1.year) # => false
なんですね。
1回目のTime.nowと2回目のTime.nowの間に1秒進んでたというオチではなく。。。
閏年だった。。。というオチでもなく。。。
今までの理解
今までの理解はこうでした。
Time.now.next_year # => Time.nowのyearに1を足したTimeオブジェクト Time.now + 1.year # => Time.nowに 31536000秒( 60秒 x 60分 x 24時間 x 365日) を足したTimeオブジェクト
しかし違うようです。
ソースを読んでみる
まずは next_year から
/usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/core_ext/time/calculations.rb
def change(options) ::Time.send( self.utc? ? :utc : :local, options[:year] || self.year, options[:month] || self.month, options[:mday] || self.mday, options[:hour] || self.hour, options[:min] || (options[:hour] ? 0 : self.min), options[:sec] || ((options[:hour] || options[:min]) ? 0 : self.sec), options[:usec] || ((options[:hour] || options[:min] || options[:sec]) ? 0 : self.usec) ) end : def years_since(years) change(:year => self.year + years) end : def next_year years_since(1) end
理解は合っていたようです。
と、いう事は。。。? 1.yearが365日じゃない??
/usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/core_ext/numeric/time.rb
def minutes self * 60 end alias :minute :minutes def hours self * 60.minutes end alias :hour :hours def days self * 24.hours end alias :day :days : def years (self * 365.25.days).to_i end alias :year :years
は????
え???
何それ 365.25.days って何??? .25 って何?
冷静になると。。。
60 * 60 * 24 * 365 # => 31536000 1.year # => 31557600 (1.year - 0.25.days).to_i == (60 * 60 * 24 * 365) # => true
って事ですね。
(Time.now.next_year) == (Time.now + 1.year) が false な件 については理解した。
しかし。。。
なんでダイワハウス365.25日なんだ???
365.25日の謎
google様に聞いてみるのが早いですね。google:365.24日
すると。。。
ユリウスとかグレゴリオとか何度かお目にかかるけど、いつも避けて通っている単語が出てきます。
wikipedia:年さすがによくまとまってます。
簡単に言うとこういう事ですね。
(365日 + 365日 + 365日 + 1日) ÷ 4年 = 365.25日
つまりユリウス暦の1年は平均365.25日だと定義されていてそれに従っているという事のようです。
でも我々が使ってるのってグレゴリオ暦じゃなかったっけ?