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

Changeset 9045

Show
Ignore:
Timestamp:
03/17/08 05:07:50 (6 months ago)
Author:
gbuesing
Message:

Adding TimeZone#parse

Files:

Legend:

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

    r9042 r9045  
    11*SVN* 
     2 
     3* Adding TimeZone#parse [Geoff Buesing] 
    24 
    35* Adding TimeZone#at and DateTime#to_f [Geoff Buesing] 
  • trunk/activesupport/lib/active_support/time_with_zone.rb

    r9042 r9045  
    1111    end 
    1212   
    13     # Returns a Time instance that represents the time in time_zone 
     13    # Returns a Time or DateTime instance that represents the time in time_zone 
    1414    def time 
    1515      @time ||= utc_to_local 
    1616    end 
    1717 
    18     # Returns a Time instance that represents the time in UTC 
     18    # Returns a Time or DateTime instance that represents the time in UTC 
    1919    def utc 
    2020      @utc ||= local_to_utc 
     
    223223    private       
    224224      def get_period_and_ensure_valid_local_time 
    225         @time_zone.period_for_local(@time) 
    226       rescue ::TZInfo::PeriodNotFound 
    227         # time is in the "spring forward" hour gap, so we're moving the time forward one hour and trying again 
    228         @time += 1.hour 
    229         retry 
     225        # we don't want a Time.local instance enforcing its own DST rules as well,  
     226        # so transfer time values to a utc constructor if necessary 
     227        @time = transfer_time_values_to_utc_constructor(@time) unless @time.utc? 
     228        begin 
     229          @time_zone.period_for_local(@time) 
     230        rescue ::TZInfo::PeriodNotFound 
     231          # time is in the "spring forward" hour gap, so we're moving the time forward one hour and trying again 
     232          @time += 1.hour 
     233          retry 
     234        end 
     235      end 
     236       
     237      def transfer_time_values_to_utc_constructor(time) 
     238        ::Time.utc_time(time.year, time.month, time.day, time.hour, time.min, time.sec, time.respond_to?(:usec) ? time.usec : 0) 
    230239      end 
    231240     
  • trunk/activesupport/lib/active_support/values/time_zone.rb

    r9042 r9045  
    198198    end 
    199199     
     200    # Method for creating new ActiveSupport::TimeWithZone instance in time zone of +self+ from parsed string. Example: 
     201    # 
     202    #   Time.zone = "Hawaii"                      # => "Hawaii" 
     203    #   Time.zone.parse('1999-12-31 14:00:00')    # => Fri, 31 Dec 1999 14:00:00 HST -10:00 
     204    # 
     205    # If upper components are missing from the string, they are supplied from TimeZone#now: 
     206    # 
     207    #   Time.zone.now                 # => Fri, 31 Dec 1999 14:00:00 HST -10:00 
     208    #   Time.zone.parse('22:30:00')   # => Fri, 31 Dec 1999 22:30:00 HST -10:00 
     209    def parse(str, now=now) 
     210      time = Time.parse(str, now) rescue DateTime.parse(str) 
     211      ActiveSupport::TimeWithZone.new(nil, self, time) 
     212    end 
     213     
    200214    # Returns an ActiveSupport::TimeWithZone instance representing the current time 
    201215    # in the time zone represented by +self+. Example: 
     
    245259  rescue LoadError # Tzinfo gem is not available 
    246260    # re-raise LoadError only when a tzinfo-dependent method is called: 
    247     %w(local at now today utc_to_local local_to_utc period_for_utc period_for_local tzinfo).each do |method| 
     261    %w(local at parse now today utc_to_local local_to_utc period_for_utc period_for_local tzinfo).each do |method| 
    248262      define_method(method) {|*args| raise LoadError, "TZInfo gem is required for TimeZone##{method}. `gem install tzinfo` and try again."} 
    249263    end 
  • trunk/activesupport/test/time_zone_test.rb

    r9042 r9045  
    155155    assert_equal Time.utc(2007, 2, 5, 15, 30, 45), time.time 
    156156    assert_equal TimeZone["Hawaii"], time.time_zone 
     157  end 
     158   
     159  def test_local_with_old_date 
     160    silence_warnings do # silence warnings raised by tzinfo gem 
     161      time = TimeZone["Hawaii"].local(1850, 2, 5, 15, 30, 45) 
     162      assert_equal [45,30,15,5,2,1850], time.to_a[0,6] 
     163      assert_equal TimeZone["Hawaii"], time.time_zone 
     164    end 
    157165  end 
    158166   
     
    198206   
    199207  def test_at_with_old_date 
    200     zone = TimeZone['UTC'] 
    201     secs = -3786825600.0 
     208    zone = TimeZone['Eastern Time (US & Canada)'] 
     209    secs = DateTime.civil(1850).to_f 
    202210    twz = zone.at(secs) 
    203     assert_equal [0,0,0,1,1,1850], twz.to_a[0,6
     211    assert_equal [1850, 1, 1, 0], [twz.utc.year, twz.utc.mon, twz.utc.day, twz.utc.hour
    204212    assert_equal zone, twz.time_zone 
    205213    assert_equal secs, twz.to_f 
     214  end 
     215   
     216  def test_parse 
     217    zone = TimeZone['Eastern Time (US & Canada)'] 
     218    twz = zone.parse('1999-12-31 19:00:00') 
     219    assert_equal Time.utc(1999,12,31,19), twz.time 
     220    assert_equal Time.utc(2000), twz.utc 
     221    assert_equal zone, twz.time_zone 
     222  end 
     223   
     224  def test_parse_with_old_date 
     225    silence_warnings do # silence warnings raised by tzinfo gem 
     226      zone = TimeZone['Eastern Time (US & Canada)'] 
     227      twz = zone.parse('1850-12-31 19:00:00') 
     228      assert_equal [0,0,19,31,12,1850], twz.to_a[0,6] 
     229      assert_equal zone, twz.time_zone 
     230    end 
     231  end 
     232   
     233  uses_mocha 'TestParseWithIncompleteDate' do 
     234    def test_parse_with_incomplete_date 
     235      zone = TimeZone['Eastern Time (US & Canada)'] 
     236      zone.stubs(:now).returns zone.local(1999,12,31) 
     237      twz = zone.parse('19:00:00') 
     238      assert_equal Time.utc(1999,12,31,19), twz.time 
     239    end 
    206240  end 
    207241