diff options
author | Andrew White <andyw@pixeltrix.co.uk> | 2014-01-05 11:19:00 -0800 |
---|---|---|
committer | Andrew White <andyw@pixeltrix.co.uk> | 2014-01-05 11:19:00 -0800 |
commit | ffc273577a795bb41068bfc2a1bb575ec51a9712 (patch) | |
tree | a67944ccfbec8fca504f7377b911f65e1849a9d7 | |
parent | 6e867a4dfed31f364d3917dcb8d555f655a73f9e (diff) | |
parent | 0c6ddbe7b0056cb5c17d6b483c0e5a7cbd596b97 (diff) | |
download | rails-ffc273577a795bb41068bfc2a1bb575ec51a9712.tar.gz rails-ffc273577a795bb41068bfc2a1bb575ec51a9712.tar.bz2 rails-ffc273577a795bb41068bfc2a1bb575ec51a9712.zip |
Merge pull request #10634 from teleological/time_advance_gregorian
Maintain proleptic gregorian in Time#advance
-rw-r--r-- | activesupport/lib/active_support/core_ext/time/calculations.rb | 10 | ||||
-rw-r--r-- | activesupport/test/core_ext/time_ext_test.rb | 7 |
2 files changed, 13 insertions, 4 deletions
diff --git a/activesupport/lib/active_support/core_ext/time/calculations.rb b/activesupport/lib/active_support/core_ext/time/calculations.rb index 5ebafcc26e..89cd7516cd 100644 --- a/activesupport/lib/active_support/core_ext/time/calculations.rb +++ b/activesupport/lib/active_support/core_ext/time/calculations.rb @@ -91,10 +91,11 @@ class Time end end - # Uses Date to provide precise Time calculations for years, months, and days. - # The +options+ parameter takes a hash with any of these keys: <tt>:years</tt>, - # <tt>:months</tt>, <tt>:weeks</tt>, <tt>:days</tt>, <tt>:hours</tt>, - # <tt>:minutes</tt>, <tt>:seconds</tt>. + # Uses Date to provide precise Time calculations for years, months, and days + # according to the proleptic Gregorian calendar. The +options+ parameter + # takes a hash with any of these keys: <tt>:years</tt>, <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) @@ -107,6 +108,7 @@ class Time end d = to_date.advance(options) + d = d.gregorian if d.julian? time_advanced_by_date = change(:year => d.year, :month => d.month, :day => d.day) seconds_to_advance = \ options.fetch(:seconds, 0) + diff --git a/activesupport/test/core_ext/time_ext_test.rb b/activesupport/test/core_ext/time_ext_test.rb index 41a1df084e..e0a4b1be3e 100644 --- a/activesupport/test/core_ext/time_ext_test.rb +++ b/activesupport/test/core_ext/time_ext_test.rb @@ -476,6 +476,13 @@ class TimeExtCalculationsTest < ActiveSupport::TestCase assert_equal t, t.advance(:months => 0) end + def test_advance_gregorian_proleptic + assert_equal Time.local(1582,10,14,15,15,10), Time.local(1582,10,15,15,15,10).advance(:days => -1) + assert_equal Time.local(1582,10,15,15,15,10), Time.local(1582,10,14,15,15,10).advance(:days => 1) + assert_equal Time.local(1582,10,5,15,15,10), Time.local(1582,10,4,15,15,10).advance(:days => 1) + assert_equal Time.local(1582,10,4,15,15,10), Time.local(1582,10,5,15,15,10).advance(:days => -1) + end + def test_last_week with_env_tz 'US/Eastern' do assert_equal Time.local(2005,2,21), Time.local(2005,3,1,15,15,10).last_week |