From bb6b14b04f9d3f1e8d8811588902eb7d61fb054e Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Fri, 24 Jun 2005 20:42:16 +0000 Subject: Increased accuracy of 1.year by accounting for leap years. The time extensions are not meant to be super-precise but this seems worth it since otherwise you lose a lot of days doing 40.years.ago. Closes #1488 [tuxie@dekadance.se] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1499 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activesupport/CHANGELOG | 2 ++ activesupport/lib/active_support/core_ext/numeric/time.rb | 4 ++-- activesupport/test/core_ext/numeric_ext_test.rb | 2 +- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG index 961c912f01..3919c86540 100644 --- a/activesupport/CHANGELOG +++ b/activesupport/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Made 1.year == 365.25.days to account for leap years. This allows you to do User.find(:all, :conditions => ['birthday > ?', 50.years.ago]) without losing a lot of days. #1488 [tuxie@dekadance.se] + * Added an exception if calling id on nil to WhinyNil #584 [kevin-temp@writesoon.com] * Added Fix/Bignum#multiple_of? which returns true on 14.multiple_of?(7) and false on 16.multiple_of?(7) #1464 [Thomas Fuchs] diff --git a/activesupport/lib/active_support/core_ext/numeric/time.rb b/activesupport/lib/active_support/core_ext/numeric/time.rb index 43c0425b00..de726281b8 100644 --- a/activesupport/lib/active_support/core_ext/numeric/time.rb +++ b/activesupport/lib/active_support/core_ext/numeric/time.rb @@ -32,9 +32,9 @@ module ActiveSupport #:nodoc: self * 30.days end alias :month :months - + def years - self * 365.days + (self * 365.25.days).to_i end alias :year :years diff --git a/activesupport/test/core_ext/numeric_ext_test.rb b/activesupport/test/core_ext/numeric_ext_test.rb index 0bcb8a2b51..770c82c872 100644 --- a/activesupport/test/core_ext/numeric_ext_test.rb +++ b/activesupport/test/core_ext/numeric_ext_test.rb @@ -9,7 +9,7 @@ class NumericExtTimeTest < Test::Unit::TestCase 10.minutes => 600, 1.hour + 15.minutes => 4500, 2.days + 4.hours + 30.minutes => 189000, - 5.years + 1.month + 1.fortnight => 161481600 + 5.years + 1.month + 1.fortnight => 161589600 } end -- cgit v1.2.3