aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/core_ext/date_time
diff options
context:
space:
mode:
authorShay Davidson <shay.h.davidson@gmail.com>2013-08-30 12:05:25 +0300
committerShay Davidson <shay.h.davidson@gmail.com>2013-08-30 12:21:34 +0300
commitb2ae07f0b218b7e92be1e8d7c23f9f9fb7aae56e (patch)
treec785f3f381c0e9645b80847d836abb0fe67ee00b /activesupport/lib/active_support/core_ext/date_time
parentf90aa722fad2b33a95c85319070891d1eab352f5 (diff)
downloadrails-b2ae07f0b218b7e92be1e8d7c23f9f9fb7aae56e.tar.gz
rails-b2ae07f0b218b7e92be1e8d7c23f9f9fb7aae56e.tar.bz2
rails-b2ae07f0b218b7e92be1e8d7c23f9f9fb7aae56e.zip
Added partial days support to `DateTime`'s `advance` method.
You can now add partial days (e.g. 2.5.days) to `DateTime` with the advance method. This was acheived by mimicing the `advance` implementation in `Time`.
Diffstat (limited to 'activesupport/lib/active_support/core_ext/date_time')
-rw-r--r--activesupport/lib/active_support/core_ext/date_time/calculations.rb20
1 files changed, 15 insertions, 5 deletions
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 8e5d723074..2d33a86466 100644
--- a/activesupport/lib/active_support/core_ext/date_time/calculations.rb
+++ b/activesupport/lib/active_support/core_ext/date_time/calculations.rb
@@ -53,17 +53,27 @@ class DateTime
# <tt>:months</tt>, <tt>:weeks</tt>, <tt>:days</tt>, <tt>:hours</tt>,
# <tt>:minutes</tt>, <tt>:seconds</tt>.
def advance(options)
+ unless options[:weeks].nil?
+ options[:weeks], partial_weeks = options[:weeks].divmod(1)
+ options[:days] = options.fetch(:days, 0) + 7 * partial_weeks
+ end
+
+ unless options[:days].nil?
+ options[:days], partial_days = options[:days].divmod(1)
+ options[:hours] = options.fetch(:hours, 0) + 24 * partial_days
+ end
+
d = to_date.advance(options)
- datetime_advanced_by_date = change(:year => d.year, :month => d.month, :day => d.day)
+ time_advanced_by_date = change(:year => d.year, :month => d.month, :day => d.day)
seconds_to_advance = \
options.fetch(:seconds, 0) +
- options.fetch(:minutes, 0) * 60 +
- options.fetch(:hours, 0) * 3600
+ options.fetch(:minutes, 0) * 60 +
+ options.fetch(:hours, 0) * 3600
if seconds_to_advance.zero?
- datetime_advanced_by_date
+ time_advanced_by_date
else
- datetime_advanced_by_date.since seconds_to_advance
+ time_advanced_by_date.since(seconds_to_advance)
end
end