diff options
author | Geoff Buesing <gbuesing@gmail.com> | 2008-01-25 23:55:07 +0000 |
---|---|---|
committer | Geoff Buesing <gbuesing@gmail.com> | 2008-01-25 23:55:07 +0000 |
commit | dad3e835f77d402aad3b0802ee14bf7a20fd9365 (patch) | |
tree | 8242ac550be31bf80cb5ff616cafc563e942d8b2 /activesupport | |
parent | 4b2ac708b7666510d522fc706936d3c2ea5cbb80 (diff) | |
download | rails-dad3e835f77d402aad3b0802ee14bf7a20fd9365.tar.gz rails-dad3e835f77d402aad3b0802ee14bf7a20fd9365.tar.bz2 rails-dad3e835f77d402aad3b0802ee14bf7a20fd9365.zip |
Time#- coerces TimeWithZone argument to a Time instance so that difference in seconds can be calculated. Closes #10914 [Geoff Buesing, yyyc514]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8730 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activesupport')
-rw-r--r-- | activesupport/CHANGELOG | 2 | ||||
-rw-r--r-- | activesupport/lib/active_support/core_ext/time/calculations.rb | 11 | ||||
-rw-r--r-- | activesupport/test/core_ext/time_ext_test.rb | 4 | ||||
-rw-r--r-- | activesupport/test/core_ext/time_with_zone_test.rb | 10 |
4 files changed, 27 insertions, 0 deletions
diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG index 40ddbf473e..3883925fc8 100644 --- a/activesupport/CHANGELOG +++ b/activesupport/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Time#- coerces TimeWithZone argument to a Time instance so that difference in seconds can be calculated. Closes #10914 [Geoff Buesing, yyyc514] + * Adding UTC zone to TimeZone; TimeWithZone no longer has to fake UTC zone with nil [Geoff Buesing] * Time.get_zone refactored to private method, given that the encapsulated logic is only useful internally [Geoff Buesing] diff --git a/activesupport/lib/active_support/core_ext/time/calculations.rb b/activesupport/lib/active_support/core_ext/time/calculations.rb index eb342084a8..7fbfca5b2a 100644 --- a/activesupport/lib/active_support/core_ext/time/calculations.rb +++ b/activesupport/lib/active_support/core_ext/time/calculations.rb @@ -13,6 +13,9 @@ module ActiveSupport #:nodoc: alias_method :minus_without_duration, :- alias_method :-, :minus_with_duration + alias_method :minus_without_coercion, :- + alias_method :-, :minus_with_coercion + alias_method :compare_without_coercion, :<=> alias_method :<=>, :compare_with_coercion end @@ -218,6 +221,14 @@ module ActiveSupport #:nodoc: end end + # Time#- can also be used to determine the number of seconds between two Time instances. + # We're layering on additional behavior so that ActiveSupport::TimeWithZone instances + # are coerced into values that Time#- will recognize + def minus_with_coercion(other) + other = other.comparable_time if other.respond_to?(:comparable_time) + minus_without_coercion(other) + end + # Layers additional behavior on Time#<=> so that DateTime and ActiveSupport::TimeWithZone instances # can be chronologically compared with a Time def compare_with_coercion(other) diff --git a/activesupport/test/core_ext/time_ext_test.rb b/activesupport/test/core_ext/time_ext_test.rb index 2377d3c63c..fef8a521da 100644 --- a/activesupport/test/core_ext/time_ext_test.rb +++ b/activesupport/test/core_ext/time_ext_test.rb @@ -454,6 +454,10 @@ class TimeExtCalculationsTest < Test::Unit::TestCase assert_equal 0, Time.utc(2000) <=> ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1, 0, 0, 0), TimeZone['UTC'] ) assert_equal(-1, Time.utc(2000) <=> ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1, 0, 0, 1), TimeZone['UTC'] )) end + + def test_minus_with_time_with_zone + assert_equal 86_400.0, Time.utc(2000, 1, 2) - ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1), TimeZone['UTC'] ) + end protected def with_timezone(new_tz = 'US/Eastern') diff --git a/activesupport/test/core_ext/time_with_zone_test.rb b/activesupport/test/core_ext/time_with_zone_test.rb index 1a2f3e6e47..9276656db5 100644 --- a/activesupport/test/core_ext/time_with_zone_test.rb +++ b/activesupport/test/core_ext/time_with_zone_test.rb @@ -121,6 +121,16 @@ uses_tzinfo 'TimeWithZoneTest' do def test_minus_with_duration assert_equal Time.utc(1999, 12, 26, 19, 0 ,0), (@twz - 5.days).time end + + def test_minus_with_time + assert_equal 86_400.0, ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 2), TimeZone['UTC'] ) - Time.utc(2000, 1, 1) + end + + def test_minus_with_time_with_zone + twz1 = ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1), TimeZone['UTC'] ) + twz2 = ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 2), TimeZone['UTC'] ) + assert_equal 86_400.0, twz2 - twz1 + end def test_to_time assert_equal @twz, @twz.to_time |