diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2007-10-13 21:44:48 +0000 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2007-10-13 21:44:48 +0000 |
commit | c7dd2a1c11c4cf66eac1b6c00ded956b1c3d55f6 (patch) | |
tree | c0b67cf0106dfb07ace77017256d3613a22e5a9b | |
parent | 003df72d007725ac19aaf95c406dfb36f38ccd43 (diff) | |
download | rails-c7dd2a1c11c4cf66eac1b6c00ded956b1c3d55f6.tar.gz rails-c7dd2a1c11c4cf66eac1b6c00ded956b1c3d55f6.tar.bz2 rails-c7dd2a1c11c4cf66eac1b6c00ded956b1c3d55f6.zip |
Time and DateTime#advance accept :hours, :minutes, and :seconds options. Closes #9825.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7866 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
5 files changed, 35 insertions, 7 deletions
diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG index ad889aa923..8eb145bb28 100644 --- a/activesupport/CHANGELOG +++ b/activesupport/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Time and DateTime#advance accept :hours, :minutes, and :seconds options. #9825 [Geoff Buesing] + * Fix Date#years_ago and #years_since from leap days. #9864 [Geoff Buesing] * Refactor Time and Date#months_since and #months_ago to use #advance. #9863 [Geoff Buesing] 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 4ec75288ca..9cb7455dec 100644 --- a/activesupport/lib/active_support/core_ext/date_time/calculations.rb +++ b/activesupport/lib/active_support/core_ext/date_time/calculations.rb @@ -28,10 +28,12 @@ module ActiveSupport #:nodoc: end # 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. + # any of these keys: :years, :months, :days, :hours, :minutes, :seconds. def advance(options) d = to_date.advance(options) - change(options.merge(:year => d.year, :month => d.month, :day => d.day)) + datetime_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 + seconds_to_advance == 0 ? datetime_advanced_by_date : datetime_advanced_by_date.since(seconds_to_advance) end # Returns a new DateTime representing the time a number of seconds ago diff --git a/activesupport/lib/active_support/core_ext/time/calculations.rb b/activesupport/lib/active_support/core_ext/time/calculations.rb index 04da03110f..94d660afbd 100644 --- a/activesupport/lib/active_support/core_ext/time/calculations.rb +++ b/activesupport/lib/active_support/core_ext/time/calculations.rb @@ -72,10 +72,12 @@ module ActiveSupport #:nodoc: end # 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. + # any of these keys: :years, :months, :days, :hours, :minutes, :seconds. def advance(options) d = to_date.advance(options) - change(options.merge(: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[:seconds] || 0) + (options[:minutes] || 0) * 60 + (options[:hours] || 0) * 3600 + seconds_to_advance == 0 ? time_advanced_by_date : time_advanced_by_date.since(seconds_to_advance) end # Returns a new Time representing the time a number of seconds ago, this is basically a wrapper around the Numeric extension diff --git a/activesupport/test/core_ext/date_time_ext_test.rb b/activesupport/test/core_ext/date_time_ext_test.rb index 6fb4ad5881..f79c73536c 100644 --- a/activesupport/test/core_ext/date_time_ext_test.rb +++ b/activesupport/test/core_ext/date_time_ext_test.rb @@ -165,12 +165,20 @@ class DateTimeExtCalculationsTest < Test::Unit::TestCase assert_equal DateTime.civil(2005,2,22,15,45), DateTime.civil(2005,2,22,15,15,10).change(:min => 45) end - def test_plus + def test_advance assert_equal DateTime.civil(2006,2,28,15,15,10), DateTime.civil(2005,2,28,15,15,10).advance(:years => 1) assert_equal DateTime.civil(2005,6,28,15,15,10), DateTime.civil(2005,2,28,15,15,10).advance(:months => 4) assert_equal DateTime.civil(2012,9,28,15,15,10), DateTime.civil(2005,2,28,15,15,10).advance(:years => 7, :months => 7) assert_equal DateTime.civil(2013,10,3,15,15,10), DateTime.civil(2005,2,28,15,15,10).advance(:years => 7, :months => 19, :days => 5) + assert_equal DateTime.civil(2001,12,27,15,15,10), DateTime.civil(2005,2,28,15,15,10).advance(:years => -3, :months => -2, :days => -1) assert_equal DateTime.civil(2005,2,28,15,15,10), DateTime.civil(2004,2,29,15,15,10).advance(:years => 1) #leap day plus one year + assert_equal DateTime.civil(2005,2,28,20,15,10), DateTime.civil(2005,2,28,15,15,10).advance(:hours => 5) + assert_equal DateTime.civil(2005,2,28,15,22,10), DateTime.civil(2005,2,28,15,15,10).advance(:minutes => 7) + assert_equal DateTime.civil(2005,2,28,15,15,19), DateTime.civil(2005,2,28,15,15,10).advance(:seconds => 9) + assert_equal DateTime.civil(2005,2,28,20,22,19), DateTime.civil(2005,2,28,15,15,10).advance(:hours => 5, :minutes => 7, :seconds => 9) + assert_equal DateTime.civil(2005,2,28,10,8,1), DateTime.civil(2005,2,28,15,15,10).advance(:hours => -5, :minutes => -7, :seconds => -9) + assert_equal DateTime.civil(2013,10,3,20,22,19), DateTime.civil(2005,2,28,15,15,10).advance(:years => 7, :months => 19, :days => 5, :hours => 5, :minutes => 7, :seconds => 9) + end def test_next_week diff --git a/activesupport/test/core_ext/time_ext_test.rb b/activesupport/test/core_ext/time_ext_test.rb index da8cd6ec5b..f1b4e23622 100644 --- a/activesupport/test/core_ext/time_ext_test.rb +++ b/activesupport/test/core_ext/time_ext_test.rb @@ -237,20 +237,34 @@ class TimeExtCalculationsTest < Test::Unit::TestCase assert_equal Time.utc(2005,2,22,15,45), Time.utc(2005,2,22,15,15,10).change(:min => 45) end - def test_plus + def test_advance assert_equal Time.local(2006,2,28,15,15,10), Time.local(2005,2,28,15,15,10).advance(:years => 1) assert_equal Time.local(2005,6,28,15,15,10), Time.local(2005,2,28,15,15,10).advance(:months => 4) assert_equal Time.local(2012,9,28,15,15,10), Time.local(2005,2,28,15,15,10).advance(:years => 7, :months => 7) assert_equal Time.local(2013,10,3,15,15,10), Time.local(2005,2,28,15,15,10).advance(:years => 7, :months => 19, :days => 5) + assert_equal Time.local(2001,12,27,15,15,10), Time.local(2005,2,28,15,15,10).advance(:years => -3, :months => -2, :days => -1) assert_equal Time.local(2005,2,28,15,15,10), Time.local(2004,2,29,15,15,10).advance(:years => 1) #leap day plus one year + assert_equal Time.local(2005,2,28,20,15,10), Time.local(2005,2,28,15,15,10).advance(:hours => 5) + assert_equal Time.local(2005,2,28,15,22,10), Time.local(2005,2,28,15,15,10).advance(:minutes => 7) + assert_equal Time.local(2005,2,28,15,15,19), Time.local(2005,2,28,15,15,10).advance(:seconds => 9) + assert_equal Time.local(2005,2,28,20,22,19), Time.local(2005,2,28,15,15,10).advance(:hours => 5, :minutes => 7, :seconds => 9) + assert_equal Time.local(2005,2,28,10,8,1), Time.local(2005,2,28,15,15,10).advance(:hours => -5, :minutes => -7, :seconds => -9) + assert_equal Time.local(2013,10,3,20,22,19), Time.local(2005,2,28,15,15,10).advance(:years => 7, :months => 19, :days => 5, :hours => 5, :minutes => 7, :seconds => 9) end - def test_utc_plus + def test_utc_advance assert_equal Time.utc(2006,2,22,15,15,10), Time.utc(2005,2,22,15,15,10).advance(:years => 1) assert_equal Time.utc(2005,6,22,15,15,10), Time.utc(2005,2,22,15,15,10).advance(:months => 4) assert_equal Time.utc(2012,9,22,15,15,10), Time.utc(2005,2,22,15,15,10).advance(:years => 7, :months => 7) assert_equal Time.utc(2013,10,3,15,15,10), Time.utc(2005,2,22,15,15,10).advance(:years => 7, :months => 19, :days => 11) + assert_equal Time.utc(2001,12,27,15,15,10), Time.utc(2005,2,28,15,15,10).advance(:years => -3, :months => -2, :days => -1) assert_equal Time.utc(2005,2,28,15,15,10), Time.utc(2004,2,29,15,15,10).advance(:years => 1) #leap day plus one year + assert_equal Time.utc(2005,2,28,20,15,10), Time.utc(2005,2,28,15,15,10).advance(:hours => 5) + assert_equal Time.utc(2005,2,28,15,22,10), Time.utc(2005,2,28,15,15,10).advance(:minutes => 7) + assert_equal Time.utc(2005,2,28,15,15,19), Time.utc(2005,2,28,15,15,10).advance(:seconds => 9) + assert_equal Time.utc(2005,2,28,20,22,19), Time.utc(2005,2,28,15,15,10).advance(:hours => 5, :minutes => 7, :seconds => 9) + assert_equal Time.utc(2005,2,28,10,8,1), Time.utc(2005,2,28,15,15,10).advance(:hours => -5, :minutes => -7, :seconds => -9) + assert_equal Time.utc(2013,10,3,20,22,19), Time.utc(2005,2,28,15,15,10).advance(:years => 7, :months => 19, :days => 5, :hours => 5, :minutes => 7, :seconds => 9) end def test_next_week |