In Rails, why is 1.year.from_now not the same as 1.year.to_i.seconds.from_now?

Can I do Rails using the same logic for my calculations in seconds, as is the case with my calculations in years?

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 

I do not understand where the 6-hour difference comes from.

+7
ruby ruby-on-rails dst
source share
1 answer

The difference is 6 hours . And this is because 1 year in seconds (converted by the to_i method) is defined as 365.25 days in Ruby on Rails kernel extensions:

 >> 1.year.to_i / 60 / 60 / 24.0 => 365.25 

Those 0.25 days are the actual difference of 6 hours. Thus, RoR tries to count in leap years, which (through the base approximation ) occur once every 4 years. The same can be seen from the definition of years in the Rails source code .

1.year.from_now , 1.year.from_now other hand, 1.year.from_now particular calendar day instead. It’s as if you have turned pages into your wall calendar.

+9
source share

All Articles