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

Changeset 9042

Show
Ignore:
Timestamp:
03/17/08 03:45:32 (4 months ago)
Author:
gbuesing
Message:

Adding TimeZone#at and DateTime#to_f

Files:

Legend:

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

    r9041 r9042  
    11*SVN* 
     2 
     3* Adding TimeZone#at and DateTime#to_f [Geoff Buesing] 
    24 
    35* TimeWithZone responds to Ruby 1.9 weekday-named query methods [Geoff Buesing] 
  • trunk/activesupport/lib/active_support/core_ext/date_time/conversions.rb

    r8705 r9042  
    7979          strftime("%Y-%m-%dT%H:%M:%S%Z") 
    8080        end if RUBY_VERSION < '1.9' 
     81         
     82        # Converts self to a floating-point number of seconds since the Unix epoch  
     83        def to_f 
     84          days_since_unix_epoch = self - ::DateTime.civil(1970) 
     85          (days_since_unix_epoch * 86_400).to_f 
     86        end 
    8187      end 
    8288    end 
  • trunk/activesupport/lib/active_support/time_with_zone.rb

    r9041 r9042  
    161161     
    162162    def to_a 
    163       time.to_a[0, 8].push(dst?, zone) 
     163      [time.sec, time.min, time.hour, time.day, time.mon, time.year, time.wday, time.yday, dst?, zone] 
    164164    end 
    165165     
  • trunk/activesupport/lib/active_support/values/time_zone.rb

    r9040 r9042  
    179179    require_library_or_gem "tzinfo" unless Object.const_defined?(:TZInfo) 
    180180     
    181     # Method for creating new ActiveSupport::TimeWithZone instance in time zone of +self+. Example: 
     181    # Method for creating new ActiveSupport::TimeWithZone instance in time zone of +self+ from given values. Example: 
    182182    # 
    183183    #   Time.zone = "Hawaii"                      # => "Hawaii" 
     
    186186      time = Time.utc_time(*args) 
    187187      ActiveSupport::TimeWithZone.new(nil, self, time) 
     188    end 
     189 
     190    # Method for creating new ActiveSupport::TimeWithZone instance in time zone of +self+ from number of seconds since the Unix epoch. Example: 
     191    # 
     192    #   Time.zone = "Hawaii"        # => "Hawaii" 
     193    #   Time.utc(2000).to_f         # => 946684800.0 
     194    #   Time.zone.at(946684800.0)   # => Fri, 31 Dec 1999 14:00:00 HST -10:00 
     195    def at(secs) 
     196      utc = Time.at(secs).utc rescue DateTime.civil(1970).since(secs) 
     197      utc.in_time_zone(self) 
    188198    end 
    189199     
     
    235245  rescue LoadError # Tzinfo gem is not available 
    236246    # re-raise LoadError only when a tzinfo-dependent method is called: 
    237     %w(local now today utc_to_local local_to_utc period_for_utc period_for_local tzinfo).each do |method| 
     247    %w(local at now today utc_to_local local_to_utc period_for_utc period_for_local tzinfo).each do |method| 
    238248      define_method(method) {|*args| raise LoadError, "TZInfo gem is required for TimeZone##{method}. `gem install tzinfo` and try again."} 
    239249    end 
  • trunk/activesupport/test/core_ext/date_time_ext_test.rb

    r8731 r9042  
    272272    assert_equal(-1, DateTime.civil(2000) <=> ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1, 0, 0, 1), TimeZone['UTC'] )) 
    273273  end 
     274   
     275  def test_to_f 
     276    assert_equal 946684800.0, DateTime.civil(2000).to_f 
     277    assert_equal 946684800.0, DateTime.civil(1999,12,31,19,0,0,Rational(-5,24)).to_f 
     278  end 
    274279 
    275280  protected 
  • trunk/activesupport/test/time_zone_test.rb

    r9009 r9042  
    186186    assert_equal 'EDT', twz.zone 
    187187  end 
     188   
     189  def test_at 
     190    zone = TimeZone['Eastern Time (US & Canada)'] 
     191    secs = 946684800.0 
     192    twz = zone.at(secs) 
     193    assert_equal Time.utc(1999,12,31,19), twz.time 
     194    assert_equal Time.utc(2000), twz.utc 
     195    assert_equal zone, twz.time_zone 
     196    assert_equal secs, twz.to_f 
     197  end 
     198   
     199  def test_at_with_old_date 
     200    zone = TimeZone['UTC'] 
     201    secs = -3786825600.0 
     202    twz = zone.at(secs) 
     203    assert_equal [0,0,0,1,1,1850], twz.to_a[0,6] 
     204    assert_equal zone, twz.time_zone 
     205    assert_equal secs, twz.to_f 
     206  end 
    188207 
    189208  protected