aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/core_ext
diff options
context:
space:
mode:
authorRick Olson <technoweenie@gmail.com>2007-08-03 00:34:19 +0000
committerRick Olson <technoweenie@gmail.com>2007-08-03 00:34:19 +0000
commit4b6411008675dbbfb7da4dfb6ef73c5528c196d1 (patch)
tree7c9012bb5296ffb04c9576bbc29d61f5b8a4d0da /activesupport/lib/active_support/core_ext
parentbbbc45156bb2f903513116036619b78065f6d7e8 (diff)
downloadrails-4b6411008675dbbfb7da4dfb6ef73c5528c196d1.tar.gz
rails-4b6411008675dbbfb7da4dfb6ef73c5528c196d1.tar.bz2
rails-4b6411008675dbbfb7da4dfb6ef73c5528c196d1.zip
Fix Time#advance bug when trying to advance a year from leap day. Closes #8655 [gbuesing]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7262 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activesupport/lib/active_support/core_ext')
-rw-r--r--activesupport/lib/active_support/core_ext/date/calculations.rb9
-rw-r--r--activesupport/lib/active_support/core_ext/date_time/calculations.rb4
-rw-r--r--activesupport/lib/active_support/core_ext/time/calculations.rb6
3 files changed, 8 insertions, 11 deletions
diff --git a/activesupport/lib/active_support/core_ext/date/calculations.rb b/activesupport/lib/active_support/core_ext/date/calculations.rb
index 637c9ea02a..c28d0c876b 100644
--- a/activesupport/lib/active_support/core_ext/date/calculations.rb
+++ b/activesupport/lib/active_support/core_ext/date/calculations.rb
@@ -68,9 +68,10 @@ module ActiveSupport #:nodoc:
# Provides precise Date calculations for years, months, and days. The +options+ parameter takes a hash with
# any of these keys: :months, :days, :years.
def advance(options)
- d = ::Date.new(year + (options.delete(:years) || 0), month, day)
- d = d >> options.delete(:months) if options[:months]
- d = d + options.delete(:days) if options[:days]
+ d = self
+ d = d >> options.delete(:years) * 12 if options[:years]
+ d = d >> options.delete(:months) if options[:months]
+ d = d + options.delete(:days) if options[:days]
d
end
@@ -78,7 +79,7 @@ module ActiveSupport #:nodoc:
#
# Examples:
#
- # Date.new(2007, 5, 12).change(:day => 1) # => Date.new(2007, 5, 12)
+ # Date.new(2007, 5, 12).change(:day => 1) # => Date.new(2007, 5, 1)
# Date.new(2007, 5, 12).change(:year => 2005, :month => 1) # => Date.new(2005, 1, 12)
def change(options)
::Date.new(
diff --git a/activesupport/lib/active_support/core_ext/date_time/calculations.rb b/activesupport/lib/active_support/core_ext/date_time/calculations.rb
index 8e4197d34b..e16ad55fbd 100644
--- a/activesupport/lib/active_support/core_ext/date_time/calculations.rb
+++ b/activesupport/lib/active_support/core_ext/date_time/calculations.rb
@@ -30,9 +30,7 @@ module ActiveSupport #:nodoc:
# Uses Date to provide precise Time calculations for years, months, and days. The +options+ parameter takes a hash with
# any of these keys: :months, :days, :years.
def advance(options)
- d = ::Date.new(year + (options.delete(:years) || 0), month, day)
- d = d >> options.delete(:months) if options[:months]
- d = d + options.delete(:days) if options[:days]
+ d = to_date.advance(options)
change(options.merge(:year => d.year, :month => d.month, :day => d.day))
end
diff --git a/activesupport/lib/active_support/core_ext/time/calculations.rb b/activesupport/lib/active_support/core_ext/time/calculations.rb
index b6eb9b4288..507bb5f180 100644
--- a/activesupport/lib/active_support/core_ext/time/calculations.rb
+++ b/activesupport/lib/active_support/core_ext/time/calculations.rb
@@ -72,10 +72,8 @@ module ActiveSupport #:nodoc:
# Uses Date to provide precise Time calculations for years, months, and days. The +options+ parameter takes a hash with
# any of these keys: :months, :days, :years.
def advance(options)
- d = ::Date.new(year + (options.delete(:years) || 0), month, day)
- d = d >> options.delete(:months) if options[:months]
- d = d + options.delete(:days) if options[:days]
- change(options.merge(:year => d.year, :month => d.month, :mday => d.day))
+ d = to_date.advance(options)
+ change(options.merge(:year => d.year, :month => d.month, :day => d.day))
end
# Returns a new Time representing the time a number of seconds ago, this is basically a wrapper around the Numeric extension