aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib
diff options
context:
space:
mode:
authorJeremy Kemper <jeremy@bitsweat.net>2007-10-13 20:12:36 +0000
committerJeremy Kemper <jeremy@bitsweat.net>2007-10-13 20:12:36 +0000
commit9e96286e7aa6d0656d1c8481635dc7e2dca6e067 (patch)
treeed2b27c062224a5ed8eb174c01507291eaabeb05 /activesupport/lib
parent52235a70db09a9f935cb95df2902be28d97e0c5b (diff)
downloadrails-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
Diffstat (limited to 'activesupport/lib')
-rw-r--r--activesupport/lib/active_support/core_ext/date/calculations.rb25
-rw-r--r--activesupport/lib/active_support/core_ext/time/calculations.rb25
2 files changed, 8 insertions, 42 deletions
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