aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib
diff options
context:
space:
mode:
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