aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport
diff options
context:
space:
mode:
Diffstat (limited to 'activesupport')
-rw-r--r--activesupport/CHANGELOG2
-rw-r--r--activesupport/lib/active_support/core_ext/date/calculations.rb25
-rw-r--r--activesupport/lib/active_support/core_ext/time/calculations.rb25
-rw-r--r--activesupport/test/core_ext/date_ext_test.rb4
-rw-r--r--activesupport/test/core_ext/date_time_ext_test.rb4
-rw-r--r--activesupport/test/core_ext/time_ext_test.rb4
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