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

Changeset 8854

Show
Ignore:
Timestamp:
02/10/08 22:26:16 (5 months ago)
Author:
gbuesing
Message:

Adding TimeWithZone #to_yaml, #to_datetime, #eql? and method aliases for duck-typing compatibility with Time

Files:

Legend:

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

    r8853 r8854  
    11*SVN* 
     2 
     3* Adding TimeWithZone #to_yaml, #to_datetime, #eql? and method aliases for duck-typing compatibility with Time [Geoff Buesing] 
    24 
    35* TimeWithZone #in_time_zone returns +self+ if zone argument is the same as #time_zone [Geoff Buesing] 
  • trunk/activesupport/lib/active_support/time_with_zone.rb

    r8853 r8854  
    2222    end 
    2323    alias_method :comparable_time, :utc 
     24    alias_method :getgm, :utc 
     25    alias_method :getutc, :utc 
     26    alias_method :gmtime, :utc 
    2427   
    2528    # Returns the underlying TZInfo::TimezonePeriod for the local time 
     
    4851      utc.getlocal 
    4952    end 
     53    alias_method :getlocal, :localtime 
    5054   
    5155    def dst? 
    5256      period.dst? 
    5357    end 
     58    alias_method :isdst, :dst? 
    5459   
    5560    def utc? 
    5661      time_zone.name == 'UTC' 
    5762    end 
     63    alias_method :gmt?, :utc? 
    5864   
    5965    def utc_offset 
    6066      period.utc_total_offset 
    6167    end 
     68    alias_method :gmt_offset, :utc_offset 
     69    alias_method :gmtoff, :utc_offset 
    6270   
    6371    def formatted_offset(colon = true, alternate_utc_string = nil) 
     
    7785      "#{time.strftime("%Y-%m-%dT%H:%M:%S")}#{formatted_offset(true, 'Z')}" 
    7886    end 
     87    alias_method :iso8601, :xmlschema 
    7988   
    8089    def to_json(options = nil) 
    8190      %("#{time.strftime("%Y/%m/%d %H:%M:%S")} #{formatted_offset(false)}") 
     91    end 
     92     
     93    def to_yaml(options = {}) 
     94      time.to_yaml(options).gsub('Z', formatted_offset(true, 'Z')) 
    8295    end 
    8396     
     
    113126    end 
    114127     
     128    def eql?(other) 
     129      utc == other 
     130    end 
     131     
    115132    # Need to override #- to intercept situation where a Time or Time With Zone object is passed in 
    116133    # Otherwise, just pass on to method missing 
     
    130147      utc.to_i 
    131148    end 
     149    alias_method :hash, :to_i 
     150    alias_method :tv_sec, :to_i 
    132151   
    133152    # A TimeProxy acts like a Time, so just return self 
    134153    def to_time 
    135154      self 
     155    end 
     156     
     157    def to_datetime 
     158      utc.to_datetime.new_offset(Rational(utc_offset, 86_400)) 
    136159    end 
    137160     
  • trunk/activesupport/test/core_ext/time_with_zone_test.rb

    r8853 r8854  
    8787    end 
    8888     
     89    def test_to_yaml 
     90      assert_equal "--- 1999-12-31 19:00:00 -05:00\n", @twz.to_yaml 
     91    end 
     92     
    8993    def test_httpdate 
    9094      assert_equal 'Sat, 01 Jan 2000 00:00:00 GMT', @twz.httpdate 
     
    112116      assert_equal(-1, @twz <=> ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1, 0, 0, 1), TimeZone['UTC'] )) 
    113117    end 
     118     
     119    def test_eql? 
     120      assert @twz.eql?(Time.utc(2000)) 
     121      assert @twz.eql?( ActiveSupport::TimeWithZone.new(Time.utc(2000), TimeZone["Hawaii"]) ) 
     122    end 
    114123       
    115124    def test_plus 
     
    158167    def test_to_time 
    159168      assert_equal @twz, @twz.to_time 
     169    end 
     170     
     171    def test_to_datetime 
     172      assert_equal DateTime.civil(1999, 12, 31, 19, 0, 0, Rational(-18_000, 86_400)),  @twz.to_datetime 
    160173    end 
    161174