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

Changeset 9071

Show
Ignore:
Timestamp:
03/21/08 22:48:00 (4 months ago)
Author:
gbuesing
Message:

Make TimeWithZone work with tzinfo 0.2.x: use TZInfo::Timezone#zone_identifier alias for #abbreviation, silence warnings on tests. Raise LoadError when TZInfo version is < 0.2 by sniffing for TZInfo::TimeOrDateTime constant. Move all tzinfo-dependent TimeZone tests into uses_tzinfo block

Files:

Legend:

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

    r9047 r9071  
    11*SVN* 
     2 
     3* Make TimeWithZone work with tzinfo 0.2.x: use TZInfo::Timezone#zone_identifier alias for #abbreviation, silence warnings on tests. Raise LoadError when TZInfo version is < 0.2 by sniffing for TZInfo::TimeOrDateTime constant. Move all tzinfo-dependent TimeZone tests into uses_tzinfo block [Geoff Buesing] 
    24 
    35* Time, DateTime and TimeWithZone #in_time_zone defaults to Time.zone. Removing now unneeded #in_current_time_zone [Geoff Buesing] 
  • trunk/activesupport/lib/active_support/time_with_zone.rb

    r9047 r9071  
    6464    # Time uses #zone to display the time zone abbreviation, so we're duck-typing it 
    6565    def zone 
    66       period.abbreviation.to_s 
     66      period.zone_identifier.to_s 
    6767    end 
    6868   
  • trunk/activesupport/lib/active_support/values/time_zone.rb

    r9046 r9071  
    178178  begin # the following methods depend on the tzinfo gem 
    179179    require_library_or_gem "tzinfo" unless Object.const_defined?(:TZInfo) 
     180    raise LoadError unless TZInfo.const_defined?(:TimeOrDateTime) 
    180181     
    181182    # Method for creating new ActiveSupport::TimeWithZone instance in time zone of +self+ from given values. Example: 
     
    254255    # re-raise LoadError only when a tzinfo-dependent method is called: 
    255256    %w(local at parse now today utc_to_local local_to_utc period_for_utc period_for_local tzinfo).each do |method| 
    256       define_method(method) {|*args| raise LoadError, "TZInfo gem is required for TimeZone##{method}. `gem install tzinfo` and try again."} 
     257      define_method(method) {|*args| raise LoadError, "TZInfo version >= 0.2 is required for TimeZone##{method}(). `gem install tzinfo` and try again."} 
    257258    end 
    258259  end 
  • trunk/activesupport/test/abstract_unit.rb

    r8679 r9071  
    2424unless defined? uses_tzinfo 
    2525  def uses_tzinfo(test_name, &block) 
    26     uses_gem('tzinfo', test_name, &block) 
     26    uses_gem('tzinfo', test_name, '>= 0.2.0', &block) 
    2727  end 
    2828end 
  • trunk/activesupport/test/core_ext/time_with_zone_test.rb

    r9047 r9071  
    1616   
    1717    def test_time 
    18       assert_equal Time.utc(1999, 12, 31, 19), @twz.time 
     18      silence_warnings do # silence warnings raised by tzinfo gem 
     19        assert_equal Time.utc(1999, 12, 31, 19), @twz.time 
     20      end 
    1921    end 
    2022   
     
    4345       
    4446    def test_formatted_offset 
    45       assert_equal '-05:00', @twz.formatted_offset 
    46       assert_equal '-04:00', ActiveSupport::TimeWithZone.new(Time.utc(2000, 6), @time_zone).formatted_offset #dst 
     47      silence_warnings do # silence warnings raised by tzinfo gem 
     48        assert_equal '-05:00', @twz.formatted_offset 
     49        assert_equal '-04:00', ActiveSupport::TimeWithZone.new(Time.utc(2000, 6), @time_zone).formatted_offset #dst 
     50      end 
    4751    end 
    4852       
     
    5559       
    5660    def test_zone 
    57       assert_equal 'EST', @twz.zone 
    58       assert_equal 'EDT', ActiveSupport::TimeWithZone.new(Time.utc(2000, 6), @time_zone).zone #dst 
     61      silence_warnings do # silence warnings raised by tzinfo gem 
     62        assert_equal 'EST', @twz.zone 
     63        assert_equal 'EDT', ActiveSupport::TimeWithZone.new(Time.utc(2000, 6), @time_zone).zone #dst 
     64      end 
    5965    end 
    6066       
    6167    def test_to_json 
    62       assert_equal "\"1999/12/31 19:00:00 -0500\"", @twz.to_json 
     68      silence_warnings do # silence warnings raised by tzinfo gem 
     69        assert_equal "\"1999/12/31 19:00:00 -0500\"", @twz.to_json 
     70      end 
    6371    end 
    6472       
    6573    def test_strftime 
    66       assert_equal '1999-12-31 19:00:00 EST -0500', @twz.strftime('%Y-%m-%d %H:%M:%S %Z %z') 
     74      silence_warnings do # silence warnings raised by tzinfo gem 
     75        assert_equal '1999-12-31 19:00:00 EST -0500', @twz.strftime('%Y-%m-%d %H:%M:%S %Z %z') 
     76      end 
    6777    end 
    6878       
    6979    def test_inspect 
    70       assert_equal 'Fri, 31 Dec 1999 19:00:00 EST -05:00', @twz.inspect 
     80      silence_warnings do # silence warnings raised by tzinfo gem 
     81        assert_equal 'Fri, 31 Dec 1999 19:00:00 EST -05:00', @twz.inspect 
     82      end 
    7183    end 
    7284       
    7385    def test_to_s 
    74       assert_equal '1999-12-31 19:00:00 -0500', @twz.to_s 
     86      silence_warnings do # silence warnings raised by tzinfo gem 
     87        assert_equal '1999-12-31 19:00:00 -0500', @twz.to_s 
     88      end 
    7589    end 
    7690       
    7791    def test_to_s_db 
    78       assert_equal '2000-01-01 00:00:00', @twz.to_s(:db) 
     92      silence_warnings do # silence warnings raised by tzinfo gem 
     93        assert_equal '2000-01-01 00:00:00', @twz.to_s(:db) 
     94      end 
    7995    end 
    8096       
    8197    def test_xmlschema 
    82       assert_equal "1999-12-31T19:00:00-05:00", @twz.xmlschema 
     98      silence_warnings do # silence warnings raised by tzinfo gem 
     99        assert_equal "1999-12-31T19:00:00-05:00", @twz.xmlschema 
     100      end 
    83101    end 
    84102     
    85103    def test_to_yaml 
    86       assert_equal "--- 1999-12-31 19:00:00 -05:00\n", @twz.to_yaml 
     104      silence_warnings do # silence warnings raised by tzinfo gem 
     105        assert_equal "--- 1999-12-31 19:00:00 -05:00\n", @twz.to_yaml 
     106      end 
    87107    end 
    88108     
    89109    def test_httpdate 
    90       assert_equal 'Sat, 01 Jan 2000 00:00:00 GMT', @twz.httpdate 
     110      silence_warnings do # silence warnings raised by tzinfo gem 
     111        assert_equal 'Sat, 01 Jan 2000 00:00:00 GMT', @twz.httpdate 
     112      end 
    91113    end 
    92114     
    93115    def test_rfc2822 
    94       assert_equal "Fri, 31 Dec 1999 19:00:00 -0500", @twz.rfc2822 
     116      silence_warnings do # silence warnings raised by tzinfo gem 
     117        assert_equal "Fri, 31 Dec 1999 19:00:00 -0500", @twz.rfc2822 
     118      end 
    95119    end 
    96120     
     
    124148       
    125149    def test_plus_with_integer 
    126       assert_equal Time.utc(1999, 12, 31, 19, 0 ,5), (@twz + 5).time 
     150      silence_warnings do # silence warnings raised by tzinfo gem 
     151        assert_equal Time.utc(1999, 12, 31, 19, 0 ,5), (@twz + 5).time 
     152      end 
    127153    end 
    128154       
    129155    def test_plus_with_integer_when_self_wraps_datetime 
    130       datetime = DateTime.civil(2000, 1, 1, 0) 
    131       twz = ActiveSupport::TimeWithZone.new(datetime, @time_zone) 
    132       assert_equal DateTime.civil(1999, 12, 31, 19, 0 ,5), (twz + 5).time 
     156      silence_warnings do # silence warnings raised by tzinfo gem 
     157        datetime = DateTime.civil(2000, 1, 1, 0) 
     158        twz = ActiveSupport::TimeWithZone.new(datetime, @time_zone) 
     159        assert_equal DateTime.civil(1999, 12, 31, 19, 0 ,5), (twz + 5).time 
     160      end 
    133161    end 
    134162       
    135163    def test_plus_with_duration 
    136       assert_equal Time.utc(2000, 1, 5, 19, 0 ,0), (@twz + 5.days).time 
     164      silence_warnings do # silence warnings raised by tzinfo gem 
     165        assert_equal Time.utc(2000, 1, 5, 19, 0 ,0), (@twz + 5.days).time 
     166      end 
    137167    end 
    138168       
    139169    def test_minus_with_integer 
    140       assert_equal Time.utc(1999, 12, 31, 18, 59 ,55), (@twz - 5).time 
     170      silence_warnings do # silence warnings raised by tzinfo gem 
     171        assert_equal Time.utc(1999, 12, 31, 18, 59 ,55), (@twz - 5).time 
     172      end 
    141173    end 
    142174     
    143175    def test_minus_with_integer_when_self_wraps_datetime 
    144       datetime = DateTime.civil(2000, 1, 1, 0) 
    145       twz = ActiveSupport::TimeWithZone.new(datetime, @time_zone) 
    146       assert_equal DateTime.civil(1999, 12, 31, 18, 59 ,55), (twz - 5).time 
     176      silence_warnings do # silence warnings raised by tzinfo gem 
     177        datetime = DateTime.civil(2000, 1, 1, 0) 
     178        twz = ActiveSupport::TimeWithZone.new(datetime, @time_zone) 
     179        assert_equal DateTime.civil(1999, 12, 31, 18, 59 ,55), (twz - 5).time 
     180      end 
    147181    end 
    148182       
    149183    def test_minus_with_duration 
    150       assert_equal Time.utc(1999, 12, 26, 19, 0 ,0), (@twz - 5.days).time 
     184      silence_warnings do # silence warnings raised by tzinfo gem 
     185        assert_equal Time.utc(1999, 12, 26, 19, 0 ,0), (@twz - 5.days).time 
     186      end 
    151187    end 
    152188     
     
    163199     
    164200    def test_plus_and_minus_enforce_spring_dst_rules 
    165       utc = Time.utc(2006,4,2,6,59,59) # == Apr 2 2006 01:59:59 EST; i.e., 1 second before daylight savings start 
    166       twz = ActiveSupport::TimeWithZone.new(utc, @time_zone) 
    167       assert_equal Time.utc(2006,4,2,1,59,59), twz.time 
    168       assert_equal false, twz.dst? 
    169       assert_equal 'EST', twz.zone 
    170       twz = twz + 1 
    171       assert_equal Time.utc(2006,4,2,3), twz.time # adding 1 sec springs forward to 3:00AM EDT 
    172       assert_equal true, twz.dst? 
    173       assert_equal 'EDT', twz.zone 
    174       twz = twz - 1 # subtracting 1 second takes goes back to 1:59:59AM EST 
    175       assert_equal Time.utc(2006,4,2,1,59,59), twz.time 
    176       assert_equal false, twz.dst? 
    177       assert_equal 'EST', twz.zone 
     201      silence_warnings do # silence warnings raised by tzinfo gem 
     202        utc = Time.utc(2006,4,2,6,59,59) # == Apr 2 2006 01:59:59 EST; i.e., 1 second before daylight savings start 
     203        twz = ActiveSupport::TimeWithZone.new(utc, @time_zone) 
     204        assert_equal Time.utc(2006,4,2,1,59,59), twz.time 
     205        assert_equal false, twz.dst? 
     206        assert_equal 'EST', twz.zone 
     207        twz = twz + 1 
     208        assert_equal Time.utc(2006,4,2,3), twz.time # adding 1 sec springs forward to 3:00AM EDT 
     209        assert_equal true, twz.dst? 
     210        assert_equal 'EDT', twz.zone 
     211        twz = twz - 1 # subtracting 1 second takes goes back to 1:59:59AM EST 
     212        assert_equal Time.utc(2006,4,2,1,59,59), twz.time 
     213        assert_equal false, twz.dst? 
     214        assert_equal 'EST', twz.zone 
     215      end 
    178216    end 
    179217     
    180218    def test_plus_and_minus_enforce_fall_dst_rules 
    181       utc = Time.utc(2006,10,29,5,59,59) # == Oct 29 2006 01:59:59 EST; i.e., 1 second before daylight savings end 
    182       twz = ActiveSupport::TimeWithZone.new(utc, @time_zone) 
    183       assert_equal Time.utc(2006,10,29,1,59,59), twz.time 
    184       assert_equal true, twz.dst? 
    185       assert_equal 'EDT', twz.zone 
    186       twz = twz + 1 
    187       assert_equal Time.utc(2006,10,29,1), twz.time # adding 1 sec falls back from 1:59:59 EDT to 1:00AM EST 
    188       assert_equal false, twz.dst? 
    189       assert_equal 'EST', twz.zone 
    190       twz = twz - 1 
    191       assert_equal Time.utc(2006,10,29,1,59,59), twz.time # subtracting 1 sec goes back to 1:59:59AM EDT 
    192       assert_equal true, twz.dst? 
    193       assert_equal 'EDT', twz.zone 
     219      silence_warnings do # silence warnings raised by tzinfo gem 
     220        utc = Time.utc(2006,10,29,5,59,59) # == Oct 29 2006 01:59:59 EST; i.e., 1 second before daylight savings end 
     221        twz = ActiveSupport::TimeWithZone.new(utc, @time_zone) 
     222        assert_equal Time.utc(2006,10,29,1,59,59), twz.time 
     223        assert_equal true, twz.dst? 
     224        assert_equal 'EDT', twz.zone 
     225        twz = twz + 1 
     226        assert_equal Time.utc(2006,10,29,1), twz.time # adding 1 sec falls back from 1:59:59 EDT to 1:00AM EST 
     227        assert_equal false, twz.dst? 
     228        assert_equal 'EST', twz.zone 
     229        twz = twz - 1 
     230        assert_equal Time.utc(2006,10,29,1,59,59), twz.time # subtracting 1 sec goes back to 1:59:59AM EDT 
     231        assert_equal true, twz.dst? 
     232        assert_equal 'EDT', twz.zone 
     233      end 
    194234    end 
    195235     
    196236    def test_to_a 
    197       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 
     237      silence_warnings do # silence warnings raised by tzinfo gem 
     238        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 
     239      end 
    198240    end 
    199241     
     
    215257     
    216258    def test_to_datetime 
    217       assert_equal DateTime.civil(1999, 12, 31, 19, 0, 0, Rational(-18_000, 86_400)),  @twz.to_datetime 
     259      silence_warnings do # silence warnings raised by tzinfo gem 
     260        assert_equal DateTime.civil(1999, 12, 31, 19, 0, 0, Rational(-18_000, 86_400)),  @twz.to_datetime 
     261      end 
    218262    end 
    219263       
     
    235279       
    236280    def test_method_missing_with_time_return_value 
    237       assert_instance_of ActiveSupport::TimeWithZone, @twz.months_since(1) 
    238       assert_equal Time.utc(2000, 1, 31, 19, 0 ,0), @twz.months_since(1).time 
     281      silence_warnings do # silence warnings raised by tzinfo gem 
     282        assert_instance_of ActiveSupport::TimeWithZone, @twz.months_since(1) 
     283        assert_equal Time.utc(2000, 1, 31, 19, 0 ,0), @twz.months_since(1).time 
     284      end 
    239285    end 
    240286     
    241287    def test_marshal_dump_and_load 
    242       marshal_str = Marshal.dump(@twz) 
    243       mtime = Marshal.load(marshal_str) 
    244       assert_equal Time.utc(2000, 1, 1, 0), mtime.utc 
    245       assert_equal TimeZone['Eastern Time (US & Canada)'], mtime.time_zone 
    246       assert_equal Time.utc(1999, 12, 31, 19), mtime.time 
     288      silence_warnings do # silence warnings raised by tzinfo gem 
     289        marshal_str = Marshal.dump(@twz) 
     290        mtime = Marshal.load(marshal_str) 
     291        assert_equal Time.utc(2000, 1, 1, 0), mtime.utc 
     292        assert_equal TimeZone['Eastern Time (US & Canada)'], mtime.time_zone 
     293        assert_equal Time.utc(1999, 12, 31, 19), mtime.time 
     294      end 
    247295    end 
    248296       
    249297    def test_method_missing_with_non_time_return_value 
    250       twz = ActiveSupport::TimeWithZone.new(Time.utc(1999,12,31,19,18,17,500), @time_zone) 
    251       assert_equal 1999, twz.year 
    252       assert_equal 12, twz.month 
    253       assert_equal 31, twz.day 
    254       assert_equal 14, twz.hour 
    255       assert_equal 18, twz.min 
    256       assert_equal 17, twz.sec 
    257       assert_equal 500, twz.usec 
     298      silence_warnings do # silence warnings raised by tzinfo gem 
     299        twz = ActiveSupport::TimeWithZone.new(Time.utc(1999,12,31,19,18,17,500), @time_zone) 
     300        assert_equal 1999, twz.year 
     301        assert_equal 12, twz.month 
     302        assert_equal 31, twz.day 
     303        assert_equal 14, twz.hour 
     304        assert_equal 18, twz.min 
     305        assert_equal 17, twz.sec 
     306        assert_equal 500, twz.usec 
     307      end 
    258308    end 
    259309     
    260310    def test_utc_to_local_conversion_saves_period_in_instance_variable 
    261       assert_nil @twz.instance_variable_get('@period') 
    262       @twz.time 
    263       assert_kind_of TZInfo::TimezonePeriod, @twz.instance_variable_get('@period') 
     311      silence_warnings do # silence warnings raised by tzinfo gem 
     312        assert_nil @twz.instance_variable_get('@period') 
     313        @twz.time 
     314        assert_kind_of TZInfo::TimezonePeriod, @twz.instance_variable_get('@period') 
     315      end 
    264316    end 
    265317     
    266318    def test_instance_created_with_local_time_returns_correct_utc_time 
    267       twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(1999, 12, 31, 19)) 
    268       assert_equal Time.utc(2000), twz.utc 
     319      silence_warnings do # silence warnings raised by tzinfo gem 
     320        twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(1999, 12, 31, 19)) 
     321        assert_equal Time.utc(2000), twz.utc 
     322      end 
    269323    end 
    270324     
    271325    def test_instance_created_with_local_time_enforces_spring_dst_rules 
    272       twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(2006,4,2,2)) # first second of DST 
    273       assert_equal Time.utc(2006,4,2,3), twz.time # springs forward to 3AM 
    274       assert_equal true, twz.dst? 
    275       assert_equal 'EDT', twz.zone 
     326      silence_warnings do # silence warnings raised by tzinfo gem 
     327        twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(2006,4,2,2)) # first second of DST 
     328        assert_equal Time.utc(2006,4,2,3), twz.time # springs forward to 3AM 
     329        assert_equal true, twz.dst? 
     330        assert_equal 'EDT', twz.zone 
     331      end 
    276332    end 
    277333     
    278334    def test_instance_created_with_local_time_enforces_fall_dst_rules 
    279       twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(2006,10,29,1)) # 1AM can be either DST or non-DST; we'll pick DST 
    280       assert_equal Time.utc(2006,10,29,1), twz.time 
    281       assert_equal true, twz.dst? 
    282       assert_equal 'EDT', twz.zone 
     335      silence_warnings do # silence warnings raised by tzinfo gem 
     336        twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(2006,10,29,1)) # 1AM can be either DST or non-DST; we'll pick DST 
     337        assert_equal Time.utc(2006,10,29,1), twz.time 
     338        assert_equal true, twz.dst? 
     339        assert_equal 'EDT', twz.zone 
     340      end 
    283341    end 
    284342     
  • trunk/activesupport/test/time_zone_test.rb

    r9045 r9071  
    9292     
    9393      def test_today 
    94         TZInfo::DataTimezone.any_instance.stubs(:now).returns(Time.utc(2000)) 
    95         assert_equal Date.new(2000), TimeZone['Eastern Time (US & Canada)'].today 
     94        Time.stubs(:now).returns(Time.utc(2000, 1, 1, 4, 59, 59)) # 1 sec before midnight Jan 1 EST 
     95        assert_equal Date.new(1999, 12, 31), TimeZone['Eastern Time (US & Canada)'].today 
     96        Time.stubs(:now).returns(Time.utc(2000, 1, 1, 5)) # midnight Jan 1 EST 
     97        assert_equal Date.new(2000, 1, 1), TimeZone['Eastern Time (US & Canada)'].today 
     98        Time.stubs(:now).returns(Time.utc(2000, 1, 2, 4, 59, 59)) # 1 sec before midnight Jan 2 EST 
     99        assert_equal Date.new(2000, 1, 1), TimeZone['Eastern Time (US & Canada)'].today 
     100        Time.stubs(:now).returns(Time.utc(2000, 1, 2, 5)) # midnight Jan 2 EST 
     101        assert_equal Date.new(2000, 1, 2), TimeZone['Eastern Time (US & Canada)'].today 
     102      end 
     103    end 
     104     
     105    def test_local 
     106      silence_warnings do # silence warnings raised by tzinfo gem 
     107        time = TimeZone["Hawaii"].local(2007, 2, 5, 15, 30, 45) 
     108        assert_equal Time.utc(2007, 2, 5, 15, 30, 45), time.time 
     109        assert_equal TimeZone["Hawaii"], time.time_zone 
     110      end 
     111    end 
     112 
     113    def test_local_with_old_date 
     114      silence_warnings do # silence warnings raised by tzinfo gem 
     115        time = TimeZone["Hawaii"].local(1850, 2, 5, 15, 30, 45) 
     116        assert_equal [45,30,15,5,2,1850], time.to_a[0,6] 
     117        assert_equal TimeZone["Hawaii"], time.time_zone 
     118      end 
     119    end 
     120 
     121    def test_local_enforces_spring_dst_rules 
     122      zone = TimeZone['Eastern Time (US & Canada)'] 
     123      twz = zone.local(2006,4,2,1,59,59) # 1 second before DST start 
     124      assert_equal Time.utc(2006,4,2,1,59,59), twz.time 
     125      assert_equal Time.utc(2006,4,2,6,59,59), twz.utc 
     126      assert_equal false, twz.dst? 
     127      assert_equal 'EST', twz.zone 
     128      twz2 = zone.local(2006,4,2,2) # 2AM does not exist because at 2AM, time springs forward to 3AM 
     129      assert_equal Time.utc(2006,4,2,3), twz2.time # twz is created for 3AM 
     130      assert_equal Time.utc(2006,4,2,7), twz2.utc 
     131      assert_equal true, twz2.dst? 
     132      assert_equal 'EDT', twz2.zone 
     133      twz3 = zone.local(2006,4,2,2,30) # 2:30AM does not exist because at 2AM, time springs forward to 3AM 
     134      assert_equal Time.utc(2006,4,2,3,30), twz3.time # twz is created for 3:30AM 
     135      assert_equal Time.utc(2006,4,2,7,30), twz3.utc 
     136      assert_equal true, twz3.dst? 
     137      assert_equal 'EDT', twz3.zone 
     138    end 
     139 
     140    def test_local_enforces_fall_dst_rules 
     141      # 1AM during fall DST transition is ambiguous, it could be either DST or non-DST 1AM 
     142      # Mirroring Time.local behavior, this method selects the DST time 
     143      zone = TimeZone['Eastern Time (US & Canada)'] 
     144      twz = zone.local(2006,10,29,1) 
     145      assert_equal Time.utc(2006,10,29,1), twz.time 
     146      assert_equal Time.utc(2006,10,29,5), twz.utc 
     147      assert_equal true, twz.dst?  
     148      assert_equal 'EDT', twz.zone 
     149    end 
     150 
     151    def test_at 
     152      zone = TimeZone['Eastern Time (US & Canada)'] 
     153      secs = 946684800.0 
     154      twz = zone.at(secs) 
     155      assert_equal Time.utc(1999,12,31,19), twz.time 
     156      assert_equal Time.utc(2000), twz.utc 
     157      assert_equal zone, twz.time_zone 
     158      assert_equal secs, twz.to_f 
     159    end 
     160 
     161    def test_at_with_old_date 
     162      zone = TimeZone['Eastern Time (US & Canada)'] 
     163      secs = DateTime.civil(1850).to_f 
     164      twz = zone.at(secs) 
     165      assert_equal [1850, 1, 1, 0], [twz.utc.year, twz.utc.mon, twz.utc.day, twz.utc.hour] 
     166      assert_equal zone, twz.time_zone 
     167      assert_equal secs, twz.to_f 
     168    end 
     169 
     170    def test_parse 
     171      zone = TimeZone['Eastern Time (US & Canada)'] 
     172      twz = zone.parse('1999-12-31 19:00:00') 
     173      assert_equal Time.utc(1999,12,31,19), twz.time 
     174      assert_equal Time.utc(2000), twz.utc 
     175      assert_equal zone, twz.time_zone 
     176    end 
     177 
     178    def test_parse_with_old_date 
     179      silence_warnings do # silence warnings raised by tzinfo gem 
     180        zone = TimeZone['Eastern Time (US & Canada)'] 
     181        twz = zone.parse('1850-12-31 19:00:00') 
     182        assert_equal [0,0,19,31,12,1850], twz.to_a[0,6] 
     183        assert_equal zone, twz.time_zone 
     184      end 
     185    end 
     186 
     187    uses_mocha 'TestParseWithIncompleteDate' do 
     188      def test_parse_with_incomplete_date 
     189        zone = TimeZone['Eastern Time (US & Canada)'] 
     190        zone.stubs(:now).returns zone.local(1999,12,31) 
     191        twz = zone.parse('19:00:00') 
     192        assert_equal Time.utc(1999,12,31,19), twz.time 
    96193      end 
    97194    end 
     
    151248  end  
    152249   
    153   def test_local 
    154     time = TimeZone["Hawaii"].local(2007, 2, 5, 15, 30, 45) 
    155     assert_equal Time.utc(2007, 2, 5, 15, 30, 45), time.time 
    156     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 
    165   end 
    166    
    167   def test_local_enforces_spring_dst_rules 
    168     zone = TimeZone['Eastern Time (US & Canada)'] 
    169     twz = zone.local(2006,4,2,1,59,59) # 1 second before DST start 
    170     assert_equal Time.utc(2006,4,2,1,59,59), twz.time 
    171     assert_equal Time.utc(2006,4,2,6,59,59), twz.utc 
    172     assert_equal false, twz.dst? 
    173     assert_equal 'EST', twz.zone 
    174     twz2 = zone.local(2006,4,2,2) # 2AM does not exist because at 2AM, time springs forward to 3AM 
    175     assert_equal Time.utc(2006,4,2,3), twz2.time # twz is created for 3AM 
    176     assert_equal Time.utc(2006,4,2,7), twz2.utc 
    177     assert_equal true, twz2.dst? 
    178     assert_equal 'EDT', twz2.zone 
    179     twz3 = zone.local(2006,4,2,2,30) # 2:30AM does not exist because at 2AM, time springs forward to 3AM 
    180     assert_equal Time.utc(2006,4,2,3,30), twz3.time # twz is created for 3:30AM 
    181     assert_equal Time.utc(2006,4,2,7,30), twz3.utc 
    182     assert_equal true, twz3.dst? 
    183     assert_equal 'EDT', twz3.zone 
    184   end 
    185    
    186   def test_local_enforces_fall_dst_rules 
    187     # 1AM during fall DST transition is ambiguous, it could be either DST or non-DST 1AM 
    188     # Mirroring Time.local behavior, this method selects the DST time 
    189     zone = TimeZone['Eastern Time (US & Canada)'] 
    190     twz = zone.local(2006,10,29,1) 
    191     assert_equal Time.utc(2006,10,29,1), twz.time 
    192     assert_equal Time.utc(2006,10,29,5), twz.utc 
    193     assert_equal true, twz.dst?  
    194     assert_equal 'EDT', twz.zone 
    195   end 
    196    
    197   def test_at 
    198     zone = TimeZone['Eastern Time (US & Canada)'] 
    199     secs = 946684800.0 
    200     twz = zone.at(secs) 
    201     assert_equal Time.utc(1999,12,31,19), twz.time 
    202     assert_equal Time.utc(2000), twz.utc 
    203     assert_equal zone, twz.time_zone 
    204     assert_equal secs, twz.to_f 
    205   end 
    206    
    207   def test_at_with_old_date 
    208     zone = TimeZone['Eastern Time (US & Canada)'] 
    209     secs = DateTime.civil(1850).to_f 
    210     twz = zone.at(secs) 
    211     assert_equal [1850, 1, 1, 0], [twz.utc.year, twz.utc.mon, twz.utc.day, twz.utc.hour] 
    212     assert_equal zone, twz.time_zone 
    213     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 
    240   end 
    241  
    242250  protected 
    243251    def with_env_tz(new_tz = 'US/Eastern')