Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 8730

Show
Ignore:
Timestamp:
01/25/08 23:55:07 (8 months ago)
Author:
gbuesing
Message:

Time#- coerces TimeWithZone argument to a Time instance so that difference in seconds can be calculated. Closes #10914 [Geoff Buesing, yyyc514]

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activesupport/CHANGELOG

    r8720 r8730  
    11*SVN* 
     2 
     3* Time#- coerces TimeWithZone argument to a Time instance so that difference in seconds can be calculated. Closes #10914 [Geoff Buesing, yyyc514] 
    24 
    35* Adding UTC zone to TimeZone; TimeWithZone no longer has to fake UTC zone with nil [Geoff Buesing] 
  • trunk/activesupport/lib/active_support/core_ext/time/calculations.rb

    r8715 r8730  
    1313            alias_method :minus_without_duration, :- 
    1414            alias_method :-, :minus_with_duration 
     15             
     16            alias_method :minus_without_coercion, :- 
     17            alias_method :-, :minus_with_coercion 
    1518             
    1619            alias_method :compare_without_coercion, :<=> 
     
    219222        end 
    220223         
     224        # Time#- can also be used to determine the number of seconds between two Time instances. 
     225        # We're layering on additional behavior so that ActiveSupport::TimeWithZone instances 
     226        # are coerced into values that Time#- will recognize 
     227        def minus_with_coercion(other) 
     228          other = other.comparable_time if other.respond_to?(:comparable_time) 
     229          minus_without_coercion(other) 
     230        end 
     231         
    221232        # Layers additional behavior on Time#<=> so that DateTime and ActiveSupport::TimeWithZone instances 
    222233        # can be chronologically compared with a Time 
  • trunk/activesupport/test/core_ext/time_ext_test.rb

    r8720 r8730  
    455455    assert_equal(-1, Time.utc(2000) <=> ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1, 0, 0, 1), TimeZone['UTC'] )) 
    456456  end 
     457   
     458  def test_minus_with_time_with_zone 
     459    assert_equal  86_400.0, Time.utc(2000, 1, 2) - ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1), TimeZone['UTC'] ) 
     460  end 
    457461 
    458462  protected 
  • trunk/activesupport/test/core_ext/time_with_zone_test.rb

    r8720 r8730  
    121121    def test_minus_with_duration 
    122122      assert_equal Time.utc(1999, 12, 26, 19, 0 ,0), (@twz - 5.days).time 
     123    end 
     124     
     125    def test_minus_with_time 
     126      assert_equal  86_400.0,  ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 2), TimeZone['UTC'] ) - Time.utc(2000, 1, 1) 
     127    end 
     128     
     129    def test_minus_with_time_with_zone 
     130      twz1 = ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1), TimeZone['UTC'] ) 
     131      twz2 = ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 2), TimeZone['UTC'] ) 
     132      assert_equal  86_400.0,  twz2 - twz1 
    123133    end 
    124134