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

Changeset 8649

Show
Ignore:
Timestamp:
01/16/08 20:07:10 (8 months ago)
Author:
gbuesing
Message:

Introducing DateTime #utc, #utc? and #utc_offset, for duck-typing compatibility with Time. Closes #10002

Files:

Legend:

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

    r8636 r8649  
    11*SVN* 
     2 
     3* Introducing DateTime #utc, #utc? and #utc_offset, for duck-typing compatibility with Time. Closes #10002 [Geoff Buesing] 
    24 
    35* Time#to_json uses Numeric#to_utc_offset_s to output a cross-platform-consistent representation without having to convert to DateTime. References #9750 [Geoff Buesing] 
  • trunk/activesupport/lib/active_support/core_ext/date_time/calculations.rb

    r8076 r8649  
    7272          change(:hour => 23, :min => 59, :sec => 59) 
    7373        end 
     74         
     75        # Adjusts DateTime to UTC by adding its offset value; offset is set to 0 
     76        # 
     77        # Example: 
     78        # 
     79        #   DateTime.civil(2005, 2, 21, 10, 11, 12, Rational(-6, 24))       # => Mon, 21 Feb 2005 10:11:12 -0600 
     80        #   DateTime.civil(2005, 2, 21, 10, 11, 12, Rational(-6, 24)).utc   # => Mon, 21 Feb 2005 16:11:12 +0000 
     81        def utc 
     82          new_offset(0) 
     83        end 
     84         
     85        # Returns true if offset == 0 
     86        def utc? 
     87          offset == 0 
     88        end 
     89         
     90        # Returns the offset value in seconds 
     91        def utc_offset 
     92          (offset * 86400).to_i 
     93        end 
    7494      end 
    7595    end 
  • trunk/activesupport/test/core_ext/date_time_ext_test.rb

    r8563 r8649  
    220220    end 
    221221  end 
     222   
     223  def test_utc? 
     224    assert_equal true, DateTime.civil(2005, 2, 21, 10, 11, 12).utc? 
     225    assert_equal true, DateTime.civil(2005, 2, 21, 10, 11, 12, 0).utc? 
     226    assert_equal false, DateTime.civil(2005, 2, 21, 10, 11, 12, 0.25).utc? 
     227    assert_equal false, DateTime.civil(2005, 2, 21, 10, 11, 12, -0.25).utc? 
     228  end 
     229   
     230  def test_utc_offset 
     231    assert_equal 0, DateTime.civil(2005, 2, 21, 10, 11, 12).utc_offset 
     232    assert_equal 0, DateTime.civil(2005, 2, 21, 10, 11, 12, 0).utc_offset 
     233    assert_equal 21600, DateTime.civil(2005, 2, 21, 10, 11, 12, 0.25).utc_offset 
     234    assert_equal( -21600, DateTime.civil(2005, 2, 21, 10, 11, 12, -0.25).utc_offset ) 
     235    assert_equal( -18000, DateTime.civil(2005, 2, 21, 10, 11, 12, Rational(-5, 24)).utc_offset ) 
     236  end 
     237   
     238  def test_utc 
     239    assert_equal DateTime.civil(2005, 2, 21, 16, 11, 12, 0), DateTime.civil(2005, 2, 21, 10, 11, 12, Rational(-6, 24)).utc 
     240    assert_equal DateTime.civil(2005, 2, 21, 15, 11, 12, 0), DateTime.civil(2005, 2, 21, 10, 11, 12, Rational(-5, 24)).utc 
     241    assert_equal DateTime.civil(2005, 2, 21, 10, 11, 12, 0), DateTime.civil(2005, 2, 21, 10, 11, 12, 0).utc 
     242    assert_equal DateTime.civil(2005, 2, 21, 9, 11, 12, 0), DateTime.civil(2005, 2, 21, 10, 11, 12, Rational(1, 24)).utc 
     243  end 
    222244 
    223245  protected