diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2007-10-13 20:12:36 +0000 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2007-10-13 20:12:36 +0000 |
commit | 9e96286e7aa6d0656d1c8481635dc7e2dca6e067 (patch) | |
tree | ed2b27c062224a5ed8eb174c01507291eaabeb05 | |
parent | 52235a70db09a9f935cb95df2902be28d97e0c5b (diff) | |
download | rails-9e96286e7aa6d0656d1c8481635dc7e2dca6e067.tar.gz rails-9e96286e7aa6d0656d1c8481635dc7e2dca6e067.tar.bz2 rails-9e96286e7aa6d0656d1c8481635dc7e2dca6e067.zip |
Refactor Time and Date#months_since and #months_ago to use #advance. Closes #9863.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7862 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
-rw-r--r-- | activesupport/CHANGELOG | 2 | ||||
-rw-r--r-- | activesupport/lib/active_support/core_ext/date/calculations.rb | 25 | ||||
-rw-r--r-- | activesupport/lib/active_support/core_ext/time/calculations.rb | 25 | ||||
-rw-r--r-- | activesupport/test/core_ext/date_ext_test.rb | 4 | ||||
-rw-r--r-- | activesupport/test/core_ext/date_time_ext_test.rb | 4 | ||||
-rw-r--r-- | activesupport/test/core_ext/time_ext_test.rb | 4 |
6 files changed, 22 insertions, 42 deletions
diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG index 92767bafa3..3bd6a4adcf 100644 --- a/activesupport/CHANGELOG +++ b/activesupport/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* 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] * Add Range#overlaps?(range), Range#include?(range), and Range#step without a block. [brandon] diff --git a/activesupport/lib/active_support/core_ext/date/calculations.rb b/activesupport/lib/active_support/core_ext/date/calculations.rb index df35410b3e..c1f4eb21be 100644 --- a/activesupport/lib/active_support/core_ext/date/calculations.rb +++ b/activesupport/lib/active_support/core_ext/date/calculations.rb @@ -93,30 +93,12 @@ module ActiveSupport #:nodoc: # Returns a new Date/DateTime representing the time a number of specified months ago def months_ago(months) - months_since(-months) + advance(:months => -months) end + # Returns a new Date/DateTime representing the time a number of specified months in the future def months_since(months) - year, month, day = self.year, self.month, self.day - - month += months - - # in case months is negative - while month < 1 - month += 12 - year -= 1 - end - - # in case months is positive - while month > 12 - month -= 12 - year += 1 - end - - max = ::Time.days_in_month(month, year) - day = max if day > max - - change(:year => year, :month => month, :day => day) + advance(:months => months) end # Returns a new Date/DateTime representing the time a number of specified years ago @@ -124,6 +106,7 @@ module ActiveSupport #:nodoc: change(:year => self.year - 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) end diff --git a/activesupport/lib/active_support/core_ext/time/calculations.rb b/activesupport/lib/active_support/core_ext/time/calculations.rb index 5754ca1bca..04da03110f 100644 --- a/activesupport/lib/active_support/core_ext/time/calculations.rb +++ b/activesupport/lib/active_support/core_ext/time/calculations.rb @@ -98,30 +98,12 @@ module ActiveSupport #:nodoc: # Returns a new Time representing the time a number of specified months ago def months_ago(months) - months_since(-months) + advance(:months => -months) end + # Returns a new Time representing the time a number of specified months in the future def months_since(months) - year, month, mday = self.year, self.month, self.mday - - month += months - - # in case months is negative - while month < 1 - month += 12 - year -= 1 - end - - # in case months is positive - while month > 12 - month -= 12 - year += 1 - end - - max = ::Time.days_in_month(month, year) - mday = max if mday > max - - change(:year => year, :month => month, :day => mday) + advance(:months => months) end # Returns a new Time representing the time a number of specified years ago @@ -129,6 +111,7 @@ module ActiveSupport #:nodoc: change(:year => self.year - 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) end diff --git a/activesupport/test/core_ext/date_ext_test.rb b/activesupport/test/core_ext/date_ext_test.rb index 7396c5c9a7..b86bb2dc6b 100644 --- a/activesupport/test/core_ext/date_ext_test.rb +++ b/activesupport/test/core_ext/date_ext_test.rb @@ -85,6 +85,10 @@ class DateExtCalculationsTest < Test::Unit::TestCase assert_equal Date.new(2006,1,5), Date.new(2005,6,5).months_since(7) assert_equal Date.new(2006,6,5), Date.new(2005,6,5).months_since(12) assert_equal Date.new(2007,6,5), Date.new(2005,6,5).months_since(24) + assert_equal Date.new(2005,4,30), Date.new(2005,3,31).months_since(1) + assert_equal Date.new(2005,2,28), Date.new(2005,1,29).months_since(1) + assert_equal Date.new(2005,2,28), Date.new(2005,1,30).months_since(1) + assert_equal Date.new(2005,2,28), Date.new(2005,1,31).months_since(1) end def test_years_ago diff --git a/activesupport/test/core_ext/date_time_ext_test.rb b/activesupport/test/core_ext/date_time_ext_test.rb index 911bd7925e..27c2291442 100644 --- a/activesupport/test/core_ext/date_time_ext_test.rb +++ b/activesupport/test/core_ext/date_time_ext_test.rb @@ -103,6 +103,10 @@ class DateTimeExtCalculationsTest < Test::Unit::TestCase assert_equal DateTime.civil(2006,1,5,10), DateTime.civil(2005,6,5,10,0,0).months_since(7) assert_equal DateTime.civil(2006,6,5,10), DateTime.civil(2005,6,5,10,0,0).months_since(12) assert_equal DateTime.civil(2007,6,5,10), DateTime.civil(2005,6,5,10,0,0).months_since(24) + assert_equal DateTime.civil(2005,4,30,10), DateTime.civil(2005,3,31,10,0,0).months_since(1) + assert_equal DateTime.civil(2005,2,28,10), DateTime.civil(2005,1,29,10,0,0).months_since(1) + assert_equal DateTime.civil(2005,2,28,10), DateTime.civil(2005,1,30,10,0,0).months_since(1) + assert_equal DateTime.civil(2005,2,28,10), DateTime.civil(2005,1,31,10,0,0).months_since(1) end def test_years_ago diff --git a/activesupport/test/core_ext/time_ext_test.rb b/activesupport/test/core_ext/time_ext_test.rb index 5f06a4efa7..da8cd6ec5b 100644 --- a/activesupport/test/core_ext/time_ext_test.rb +++ b/activesupport/test/core_ext/time_ext_test.rb @@ -110,6 +110,10 @@ class TimeExtCalculationsTest < Test::Unit::TestCase assert_equal Time.local(2006,1,5,10), Time.local(2005,6,5,10,0,0).months_since(7) assert_equal Time.local(2006,6,5,10), Time.local(2005,6,5,10,0,0).months_since(12) assert_equal Time.local(2007,6,5,10), Time.local(2005,6,5,10,0,0).months_since(24) + assert_equal Time.local(2005,4,30,10), Time.local(2005,3,31,10,0,0).months_since(1) + assert_equal Time.local(2005,2,28,10), Time.local(2005,1,29,10,0,0).months_since(1) + assert_equal Time.local(2005,2,28,10), Time.local(2005,1,30,10,0,0).months_since(1) + assert_equal Time.local(2005,2,28,10), Time.local(2005,1,31,10,0,0).months_since(1) end def test_years_ago |