From 27c70ff386e76ed0af41b53f2ba8cdb09bc94ace Mon Sep 17 00:00:00 2001 From: Tom Lea Date: Thu, 4 Sep 2008 17:38:53 +0100 Subject: Time#advance recognizes fractional days and weeks [#970 state:resolved] --- activesupport/lib/active_support/core_ext/time/calculations.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'activesupport/lib') diff --git a/activesupport/lib/active_support/core_ext/time/calculations.rb b/activesupport/lib/active_support/core_ext/time/calculations.rb index 3cc6d59907..00078de692 100644 --- a/activesupport/lib/active_support/core_ext/time/calculations.rb +++ b/activesupport/lib/active_support/core_ext/time/calculations.rb @@ -98,6 +98,16 @@ module ActiveSupport #:nodoc: # :months, :weeks, :days, :hours, # :minutes, :seconds. def advance(options) + unless options[:weeks].nil? + options[:weeks], partial_weeks = options[:weeks].divmod(1) + options[:days] = (options[:days] || 0) + 7 * partial_weeks + end + + unless options[:days].nil? + options[:days], partial_days = options[:days].divmod(1) + options[:hours] = (options[:hours] || 0) + 24 * partial_days + end + d = to_date.advance(options) time_advanced_by_date = change(:year => d.year, :month => d.month, :day => d.day) seconds_to_advance = (options[:seconds] || 0) + (options[:minutes] || 0) * 60 + (options[:hours] || 0) * 3600 -- cgit v1.2.3