Permalink
Browse files

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
  • Loading branch information...
1 parent 52235a7 commit 9e96286e7aa6d0656d1c8481635dc7e2dca6e067 @jeremy jeremy committed Oct 13, 2007
View
@@ -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]
@@ -93,37 +93,20 @@ def change(options)
# 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
def years_ago(years)
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
@@ -98,37 +98,20 @@ def since(seconds)
# 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
def years_ago(years)
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
@@ -85,6 +85,10 @@ def test_months_since
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
@@ -103,6 +103,10 @@ def test_months_since
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
@@ -110,6 +110,10 @@ def test_months_since
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

0 comments on commit 9e96286

Please sign in to comment.