aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport
diff options
context:
space:
mode:
authorJeremy Kemper <jeremy@bitsweat.net>2007-10-13 20:13:18 +0000
committerJeremy Kemper <jeremy@bitsweat.net>2007-10-13 20:13:18 +0000
commita8077355edef04d2e1844529d6d146bbe41dfa3a (patch)
tree0d0bc53696ef2915eb2070a91efe5ecbea20896d /activesupport
parent9e96286e7aa6d0656d1c8481635dc7e2dca6e067 (diff)
downloadrails-a8077355edef04d2e1844529d6d146bbe41dfa3a.tar.gz
rails-a8077355edef04d2e1844529d6d146bbe41dfa3a.tar.bz2
rails-a8077355edef04d2e1844529d6d146bbe41dfa3a.zip
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
Diffstat (limited to 'activesupport')
-rw-r--r--activesupport/CHANGELOG2
-rw-r--r--activesupport/lib/active_support/core_ext/date/calculations.rb4
-rw-r--r--activesupport/test/core_ext/date_ext_test.rb2
-rw-r--r--activesupport/test/core_ext/date_time_ext_test.rb2
4 files changed, 8 insertions, 2 deletions
diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG
index 3bd6a4adcf..ad889aa923 100644
--- a/activesupport/CHANGELOG
+++ b/activesupport/CHANGELOG
@@ -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]
diff --git a/activesupport/lib/active_support/core_ext/date/calculations.rb b/activesupport/lib/active_support/core_ext/date/calculations.rb
index c1f4eb21be..9da7303f27 100644
--- a/activesupport/lib/active_support/core_ext/date/calculations.rb
+++ b/activesupport/lib/active_support/core_ext/date/calculations.rb
@@ -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)
diff --git a/activesupport/test/core_ext/date_ext_test.rb b/activesupport/test/core_ext/date_ext_test.rb
index b86bb2dc6b..beeaa6ce4d 100644
--- a/activesupport/test/core_ext/date_ext_test.rb
+++ b/activesupport/test/core_ext/date_ext_test.rb
@@ -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
diff --git a/activesupport/test/core_ext/date_time_ext_test.rb b/activesupport/test/core_ext/date_time_ext_test.rb
index 27c2291442..6fb4ad5881 100644
--- a/activesupport/test/core_ext/date_time_ext_test.rb
+++ b/activesupport/test/core_ext/date_time_ext_test.rb
@@ -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