Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Ruby, 12.months != 1.year

Ruby's date/time helpers are useful but I found a discrepancy. It seems that 12.months does not equal 1.year. Check 1.month and you'll find it's equal to 30.days and, of course, 12 * 30.days = 360.days, 5.25 days short of an actual year's length.

I came across this when I set access to certain components of our web site based on the number of months granted, as specified by the client. I discovered that a 36.month term expired a couple of weeks early when running my tests. The solution was something like this:

def months_to_seconds(number_of_months)
  ( (number_of_months.to_f / 12) * 1.year).to_i.seconds
end

This returns the number of seconds in whatever fraction of a year is represented by the number_of_months.

Since 1.year is equal in seconds to 365.25 days, why do you suppose they didn't have 1.month return the seconds for 1/12 of a year instead of 30 days?

Has anyone run across this before? Does anyone have a better solution?

like image 278
Warren Avatar asked Nov 12 '10 21:11

Warren


2 Answers

The calendar, and time in general, is such an arbitrary thing and is riddled with inconsistencies like this. There is no standard length of "month", as the variation is anywhere between 28 and 31 days, just as there is no standard length of "day", which can be anywhere from 23 to 25 hours, and even minute can have anywhere from 59 to 62 seconds when "leap seconds" are taken into account. Likewise a year can be 365 or 366 days, or even 351 as was the case in 1582.

Unless you're doing calculations specific to a particular interval, like the number of seconds between January 3, 2010 and October 19, 2011, you will not be able to know how long the interval is using abstract months, days, or years. These things are dependent on a huge number of factors.

Your solution is suitable for your application, but in Rails terms a "month" is simply 30 days for simplicity's sake, just as a "day" is 24 hours.

like image 156
tadman Avatar answered Oct 14 '22 03:10

tadman


Yes and no:

12 months are the same as 1 year when you use them as relative intervals, because they are not converted to seconds then:

t = Time.now
#=> 2010-11-12 22:34:57 0100
t - 1.year == t - 12.months
#=> true

Internally, these intervals are kept as arrays of number of years, months and days, so if you say, for example:

1.year - 12.months
#=> 1 year and -12 months

it means that the subtraction resulted in an interval consisting of "one year and minus 12 months".

But if you to_i them, they need to be converted to exact amount of seconds, and someone decided that "a month" means "30 days", which is as good approximation as any other, while you stick with it.

That's pointed out in the docs:

While these methods provide precise calculation when used as in the examples above, care should be taken to note that this is not true if the result of 'months', 'years', etc is converted before use

I guess these intervals can be looked at as "lazily evaluated" in a way.

like image 23
Mladen Jablanović Avatar answered Oct 14 '22 03:10

Mladen Jablanović