tzinfo 1.2.5

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of tzinfo might be problematic. Click here for more details.

Files changed (111) hide show
  1. checksums.yaml +7 -0
  2. checksums.yaml.gz.sig +3 -0
  3. data.tar.gz.sig +0 -0
  4. data/.yardopts +6 -0
  5. data/CHANGES.md +786 -0
  6. data/LICENSE +19 -0
  7. data/README.md +151 -0
  8. data/Rakefile +107 -0
  9. data/lib/tzinfo.rb +40 -0
  10. data/lib/tzinfo/country.rb +196 -0
  11. data/lib/tzinfo/country_index_definition.rb +31 -0
  12. data/lib/tzinfo/country_info.rb +42 -0
  13. data/lib/tzinfo/country_timezone.rb +135 -0
  14. data/lib/tzinfo/data_source.rb +190 -0
  15. data/lib/tzinfo/data_timezone.rb +58 -0
  16. data/lib/tzinfo/data_timezone_info.rb +55 -0
  17. data/lib/tzinfo/info_timezone.rb +30 -0
  18. data/lib/tzinfo/linked_timezone.rb +63 -0
  19. data/lib/tzinfo/linked_timezone_info.rb +26 -0
  20. data/lib/tzinfo/offset_rationals.rb +77 -0
  21. data/lib/tzinfo/ruby_core_support.rb +146 -0
  22. data/lib/tzinfo/ruby_country_info.rb +74 -0
  23. data/lib/tzinfo/ruby_data_source.rb +136 -0
  24. data/lib/tzinfo/time_or_datetime.rb +340 -0
  25. data/lib/tzinfo/timezone.rb +669 -0
  26. data/lib/tzinfo/timezone_definition.rb +36 -0
  27. data/lib/tzinfo/timezone_index_definition.rb +54 -0
  28. data/lib/tzinfo/timezone_info.rb +30 -0
  29. data/lib/tzinfo/timezone_offset.rb +101 -0
  30. data/lib/tzinfo/timezone_period.rb +245 -0
  31. data/lib/tzinfo/timezone_proxy.rb +105 -0
  32. data/lib/tzinfo/timezone_transition.rb +130 -0
  33. data/lib/tzinfo/timezone_transition_definition.rb +104 -0
  34. data/lib/tzinfo/transition_data_timezone_info.rb +274 -0
  35. data/lib/tzinfo/zoneinfo_country_info.rb +37 -0
  36. data/lib/tzinfo/zoneinfo_data_source.rb +488 -0
  37. data/lib/tzinfo/zoneinfo_timezone_info.rb +296 -0
  38. data/test/tc_country.rb +234 -0
  39. data/test/tc_country_index_definition.rb +69 -0
  40. data/test/tc_country_info.rb +16 -0
  41. data/test/tc_country_timezone.rb +173 -0
  42. data/test/tc_data_source.rb +218 -0
  43. data/test/tc_data_timezone.rb +99 -0
  44. data/test/tc_data_timezone_info.rb +18 -0
  45. data/test/tc_info_timezone.rb +34 -0
  46. data/test/tc_linked_timezone.rb +155 -0
  47. data/test/tc_linked_timezone_info.rb +23 -0
  48. data/test/tc_offset_rationals.rb +23 -0
  49. data/test/tc_ruby_core_support.rb +168 -0
  50. data/test/tc_ruby_country_info.rb +110 -0
  51. data/test/tc_ruby_data_source.rb +143 -0
  52. data/test/tc_time_or_datetime.rb +654 -0
  53. data/test/tc_timezone.rb +1350 -0
  54. data/test/tc_timezone_definition.rb +113 -0
  55. data/test/tc_timezone_index_definition.rb +73 -0
  56. data/test/tc_timezone_info.rb +11 -0
  57. data/test/tc_timezone_london.rb +143 -0
  58. data/test/tc_timezone_melbourne.rb +142 -0
  59. data/test/tc_timezone_new_york.rb +142 -0
  60. data/test/tc_timezone_offset.rb +126 -0
  61. data/test/tc_timezone_period.rb +555 -0
  62. data/test/tc_timezone_proxy.rb +136 -0
  63. data/test/tc_timezone_transition.rb +366 -0
  64. data/test/tc_timezone_transition_definition.rb +295 -0
  65. data/test/tc_timezone_utc.rb +27 -0
  66. data/test/tc_transition_data_timezone_info.rb +423 -0
  67. data/test/tc_zoneinfo_country_info.rb +78 -0
  68. data/test/tc_zoneinfo_data_source.rb +1195 -0
  69. data/test/tc_zoneinfo_timezone_info.rb +1232 -0
  70. data/test/test_utils.rb +163 -0
  71. data/test/ts_all.rb +7 -0
  72. data/test/ts_all_ruby.rb +5 -0
  73. data/test/ts_all_zoneinfo.rb +7 -0
  74. data/test/tzinfo-data/tzinfo/data.rb +8 -0
  75. data/test/tzinfo-data/tzinfo/data/definitions/America/Argentina/Buenos_Aires.rb +89 -0
  76. data/test/tzinfo-data/tzinfo/data/definitions/America/New_York.rb +315 -0
  77. data/test/tzinfo-data/tzinfo/data/definitions/Australia/Melbourne.rb +218 -0
  78. data/test/tzinfo-data/tzinfo/data/definitions/EST.rb +19 -0
  79. data/test/tzinfo-data/tzinfo/data/definitions/Etc/GMT__m__1.rb +21 -0
  80. data/test/tzinfo-data/tzinfo/data/definitions/Etc/GMT__p__1.rb +21 -0
  81. data/test/tzinfo-data/tzinfo/data/definitions/Etc/UTC.rb +21 -0
  82. data/test/tzinfo-data/tzinfo/data/definitions/Europe/Amsterdam.rb +261 -0
  83. data/test/tzinfo-data/tzinfo/data/definitions/Europe/Andorra.rb +186 -0
  84. data/test/tzinfo-data/tzinfo/data/definitions/Europe/London.rb +321 -0
  85. data/test/tzinfo-data/tzinfo/data/definitions/Europe/Paris.rb +265 -0
  86. data/test/tzinfo-data/tzinfo/data/definitions/Europe/Prague.rb +220 -0
  87. data/test/tzinfo-data/tzinfo/data/definitions/UTC.rb +16 -0
  88. data/test/tzinfo-data/tzinfo/data/indexes/countries.rb +927 -0
  89. data/test/tzinfo-data/tzinfo/data/indexes/timezones.rb +596 -0
  90. data/test/tzinfo-data/tzinfo/data/version.rb +14 -0
  91. data/test/zoneinfo/America/Argentina/Buenos_Aires +0 -0
  92. data/test/zoneinfo/America/New_York +0 -0
  93. data/test/zoneinfo/Australia/Melbourne +0 -0
  94. data/test/zoneinfo/EST +0 -0
  95. data/test/zoneinfo/Etc/UTC +0 -0
  96. data/test/zoneinfo/Europe/Amsterdam +0 -0
  97. data/test/zoneinfo/Europe/Andorra +0 -0
  98. data/test/zoneinfo/Europe/London +0 -0
  99. data/test/zoneinfo/Europe/Paris +0 -0
  100. data/test/zoneinfo/Europe/Prague +0 -0
  101. data/test/zoneinfo/Factory +0 -0
  102. data/test/zoneinfo/iso3166.tab +275 -0
  103. data/test/zoneinfo/leapseconds +61 -0
  104. data/test/zoneinfo/posix/Europe/London +0 -0
  105. data/test/zoneinfo/posixrules +0 -0
  106. data/test/zoneinfo/right/Europe/London +0 -0
  107. data/test/zoneinfo/zone.tab +439 -0
  108. data/test/zoneinfo/zone1970.tab +369 -0
  109. data/tzinfo.gemspec +21 -0
  110. metadata +193 -0
  111. metadata.gz.sig +2 -0
