From 9e96286e7aa6d0656d1c8481635dc7e2dca6e067 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Sat, 13 Oct 2007 20:12:36 +0000 Subject: 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 --- .../active_support/core_ext/date/calculations.rb | 25 ++++------------------ .../active_support/core_ext/time/calculations.rb | 25 ++++------------------ 2 files changed, 8 insertions(+), 42 deletions(-) (limited to 'activesupport/lib') 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 -- cgit v1.2.3