Changeset 9071
- Timestamp:
- 03/21/08 22:48:00 (4 months ago)
- Files:
-
- trunk/activesupport/CHANGELOG (modified) (1 diff)
- trunk/activesupport/lib/active_support/time_with_zone.rb (modified) (1 diff)
- trunk/activesupport/lib/active_support/values/time_zone.rb (modified) (2 diffs)
- trunk/activesupport/test/abstract_unit.rb (modified) (1 diff)
- trunk/activesupport/test/core_ext/time_with_zone_test.rb (modified) (7 diffs)
- trunk/activesupport/test/time_zone_test.rb (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/activesupport/CHANGELOG
r9047 r9071 1 1 *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] 2 4 3 5 * 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 64 64 # Time uses #zone to display the time zone abbreviation, so we're duck-typing it 65 65 def zone 66 period. abbreviation.to_s66 period.zone_identifier.to_s 67 67 end 68 68 trunk/activesupport/lib/active_support/values/time_zone.rb
r9046 r9071 178 178 begin # the following methods depend on the tzinfo gem 179 179 require_library_or_gem "tzinfo" unless Object.const_defined?(:TZInfo) 180 raise LoadError unless TZInfo.const_defined?(:TimeOrDateTime) 180 181 181 182 # Method for creating new ActiveSupport::TimeWithZone instance in time zone of +self+ from given values. Example: … … 254 255 # re-raise LoadError only when a tzinfo-dependent method is called: 255 256 %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."} 257 258 end 258 259 end trunk/activesupport/test/abstract_unit.rb
r8679 r9071 24 24 unless defined? uses_tzinfo 25 25 def uses_tzinfo(test_name, &block) 26 uses_gem('tzinfo', test_name, &block)26 uses_gem('tzinfo', test_name, '>= 0.2.0', &block) 27 27 end 28 28 end trunk/activesupport/test/core_ext/time_with_zone_test.rb
r9047 r9071 16 16 17 17 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 19 21 end 20 22 … … 43 45 44 46 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 47 51 end 48 52 … … 55 59 56 60 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 59 65 end 60 66 61 67 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 63 71 end 64 72 65 73 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 67 77 end 68 78 69 79 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 71 83 end 72 84 73 85 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 75 89 end 76 90 77 91 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 79 95 end 80 96 81 97 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 83 101 end 84 102 85 103 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 87 107 end 88 108 89 109 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 91 113 end 92 114 93 115 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 95 119 end 96 120 … … 124 148 125 149 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 127 153 end 128 154 129 155 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 133 161 end 134 162 135 163 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 137 167 end 138 168 139 169 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 141 173 end 142 174 143 175 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 147 181 end 148 182 149 183 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 151 187 end 152 188 … … 163 199 164 200 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 178 216 end 179 217 180 218 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 194 234 end 195 235 196 236 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 198 240 end 199 241 … … 215 257 216 258 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 218 262 end 219 263 … … 235 279 236 280 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 239 285 end 240 286 241 287 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 247 295 end 248 296 249 297 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 258 308 end 259 309 260 310 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 264 316 end 265 317 266 318 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 269 323 end 270 324 271 325 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 276 332 end 277 333 278 334 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 283 341 end 284 342 trunk/activesupport/test/time_zone_test.rb
r9045 r9071 92 92 93 93 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 96 193 end 97 194 end … … 151 248 end 152 249 153 def test_local154 time = TimeZone["Hawaii"].local(2007, 2, 5, 15, 30, 45)155 assert_equal Time.utc(2007, 2, 5, 15, 30, 45), time.time156 assert_equal TimeZone["Hawaii"], time.time_zone157 end158 159 def test_local_with_old_date160 silence_warnings do # silence warnings raised by tzinfo gem161 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_zone164 end165 end166 167 def test_local_enforces_spring_dst_rules168 zone = TimeZone['Eastern Time (US & Canada)']169 twz = zone.local(2006,4,2,1,59,59) # 1 second before DST start170 assert_equal Time.utc(2006,4,2,1,59,59), twz.time171 assert_equal Time.utc(2006,4,2,6,59,59), twz.utc172 assert_equal false, twz.dst?173 assert_equal 'EST', twz.zone174 twz2 = zone.local(2006,4,2,2) # 2AM does not exist because at 2AM, time springs forward to 3AM175 assert_equal Time.utc(2006,4,2,3), twz2.time # twz is created for 3AM176 assert_equal Time.utc(2006,4,2,7), twz2.utc177 assert_equal true, twz2.dst?178 assert_equal 'EDT', twz2.zone179 twz3 = zone.local(2006,4,2,2,30) # 2:30AM does not exist because at 2AM, time springs forward to 3AM180 assert_equal Time.utc(2006,4,2,3,30), twz3.time # twz is created for 3:30AM181 assert_equal Time.utc(2006,4,2,7,30), twz3.utc182 assert_equal true, twz3.dst?183 assert_equal 'EDT', twz3.zone184 end185 186 def test_local_enforces_fall_dst_rules187 # 1AM during fall DST transition is ambiguous, it could be either DST or non-DST 1AM188 # Mirroring Time.local behavior, this method selects the DST time189 zone = TimeZone['Eastern Time (US & Canada)']190 twz = zone.local(2006,10,29,1)191 assert_equal Time.utc(2006,10,29,1), twz.time192 assert_equal Time.utc(2006,10,29,5), twz.utc193 assert_equal true, twz.dst?194 assert_equal 'EDT', twz.zone195 end196 197 def test_at198 zone = TimeZone['Eastern Time (US & Canada)']199 secs = 946684800.0200 twz = zone.at(secs)201 assert_equal Time.utc(1999,12,31,19), twz.time202 assert_equal Time.utc(2000), twz.utc203 assert_equal zone, twz.time_zone204 assert_equal secs, twz.to_f205 end206 207 def test_at_with_old_date208 zone = TimeZone['Eastern Time (US & Canada)']209 secs = DateTime.civil(1850).to_f210 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_zone213 assert_equal secs, twz.to_f214 end215 216 def test_parse217 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.time220 assert_equal Time.utc(2000), twz.utc221 assert_equal zone, twz.time_zone222 end223 224 def test_parse_with_old_date225 silence_warnings do # silence warnings raised by tzinfo gem226 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_zone230 end231 end232 233 uses_mocha 'TestParseWithIncompleteDate' do234 def test_parse_with_incomplete_date235 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.time239 end240 end241 242 250 protected 243 251 def with_env_tz(new_tz = 'US/Eastern')