@@ -0,0 +1,1350 @@
1
+ require File.join(File.expand_path(File.dirname(__FILE__)), 'test_utils')
2
+
3
+ include TZInfo
4
+
5
+ class TCTimezone < Minitest::Test
6
+
7
+ class BlockCalled < StandardError
8
+ end
9
+
10
+ class TestTimezone < Timezone
11
+ def self.new(identifier, period_for_utc = nil, periods_for_local = nil, expected = nil)
12
+ t = super()
13
+ t.send(:setup, identifier, period_for_utc, periods_for_local, expected)
14
+ t
15
+ end
16
+
17
+ def identifier
18
+ @identifier
19
+ end
20
+
21
+ def period_for_utc(utc)
22
+ utc = TimeOrDateTime.wrap(utc)
23
+ raise "Unexpected utc #{utc} in period_for_utc" unless @expected.eql?(utc)
24
+ @period_for_utc
25
+ end
26
+
27
+ def periods_for_local(local)
28
+ local = TimeOrDateTime.wrap(local)
29
+ raise "Unexpected local #{local} in periods_for_local" unless @expected.eql?(local)
30
+ @periods_for_local.clone
31
+ end
32
+
33
+ def transitions_up_to(utc_to, utc_from = nil)
34
+ raise 'transitions_up_to called'
35
+ end
36
+
37
+ private
38
+ def setup(identifier, period_for_utc, periods_for_local, expected)
39
+ @identifier = identifier
40
+ @period_for_utc = period_for_utc
41
+ @periods_for_local = periods_for_local || []
42
+ @expected = TimeOrDateTime.wrap(expected)
43
+ end
44
+ end
45
+
46
+ class OffsetsUpToTestTimezone < Timezone
47
+ def self.new(identifier, expected_utc_to, expected_utc_from, transitions_up_to)
48
+ t = super()
49
+ t.send(:setup, identifier, expected_utc_to, expected_utc_from, transitions_up_to)
50
+ t
51
+ end
52
+
53
+ def identifier
54
+ @identifier
55
+ end
56
+
57
+ def period_for_utc(utc)
58
+ raise 'period_for_utc called'
59
+ end
60
+
61
+ def periods_for_local(local)
62
+ raise 'periods_for_local called'
63
+ end
64
+
65
+ def transitions_up_to(utc_to, utc_from = nil)
66
+ utc_to = TimeOrDateTime.wrap(utc_to)
67
+ raise "Unexpected utc_to #{utc_to || 'nil'} in transitions_up_to" unless @expected_utc_to.eql?(utc_to)
68
+
69
+ utc_from = utc_from ? TimeOrDateTime.wrap(utc_from) : nil
70
+ raise "Unexpected utc_from #{utc_from || 'nil'} in transitions_up_to" unless @expected_utc_from.eql?(utc_from)
71
+
72
+ if utc_from && utc_to <= utc_from
73
+ raise ArgumentError, 'utc_to must be greater than utc_from'
74
+ end
75
+
76
+ @transitions_up_to
77
+ end
78
+
79
+ private
80
+
81
+ def setup(identifier, expected_utc_to, expected_utc_from, transitions_up_to)
82
+ @identifier = identifier
83
+ @expected_utc_to = TimeOrDateTime.wrap(expected_utc_to)
84
+ @expected_utc_from = expected_utc_from ? TimeOrDateTime.wrap(expected_utc_from) : nil
85
+ @transitions_up_to = transitions_up_to
86
+ end
87
+ end
88
+
89
+ class OffsetsUpToNoTransitionsTestTimezone < Timezone
90
+ def self.new(identifier, expected_utc_to, expected_utc_from, period_for_utc)
91
+ t = super()
92
+ t.send(:setup, identifier, expected_utc_to, expected_utc_from, period_for_utc)
93
+ t
94
+ end
95
+
96
+ def identifier
97
+ @identifier
98
+ end
99
+
100
+ def period_for_utc(utc)
101
+ utc = TimeOrDateTime.wrap(utc)
102
+
103
+ raise "Unexpected utc #{utc} in period_for_utc (should be utc_from)" if @expected_utc_from && !@expected_utc_from.eql?(utc)
104
+ raise "Unexpected utc #{utc} in period_for_utc (should be < utc_to)" if !@expected_utc_from && @expected_utc_to <= utc
105
+
106
+ @period_for_utc
107
+ end
108
+
109
+ def periods_for_local(local)
110
+ raise 'periods_for_local called'
111
+ end
112
+
113
+ def transitions_up_to(utc_to, utc_from = nil)
114
+ utc_to = TimeOrDateTime.wrap(utc_to)
115
+ raise "Unexpected utc_to #{utc_to || 'nil'} in transitions_up_to" unless @expected_utc_to.eql?(utc_to)
116
+
117
+ utc_from = utc_from ? TimeOrDateTime.wrap(utc_from) : nil
118
+ raise "Unexpected utc_from #{utc_from || 'nil'} in transitions_up_to" unless @expected_utc_from.eql?(utc_from)
119
+
120
+ if utc_from && utc_to <= utc_from
121
+ raise ArgumentError, 'utc_to must be greater than utc_from'
122
+ end
123
+
124
+ []
125
+ end
126
+
127
+ private
128
+
129
+ def setup(identifier, expected_utc_to, expected_utc_from, period_for_utc)
130
+ @identifier = identifier
131
+ @expected_utc_to = TimeOrDateTime.wrap(expected_utc_to)
132
+ @expected_utc_from = expected_utc_from ? TimeOrDateTime.wrap(expected_utc_from) : nil
133
+ @period_for_utc = period_for_utc
134
+ end
135
+ end
136
+
137
+ class TestTimezoneTransition < TimezoneTransition
138
+ def initialize(offset, previous_offset, at)
139
+ super(offset, previous_offset)
140
+ @at = TimeOrDateTime.wrap(at)
141
+ end
142
+
143
+ def at
144
+ @at
145
+ end
146
+ end
147
+
148
+ def setup
149
+ @orig_default_dst = Timezone.default_dst
150
+ @orig_data_source = DataSource.get
151
+ Timezone.send :init_loaded_zones
152
+ end
153
+
154
+ def teardown
155
+ Timezone.default_dst = @orig_default_dst
156
+ DataSource.set(@orig_data_source)
157
+ end
158
+
159
+ def test_default_dst_initial_value
160
+ assert_nil(Timezone.default_dst)
161
+ end
162
+
163
+ def test_set_default_dst
164
+ Timezone.default_dst = true
165
+ assert_equal(true, Timezone.default_dst)
166
+ Timezone.default_dst = false
167
+ assert_equal(false, Timezone.default_dst)
168
+ Timezone.default_dst = nil
169
+ assert_nil(Timezone.default_dst)
170
+ Timezone.default_dst = 0
171
+ assert_equal(true, Timezone.default_dst)
172
+ end
173
+
174
+ def test_get_valid_1
175
+ tz = Timezone.get('Europe/London')
176
+
177
+ assert_kind_of(DataTimezone, tz)
178
+ assert_equal('Europe/London', tz.identifier)
179
+ end
180
+
181
+ def test_get_valid_2
182
+ tz = Timezone.get('UTC')
183
+
184
+ # ZoneinfoDataSource doesn't return LinkedTimezoneInfo for any timezone.
185
+ if DataSource.get.load_timezone_info('UTC').kind_of?(LinkedTimezoneInfo)
186
+ assert_kind_of(LinkedTimezone, tz)
187
+ else
188
+ assert_kind_of(DataTimezone, tz)
189
+ end
190
+
191
+ assert_equal('UTC', tz.identifier)
192
+ end
193
+
194
+ def test_get_valid_3
195
+ tz = Timezone.get('America/Argentina/Buenos_Aires')
196
+
197
+ assert_kind_of(DataTimezone, tz)
198
+ assert_equal('America/Argentina/Buenos_Aires', tz.identifier)
199
+ end
200
+
201
+ def test_get_same_instance
202
+ tz1 = Timezone.get('Europe/London')
203
+ tz2 = Timezone.get('Europe/London')
204
+ assert_same(tz1, tz2)
205
+ end
206
+
207
+ def test_get_not_exist
208
+ assert_raises(InvalidTimezoneIdentifier) { Timezone.get('Nowhere/Special') }
209
+ end
210
+
211
+ def test_get_invalid
212
+ assert_raises(InvalidTimezoneIdentifier) { Timezone.get('../Definitions/UTC') }
213
+ end
214
+
215
+ def test_get_nil
216
+ assert_raises(InvalidTimezoneIdentifier) { Timezone.get(nil) }
217
+ end
218
+
219
+ def test_get_case
220
+ Timezone.get('Europe/Prague')
221
+ assert_raises(InvalidTimezoneIdentifier) { Timezone.get('Europe/prague') }
222
+ end
223
+
224
+ def test_get_proxy_valid
225
+ proxy = Timezone.get_proxy('Europe/London')
226
+ assert_kind_of(TimezoneProxy, proxy)
227
+ assert_equal('Europe/London', proxy.identifier)
228
+ end
229
+
230
+ def test_get_proxy_not_exist
231
+ proxy = Timezone.get_proxy('Not/There')
232
+ assert_kind_of(TimezoneProxy, proxy)
233
+ assert_equal('Not/There', proxy.identifier)
234
+ end
235
+
236
+ def test_get_proxy_invalid
237
+ proxy = Timezone.get_proxy('../Invalid/Identifier')
238
+ assert_kind_of(TimezoneProxy, proxy)
239
+ assert_equal('../Invalid/Identifier', proxy.identifier)
240
+ end
241
+
242
+ def test_get_tainted_loaded
243
+ Timezone.get('Europe/Andorra')
244
+
245
+ safe_test do
246
+ identifier = 'Europe/Andorra'.dup.taint
247
+ assert(identifier.tainted?)
248
+ tz = Timezone.get(identifier)
249
+ assert_equal('Europe/Andorra', tz.identifier)
250
+ assert(identifier.tainted?)
251
+ end
252
+ end
253
+
254
+ def test_get_tainted_and_frozen_loaded
255
+ Timezone.get('Europe/Andorra')
256
+
257
+ safe_test do
258
+ tz = Timezone.get('Europe/Andorra'.dup.taint.freeze)
259
+ assert_equal('Europe/Andorra', tz.identifier)
260
+ end
261
+ end
262
+
263
+ def test_get_tainted_not_previously_loaded
264
+ safe_test do
265
+ identifier = 'Europe/Andorra'.dup.taint
266
+ assert(identifier.tainted?)
267
+ tz = Timezone.get(identifier)
268
+ assert_equal('Europe/Andorra', tz.identifier)
269
+ assert(identifier.tainted?)
270
+ end
271
+ end
272
+
273
+ def test_get_tainted_and_frozen_not_previously_loaded
274
+ safe_test do
275
+ tz = Timezone.get('Europe/Amsterdam'.dup.taint.freeze)
276
+ assert_equal('Europe/Amsterdam', tz.identifier)
277
+ end
278
+ end
279
+
280
+ def test_new_no_args
281
+ tz = Timezone.new
282
+
283
+ assert_raises(UnknownTimezone) { tz.identifier }
284
+ assert_raises(UnknownTimezone) { tz.friendly_identifier }
285
+ assert_raises(UnknownTimezone) { tz.utc_to_local(DateTime.new(2006,1,1,1,0,0)) }
286
+ assert_raises(UnknownTimezone) { tz.local_to_utc(DateTime.new(2006,1,1,1,0,0)) }
287
+ assert_raises(UnknownTimezone) { tz.period_for_utc(DateTime.new(2006,1,1,1,0,0)) }
288
+ assert_raises(UnknownTimezone) { tz.periods_for_local(DateTime.new(2006,1,1,1,0,0)) }
289
+ assert_raises(UnknownTimezone) { tz.period_for_local(DateTime.new(2006,1,1,1,0,0)) }
290
+ assert_raises(UnknownTimezone) { tz.now }
291
+ assert_raises(UnknownTimezone) { tz.current_period_and_time }
292
+ assert_raises(UnknownTimezone) { tz.transitions_up_to(DateTime.new(2006,1,1,1,0,0)) }
293
+ assert_raises(UnknownTimezone) { tz.canonical_identifier }
294
+ assert_raises(UnknownTimezone) { tz.canonical_zone }
295
+ end
296
+
297
+ def test_new_nil
298
+ tz = Timezone.new(nil)
299
+
300
+ assert_raises(UnknownTimezone) { tz.identifier }
301
+ assert_raises(UnknownTimezone) { tz.friendly_identifier }
302
+ assert_raises(UnknownTimezone) { tz.utc_to_local(DateTime.new(2006,1,1,1,0,0)) }
303
+ assert_raises(UnknownTimezone) { tz.local_to_utc(DateTime.new(2006,1,1,1,0,0)) }
304
+ assert_raises(UnknownTimezone) { tz.period_for_utc(DateTime.new(2006,1,1,1,0,0)) }
305
+ assert_raises(UnknownTimezone) { tz.periods_for_local(DateTime.new(2006,1,1,1,0,0)) }
306
+ assert_raises(UnknownTimezone) { tz.period_for_local(DateTime.new(2006,1,1,1,0,0)) }
307
+ assert_raises(UnknownTimezone) { tz.now }
308
+ assert_raises(UnknownTimezone) { tz.current_period_and_time }
309
+ assert_raises(UnknownTimezone) { tz.transitions_up_to(DateTime.new(2006,1,1,1,0,0)) }
310
+ assert_raises(UnknownTimezone) { tz.canonical_identifier }
311
+ assert_raises(UnknownTimezone) { tz.canonical_zone }
312
+ end
313
+
314
+ def test_new_arg
315
+ tz = Timezone.new('Europe/London')
316
+ assert_same(Timezone.get('Europe/London'), tz)
317
+ end
318
+
319
+ def test_new_arg_not_exist
320
+ assert_raises(InvalidTimezoneIdentifier) { Timezone.new('Nowhere/Special') }
321
+ end
322
+
323
+ def test_all
324
+ all = Timezone.all
325
+ expected = DataSource.get.timezone_identifiers.collect {|identifier| Timezone.get_proxy(identifier)}
326
+ assert_equal(expected, all)
327
+ end
328
+
329
+ def test_all_identifiers
330
+ all = Timezone.all_identifiers
331
+ assert_equal(DataSource.get.timezone_identifiers, all)
332
+ end
333
+
334
+ def test_all_data_zones
335
+ all_data = Timezone.all_data_zones
336
+ expected = DataSource.get.data_timezone_identifiers.collect {|identifier| Timezone.get_proxy(identifier)}
337
+ assert_equal(expected, all_data)
338
+ end
339
+
340
+ def test_all_data_zone_identifiers
341
+ all_data = Timezone.all_data_zone_identifiers
342
+ assert_equal(DataSource.get.data_timezone_identifiers, all_data)
343
+ end
344
+
345
+ def test_all_linked_zones
346
+ all_linked = Timezone.all_linked_zones
347
+ expected = DataSource.get.linked_timezone_identifiers.collect {|identifier| Timezone.get_proxy(identifier)}
348
+ assert_equal(expected, all_linked)
349
+ end
350
+
351
+ def test_all_linked_zone_identifiers
352
+ all_linked = Timezone.all_linked_zone_identifiers
353
+ assert_equal(DataSource.get.linked_timezone_identifiers, all_linked)
354
+ end
355
+
356
+ def test_all_country_zones
357
+ # Probably should relax this test - just need all the zones, don't care
358
+ # about order.
359
+ expected = Country.all.inject([]) {|result,country|
360
+ result += country.zones
361
+ }
362
+ expected.uniq!
363
+
364
+ all_country_zones = Timezone.all_country_zones
365
+ assert_equal(expected, all_country_zones)
366
+
367
+ all_country_zone_identifiers = Timezone.all_country_zone_identifiers
368
+ assert_equal(all_country_zone_identifiers.length, all_country_zones.length)
369
+
370
+ all_country_zones.each {|zone|
371
+ assert_kind_of(TimezoneProxy, zone)
372
+ assert(all_country_zone_identifiers.include?(zone.identifier))
373
+ }
374
+ end
375
+
376
+ def test_all_country_zone_identifiers
377
+ # Probably should relax this test - just need all the zones, don't care
378
+ # about order.
379
+ expected = Country.all.inject([]) {|result,country|
380
+ result += country.zone_identifiers
381
+ }
382
+ expected.uniq!
383
+
384
+ assert_equal(expected, Timezone.all_country_zone_identifiers)
385
+ end
386
+
387
+ def test_us_zones
388
+ # Probably should relax this test - just need all the zones, don't care
389
+ # about order.
390
+ us_zones = Timezone.us_zones
391
+ assert_equal(Country.get('US').zones.uniq, us_zones)
392
+
393
+ us_zone_identifiers = Timezone.us_zone_identifiers
394
+ assert_equal(us_zone_identifiers.length, us_zones.length)
395
+
396
+ us_zones.each {|zone|
397
+ assert_kind_of(TimezoneProxy, zone)
398
+ assert(us_zone_identifiers.include?(zone.identifier))
399
+ }
400
+ end
401
+
402
+ def test_us_zone_identifiers
403
+ # Probably should relax this test - just need all the zones, don't care
404
+ # about order.
405
+ assert_equal(Country.get('US').zone_identifiers.uniq, Timezone.us_zone_identifiers)
406
+ end
407
+
408
+ def test_identifier
409
+ assert_raises(UnknownTimezone) { Timezone.new.identifier }
410
+ assert_equal('Europe/Paris', TestTimezone.new('Europe/Paris').identifier)
411
+ end
412
+
413
+ def test_name
414
+ assert_raises(UnknownTimezone) { Timezone.new.name }
415
+ assert_equal('Europe/Paris', TestTimezone.new('Europe/Paris').name)
416
+ end
417
+
418
+ def test_friendly_identifier
419
+ assert_equal('Paris', TestTimezone.new('Europe/Paris').friendly_identifier(true))
420
+ assert_equal('Europe - Paris', TestTimezone.new('Europe/Paris').friendly_identifier(false))
421
+ assert_equal('Europe - Paris', TestTimezone.new('Europe/Paris').friendly_identifier)
422
+ assert_equal('Knox, Indiana', TestTimezone.new('America/Indiana/Knox').friendly_identifier(true))
423
+ assert_equal('America - Knox, Indiana', TestTimezone.new('America/Indiana/Knox').friendly_identifier(false))
424
+ assert_equal('America - Knox, Indiana', TestTimezone.new('America/Indiana/Knox').friendly_identifier)
425
+ assert_equal('Dumont D\'Urville', TestTimezone.new('Antarctica/DumontDUrville').friendly_identifier(true))
426
+ assert_equal('Antarctica - Dumont D\'Urville', TestTimezone.new('Antarctica/DumontDUrville').friendly_identifier(false))
427
+ assert_equal('Antarctica - Dumont D\'Urville', TestTimezone.new('Antarctica/DumontDUrville').friendly_identifier)
428
+ assert_equal('McMurdo', TestTimezone.new('Antarctica/McMurdo').friendly_identifier(true))
429
+ assert_equal('Antarctica - McMurdo', TestTimezone.new('Antarctica/McMurdo').friendly_identifier(false))
430
+ assert_equal('Antarctica - McMurdo', TestTimezone.new('Antarctica/McMurdo').friendly_identifier)
431
+ assert_equal('GMT+1', TestTimezone.new('Etc/GMT+1').friendly_identifier(true))
432
+ assert_equal('Etc - GMT+1', TestTimezone.new('Etc/GMT+1').friendly_identifier(false))
433
+ assert_equal('Etc - GMT+1', TestTimezone.new('Etc/GMT+1').friendly_identifier)
434
+ assert_equal('UTC', TestTimezone.new('UTC').friendly_identifier(true))
435
+ assert_equal('UTC', TestTimezone.new('UTC').friendly_identifier(false))
436
+ assert_equal('UTC', TestTimezone.new('UTC').friendly_identifier)
437
+ end
438
+
439
+ if defined?(Encoding)
440
+ def test_friendly_identifier_non_binary_encoding
441
+ refute_equal(Encoding::ASCII_8BIT, TestTimezone.new('Europe/Paris').friendly_identifier(true).encoding)
442
+ refute_equal(Encoding::ASCII_8BIT, TestTimezone.new('Europe/Paris').friendly_identifier(false).encoding)
443
+ end
444
+ end
445
+
446
+ def test_to_s
447
+ assert_equal('Europe - Paris', TestTimezone.new('Europe/Paris').to_s)
448
+ assert_equal('America - Knox, Indiana', TestTimezone.new('America/Indiana/Knox').to_s)
449
+ assert_equal('Antarctica - Dumont D\'Urville', TestTimezone.new('Antarctica/DumontDUrville').to_s)
450
+ assert_equal('Antarctica - McMurdo', TestTimezone.new('Antarctica/McMurdo').to_s)
451
+ assert_equal('Etc - GMT+1', TestTimezone.new('Etc/GMT+1').to_s)
452
+ assert_equal('UTC', TestTimezone.new('UTC').to_s)
453
+ end
454
+
455
+ def test_period_for_local
456
+ dt = DateTime.new(2005,2,18,16,24,23)
457
+ dt2 = DateTime.new(2005,2,18,16,24,23).new_offset(Rational(5,24))
458
+ dt3 = DateTime.new(2005,2,18,16,24,23 + Rational(789, 1000))
459
+ t = Time.utc(2005,2,18,16,24,23)
460
+ t2 = Time.local(2005,2,18,16,24,23)
461
+ t3 = Time.utc(2005,2,18,16,24,23,789000)
462
+ ts = t.to_i
463
+
464
+ o1 = TimezoneOffset.new(0, 0, :GMT)
465
+ o2 = TimezoneOffset.new(0, 3600, :BST)
466
+
467
+ period = TimezonePeriod.new(
468
+ TestTimezoneTransition.new(o1, o2, 1099184400),
469
+ TestTimezoneTransition.new(o2, o1, 1111885200))
470
+
471
+ dt_period = TestTimezone.new('Europe/London', nil, [period], dt).period_for_local(dt)
472
+ dt2_period = TestTimezone.new('Europe/London', nil, [period], dt2).period_for_local(dt2)
473
+ dt3_period = TestTimezone.new('Europe/London', nil, [period], dt3).period_for_local(dt3)
474
+ t_period = TestTimezone.new('Europe/London', nil, [period], t).period_for_local(t)
475
+ t2_period = TestTimezone.new('Europe/London', nil, [period], t2).period_for_local(t2)
476
+ t3_period = TestTimezone.new('Europe/London', nil, [period], t3).period_for_local(t3)
477
+ ts_period = TestTimezone.new('Europe/London', nil, [period], ts).period_for_local(ts)
478
+
479
+ assert_equal(period, dt_period)
480
+ assert_equal(period, dt2_period)
481
+ assert_equal(period, dt3_period)
482
+ assert_equal(period, t_period)
483
+ assert_equal(period, t2_period)
484
+ assert_equal(period, t3_period)
485
+ assert_equal(period, ts_period)
486
+ end
487
+
488
+ def test_period_for_local_invalid
489
+ dt = DateTime.new(2004,4,4,2,30,0)
490
+ tz = TestTimezone.new('America/New_York', nil, [], dt)
491
+
492
+ assert_raises(PeriodNotFound) do
493
+ tz.period_for_local(dt)
494
+ end
495
+ end
496
+
497
+ def test_period_for_local_ambiguous
498
+ o1 = TimezoneOffset.new(-18000, 0, :EST)
499
+ o2 = TimezoneOffset.new(-18000, 3600, :EDT)
500
+
501
+ t1 = TestTimezoneTransition.new(o2, o1, 1081062000)
502
+ t2 = TestTimezoneTransition.new(o1, o2, 1099202400)
503
+ t3 = TestTimezoneTransition.new(o2, o1, 1112511600)
504
+
505
+ p1 = TimezonePeriod.new(t1, t2)
506
+ p2 = TimezonePeriod.new(t2, t3)
507
+
508
+ dt = DateTime.new(2004,10,31,1,0,0)
509
+ dt2 = DateTime.new(2004,10,31,1,0,Rational(555,1000))
510
+ t = Time.utc(2004,10,31,1,30,0)
511
+ t2 = Time.utc(2004,10,31,1,30,0,555000)
512
+ i = Time.utc(2004,10,31,1,59,59).to_i
513
+
514
+ dt_tz = TestTimezone.new('America/New_York', nil, [p1, p2], dt)
515
+ dt2_tz = TestTimezone.new('America/New_York', nil, [p1, p2], dt2)
516
+ t_tz = TestTimezone.new('America/New_York', nil, [p1, p2], t)
517
+ t2_tz = TestTimezone.new('America/New_York', nil, [p1, p2], t2)
518
+ i_tz = TestTimezone.new('America/New_York', nil, [p1, p2], i)
519
+
520
+ assert_raises(AmbiguousTime) { dt_tz.period_for_local(dt) }
521
+ assert_raises(AmbiguousTime) { dt2_tz.period_for_local(dt2) }
522
+ assert_raises(AmbiguousTime) { t_tz.period_for_local(t) }
523
+ assert_raises(AmbiguousTime) { t2_tz.period_for_local(t2) }
524
+ assert_raises(AmbiguousTime) { i_tz.period_for_local(i) }
525
+ end
526
+
527
+ def test_period_for_local_not_found
528
+ dt = DateTime.new(2004,4,4,2,0,0)
529
+ dt2 = DateTime.new(2004,4,4,2,0,Rational(987,1000))
530
+ t = Time.utc(2004,4,4,2,30,0)
531
+ t2 = Time.utc(2004,4,4,2,30,0,987000)
532
+ i = Time.utc(2004,4,4,2,59,59).to_i
533
+
534
+ dt_tz = TestTimezone.new('America/New_York', nil, [], dt)
535
+ dt2_tz = TestTimezone.new('America/New_York', nil, [], dt2)
536
+ t_tz = TestTimezone.new('America/New_York', nil, [], t)
537
+ t2_tz = TestTimezone.new('America/New_York', nil, [], t2)
538
+ i_tz = TestTimezone.new('America/New_York', nil, [], i)
539
+
540
+ assert_raises(PeriodNotFound) { dt_tz.period_for_local(dt) }
541
+ assert_raises(PeriodNotFound) { dt2_tz.period_for_local(dt2) }
542
+ assert_raises(PeriodNotFound) { t_tz.period_for_local(t) }
543
+ assert_raises(PeriodNotFound) { t2_tz.period_for_local(t2) }
544
+ assert_raises(PeriodNotFound) { i_tz.period_for_local(i) }
545
+ end
546
+
547
+ def test_period_for_local_default_dst_set_true
548
+ Timezone.default_dst = true
549
+
550
+ o1 = TimezoneOffset.new(-18000, 0, :EST)
551
+ o2 = TimezoneOffset.new(-18000, 3600, :EDT)
552
+
553
+ t1 = TestTimezoneTransition.new(o2, o1, 1081062000)
554
+ t2 = TestTimezoneTransition.new(o1, o2, 1099202400)
555
+ t3 = TestTimezoneTransition.new(o2, o1, 1112511600)
556
+
557
+ p1 = TimezonePeriod.new(t1, t2)
558
+ p2 = TimezonePeriod.new(t2, t3)
559
+
560
+ dt = DateTime.new(2004,10,31,1,30,0)
561
+
562
+ tz = TestTimezone.new('America/New_York', nil, [p1, p2], dt)
563
+
564
+ assert_equal(p1, tz.period_for_local(dt))
565
+ assert_equal(p1, tz.period_for_local(dt, true))
566
+ assert_equal(p2, tz.period_for_local(dt, false))
567
+ assert_raises(AmbiguousTime) { tz.period_for_local(dt, nil) }
568
+ end
569
+
570
+ def test_period_for_local_default_dst_set_false
571
+ Timezone.default_dst = false
572
+
573
+ o1 = TimezoneOffset.new(-18000, 0, :EST)
574
+ o2 = TimezoneOffset.new(-18000, 3600, :EDT)
575
+
576
+ t1 = TestTimezoneTransition.new(o2, o1, 1081062000)
577
+ t2 = TestTimezoneTransition.new(o1, o2, 1099202400)
578
+ t3 = TestTimezoneTransition.new(o2, o1, 1112511600)
579
+
580
+ p1 = TimezonePeriod.new(t1, t2)
581
+ p2 = TimezonePeriod.new(t2, t3)
582
+
583
+ dt = DateTime.new(2004,10,31,1,30,0)
584
+
585
+ tz = TestTimezone.new('America/New_York', nil, [p1, p2], dt)
586
+
587
+ assert_equal(p2, tz.period_for_local(dt))
588
+ assert_equal(p1, tz.period_for_local(dt, true))
589
+ assert_equal(p2, tz.period_for_local(dt, false))
590
+ assert_raises(AmbiguousTime) { tz.period_for_local(dt, nil) }
591
+ end
592
+
593
+ def test_period_for_local_dst_flag_resolved
594
+ o1 = TimezoneOffset.new(-18000, 0, :EST)
595
+ o2 = TimezoneOffset.new(-18000, 3600, :EDT)
596
+
597
+ t1 = TestTimezoneTransition.new(o2, o1, 1081062000)
598
+ t2 = TestTimezoneTransition.new(o1, o2, 1099202400)
599
+ t3 = TestTimezoneTransition.new(o2, o1, 1112511600)
600
+
601
+ p1 = TimezonePeriod.new(t1, t2)
602
+ p2 = TimezonePeriod.new(t2, t3)
603
+
604
+ dt = DateTime.new(2004,10,31,1,30,0)
605
+
606
+ tz = TestTimezone.new('America/New_York', nil, [p1, p2], dt)
607
+
608
+ assert_equal(p1, tz.period_for_local(dt, true))
609
+ assert_equal(p2, tz.period_for_local(dt, false))
610
+ assert_equal(p1, tz.period_for_local(dt, true) {|periods| raise BlockCalled, 'should not be called' })
611
+ assert_equal(p2, tz.period_for_local(dt, false) {|periods| raise BlockCalled, 'should not be called' })
612
+ end
613
+
614
+ def test_period_for_local_dst_block_called
615
+ o1 = TimezoneOffset.new(-18000, 0, :EST)
616
+ o2 = TimezoneOffset.new(-18000, 3600, :EDT)
617
+
618
+ t1 = TestTimezoneTransition.new(o2, o1, 1081062000)
619
+ t2 = TestTimezoneTransition.new(o1, o2, 1099202400)
620
+ t3 = TestTimezoneTransition.new(o2, o1, 1112511600)
621
+
622
+ p1 = TimezonePeriod.new(t1, t2)
623
+ p2 = TimezonePeriod.new(t2, t3)
624
+
625
+ dt = DateTime.new(2004,10,31,1,30,0)
626
+
627
+ tz = TestTimezone.new('America/New_York', nil, [p1, p2], dt)
628
+
629
+ assert_raises(BlockCalled) {
630
+ tz.period_for_local(dt) {|periods|
631
+ assert_equal([p1, p2], periods)
632
+
633
+ # raise exception to test that the block was called
634
+ raise BlockCalled, 'should be raised'
635
+ }
636
+ }
637
+
638
+ assert_equal(p1, tz.period_for_local(dt) {|periods| periods.first})
639
+ assert_equal(p2, tz.period_for_local(dt) {|periods| periods.last})
640
+ assert_equal(p1, tz.period_for_local(dt) {|periods| [periods.first]})
641
+ assert_equal(p2, tz.period_for_local(dt) {|periods| [periods.last]})
642
+ end
643
+
644
+ def test_period_for_local_dst_cannot_resolve
645
+ # At midnight local time on Aug 5 1915 in Warsaw, the clocks were put back
646
+ # 24 minutes and both periods were non-DST. Hence the block should be
647
+ # called regardless of the value of the Boolean dst parameter.
648
+
649
+ o0 = TimezoneOffset.new(5040, 0, :LMT)
650
+ o1 = TimezoneOffset.new(5040, 0, :WMT)
651
+ o2 = TimezoneOffset.new(3600, 0, :CET)
652
+ o3 = TimezoneOffset.new(3600, 3600, :CEST)
653
+
654
+ t1 = TestTimezoneTransition.new(o1, o0, DateTime.new(1879, 12, 31, 22, 36, 0))
655
+ t2 = TestTimezoneTransition.new(o2, o1, DateTime.new(1915, 8, 4, 22, 36, 0))
656
+ t3 = TestTimezoneTransition.new(o3, o2, DateTime.new(1916, 4, 30, 22, 0, 0))
657
+
658
+ p1 = TimezonePeriod.new(t1, t2)
659
+ p2 = TimezonePeriod.new(t2, t3)
660
+
661
+ dt = DateTime.new(1915,8,4,23,40,0)
662
+
663
+ tz = TestTimezone.new('Europe/Warsaw', nil, [p1, p2], dt)
664
+
665
+ assert_raises(BlockCalled) {
666
+ tz.period_for_local(dt, true) {|periods|
667
+ assert_equal([p1, p2], periods)
668
+ raise BlockCalled, 'should be raised'
669
+ }
670
+ }
671
+
672
+ assert_raises(BlockCalled) {
673
+ tz.period_for_local(dt, false) {|periods|
674
+ assert_equal([p1, p2], periods)
675
+ raise BlockCalled, 'should be raised'
676
+ }
677
+ }
678
+ end
679
+
680
+ def test_period_for_local_block_ambiguous
681
+ o1 = TimezoneOffset.new(-18000, 0, :EST)
682
+ o2 = TimezoneOffset.new(-18000, 3600, :EDT)
683
+
684
+ t1 = TestTimezoneTransition.new(o2, o1, 1081062000)
685
+ t2 = TestTimezoneTransition.new(o1, o2, 1099202400)
686
+ t3 = TestTimezoneTransition.new(o2, o1, 1112511600)
687
+
688
+ p1 = TimezonePeriod.new(t1, t2)
689
+ p2 = TimezonePeriod.new(t2, t3)
690
+
691
+ dt = DateTime.new(2004,10,31,1,30,0)
692
+
693
+ tz = TestTimezone.new('America/New_York', nil, [p1, p2], dt)
694
+
695
+ assert_raises(AmbiguousTime) do
696
+ tz.period_for_local(dt) {|periods| nil}
697
+ end
698
+
699
+ assert_raises(AmbiguousTime) do
700
+ tz.period_for_local(dt) {|periods| periods}
701
+ end
702
+
703
+ assert_raises(AmbiguousTime) do
704
+ tz.period_for_local(dt) {|periods| []}
705
+ end
706
+
707
+ assert_raises(AmbiguousTime) do
708
+ tz.period_for_local(dt) {|periods| raise AmbiguousTime, 'Ambiguous time'}
709
+ end
710
+ end
711
+
712
+ def test_utc_to_local
713
+ dt = DateTime.new(2005,6,18,16,24,23)
714
+ dt2 = DateTime.new(2005,6,18,16,24,23).new_offset(Rational(5,24))
715
+ dtu = DateTime.new(2005,6,18,16,24,23 + Rational(567,1000))
716
+ dtu2 = DateTime.new(2005,6,18,16,24,23 + Rational(567,1000)).new_offset(Rational(5,24))
717
+ t = Time.utc(2005,6,18,16,24,23)
718
+ t2 = Time.local(2005,6,18,16,24,23)
719
+ tu = Time.utc(2005,6,18,16,24,23,567000)
720
+ tu2 = Time.local(2005,6,18,16,24,23,567000)
721
+ ts = t.to_i
722
+
723
+ o1 = TimezoneOffset.new(0, 0, :GMT)
724
+ o2 = TimezoneOffset.new(0, 3600, :BST)
725
+
726
+ period = TimezonePeriod.new(
727
+ TestTimezoneTransition.new(o2, o1, 1111885200),
728
+ TestTimezoneTransition.new(o1, o2, 1130634000))
729
+
730
+ assert_equal(DateTime.new(2005,6,18,17,24,23), TestTimezone.new('Europe/London', period, [], dt).utc_to_local(dt))
731
+ assert_equal(DateTime.new(2005,6,18,17,24,23), TestTimezone.new('Europe/London', period, [], dt2).utc_to_local(dt2))
732
+ assert_equal(DateTime.new(2005,6,18,17,24,23 + Rational(567,1000)), TestTimezone.new('Europe/London', period, [], dtu).utc_to_local(dtu))
733
+ assert_equal(DateTime.new(2005,6,18,17,24,23 + Rational(567,1000)), TestTimezone.new('Europe/London', period, [], dtu2).utc_to_local(dtu2))
734
+ assert_equal(Time.utc(2005,6,18,17,24,23), TestTimezone.new('Europe/London', period, [], t).utc_to_local(t))
735
+ assert_equal(Time.utc(2005,6,18,17,24,23), TestTimezone.new('Europe/London', period, [], t2).utc_to_local(t2))
736
+ assert_equal(Time.utc(2005,6,18,17,24,23,567000), TestTimezone.new('Europe/London', period, [], tu).utc_to_local(tu))
737
+ assert_equal(Time.utc(2005,6,18,17,24,23,567000), TestTimezone.new('Europe/London', period, [], tu2).utc_to_local(tu2))
738
+ assert_equal(Time.utc(2005,6,18,17,24,23).to_i, TestTimezone.new('Europe/London', period, [], ts).utc_to_local(ts))
739
+ end
740
+
741
+ def test_utc_to_local_offset
742
+ dt = DateTime.new(2005,6,18,16,24,23)
743
+ dt2 = DateTime.new(2005,6,18,16,24,23).new_offset(Rational(5,24))
744
+ dtu = DateTime.new(2005,6,18,16,24,23 + Rational(567,1000))
745
+ dtu2 = DateTime.new(2005,6,18,16,24,23 + Rational(567,1000)).new_offset(Rational(5,24))
746
+ t = Time.utc(2005,6,18,16,24,23)
747
+ t2 = Time.local(2005,6,18,16,24,23)
748
+ tu = Time.utc(2005,6,18,16,24,23,567000)
749
+ tu2 = Time.local(2005,6,18,16,24,23,567000)
750
+
751
+ o1 = TimezoneOffset.new(0, 0, :GMT)
752
+ o2 = TimezoneOffset.new(0, 3600, :BST)
753
+
754
+ period = TimezonePeriod.new(
755
+ TestTimezoneTransition.new(o2, o1, 1111885200),
756
+ TestTimezoneTransition.new(o1, o2, 1130634000))
757
+
758
+ assert_equal(0, TestTimezone.new('Europe/London', period, [], dt).utc_to_local(dt).offset)
759
+ assert_equal(0, TestTimezone.new('Europe/London', period, [], dt2).utc_to_local(dt2).offset)
760
+ assert_equal(0, TestTimezone.new('Europe/London', period, [], dtu).utc_to_local(dtu).offset)
761
+ assert_equal(0, TestTimezone.new('Europe/London', period, [], dtu2).utc_to_local(dtu2).offset)
762
+ assert_equal(0, TestTimezone.new('Europe/London', period, [], t).utc_to_local(t).utc_offset)
763
+ assert(TestTimezone.new('Europe/London', period, [], t).utc_to_local(t).utc?)
764
+ assert_equal(0, TestTimezone.new('Europe/London', period, [], t2).utc_to_local(t2).utc_offset)
765
+ assert(TestTimezone.new('Europe/London', period, [], t2).utc_to_local(t2).utc?)
766
+ assert_equal(0, TestTimezone.new('Europe/London', period, [], tu).utc_to_local(tu).utc_offset)
767
+ assert(TestTimezone.new('Europe/London', period, [], tu).utc_to_local(tu).utc?)
768
+ assert_equal(0, TestTimezone.new('Europe/London', period, [], tu2).utc_to_local(tu2).utc_offset)
769
+ assert(TestTimezone.new('Europe/London', period, [], tu2).utc_to_local(tu2).utc?)
770
+ end
771
+
772
+ def test_local_to_utc
773
+ dt = DateTime.new(2005,6,18,16,24,23)
774
+ dt2 = DateTime.new(2005,6,18,16,24,23).new_offset(Rational(5, 24))
775
+ dtu = DateTime.new(2005,6,18,16,24,23 + Rational(567,1000))
776
+ dtu2 = DateTime.new(2005,6,18,16,24,23 + Rational(567,1000)).new_offset(Rational(5, 24))
777
+ t = Time.utc(2005,6,18,16,24,23)
778
+ t2 = Time.local(2005,6,18,16,24,23)
779
+ tu = Time.utc(2005,6,18,16,24,23,567000)
780
+ tu2 = Time.local(2005,6,18,16,24,23,567000)
781
+ ts = t.to_i
782
+
783
+ o1 = TimezoneOffset.new(0, 0, :GMT)
784
+ o2 = TimezoneOffset.new(0, 3600, :BST)
785
+
786
+ period = TimezonePeriod.new(
787
+ TestTimezoneTransition.new(o2, o1, 1111885200),
788
+ TestTimezoneTransition.new(o1, o2, 1130634000))
789
+
790
+ assert_equal(DateTime.new(2005,6,18,15,24,23), TestTimezone.new('Europe/London', nil, [period], dt).local_to_utc(dt))
791
+ assert_equal(DateTime.new(2005,6,18,15,24,23), TestTimezone.new('Europe/London', nil, [period], dt2).local_to_utc(dt2))
792
+ assert_equal(DateTime.new(2005,6,18,15,24,23 + Rational(567,1000)), TestTimezone.new('Europe/London', nil, [period], dtu).local_to_utc(dtu))
793
+ assert_equal(DateTime.new(2005,6,18,15,24,23 + Rational(567,1000)), TestTimezone.new('Europe/London', nil, [period], dtu2).local_to_utc(dtu2))
794
+ assert_equal(Time.utc(2005,6,18,15,24,23), TestTimezone.new('Europe/London', nil, [period], t).local_to_utc(t))
795
+ assert_equal(Time.utc(2005,6,18,15,24,23), TestTimezone.new('Europe/London', nil, [period], t2).local_to_utc(t2))
796
+ assert_equal(Time.utc(2005,6,18,15,24,23,567000), TestTimezone.new('Europe/London', nil, [period], tu).local_to_utc(tu))
797
+ assert_equal(Time.utc(2005,6,18,15,24,23,567000), TestTimezone.new('Europe/London', nil, [period], tu2).local_to_utc(tu2))
798
+ assert_equal(Time.utc(2005,6,18,15,24,23).to_i, TestTimezone.new('Europe/London', nil, [period], ts).local_to_utc(ts))
799
+ end
800
+
801
+ def test_local_to_utc_offset
802
+ dt = DateTime.new(2005,6,18,16,24,23)
803
+ dt2 = DateTime.new(2005,6,18,16,24,23).new_offset(Rational(5, 24))
804
+ dtu = DateTime.new(2005,6,18,16,24,23 + Rational(567,1000))
805
+ dtu2 = DateTime.new(2005,6,18,16,24,23 + Rational(567,1000)).new_offset(Rational(5, 24))
806
+ t = Time.utc(2005,6,18,16,24,23)
807
+ t2 = Time.local(2005,6,18,16,24,23)
808
+ tu = Time.utc(2005,6,18,16,24,23,567000)
809
+ tu2 = Time.local(2005,6,18,16,24,23,567000)
810
+
811
+ o1 = TimezoneOffset.new(0, 0, :GMT)
812
+ o2 = TimezoneOffset.new(0, 3600, :BST)
813
+
814
+ period = TimezonePeriod.new(
815
+ TestTimezoneTransition.new(o2, o1, 1111885200),
816
+ TestTimezoneTransition.new(o1, o2, 1130634000))
817
+
818
+ assert_equal(0, TestTimezone.new('Europe/London', nil, [period], dt).local_to_utc(dt).offset)
819
+ assert_equal(0, TestTimezone.new('Europe/London', nil, [period], dt2).local_to_utc(dt2).offset)
820
+ assert_equal(0, TestTimezone.new('Europe/London', nil, [period], dtu).local_to_utc(dtu).offset)
821
+ assert_equal(0, TestTimezone.new('Europe/London', nil, [period], dtu2).local_to_utc(dtu2).offset)
822
+ assert_equal(0, TestTimezone.new('Europe/London', nil, [period], t).local_to_utc(t).utc_offset)
823
+ assert(TestTimezone.new('Europe/London', nil, [period], t).local_to_utc(t).utc?)
824
+ assert_equal(0, TestTimezone.new('Europe/London', nil, [period], t2).local_to_utc(t2).utc_offset)
825
+ assert(TestTimezone.new('Europe/London', nil, [period], t2).local_to_utc(t2).utc?)
826
+ assert_equal(0, TestTimezone.new('Europe/London', nil, [period], tu).local_to_utc(tu).utc_offset)
827
+ assert(TestTimezone.new('Europe/London', nil, [period], tu).local_to_utc(tu).utc?)
828
+ assert_equal(0, TestTimezone.new('Europe/London', nil, [period], tu2).local_to_utc(tu2).utc_offset)
829
+ assert(TestTimezone.new('Europe/London', nil, [period], tu2).local_to_utc(tu2).utc?)
830
+ end
831
+
832
+ def test_local_to_utc_utc_local_returns_utc
833
+ # Check that UTC time instances are always returned even if the system
834
+ # is using UTC as the time zone.
835
+
836
+ # Note that this will only test will only work correctly on platforms where
837
+ # setting the TZ environment variable has an effect. If setting TZ has no
838
+ # effect, then this test will still pass.
839
+
840
+ old_tz = ENV['TZ']
841
+ begin
842
+ ENV['TZ'] = 'UTC'
843
+
844
+ tz = Timezone.get('America/New_York')
845
+
846
+ t = tz.local_to_utc(Time.local(2014, 1, 11, 17, 18, 41))
847
+ assert_equal(Time.utc(2014, 1, 11, 22, 18, 41), t)
848
+ assert(t.utc?)
849
+ ensure
850
+ ENV['TZ'] = old_tz
851
+ end
852
+ end
853
+
854
+ def test_local_to_utc_invalid
855
+ dt = DateTime.new(2004,4,4,2,30,0)
856
+ tz = TestTimezone.new('America/New_York', nil, [], dt)
857
+ assert_raises(PeriodNotFound) { tz.local_to_utc(dt) }
858
+
859
+ t = Time.utc(2004,4,4,2,30,0)
860
+ tz = TestTimezone.new('America/New_York', nil, [], t)
861
+ assert_raises(PeriodNotFound) { tz.local_to_utc(t) }
862
+
863
+ i = Time.utc(2004,4,4,2,30,0).to_i
864
+ tz = TestTimezone.new('America/New_York', nil, [], i)
865
+ assert_raises(PeriodNotFound) { tz.local_to_utc(i) }
866
+ end
867
+
868
+ def test_local_to_utc_ambiguous
869
+ o1 = TimezoneOffset.new(-18000, 0, :EST)
870
+ o2 = TimezoneOffset.new(-18000, 3600, :EDT)
871
+
872
+ t1 = TestTimezoneTransition.new(o2, o1, 1081062000)
873
+ t2 = TestTimezoneTransition.new(o1, o2, 1099202400)
874
+ t3 = TestTimezoneTransition.new(o2, o1, 1112511600)
875
+
876
+ p1 = TimezonePeriod.new(t1, t2)
877
+ p2 = TimezonePeriod.new(t2, t3)
878
+
879
+ dt = DateTime.new(2004,10,31,1,30,0)
880
+ tz = TestTimezone.new('America/New_York', nil, [p1, p2], dt)
881
+ assert_raises(AmbiguousTime) { tz.local_to_utc(dt) }
882
+
883
+ t = Time.utc(2004,10,31,1,30,0)
884
+ tz = TestTimezone.new('America/New_York', nil, [p1, p2], t)
885
+ assert_raises(AmbiguousTime) { tz.local_to_utc(t) }
886
+
887
+ i = Time.utc(2004,10,31,1,30,0).to_i
888
+ tz = TestTimezone.new('America/New_York', nil, [p1, p2], i)
889
+ assert_raises(AmbiguousTime) { tz.local_to_utc(i) }
890
+
891
+ f = Time.utc(2004,10,31,1,30,0,501).to_i
892
+ tz = TestTimezone.new('America/New_York', nil, [p1, p2], f)
893
+ assert_raises(AmbiguousTime) { tz.local_to_utc(f) }
894
+ end
895
+
896
+ def test_local_to_utc_not_found
897
+ dt = DateTime.new(2004,4,4,2,0,0)
898
+ t = Time.utc(2004,4,4,2,30,0)
899
+ i = Time.utc(2004,4,4,2,59,59).to_i
900
+
901
+ dt_tz = TestTimezone.new('America/New_York', nil, [], dt)
902
+ t_tz = TestTimezone.new('America/New_York', nil, [], t)
903
+ i_tz = TestTimezone.new('America/New_York', nil, [], i)
904
+
905
+ assert_raises(PeriodNotFound) { dt_tz.local_to_utc(dt) }
906
+ assert_raises(PeriodNotFound) { t_tz.local_to_utc(t) }
907
+ assert_raises(PeriodNotFound) { i_tz.local_to_utc(i) }
908
+ end
909
+
910
+ def test_local_to_utc_default_dst_set_true
911
+ Timezone.default_dst = true
912
+
913
+ o1 = TimezoneOffset.new(-18000, 0, :EST)
914
+ o2 = TimezoneOffset.new(-18000, 3600, :EDT)
915
+
916
+ t1 = TestTimezoneTransition.new(o2, o1, 1081062000)
917
+ t2 = TestTimezoneTransition.new(o1, o2, 1099202400)
918
+ t3 = TestTimezoneTransition.new(o2, o1, 1112511600)
919
+
920
+ p1 = TimezonePeriod.new(t1, t2)
921
+ p2 = TimezonePeriod.new(t2, t3)
922
+
923
+ dt = DateTime.new(2004,10,31,1,30,0)
924
+ tz = TestTimezone.new('America/New_York', nil, [p1, p2], dt)
925
+
926
+ assert_equal(DateTime.new(2004,10,31,5,30,0), tz.local_to_utc(dt))
927
+ assert_equal(DateTime.new(2004,10,31,5,30,0), tz.local_to_utc(dt, true))
928
+ assert_equal(DateTime.new(2004,10,31,6,30,0), tz.local_to_utc(dt, false))
929
+ assert_raises(AmbiguousTime) { tz.local_to_utc(dt, nil) }
930
+ assert_equal(DateTime.new(2004,10,31,5,30,0), tz.local_to_utc(dt) {|periods| raise BlockCalled, 'should not be called' })
931
+ end
932
+
933
+ def test_local_to_utc_default_dst_set_false
934
+ Timezone.default_dst = false
935
+
936
+ o1 = TimezoneOffset.new(-18000, 0, :EST)
937
+ o2 = TimezoneOffset.new(-18000, 3600, :EDT)
938
+
939
+ t1 = TestTimezoneTransition.new(o2, o1, 1081062000)
940
+ t2 = TestTimezoneTransition.new(o1, o2, 1099202400)
941
+ t3 = TestTimezoneTransition.new(o2, o1, 1112511600)
942
+
943
+ p1 = TimezonePeriod.new(t1, t2)
944
+ p2 = TimezonePeriod.new(t2, t3)
945
+
946
+ dt = DateTime.new(2004,10,31,1,30,0)
947
+ tz = TestTimezone.new('America/New_York', nil, [p1, p2], dt)
948
+
949
+ assert_equal(DateTime.new(2004,10,31,6,30,0), tz.local_to_utc(dt))
950
+ assert_equal(DateTime.new(2004,10,31,6,30,0), tz.local_to_utc(dt, false))
951
+ assert_equal(DateTime.new(2004,10,31,5,30,0), tz.local_to_utc(dt, true))
952
+ assert_raises(AmbiguousTime) { tz.local_to_utc(dt, nil) }
953
+ assert_equal(DateTime.new(2004,10,31,6,30,0), tz.local_to_utc(dt) {|periods| raise BlockCalled, 'should not be called' })
954
+ end
955
+
956
+ def test_local_to_utc_dst_flag_resolved
957
+ o1 = TimezoneOffset.new(-18000, 0, :EST)
958
+ o2 = TimezoneOffset.new(-18000, 3600, :EDT)
959
+
960
+ t1 = TestTimezoneTransition.new(o2, o1, 1081062000)
961
+ t2 = TestTimezoneTransition.new(o1, o2, 1099202400)
962
+ t3 = TestTimezoneTransition.new(o2, o1, 1112511600)
963
+
964
+ p1 = TimezonePeriod.new(t1, t2)
965
+ p2 = TimezonePeriod.new(t2, t3)
966
+
967
+ dt = DateTime.new(2004,10,31,1,30,0)
968
+ tz = TestTimezone.new('America/New_York', nil, [p1, p2], dt)
969
+
970
+ assert_equal(DateTime.new(2004,10,31,5,30,0), tz.local_to_utc(dt, true))
971
+ assert_equal(DateTime.new(2004,10,31,6,30,0), tz.local_to_utc(dt, false))
972
+ assert_equal(DateTime.new(2004,10,31,5,30,0), tz.local_to_utc(dt, true) {|periods| raise BlockCalled, 'should not be called' })
973
+ assert_equal(DateTime.new(2004,10,31,6,30,0), tz.local_to_utc(dt, false) {|periods| raise BlockCalled, 'should not be called' })
974
+ end
975
+
976
+ def test_local_to_utc_dst_block_called
977
+ o1 = TimezoneOffset.new(-18000, 0, :EST)
978
+ o2 = TimezoneOffset.new(-18000, 3600, :EDT)
979
+
980
+ t1 = TestTimezoneTransition.new(o2, o1, 1081062000)
981
+ t2 = TestTimezoneTransition.new(o1, o2, 1099202400)
982
+ t3 = TestTimezoneTransition.new(o2, o1, 1112511600)
983
+
984
+ p1 = TimezonePeriod.new(t1, t2)
985
+ p2 = TimezonePeriod.new(t2, t3)
986
+
987
+ dt = DateTime.new(2004,10,31,1,30,0)
988
+ tz = TestTimezone.new('America/New_York', nil, [p1, p2], dt)
989
+
990
+ assert_raises(BlockCalled) {
991
+ tz.local_to_utc(dt) {|periods|
992
+ assert_equal([p1, p2], periods)
993
+
994
+ # raise exception to test that the block was called
995
+ raise BlockCalled, 'should be raised'
996
+ }
997
+ }
998
+
999
+ assert_equal(DateTime.new(2004,10,31,5,30,0), tz.local_to_utc(dt) {|periods| periods.first})
1000
+ assert_equal(DateTime.new(2004,10,31,6,30,0), tz.local_to_utc(dt) {|periods| periods.last})
1001
+ assert_equal(DateTime.new(2004,10,31,5,30,0), tz.local_to_utc(dt) {|periods| [periods.first]})
1002
+ assert_equal(DateTime.new(2004,10,31,6,30,0), tz.local_to_utc(dt) {|periods| [periods.last]})
1003
+ end
1004
+
1005
+ def test_local_to_utc_dst_cannot_resolve
1006
+ # At midnight local time on Aug 5 1915 in Warsaw, the clocks were put back
1007
+ # 24 minutes and both periods were non-DST. Hence the block should be
1008
+ # called regardless of the value of the Boolean dst parameter.
1009
+
1010
+ o0 = TimezoneOffset.new(5040, 0, :LMT)
1011
+ o1 = TimezoneOffset.new(5040, 0, :WMT)
1012
+ o2 = TimezoneOffset.new(3600, 0, :CET)
1013
+ o3 = TimezoneOffset.new(3600, 3600, :CEST)
1014
+
1015
+ t1 = TestTimezoneTransition.new(o1, o0, DateTime.new(1879, 12, 31, 22, 36, 0))
1016
+ t2 = TestTimezoneTransition.new(o2, o1, DateTime.new(1915, 8, 4, 22, 36, 0))
1017
+ t3 = TestTimezoneTransition.new(o3, o2, DateTime.new(1916, 4, 30, 22, 0, 0))
1018
+
1019
+ p1 = TimezonePeriod.new(t1, t2)
1020
+ p2 = TimezonePeriod.new(t2, t3)
1021
+
1022
+ dt = DateTime.new(1915,8,4,23,40,0)
1023
+
1024
+ tz = TestTimezone.new('Europe/Warsaw', nil, [p1, p2], dt)
1025
+
1026
+ assert_raises(BlockCalled) do
1027
+ tz.local_to_utc(dt, true) do |periods|
1028
+ assert_equal([p1, p2], periods)
1029
+ raise BlockCalled, 'should be raised'
1030
+ end
1031
+ end
1032
+
1033
+ assert_raises(BlockCalled) do
1034
+ tz.local_to_utc(dt, false) do |periods|
1035
+ assert_equal([p1, p2], periods)
1036
+ raise BlockCalled, 'should be raised'
1037
+ end
1038
+ end
1039
+
1040
+ assert_equal(DateTime.new(1915,8,4,22,16,0), tz.local_to_utc(dt) {|periods| periods.first})
1041
+ assert_equal(DateTime.new(1915,8,4,22,40,0), tz.local_to_utc(dt) {|periods| periods.last})
1042
+ assert_equal(DateTime.new(1915,8,4,22,16,0), tz.local_to_utc(dt) {|periods| [periods.first]})
1043
+ assert_equal(DateTime.new(1915,8,4,22,40,0), tz.local_to_utc(dt) {|periods| [periods.last]})
1044
+ end
1045
+
1046
+ def test_local_to_utc_block_ambiguous
1047
+ o1 = TimezoneOffset.new(-18000, 0, :EST)
1048
+ o2 = TimezoneOffset.new(-18000, 3600, :EDT)
1049
+
1050
+ t1 = TestTimezoneTransition.new(o2, o1, 1081062000)
1051
+ t2 = TestTimezoneTransition.new(o1, o2, 1099202400)
1052
+ t3 = TestTimezoneTransition.new(o2, o1, 1112511600)
1053
+
1054
+ p1 = TimezonePeriod.new(t1, t2)
1055
+ p2 = TimezonePeriod.new(t2, t3)
1056
+
1057
+ dt = DateTime.new(2004,10,31,1,30,0)
1058
+ tz = TestTimezone.new('America/New_York', nil, [p1, p2], dt)
1059
+
1060
+ assert_raises(AmbiguousTime) { tz.local_to_utc(dt) {|periods| nil} }
1061
+ assert_raises(AmbiguousTime) { tz.local_to_utc(dt) {|periods| periods} }
1062
+ assert_raises(AmbiguousTime) { tz.local_to_utc(dt) {|periods| []} }
1063
+ assert_raises(AmbiguousTime) { tz.local_to_utc(dt) {|periods| raise AmbiguousTime, 'Ambiguous time'} }
1064
+ end
1065
+
1066
+ def test_offsets_up_to
1067
+ o1 = TimezoneOffset.new(-17900, 0, :TESTLMT)
1068
+ o2 = TimezoneOffset.new(-18000, 3600, :TESTD)
1069
+ o3 = TimezoneOffset.new(-18000, 0, :TESTS)
1070
+ o4 = TimezoneOffset.new(-21600, 3600, :TESTD)
1071
+ o5 = TimezoneOffset.new(-21600, 0, :TESTS)
1072
+
1073
+ t1 = TestTimezoneTransition.new(o2, o1, Time.utc(2010, 4,1,1,0,0).to_i)
1074
+ t2 = TestTimezoneTransition.new(o3, o2, Time.utc(2010,10,1,1,0,0).to_i)
1075
+ t3 = TestTimezoneTransition.new(o2, o3, Time.utc(2011, 3,1,1,0,0).to_i)
1076
+ t4 = TestTimezoneTransition.new(o4, o2, Time.utc(2011, 4,1,1,0,0).to_i)
1077
+ t5 = TestTimezoneTransition.new(o3, o4, Time.utc(2011,10,1,1,0,0).to_i)
1078
+ t6 = TestTimezoneTransition.new(o5, o3, Time.utc(2012, 3,1,1,0,0).to_i)
1079
+
1080
+ assert_array_same_items([o1, o2, o3, o4, o5],
1081
+ OffsetsUpToTestTimezone.new('Test/Zone', Time.utc(2012,3,1,1,0,1), nil, [t1, t2, t3, t4, t5, t6]).
1082
+ offsets_up_to(Time.utc(2012,3,1,1,0,1)))
1083
+ assert_array_same_items([o2, o3, o4, o5],
1084
+ OffsetsUpToTestTimezone.new('Test/Zone', Time.utc(2012,3,1,1,0,1), Time.utc(2010,4,1,1,0,0), [t1, t2, t3, t4, t5, t6]).
1085
+ offsets_up_to(Time.utc(2012,3,1,1,0,1), Time.utc(2010,4,1,1,0,0)))
1086
+ assert_array_same_items([o1, o2, o3, o4],
1087
+ OffsetsUpToTestTimezone.new('Test/Zone', Time.utc(2012,3,1,1,0,0), nil, [t1, t2, t3, t4, t5]).
1088
+ offsets_up_to(Time.utc(2012,3,1,1,0,0)))
1089
+ assert_array_same_items([o2, o3, o4, o5],
1090
+ OffsetsUpToTestTimezone.new('Test/Zone', Time.utc(2012,3,1,1,0,1), Time.utc(2010,4,1,1,0,1), [t2, t3, t4, t5, t6]).
1091
+ offsets_up_to(Time.utc(2012,3,1,1,0,1), Time.utc(2010,4,1,1,0,1)))
1092
+ assert_array_same_items([o2, o3],
1093
+ OffsetsUpToTestTimezone.new('Test/Zone', Time.utc(2011,3,1,2,0,0), Time.utc(2011,3,1,0,0,0), [t3]).
1094
+ offsets_up_to(Time.utc(2011,3,1,2,0,0), Time.utc(2011,3,1,0,0,0)))
1095
+ assert_array_same_items([o3, o4],
1096
+ OffsetsUpToTestTimezone.new('Test/Zone', Time.utc(2012,3,1,1,0,0), Time.utc(2011,4,1,1,0,0), [t4, t5]).
1097
+ offsets_up_to(Time.utc(2012,3,1,1,0,0), Time.utc(2011,4,1,1,0,0)))
1098
+
1099
+ assert_array_same_items([o1, o2, o3, o4, o5],
1100
+ OffsetsUpToTestTimezone.new('Test/Zone', Time.utc(2012,3,1,1,0,1).to_i, nil, [t1, t2, t3, t4, t5, t6]).
1101
+ offsets_up_to(Time.utc(2012,3,1,1,0,1).to_i))
1102
+ assert_array_same_items([o2, o3, o4, o5],
1103
+ OffsetsUpToTestTimezone.new('Test/Zone', Time.utc(2012,3,1,1,0,1).to_i, Time.utc(2010,4,1,1,0,0).to_i, [t1, t2, t3, t4, t5, t6]).
1104
+ offsets_up_to(Time.utc(2012,3,1,1,0,1).to_i, Time.utc(2010,4,1,1,0,0).to_i))
1105
+ assert_array_same_items([o1, o2, o3, o4],
1106
+ OffsetsUpToTestTimezone.new('Test/Zone', Time.utc(2012,3,1,1,0,0).to_i, nil, [t1, t2, t3, t4, t5]).
1107
+ offsets_up_to(Time.utc(2012,3,1,1,0,0).to_i))
1108
+ assert_array_same_items([o2, o3, o4, o5],
1109
+ OffsetsUpToTestTimezone.new('Test/Zone', Time.utc(2012,3,1,1,0,1).to_i, Time.utc(2010,4,1,1,0,1).to_i, [t2, t3, t4, t5, t6]).
1110
+ offsets_up_to(Time.utc(2012,3,1,1,0,1).to_i, Time.utc(2010,4,1,1,0,1).to_i))
1111
+ assert_array_same_items([o2, o3],
1112
+ OffsetsUpToTestTimezone.new('Test/Zone', Time.utc(2011,3,1,2,0,0).to_i, Time.utc(2011,3,1,0,0,0).to_i, [t3]).
1113
+ offsets_up_to(Time.utc(2011,3,1,2,0,0).to_i, Time.utc(2011,3,1,0,0,0).to_i))
1114
+ assert_array_same_items([o3, o4],
1115
+ OffsetsUpToTestTimezone.new('Test/Zone', Time.utc(2012,3,1,1,0,0).to_i, Time.utc(2011,4,1,1,0,0).to_i, [t4, t5]).
1116
+ offsets_up_to(Time.utc(2012,3,1,1,0,0).to_i, Time.utc(2011,4,1,1,0,0).to_i))
1117
+
1118
+ assert_array_same_items([o1, o2, o3, o4, o5],
1119
+ OffsetsUpToTestTimezone.new('Test/Zone', DateTime.new(2012,3,1,1,0,1), nil, [t1, t2, t3, t4, t5, t6]).
1120
+ offsets_up_to(DateTime.new(2012,3,1,1,0,1)))
1121
+ assert_array_same_items([o2, o3, o4, o5],
1122
+ OffsetsUpToTestTimezone.new('Test/Zone', DateTime.new(2012,3,1,1,0,1), DateTime.new(2010,4,1,1,0,0), [t1, t2, t3, t4, t5, t6]).
1123
+ offsets_up_to(DateTime.new(2012,3,1,1,0,1), DateTime.new(2010,4,1,1,0,0)))
1124
+ assert_array_same_items([o1, o2, o3, o4],
1125
+ OffsetsUpToTestTimezone.new('Test/Zone', DateTime.new(2012,3,1,1,0,0), nil, [t1, t2, t3, t4, t5]).
1126
+ offsets_up_to(DateTime.new(2012,3,1,1,0,0)))
1127
+ assert_array_same_items([o2, o3, o4, o5],
1128
+ OffsetsUpToTestTimezone.new('Test/Zone', DateTime.new(2012,3,1,1,0,1), DateTime.new(2010,4,1,1,0,1), [t2, t3, t4, t5, t6]).
1129
+ offsets_up_to(DateTime.new(2012,3,1,1,0,1), DateTime.new(2010,4,1,1,0,1)))
1130
+ assert_array_same_items([o2, o3],
1131
+ OffsetsUpToTestTimezone.new('Test/Zone', DateTime.new(2011,3,1,2,0,0), DateTime.new(2011,3,1,0,0,0), [t3]).
1132
+ offsets_up_to(DateTime.new(2011,3,1,2,0,0), DateTime.new(2011,3,1,0,0,0)))
1133
+ assert_array_same_items([o3, o4],
1134
+ OffsetsUpToTestTimezone.new('Test/Zone', DateTime.new(2012,3,1,1,0,0), DateTime.new(2011,4,1,1,0,0), [t4, t5]).
1135
+ offsets_up_to(DateTime.new(2012,3,1,1,0,0), DateTime.new(2011,4,1,1,0,0)))
1136
+ end
1137
+
1138
+ def test_offsets_up_to_no_transitions
1139
+ o = TimezoneOffset.new(600, 0, :LMT)
1140
+ p = TimezonePeriod.new(nil, nil, o)
1141
+
1142
+ assert_array_same_items([o],
1143
+ OffsetsUpToNoTransitionsTestTimezone.new('Test/Zone', Time.utc(2000,1,1,1,0,0), nil, p).
1144
+ offsets_up_to(Time.utc(2000,1,1,1,0,0)))
1145
+ assert_array_same_items([o],
1146
+ OffsetsUpToNoTransitionsTestTimezone.new('Test/Zone', Time.utc(2000,1,1,1,0,0), Time.utc(1990,1,1,1,0,0), p).
1147
+ offsets_up_to(Time.utc(2000,1,1,1,0,0), Time.utc(1990,1,1,1,0,0)))
1148
+
1149
+ assert_array_same_items([o],
1150
+ OffsetsUpToNoTransitionsTestTimezone.new('Test/Zone', Time.utc(2000,1,1,1,0,0).to_i, nil, p).
1151
+ offsets_up_to(Time.utc(2000,1,1,1,0,0).to_i))
1152
+ assert_array_same_items([o],
1153
+ OffsetsUpToNoTransitionsTestTimezone.new('Test/Zone', Time.utc(2000,1,1,1,0,0).to_i, Time.utc(1990,1,1,1,0,0).to_i, p).
1154
+ offsets_up_to(Time.utc(2000,1,1,1,0,0).to_i, Time.utc(1990,1,1,1,0,0).to_i))
1155
+
1156
+ assert_array_same_items([o],
1157
+ OffsetsUpToNoTransitionsTestTimezone.new('Test/Zone', DateTime.new(2000,1,1,1,0,0), nil, p).
1158
+ offsets_up_to(DateTime.new(2000,1,1,1,0,0)))
1159
+ assert_array_same_items([o],
1160
+ OffsetsUpToNoTransitionsTestTimezone.new('Test/Zone', DateTime.new(2000,1,1,1,0,0), DateTime.new(1990,1,1,1,0,0), p).
1161
+ offsets_up_to(DateTime.new(2000,1,1,1,0,0), DateTime.new(1990,1,1,1,0,0)))
1162
+ end
1163
+
1164
+ def test_offsets_up_to_utc_to_not_greater_than_utc_from
1165
+ assert_raises(ArgumentError) do
1166
+ OffsetsUpToTestTimezone.new('Test/Zone', Time.utc(2012,8,1,0,0,0), Time.utc(2012,8,1,0,0,0), []).
1167
+ offsets_up_to(Time.utc(2012,8,1,0,0,0), Time.utc(2012,8,1,0,0,0))
1168
+ end
1169
+
1170
+ assert_raises(ArgumentError) do
1171
+ OffsetsUpToTestTimezone.new('Test/Zone', Time.utc(2012,8,1,0,0,0).to_i, Time.utc(2012,8,1,0,0,0).to_i, []).
1172
+ offsets_up_to(Time.utc(2012,8,1,0,0,0).to_i, Time.utc(2012,8,1,0,0,0).to_i)
1173
+ end
1174
+
1175
+ assert_raises(ArgumentError) do
1176
+ OffsetsUpToTestTimezone.new('Test/Zone', DateTime.new(2012,8,1,0,0,0), DateTime.new(2012,8,1,0,0,0), []).
1177
+ offsets_up_to(DateTime.new(2012,8,1,0,0,0), DateTime.new(2012,8,1,0,0,0))
1178
+ end
1179
+ end
1180
+
1181
+ def test_now
1182
+ assert_kind_of(Time, Timezone.get('Europe/London').now)
1183
+ end
1184
+
1185
+ def test_current_period
1186
+ assert_kind_of(TimezonePeriod, Timezone.get('Europe/London').current_period)
1187
+ end
1188
+
1189
+ def test_current_period_and_time
1190
+ current = Timezone.get('Europe/London').current_period_and_time
1191
+ assert_equal(2, current.length)
1192
+ assert_kind_of(Time, current[0])
1193
+ assert_kind_of(TimezonePeriod, current[1])
1194
+ end
1195
+
1196
+ def test_current_time_and_period
1197
+ current = Timezone.get('Europe/London').current_time_and_period
1198
+ assert_equal(2, current.length)
1199
+ assert_kind_of(Time, current[0])
1200
+ assert_kind_of(TimezonePeriod, current[1])
1201
+ end
1202
+
1203
+ def test_compare
1204
+ assert_equal(0, TestTimezone.new('Europe/London') <=> TestTimezone.new('Europe/London'))
1205
+ assert_equal(-1, TestTimezone.new('Europe/London') <=> TestTimezone.new('Europe/london'))
1206
+ assert_equal(-1, TestTimezone.new('Europe/London') <=> TestTimezone.new('Europe/Paris'))
1207
+ assert_equal(1, TestTimezone.new('Europe/Paris') <=> TestTimezone.new('Europe/London'))
1208
+ assert_equal(-1, TestTimezone.new('America/New_York') <=> TestTimezone.new('Europe/Paris'))
1209
+ assert_equal(1, TestTimezone.new('Europe/Paris') <=> TestTimezone.new('America/New_York'))
1210
+ end
1211
+
1212
+ def test_compare_non_comparable
1213
+ assert_nil(TestTimezone.new('Europe/London') <=> Object.new)
1214
+ end
1215
+
1216
+ def test_equality
1217
+ assert_equal(true, TestTimezone.new('Europe/London') == TestTimezone.new('Europe/London'))
1218
+ assert_equal(false, TestTimezone.new('Europe/London') == TestTimezone.new('Europe/london'))
1219
+ assert_equal(false, TestTimezone.new('Europe/London') == TestTimezone.new('Europe/Paris'))
1220
+ assert(!(TestTimezone.new('Europe/London') == Object.new))
1221
+ end
1222
+
1223
+ def test_eql
1224
+ assert_equal(true, TestTimezone.new('Europe/London').eql?(TestTimezone.new('Europe/London')))
1225
+ assert_equal(false, TestTimezone.new('Europe/London').eql?(TestTimezone.new('Europe/london')))
1226
+ assert_equal(false, TestTimezone.new('Europe/London').eql?(TestTimezone.new('Europe/Paris')))
1227
+ assert(!TestTimezone.new('Europe/London').eql?(Object.new))
1228
+ end
1229
+
1230
+ def test_hash
1231
+ assert_equal('Europe/London'.hash, TestTimezone.new('Europe/London').hash)
1232
+ assert_equal('America/New_York'.hash, TestTimezone.new('America/New_York').hash)
1233
+ end
1234
+
1235
+ def test_marshal_data
1236
+ tz = Timezone.get('Europe/London')
1237
+ assert_kind_of(DataTimezone, tz)
1238
+ assert_same(tz, Marshal.load(Marshal.dump(tz)))
1239
+ end
1240
+
1241
+ def test_marshal_linked
1242
+ tz = Timezone.get('UTC')
1243
+
1244
+ # ZoneinfoDataSource doesn't return LinkedTimezoneInfo for any timezone.
1245
+ if DataSource.get.load_timezone_info('UTC').kind_of?(LinkedTimezoneInfo)
1246
+ assert_kind_of(LinkedTimezone, tz)
1247
+ else
1248
+ assert_kind_of(DataTimezone, tz)
1249
+ end
1250
+
1251
+ assert_same(tz, Marshal.load(Marshal.dump(tz)))
1252
+ end
1253
+
1254
+ def test_strftime_datetime
1255
+ tz = Timezone.get('Europe/London')
1256
+ dt = DateTime.new(2006, 7, 15, 22, 12, 2)
1257
+ assert_equal('23:12:02 BST', tz.strftime('%H:%M:%S %Z', dt))
1258
+ assert_equal('BST', tz.strftime('%Z', dt))
1259
+ assert_equal('%ZBST', tz.strftime('%%Z%Z', dt))
1260
+ assert_equal('BST BST', tz.strftime('%Z %Z', dt))
1261
+ assert_equal('BST %Z %BST %%Z %%BST', tz.strftime('%Z %%Z %%%Z %%%%Z %%%%%Z', dt))
1262
+ assert_equal('+0100 +01:00 +01:00:00 +01 %::::z', tz.strftime('%z %:z %::z %:::z %::::z', dt))
1263
+ end
1264
+
1265
+ def test_strftime_time
1266
+ tz = Timezone.get('Europe/London')
1267
+ t = Time.utc(2006, 7, 15, 22, 12, 2)
1268
+ assert_equal('23:12:02 BST', tz.strftime('%H:%M:%S %Z', t))
1269
+ assert_equal('BST', tz.strftime('%Z', t))
1270
+ assert_equal('%ZBST', tz.strftime('%%Z%Z', t))
1271
+ assert_equal('BST BST', tz.strftime('%Z %Z', t))
1272
+ assert_equal('BST %Z %BST %%Z %%BST', tz.strftime('%Z %%Z %%%Z %%%%Z %%%%%Z', t))
1273
+ assert_equal('+0100 +01:00 +01:00:00 +01 %::::z', tz.strftime('%z %:z %::z %:::z %::::z', t))
1274
+ end
1275
+
1276
+ def test_strftime_int
1277
+ tz = Timezone.get('Europe/London')
1278
+ i = Time.utc(2006, 7, 15, 22, 12, 2).to_i
1279
+ assert_equal('23:12:02 BST', tz.strftime('%H:%M:%S %Z', i))
1280
+ assert_equal('BST', tz.strftime('%Z', i))
1281
+ assert_equal('%ZBST', tz.strftime('%%Z%Z', i))
1282
+ assert_equal('BST BST', tz.strftime('%Z %Z', i))
1283
+ assert_equal('BST %Z %BST %%Z %%BST', tz.strftime('%Z %%Z %%%Z %%%%Z %%%%%Z', i))
1284
+ assert_equal('+0100 +01:00 +01:00:00 +01 %::::z', tz.strftime('%z %:z %::z %:::z %::::z', i))
1285
+ end
1286
+
1287
+ def test_get_missing_data_source
1288
+ DataSource.set(DataSource.new)
1289
+
1290
+ assert_raises(InvalidDataSource) do
1291
+ Timezone.get('Europe/London')
1292
+ end
1293
+ end
1294
+
1295
+ def test_new_missing_data_source
1296
+ DataSource.set(DataSource.new)
1297
+
1298
+ assert_raises(InvalidDataSource) do
1299
+ Timezone.new('Europe/London')
1300
+ end
1301
+ end
1302
+
1303
+ def test_all_missing_data_source
1304
+ DataSource.set(DataSource.new)
1305
+
1306
+ assert_raises(InvalidDataSource) do
1307
+ Timezone.all
1308
+ end
1309
+ end
1310
+
1311
+ def test_all_identifiers_missing_data_source
1312
+ DataSource.set(DataSource.new)
1313
+
1314
+ assert_raises(InvalidDataSource) do
1315
+ Timezone.all_identifiers
1316
+ end
1317
+ end
1318
+
1319
+ def test_all_data_zones_missing_data_source
1320
+ DataSource.set(DataSource.new)
1321
+
1322
+ assert_raises(InvalidDataSource) do
1323
+ Timezone.all_data_zones
1324
+ end
1325
+ end
1326
+
1327
+ def test_all_data_zone_identifiers_missing_data_source
1328
+ DataSource.set(DataSource.new)
1329
+
1330
+ assert_raises(InvalidDataSource) do
1331
+ Timezone.all_data_zone_identifiers
1332
+ end
1333
+ end
1334
+
1335
+ def test_all_linked_zones_missing_data_source
1336
+ DataSource.set(DataSource.new)
1337
+
1338
+ assert_raises(InvalidDataSource) do
1339
+ Timezone.all_linked_zones
1340
+ end
1341
+ end
1342
+
1343
+ def test_all_linked_zone_identifiers_missing_data_source
1344
+ DataSource.set(DataSource.new)
1345
+
1346
+ assert_raises(InvalidDataSource) do
1347
+ Timezone.all_linked_zone_identifiers
1348
+ end
1349
+ end
1350
+ end