aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--activesupport/CHANGELOG2
-rw-r--r--activesupport/lib/active_support/core_ext/time/calculations.rb4
-rw-r--r--activesupport/test/core_ext/time_ext_test.rb2
3 files changed, 6 insertions, 2 deletions
diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG
index 8eb145bb28..3a66699f73 100644
--- a/activesupport/CHANGELOG
+++ b/activesupport/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Fix Time#years_ago and #years_since from leap days. #9865 [Geoff Buesing]
+
* Time and DateTime#advance accept :hours, :minutes, and :seconds options. #9825 [Geoff Buesing]
* Fix Date#years_ago and #years_since from leap days. #9864 [Geoff Buesing]
diff --git a/activesupport/lib/active_support/core_ext/time/calculations.rb b/activesupport/lib/active_support/core_ext/time/calculations.rb
index 94d660afbd..d594cf21f9 100644
--- a/activesupport/lib/active_support/core_ext/time/calculations.rb
+++ b/activesupport/lib/active_support/core_ext/time/calculations.rb
@@ -110,12 +110,12 @@ module ActiveSupport #:nodoc:
# Returns a new Time representing the time a number of specified years ago
def years_ago(years)
- change(:year => self.year - years)
+ advance(:years => -years)
end
# Returns a new Time representing the time a number of specified years in the future
def years_since(years)
- change(:year => self.year + years)
+ advance(:years => years)
end
# Short-hand for years_ago(1)
diff --git a/activesupport/test/core_ext/time_ext_test.rb b/activesupport/test/core_ext/time_ext_test.rb
index f1b4e23622..f3e9900510 100644
--- a/activesupport/test/core_ext/time_ext_test.rb
+++ b/activesupport/test/core_ext/time_ext_test.rb
@@ -119,11 +119,13 @@ class TimeExtCalculationsTest < Test::Unit::TestCase
def test_years_ago
assert_equal Time.local(2004,6,5,10), Time.local(2005,6,5,10,0,0).years_ago(1)
assert_equal Time.local(1998,6,5,10), Time.local(2005,6,5,10,0,0).years_ago(7)
+ assert_equal Time.local(2003,2,28,10), Time.local(2004,2,29,10,0,0).years_ago(1) # 1 year ago from leap day
end
def test_years_since
assert_equal Time.local(2006,6,5,10), Time.local(2005,6,5,10,0,0).years_since(1)
assert_equal Time.local(2012,6,5,10), Time.local(2005,6,5,10,0,0).years_since(7)
+ assert_equal Time.local(2005,2,28,10), Time.local(2004,2,29,10,0,0).years_since(1) # 1 year since leap day
# Failure because of size limitations of numeric?
# assert_equal Time.local(2182,6,5,10), Time.local(2005,6,5,10,0,0).years_since(177)
end