Permalink
Browse files

Fix Date#years_ago and #years_since from leap days. Closes #9864.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7863 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent 9e96286 commit a8077355edef04d2e1844529d6d146bbe41dfa3a @jeremy jeremy committed Oct 13, 2007
View
@@ -1,5 +1,7 @@
*SVN*
+* Fix Date#years_ago and #years_since from leap days. #9864 [Geoff Buesing]
+
* Refactor Time and Date#months_since and #months_ago to use #advance. #9863 [Geoff Buesing]
* Rebundle Builder 2.1.2 but prefer a newer RubyGem if available. [Jeremy Kemper]
@@ -103,12 +103,12 @@ def months_since(months)
# Returns a new Date/DateTime 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 Date/DateTime 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)
@@ -94,12 +94,14 @@ def test_months_since
def test_years_ago
assert_equal Date.new(2004,6,5), Date.new(2005,6,5).years_ago(1)
assert_equal Date.new(1998,6,5), Date.new(2005,6,5).years_ago(7)
+ assert_equal Date.new(2003,2,28), Date.new(2004,2,29).years_ago(1) # 1 year ago from leap day
end
def test_years_since
assert_equal Date.new(2006,6,5), Date.new(2005,6,5).years_since(1)
assert_equal Date.new(2012,6,5), Date.new(2005,6,5).years_since(7)
assert_equal Date.new(2182,6,5), Date.new(2005,6,5).years_since(177)
+ assert_equal Date.new(2005,2,28), Date.new(2004,2,29).years_since(1) # 1 year since leap day
end
def test_last_year
@@ -112,12 +112,14 @@ def test_months_since
def test_years_ago
assert_equal DateTime.civil(2004,6,5,10), DateTime.civil(2005,6,5,10,0,0).years_ago(1)
assert_equal DateTime.civil(1998,6,5,10), DateTime.civil(2005,6,5,10,0,0).years_ago(7)
+ assert_equal DateTime.civil(2003,2,28,10), DateTime.civil(2004,2,29,10,0,0).years_ago(1) # 1 year ago from leap day
end
def test_years_since
assert_equal DateTime.civil(2006,6,5,10), DateTime.civil(2005,6,5,10,0,0).years_since(1)
assert_equal DateTime.civil(2012,6,5,10), DateTime.civil(2005,6,5,10,0,0).years_since(7)
assert_equal DateTime.civil(2182,6,5,10), DateTime.civil(2005,6,5,10,0,0).years_since(177)
+ assert_equal DateTime.civil(2005,2,28,10), DateTime.civil(2004,2,29,10,0,0).years_since(1) # 1 year since leap day
end
def test_last_year

0 comments on commit a807735

Please sign in to comment.