From 4b6411008675dbbfb7da4dfb6ef73c5528c196d1 Mon Sep 17 00:00:00 2001 From: Rick Olson Date: Fri, 3 Aug 2007 00:34:19 +0000 Subject: 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 --- activesupport/lib/active_support/core_ext/date/calculations.rb | 9 +++++---- .../lib/active_support/core_ext/date_time/calculations.rb | 4 +--- activesupport/lib/active_support/core_ext/time/calculations.rb | 6 ++---- 3 files changed, 8 insertions(+), 11 deletions(-) (limited to 'activesupport/lib/active_support/core_ext') 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 -- cgit v1.2.3