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

Changeset 8852

Show
Ignore:
Timestamp:
02/10/08 20:04:14 (5 months ago)
Author:
gbuesing
Message:

Adding TimeWithZone #to_a, #to_f, #to_i, #httpdate, #rfc2822

Files:

Legend:

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

    r8851 r8852  
    11*SVN* 
     2 
     3* Adding TimeWithZone #to_a, #to_f, #to_i, #httpdate, #rfc2822 [Geoff Buesing] 
    24 
    35* Pruning unneeded TimeWithZone#change_time_zone_to_current [Geoff Buesing] 
  • trunk/activesupport/lib/active_support/time_with_zone.rb

    r8851 r8852  
    4545    # Returns a Time.local() instance of the simultaneous time in your system's ENV['TZ'] zone 
    4646    def localtime 
    47       utc.dup.localtime # use #dup because Time#localtime is destructive 
     47      utc.getlocal 
    4848    end 
    4949   
     
    8080      %("#{time.strftime("%Y/%m/%d %H:%M:%S")} #{formatted_offset(false)}") 
    8181    end 
     82     
     83    def httpdate 
     84      utc.httpdate 
     85    end 
     86   
     87    def rfc2822 
     88      to_s(:rfc822) 
     89    end 
     90    alias_method :rfc822, :rfc2822 
    8291   
    8392    # :db format outputs time in UTC; all others output time in local. Uses TimeWithZone's strftime, so %Z and %z work correctly 
     
    107116    def -(other) 
    108117      other.acts_like?(:time) ? utc - other : method_missing(:-, other) 
     118    end 
     119     
     120    def to_a 
     121      time.to_a[0, 8].push(dst?, zone) 
     122    end 
     123     
     124    def to_f 
     125      utc.to_f 
     126    end     
     127     
     128    def to_i 
     129      utc.to_i 
    109130    end 
    110131   
  • trunk/activesupport/test/core_ext/time_with_zone_test.rb

    r8850 r8852  
    8383    end 
    8484     
     85    def test_httpdate 
     86      assert_equal 'Sat, 01 Jan 2000 00:00:00 GMT', @twz.httpdate 
     87    end 
     88     
     89    def test_rfc2822 
     90      assert_equal "Fri, 31 Dec 1999 19:00:00 -0500", @twz.rfc2822 
     91    end 
     92     
    8593    def test_compare_with_time 
    8694      assert_equal  1, @twz <=> Time.utc(1999, 12, 31, 23, 59, 59) 
     
    126134      twz2 = ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 2), TimeZone['UTC'] ) 
    127135      assert_equal  86_400.0,  twz2 - twz1 
     136    end 
     137     
     138    def test_to_a 
     139      assert_equal [45, 30, 5, 1, 2, 2000, 2, 32, false, "HST"], ActiveSupport::TimeWithZone.new( Time.utc(2000, 2, 1, 15, 30, 45), TimeZone['Hawaii'] ).to_a 
     140    end 
     141     
     142    def test_to_f 
     143      result = ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1), TimeZone['Hawaii'] ).to_f 
     144      assert_equal 946684800.0, result 
     145      assert result.is_a?(Float) 
     146    end 
     147     
     148    def test_to_i 
     149      result = ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1), TimeZone['Hawaii'] ).to_i 
     150      assert_equal 946684800, result 
     151      assert result.is_a?(Integer) 
    128152    end 
    129153