mirror of
https://github.com/github/rails.git
synced 2026-01-29 08:18:03 -05:00
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
This commit is contained in:
@@ -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 @@ module ActiveSupport #:nodoc:
|
||||
|
||||
# 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 @@ class DateExtCalculationsTest < Test::Unit::TestCase
|
||||
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 @@ class DateTimeExtCalculationsTest < Test::Unit::TestCase
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user