diff options
author | Matthew Draper <matthew@trebex.net> | 2014-05-28 03:32:21 +0930 |
---|---|---|
committer | Matthew Draper <matthew@trebex.net> | 2014-05-28 03:32:21 +0930 |
commit | 086ee1cc1312d4679e32b6dc8e343548891af4fa (patch) | |
tree | 1d668a10823da293120e9a0200cfaf9b1b390493 | |
parent | 6f9aff75a5e8cdfb92e425e433807ac2972df740 (diff) | |
parent | 0fb33f3fda3958865fb62969ea6898acf7016aaf (diff) | |
download | rails-086ee1cc1312d4679e32b6dc8e343548891af4fa.tar.gz rails-086ee1cc1312d4679e32b6dc8e343548891af4fa.tar.bz2 rails-086ee1cc1312d4679e32b6dc8e343548891af4fa.zip |
Merge pull request #12080 from ShayDavidson/fix_datetime_partial_dates
Added partial days support to `DateTime`'s `advance` method.
-rw-r--r-- | activesupport/CHANGELOG.md | 14 | ||||
-rw-r--r-- | activesupport/lib/active_support/core_ext/date_time/calculations.rb | 12 | ||||
-rw-r--r-- | activesupport/test/core_ext/date_time_ext_test.rb | 6 |
3 files changed, 31 insertions, 1 deletions
diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md index 2a992f60bb..6915a491b0 100644 --- a/activesupport/CHANGELOG.md +++ b/activesupport/CHANGELOG.md @@ -1,3 +1,17 @@ +* DateTime `advance` now supports partial days. + + Before: + + DateTime.now.advance(days: 1, hours: 12) + + After: + + DateTime.now.advance(days: 1.5) + + Fixes #12005. + + *Shay Davidson* + * `Hash#deep_transform_keys` and `Hash#deep_transform_keys!` now transform hashes in nested arrays. This change also applies to `Hash#deep_stringify_keys`, `Hash#deep_stringify_keys!`, `Hash#deep_symbolize_keys` and 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 73ad0aa097..289ca12b5e 100644 --- a/activesupport/lib/active_support/core_ext/date_time/calculations.rb +++ b/activesupport/lib/active_support/core_ext/date_time/calculations.rb @@ -53,6 +53,16 @@ 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) seconds_to_advance = \ @@ -63,7 +73,7 @@ class DateTime if seconds_to_advance.zero? datetime_advanced_by_date else - datetime_advanced_by_date.since seconds_to_advance + datetime_advanced_by_date.since(seconds_to_advance) end end diff --git a/activesupport/test/core_ext/date_time_ext_test.rb b/activesupport/test/core_ext/date_time_ext_test.rb index 0a40aeb96c..224172e39f 100644 --- a/activesupport/test/core_ext/date_time_ext_test.rb +++ b/activesupport/test/core_ext/date_time_ext_test.rb @@ -162,6 +162,12 @@ class DateTimeExtCalculationsTest < ActiveSupport::TestCase assert_equal DateTime.civil(2013,10,17,20,22,19), DateTime.civil(2005,2,28,15,15,10).advance(:years => 7, :months => 19, :weeks => 2, :days => 5, :hours => 5, :minutes => 7, :seconds => 9) end + def test_advance_partial_days + assert_equal DateTime.civil(2012,9,29,13,15,10), DateTime.civil(2012,9,28,1,15,10).advance(:days => 1.5) + assert_equal DateTime.civil(2012,9,28,13,15,10), DateTime.civil(2012,9,28,1,15,10).advance(:days => 0.5) + assert_equal DateTime.civil(2012,10,29,13,15,10), DateTime.civil(2012,9,28,1,15,10).advance(:days => 1.5, :months => 1) + end + def test_advanced_processes_first_the_date_deltas_and_then_the_time_deltas # If the time deltas were processed first, the following datetimes would be advanced to 2010/04/01 instead. assert_equal DateTime.civil(2010, 3, 29), DateTime.civil(2010, 2, 28, 23, 59, 59).advance(:months => 1, :seconds => 1) |