/
tc_timezone.rb
1964 lines (1553 loc) · 79.7 KB
/
tc_timezone.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# encoding: UTF-8
# frozen_string_literal: true
require_relative 'test_utils'
include TZInfo
using TestUtils::TaintExt if TestUtils.const_defined?(:TaintExt)
class TCTimezone < Minitest::Test
class << self
private
def time_with_unspecified_offset_test(method)
define_method("test_#{method}_time_with_unspecified_offset") do
tz = Timezone.get('Europe/London')
time_types_test(:unspecified_offset) do |h|
t = h.time(2006, 7, 15, 22, 12, 2, 0, nil)
error = assert_raises(ArgumentError) { tz.public_send(method, t) }
assert_equal('time must have a specified utc_offset', error.message)
end
end
end
def nil_time_test(method)
define_method("test_#{method}_nil_time") do
tz = Timezone.get('Europe/London')
error = assert_raises(ArgumentError) { tz.public_send(method, nil) }
assert_equal('time must be specified', error.message)
end
end
end
class BlockCalled < StandardError
end
class BaseTestTimezone < Timezone
protected
def times_equal(t1, t2)
return false unless t1.class == t2.class
return false unless t1 == t2
if t1.respond_to?(:utc_offset)
return false unless t1.utc_offset == t2.utc_offset
elsif t1.respond_to?(:offset)
return false unless t1.offset == t2.offset
end
if t1.respond_to?(:utc?)
return false unless t1.utc? == t2.utc?
end
true
end
end
class TestTimezone < BaseTestTimezone
def initialize(identifier, period_for_result = nil, periods_for_local_result = nil, expected = nil)
super()
@identifier = identifier
@period_for_result = period_for_result
@periods_for_local_result = periods_for_local_result || []
@expected = expected
end
def identifier
@identifier
end
def period_for(time)
raise "Unexpected time #{time} in period_for (expecting #{@expected})" unless times_equal(@expected, time)
@period_for_result
end
def periods_for_local(local_time)
raise "Unexpected local_time #{local_time} in periods_for_local (expecting #{@expected})" unless times_equal(@expected, local_time)
@periods_for_local_result.clone
end
def transitions_up_to(utc_to, utc_from = nil)
raise 'transitions_up_to called'
end
end
class OffsetsUpToTestTimezone < BaseTestTimezone
def initialize(identifier, expected_to, expected_from, transitions_up_to_result)
super()
@identifier = identifier
@expected_to = expected_to
@expected_from = expected_from
@transitions_up_to_result = transitions_up_to_result
end
def identifier
@identifier
end
def period_for(time)
raise 'period_for called'
end
def periods_for_local(local_time)
raise 'periods_for_local called'
end
def transitions_up_to(to, from = nil)
raise ArgumentError, 'to must be specified' unless to
raise "Unexpected to #{to || 'nil'} in transitions_up_to (expecting #{@expected_to})" unless times_equal(@expected_to, to)
raise "Unexpected from #{from || 'nil'} in transitions_up_to (expecting #{@expected_from})" unless (!@expected_from && !from) || times_equal(@expected_from, from)
raise ArgumentError, 'to must have a specified utc_offset' unless to.utc_offset
if from
raise ArgumentError, 'from must have a specified utc_offset' unless from.utc_offset
end
raise ArgumentError, 'to must be greater than from' if from && to <= from
@transitions_up_to_result
end
end
class OffsetsUpToNoTransitionsTestTimezone < BaseTestTimezone
def initialize(identifier, expected_to, expected_from, period_for_result)
super()
@identifier = identifier
@expected_to = expected_to
@expected_from = expected_from
@period_for_result = period_for_result
end
def identifier
@identifier
end
def period_for(time)
raise "Unexpected time #{time} in period_for (should be #{@expected_from})" if @expected_from && !times_equal(@expected_from, time)
raise "Unexpected time #{time} in period_for (should be < #{@expected_to})" if !@expected_from && @expected_to <= time
@period_for_result
end
def periods_for_local(local_time)
raise 'periods_for_local called'
end
def transitions_up_to(to, from = nil)
raise "Unexpected to #{to || 'nil'} in transitions_up_to (expecting #{@expected_to})" unless times_equal(@expected_to, to)
raise "Unexpected from #{from || 'nil'} in transitions_up_to (expecting #{@expected_from})" unless (!@expected_from && !from) || times_equal(@expected_from, from)
if from && to <= from
raise ArgumentError, 'to must be greater than from'
end
[]
end
end
def setup
@orig_default_dst = Timezone.default_dst
@orig_data_source = DataSource.get
end
def teardown
Timezone.default_dst = @orig_default_dst
DataSource.set(@orig_data_source)
end
def test_default_dst_initial_value
assert_nil(Timezone.default_dst)
end
def test_set_default_dst
Timezone.default_dst = true
assert_equal(true, Timezone.default_dst)
Timezone.default_dst = false
assert_equal(false, Timezone.default_dst)
Timezone.default_dst = nil
assert_nil(Timezone.default_dst)
Timezone.default_dst = 0
assert_equal(true, Timezone.default_dst)
end
test_encodings('ISO-8859-1', 'UTF-8', 'UTF-16').each do |encoding|
define_method("test_get_valid_data_with_#{encoding.to_method}_encoded_identifier") do
tz = Timezone.get('Europe/London'.encode(encoding.name))
assert_kind_of(DataTimezone, tz)
assert_equal('Europe/London', tz.identifier)
end
define_method("test_get_valid_linked_with_#{encoding.to_method}_encoded_identifier") do
tz = Timezone.get('UTC'.encode(encoding.name))
# ZoneinfoDataSource doesn't return DataSources::LinkedTimezoneInfo for any timezone.
if DataSource.get.get_timezone_info('UTC').kind_of?(DataSources::LinkedTimezoneInfo)
assert_kind_of(LinkedTimezone, tz)
else
assert_kind_of(DataTimezone, tz)
end
assert_equal('UTC', tz.identifier)
end
define_method("test_get_valid_three_levels_with_#{encoding.to_method}_encoded_identifier") do
tz = Timezone.get('America/Argentina/Buenos_Aires'.encode(encoding.name))
assert_kind_of(DataTimezone, tz)
assert_equal('America/Argentina/Buenos_Aires', tz.identifier)
end
define_method("test_get_not_exist_with_#{encoding.to_method}_encoded_identifier") do
error = assert_raises(InvalidTimezoneIdentifier) { Timezone.get('Nowhere/Special'.encode(encoding.name)) }
assert_match(/\bNowhere\/Special/, error.message)
end
define_method("test_get_invalid_with_#{encoding.to_method}_encoded_identifier") do
error = assert_raises(InvalidTimezoneIdentifier) { Timezone.get('../Definitions/UTC'.encode(encoding.name)) }
assert_match(/\W\.\.\/Definitions\/UTC\b/, error.message)
end
define_method("test_get_case_with_#{encoding.to_method}_encoded_identifier") do
Timezone.get('Europe/Prague')
error = assert_raises(InvalidTimezoneIdentifier) { Timezone.get('Europe/prague'.encode(encoding.name)) }
assert_match(/\bEurope\/prague\b/, error.message)
end
end
def test_get_nil
error = assert_raises(InvalidTimezoneIdentifier) { Timezone.get(nil) }
assert_match(/\bnil\b/, error.message)
end
test_encodings('ISO-8859-1', 'UTF-8', 'UTF-16').each do |encoding|
define_method("test_get_proxy_valid_with_#{encoding.to_method}_encoded_identifier") do
identifier = 'Europe/London'.encode(encoding.name)
proxy = Timezone.get_proxy(identifier)
assert_kind_of(TimezoneProxy, proxy)
assert_same(identifier, proxy.identifier)
end
define_method("test_get_proxy_not_exist_with_#{encoding.to_method}_encoded_identifier") do
identifier = 'Not/There'.encode(encoding.name)
proxy = Timezone.get_proxy(identifier)
assert_kind_of(TimezoneProxy, proxy)
assert_same(identifier, proxy.identifier)
end
define_method("test_get_proxy_invalid_with_#{encoding.to_method}_encoded_identifier") do
identifier = '../Invalid/Identifier'.encode(encoding.name)
proxy = Timezone.get_proxy(identifier)
assert_kind_of(TimezoneProxy, proxy)
assert_same(identifier, proxy.identifier)
end
end
def test_get_tainted_loaded
Timezone.get('Europe/Andorra')
safe_test(unavailable: :skip) do
identifier = 'Europe/Andorra'.dup.taint
assert(identifier.tainted?)
tz = Timezone.get(identifier)
assert_equal('Europe/Andorra', tz.identifier)
assert(identifier.tainted?)
end
end
def test_get_tainted_and_frozen_loaded
Timezone.get('Europe/Andorra')
safe_test do
tz = Timezone.get('Europe/Andorra'.dup.taint.freeze)
assert_equal('Europe/Andorra', tz.identifier)
end
end
def test_get_tainted_not_previously_loaded
safe_test(unavailable: :skip) do
identifier = 'Europe/Andorra'.dup.taint
assert(identifier.tainted?)
tz = Timezone.get(identifier)
assert_equal('Europe/Andorra', tz.identifier)
assert(identifier.tainted?)
end
end
def test_get_tainted_and_frozen_not_previously_loaded
safe_test do
tz = Timezone.get('Europe/Amsterdam'.dup.taint.freeze)
assert_equal('Europe/Amsterdam', tz.identifier)
end
end
def test_new_no_args
tz = Timezone.new
assert_raises_unknown_timezone { tz.identifier }
assert_raises_unknown_timezone { tz.friendly_identifier }
assert_raises_unknown_timezone { tz.now }
assert_raises_unknown_timezone { tz.current_period_and_time }
assert_raises_unknown_timezone { tz.canonical_identifier }
assert_raises_unknown_timezone { tz.canonical_zone }
time_types_test do |h|
time = h.time(2006,1,1,1,0,0)
assert_raises_unknown_timezone { tz.utc_to_local(time) }
assert_raises_unknown_timezone { tz.to_local(time) }
assert_raises_unknown_timezone { tz.local_to_utc(time) }
assert_raises_unknown_timezone { tz.period_for(time) }
assert_raises_unknown_timezone { tz.period_for_utc(time) }
assert_raises_unknown_timezone { tz.periods_for_local(time) }
assert_raises_unknown_timezone { tz.period_for_local(time) }
assert_raises_unknown_timezone { tz.transitions_up_to(time) }
assert_raises_unknown_timezone { tz.offsets_up_to(time) }
end
end
def test_all
all = Timezone.all
expected = DataSource.get.timezone_identifiers.collect {|identifier| Timezone.get_proxy(identifier)}
assert_equal(expected, all)
end
def test_all_identifiers
all = Timezone.all_identifiers
assert_equal(DataSource.get.timezone_identifiers, all)
end
def test_all_data_zones
all_data = Timezone.all_data_zones
expected = DataSource.get.data_timezone_identifiers.collect {|identifier| Timezone.get_proxy(identifier)}
assert_equal(expected, all_data)
end
def test_all_data_zone_identifiers
all_data = Timezone.all_data_zone_identifiers
assert_equal(DataSource.get.data_timezone_identifiers, all_data)
end
def test_all_linked_zones
all_linked = Timezone.all_linked_zones
expected = DataSource.get.linked_timezone_identifiers.collect {|identifier| Timezone.get_proxy(identifier)}
assert_equal(expected, all_linked)
end
def test_all_linked_zone_identifiers
all_linked = Timezone.all_linked_zone_identifiers
assert_equal(DataSource.get.linked_timezone_identifiers, all_linked)
end
def test_all_country_zones
# Probably should relax this test - just need all the zones, don't care
# about order.
expected = Country.all.inject([]) do |result,country|
result += country.zones
end
expected.uniq!
all_country_zones = Timezone.all_country_zones
assert_equal(expected, all_country_zones)
all_country_zone_identifiers = Timezone.all_country_zone_identifiers
assert_equal(all_country_zone_identifiers.length, all_country_zones.length)
all_country_zones.each do |zone|
assert_kind_of(TimezoneProxy, zone)
assert(all_country_zone_identifiers.include?(zone.identifier))
end
end
def test_all_country_zone_identifiers
# Probably should relax this test - just need all the zones, don't care
# about order.
expected = Country.all.inject([]) do |result,country|
result += country.zone_identifiers
end
expected.uniq!
assert_equal(expected, Timezone.all_country_zone_identifiers)
end
def test_identifier
assert_raises_unknown_timezone { Timezone.new.identifier }
assert_equal('Europe/Paris', TestTimezone.new('Europe/Paris').identifier)
end
def test_name
assert_raises_unknown_timezone { Timezone.new.name }
assert_equal('Europe/Paris', TestTimezone.new('Europe/Paris').name)
end
def test_friendly_identifier
assert_equal('Paris', TestTimezone.new('Europe/Paris').friendly_identifier(true))
assert_equal('Europe - Paris', TestTimezone.new('Europe/Paris').friendly_identifier(false))
assert_equal('Europe - Paris', TestTimezone.new('Europe/Paris').friendly_identifier)
assert_equal('Knox, Indiana', TestTimezone.new('America/Indiana/Knox').friendly_identifier(true))
assert_equal('America - Knox, Indiana', TestTimezone.new('America/Indiana/Knox').friendly_identifier(false))
assert_equal('America - Knox, Indiana', TestTimezone.new('America/Indiana/Knox').friendly_identifier)
assert_equal('Dumont D\'Urville', TestTimezone.new('Antarctica/DumontDUrville').friendly_identifier(true))
assert_equal('Antarctica - Dumont D\'Urville', TestTimezone.new('Antarctica/DumontDUrville').friendly_identifier(false))
assert_equal('Antarctica - Dumont D\'Urville', TestTimezone.new('Antarctica/DumontDUrville').friendly_identifier)
assert_equal('McMurdo', TestTimezone.new('Antarctica/McMurdo').friendly_identifier(true))
assert_equal('Antarctica - McMurdo', TestTimezone.new('Antarctica/McMurdo').friendly_identifier(false))
assert_equal('Antarctica - McMurdo', TestTimezone.new('Antarctica/McMurdo').friendly_identifier)
assert_equal('GMT+1', TestTimezone.new('Etc/GMT+1').friendly_identifier(true))
assert_equal('Etc - GMT+1', TestTimezone.new('Etc/GMT+1').friendly_identifier(false))
assert_equal('Etc - GMT+1', TestTimezone.new('Etc/GMT+1').friendly_identifier)
assert_equal('UTC', TestTimezone.new('UTC').friendly_identifier(true))
assert_equal('UTC', TestTimezone.new('UTC').friendly_identifier(false))
assert_equal('UTC', TestTimezone.new('UTC').friendly_identifier)
assert_equal('', TestTimezone.new('').friendly_identifier(true))
assert_equal('', TestTimezone.new('').friendly_identifier(false))
assert_equal('', TestTimezone.new('').friendly_identifier)
end
test_encodings('ISO-8859-1', 'UTF-8', 'UTF-16').each do |encoding|
define_method("test_friendly_identifier_with_#{encoding.to_method}_encoded_identifier") do
tz = TestTimezone.new('Europe/Paris'.encode(encoding.name).freeze)
assert_equal('Paris', tz.friendly_identifier(true))
assert_equal('Europe - Paris', tz.friendly_identifier(false))
end
end
def test_friendly_identifier_non_binary_encoding
refute_equal(Encoding::ASCII_8BIT, TestTimezone.new('Europe/Paris').friendly_identifier(true).encoding)
refute_equal(Encoding::ASCII_8BIT, TestTimezone.new('Europe/Paris').friendly_identifier(false).encoding)
end
def test_to_s
assert_equal('Europe - Paris', TestTimezone.new('Europe/Paris').to_s)
assert_equal('America - Knox, Indiana', TestTimezone.new('America/Indiana/Knox').to_s)
assert_equal('Antarctica - Dumont D\'Urville', TestTimezone.new('Antarctica/DumontDUrville').to_s)
assert_equal('Antarctica - McMurdo', TestTimezone.new('Antarctica/McMurdo').to_s)
assert_equal('Etc - GMT+1', TestTimezone.new('Etc/GMT+1').to_s)
assert_equal('UTC', TestTimezone.new('UTC').to_s)
end
def test_period_for_local
o1 = TimezoneOffset.new(0, 0, 'GMT')
o2 = TimezoneOffset.new(0, 3600, 'BST')
period = TransitionsTimezonePeriod.new(
TimezoneTransition.new(o1, o2, Time.utc(2004,10,31,1,0,0).to_i),
TimezoneTransition.new(o2, o1, Time.utc(2005,03,27,1,0,0).to_i))
time_types_test(:unspecified_offset) do |h|
t = h.time(2005,2,18,16,24,23)
assert_equal(period, TestTimezone.new('Europe/London', nil, [period], Timestamp.for(t, :ignore)).period_for_local(t))
end
time_types_test(:utc) do |h|
t = h.time(2005,2,18,16,24,23,0,:utc)
assert_equal(period, TestTimezone.new('Europe/London', nil, [period], Timestamp.for(t, :ignore)).period_for_local(t))
end
time_types_test(:offset) do |h|
t = h.time(2005,2,18,16,24,23,0,18000)
assert_equal(period, TestTimezone.new('Europe/London', nil, [period], Timestamp.for(t, :ignore)).period_for_local(t))
end
time_types_test do |h|
t1 = h.time(2005,2,18,16,24,23,0,0)
t2 = h.time(2005,2,18,16,24,23,Rational(789,1000), 0)
assert_equal(period, TestTimezone.new('Europe/London', nil, [period], Timestamp.for(t1, :ignore)).period_for_local(t1))
assert_equal(period, TestTimezone.new('Europe/London', nil, [period], Timestamp.for(t2, :ignore)).period_for_local(t2))
end
end
def test_period_for_local_not_found
time_types_test do |h|
t = h.time(2004,4,4,2,30,0)
tz = TestTimezone.new('America/New_York', nil, [], Timestamp.for(t, :ignore))
assert_raises_period_not_found(t) { tz.period_for_local(t) }
end
end
def test_period_for_local_ambiguous
o1 = TimezoneOffset.new(-18000, 0, 'EST')
o2 = TimezoneOffset.new(-18000, 3600, 'EDT')
t1 = TimezoneTransition.new(o2, o1, Time.utc(2004,4,4,8,0,0).to_i)
t2 = TimezoneTransition.new(o1, o2, Time.utc(2004,10,31,6,0,0).to_i)
t3 = TimezoneTransition.new(o2, o1, Time.utc(2005,4,3,8,0,0).to_i)
p1 = TransitionsTimezonePeriod.new(t1, t2)
p2 = TransitionsTimezonePeriod.new(t2, t3)
time_types_test do |h|
t = h.time(2004,10,31,1,30,0)
tz = TestTimezone.new('America/New_York', nil, [p1, p2], Timestamp.for(t, :ignore))
assert_raises_ambiguous_time(t) { tz.period_for_local(t) }
end
end
def test_period_for_local_default_dst_set_true
Timezone.default_dst = true
o1 = TimezoneOffset.new(-18000, 0, 'EST')
o2 = TimezoneOffset.new(-18000, 3600, 'EDT')
t1 = TimezoneTransition.new(o2, o1, Time.utc(2004,4,4,8,0,0).to_i)
t2 = TimezoneTransition.new(o1, o2, Time.utc(2004,10,31,6,0,0).to_i)
t3 = TimezoneTransition.new(o2, o1, Time.utc(2005,4,3,8,0,0).to_i)
p1 = TransitionsTimezonePeriod.new(t1, t2)
p2 = TransitionsTimezonePeriod.new(t2, t3)
time_types_test do |h|
t = h.time(2004,10,31,1,30,0)
tz = TestTimezone.new('America/New_York', nil, [p1, p2], Timestamp.for(t, :ignore))
assert_equal(p1, tz.period_for_local(t))
assert_equal(p1, tz.period_for_local(t, true))
assert_equal(p2, tz.period_for_local(t, false))
assert_raises_ambiguous_time(t) { tz.period_for_local(t, nil) }
end
end
def test_period_for_local_default_dst_set_false
Timezone.default_dst = false
o1 = TimezoneOffset.new(-18000, 0, 'EST')
o2 = TimezoneOffset.new(-18000, 3600, 'EDT')
t1 = TimezoneTransition.new(o2, o1, Time.utc(2004,4,4,8,0,0).to_i)
t2 = TimezoneTransition.new(o1, o2, Time.utc(2004,10,31,6,0,0).to_i)
t3 = TimezoneTransition.new(o2, o1, Time.utc(2005,4,3,8,0,0).to_i)
p1 = TransitionsTimezonePeriod.new(t1, t2)
p2 = TransitionsTimezonePeriod.new(t2, t3)
time_types_test do |h|
t = h.time(2004,10,31,1,30,0)
tz = TestTimezone.new('America/New_York', nil, [p1, p2], Timestamp.for(t, :ignore))
assert_equal(p2, tz.period_for_local(t))
assert_equal(p1, tz.period_for_local(t, true))
assert_equal(p2, tz.period_for_local(t, false))
assert_raises_ambiguous_time(t) { tz.period_for_local(t, nil) }
end
end
def test_period_for_local_dst_flag_resolved
o1 = TimezoneOffset.new(-18000, 0, 'EST')
o2 = TimezoneOffset.new(-18000, 3600, 'EDT')
t1 = TimezoneTransition.new(o2, o1, Time.utc(2004,4,4,8,0,0).to_i)
t2 = TimezoneTransition.new(o1, o2, Time.utc(2004,10,31,6,0,0).to_i)
t3 = TimezoneTransition.new(o2, o1, Time.utc(2005,4,3,8,0,0).to_i)
p1 = TransitionsTimezonePeriod.new(t1, t2)
p2 = TransitionsTimezonePeriod.new(t2, t3)
t = Time.utc(2004,10,31,1,30,0)
tz = TestTimezone.new('America/New_York', nil, [p1, p2], Timestamp.for(t, :ignore))
assert_equal(p1, tz.period_for_local(t, true))
assert_equal(p2, tz.period_for_local(t, false))
assert_equal(p1, tz.period_for_local(t, true) {|periods| raise BlockCalled, 'should not be called' })
assert_equal(p2, tz.period_for_local(t, false) {|periods| raise BlockCalled, 'should not be called' })
end
def test_period_for_local_dst_block_called
o1 = TimezoneOffset.new(-18000, 0, 'EST')
o2 = TimezoneOffset.new(-18000, 3600, 'EDT')
t1 = TimezoneTransition.new(o2, o1, Time.utc(2004,4,4,8,0,0).to_i)
t2 = TimezoneTransition.new(o1, o2, Time.utc(2004,10,31,6,0,0).to_i)
t3 = TimezoneTransition.new(o2, o1, Time.utc(2005,4,3,8,0,0).to_i)
p1 = TransitionsTimezonePeriod.new(t1, t2)
p2 = TransitionsTimezonePeriod.new(t2, t3)
t = Time.utc(2004,10,31,1,30,0)
tz = TestTimezone.new('America/New_York', nil, [p1, p2], Timestamp.for(t, :ignore))
assert_raises(BlockCalled) do
tz.period_for_local(t) do |periods|
assert_equal([p1, p2], periods)
# raise exception to test that the block was called
raise BlockCalled, 'should be raised'
end
end
assert_equal(p1, tz.period_for_local(t) {|periods| periods.first})
assert_equal(p2, tz.period_for_local(t) {|periods| periods.last})
assert_equal(p1, tz.period_for_local(t) {|periods| [periods.first]})
assert_equal(p2, tz.period_for_local(t) {|periods| [periods.last]})
end
def test_period_for_local_dst_cannot_resolve
# At midnight local time on Aug 5 1915 in Warsaw, the clocks were put back
# 24 minutes and both periods were non-DST. Hence the block should be
# called regardless of the value of the Boolean dst parameter.
o0 = TimezoneOffset.new(5040, 0, 'LMT')
o1 = TimezoneOffset.new(5040, 0, 'WMT')
o2 = TimezoneOffset.new(3600, 0, 'CET')
o3 = TimezoneOffset.new(3600, 3600, 'CEST')
t1 = TimezoneTransition.new(o1, o0, Time.utc(1879, 12, 31, 22, 36, 0).to_i)
t2 = TimezoneTransition.new(o2, o1, Time.utc(1915, 8, 4, 22, 36, 0).to_i)
t3 = TimezoneTransition.new(o3, o2, Time.utc(1916, 4, 30, 22, 0, 0).to_i)
p1 = TransitionsTimezonePeriod.new(t1, t2)
p2 = TransitionsTimezonePeriod.new(t2, t3)
t = Time.utc(1915,8,4,23,40,0)
tz = TestTimezone.new('Europe/Warsaw', nil, [p1, p2], Timestamp.for(t, :ignore))
assert_raises(BlockCalled) do
tz.period_for_local(t, true) do |periods|
assert_equal([p1, p2], periods)
raise BlockCalled, 'should be raised'
end
end
assert_raises(BlockCalled) do
tz.period_for_local(t, false) do |periods|
assert_equal([p1, p2], periods)
raise BlockCalled, 'should be raised'
end
end
end
def test_period_for_local_block_ambiguous
o1 = TimezoneOffset.new(-18000, 0, 'EST')
o2 = TimezoneOffset.new(-18000, 3600, 'EDT')
t1 = TimezoneTransition.new(o2, o1, Time.utc(2004,4,4,8,0,0).to_i)
t2 = TimezoneTransition.new(o1, o2, Time.utc(2004,10,31,6,0,0).to_i)
t3 = TimezoneTransition.new(o2, o1, Time.utc(2005,4,3,8,0,0).to_i)
p1 = TransitionsTimezonePeriod.new(t1, t2)
p2 = TransitionsTimezonePeriod.new(t2, t3)
t = Time.utc(2004,10,31,1,30,0)
tz = TestTimezone.new('America/New_York', nil, [p1, p2], Timestamp.for(t, :ignore))
assert_raises_ambiguous_time(t) do
tz.period_for_local(t) {|periods| nil }
end
assert_raises_ambiguous_time(t) do
tz.period_for_local(t) {|periods| periods }
end
assert_raises_ambiguous_time(t) do
tz.period_for_local(t) {|periods| [] }
end
error = assert_raises(AmbiguousTime) do
tz.period_for_local(t) {|periods| raise AmbiguousTime, 'Custom ambiguous time message' }
end
assert_equal('Custom ambiguous time message', error.message)
end
def test_period_for_local_nil
tz = Timezone.get('Europe/London')
error = assert_raises(ArgumentError) { tz.period_for_local(nil) }
assert_match(/\blocal_time\b/, error.message)
end
def test_period_for_local_unsupported
tz = Timezone.get('Europe/London')
t = Time.utc(2004,10,31,1,30,0).to_i
error = assert_raises(ArgumentError) { tz.period_for_local(t) }
assert_match(Regexp.new("\\b#{Regexp.escape(t.class.name)}\\b"), error.message)
end
def test_period_for_utc
o1 = TimezoneOffset.new(0, 0, 'GMT')
o2 = TimezoneOffset.new(0, 3600, 'BST')
period = TransitionsTimezonePeriod.new(
TimezoneTransition.new(o1, o2, Time.utc(2004,10,31,1,0,0).to_i),
TimezoneTransition.new(o2, o1, Time.utc(2005,3,27,2,0,0).to_i))
tz = TestTimezone.new('Europe/London', period, nil, Timestamp.create(2005,2,18,16,24,23,Rational(1,10),:utc))
time_types_test(:unspecified_offset) do |h|
assert_equal(period, tz.period_for_utc(h.time(2005,2,18,16,24,23,Rational(1,10))))
end
time_types_test(:utc) do |h|
assert_equal(period, tz.period_for_utc(h.time(2005,2,18,16,24,23,Rational(1,10),:utc)))
end
time_types_test(:offset) do |h|
assert_equal(period, tz.period_for_utc(h.time(2005,2,18,16,24,23,Rational(1,10),3600)))
end
time_types_test do |h|
assert_equal(period, tz.period_for_utc(h.time(2005,2,18,16,24,23,Rational(1,10),0)))
end
end
def test_period_for_utc_nil
tz = Timezone.get('Europe/London')
error = assert_raises(ArgumentError) { tz.period_for_utc(nil) }
assert_match(/\butc_time\b/, error.message)
end
def test_period_for_utc_unsupported
tz = Timezone.get('Europe/London')
t = Time.utc(2004,10,31,1,30,0).to_i
error = assert_raises(ArgumentError) { tz.period_for_utc(t) }
assert_match(Regexp.new("\\b#{Regexp.escape(t.class.name)}\\b"), error.message)
end
def test_utc_to_local
o1 = TimezoneOffset.new(0, 0, 'GMT')
o2 = TimezoneOffset.new(0, 3600, 'BST')
period = TransitionsTimezonePeriod.new(
TimezoneTransition.new(o2, o1, Time.utc(2005,3,27,2,0,0).to_i),
TimezoneTransition.new(o1, o2, Time.utc(2005,10,30,1,0,0).to_i))
time_types_test(:unspecified_offset) do |h|
assert_equal_with_offset_and_timezone_offset(h.time_with_offset(o2,2005,6,18,17,24,23,0),
TestTimezone.new('Europe/London', period, [], Timestamp.create(2005,6,18,16,24,23,0,:utc)).utc_to_local(h.time(2005,6,18,16,24,23,0)))
end
time_types_test(:utc) do |h|
assert_equal_with_offset_and_timezone_offset(h.time_with_offset(o2,2005,6,18,17,24,23,0),
TestTimezone.new('Europe/London', period, [], Timestamp.create(2005,6,18,16,24,23,0,:utc)).utc_to_local(h.time(2005,6,18,16,24,23,0,:utc)))
end
time_types_test(:offset) do |h|
assert_equal_with_offset_and_timezone_offset(h.time_with_offset(o2,2005,6,18,17,24,23,0),
TestTimezone.new('Europe/London', period, [], Timestamp.create(2005,6,18,16,24,23,0,:utc)).utc_to_local(h.time(2005,6,18,16,24,23,0,10800)))
end
time_types_test do |h|
assert_equal_with_offset_and_timezone_offset(h.time_with_offset(o2,2005,6,18,17,24,23,0),
TestTimezone.new('Europe/London', period, [], Timestamp.create(2005,6,18,16,24,23,0,:utc)).utc_to_local(h.time(2005,6,18,16,24,23,0,0)))
assert_equal_with_offset_and_timezone_offset(h.time_with_offset(o2,2005,6,18,17,24,23,Rational(567,1000)),
TestTimezone.new('Europe/London', period, [], Timestamp.create(2005,6,18,16,24,23,Rational(567,1000),:utc)).utc_to_local(h.time(2005,6,18,16,24,23,Rational(567,1000),0)))
end
end
def test_utc_to_local_zero_offset_not_utc
o1 = TimezoneOffset.new(0, 0, 'GMT')
o2 = TimezoneOffset.new(0, 3600, 'BST')
period = TransitionsTimezonePeriod.new(
TimezoneTransition.new(o1, o2, Time.utc(2016,10,30,1,0,0).to_i),
TimezoneTransition.new(o2, o1, Time.utc(2017, 3,26,1,0,0).to_i))
time_types_test(:utc) do |h|
# test that the result doesn't have utc? set to true
assert_equal_with_offset_and_timezone_offset(h.time_with_offset(o1,2016,12,31,0,0,0,0),
TestTimezone.new('Europe/London', period, [], Timestamp.create(2016,12,31,0,0,0,0,:utc)).utc_to_local(h.time(2016,12,31,0,0,0,0,:utc)))
end
end
def test_utc_to_local_local_time_input
o1 = TimezoneOffset.new(0, 0, 'GMT')
o2 = TimezoneOffset.new(0, 3600, 'BST')
period = TransitionsTimezonePeriod.new(
TimezoneTransition.new(o2, o1, Time.utc(2005,3,27,2,0,0).to_i),
TimezoneTransition.new(o1, o2, Time.utc(2005,10,30,1,0,0).to_i))
time_types_test do |h|
assert_equal_with_offset_and_timezone_offset(h.time_with_offset(o2,2005,6,18,17,24,23,0),
TestTimezone.new('Europe/London', period, [], Timestamp.create(2005,6,18,16,24,23,0,:utc)).utc_to_local(
h.time_with_offset(TimezoneOffset.new(10800, 0, 'TEST'),2005,6,18,16,24,23,0)))
end
end
def test_utc_to_local_nil
tz = Timezone.get('Europe/London')
error = assert_raises(ArgumentError) { tz.utc_to_local(nil) }
assert_match(/\butc_time\b/, error.message)
end
def test_utc_to_local_unsupported
tz = Timezone.get('Europe/London')
t = Time.utc(2004,10,31,1,30,0).to_i
error = assert_raises(ArgumentError) { tz.utc_to_local(t) }
assert_match(Regexp.new("\\b#{Regexp.escape(t.class.name)}\\b"), error.message)
end
def test_to_local
o1 = TimezoneOffset.new(0, 0, 'GMT')
o2 = TimezoneOffset.new(0, 3600, 'BST')
period = TransitionsTimezonePeriod.new(
TimezoneTransition.new(o2, o1, Time.utc(2005,3,27,2,0,0).to_i),
TimezoneTransition.new(o1, o2, Time.utc(2005,10,30,1,0,0).to_i))
time_types_test(:utc) do |h|
assert_equal_with_offset_and_timezone_offset(h.time_with_offset(o2,2005,6,18,17,24,23,0),
TestTimezone.new('Europe/London', period, [], Timestamp.create(2005,6,18,16,24,23,0,:utc)).to_local(h.time(2005,6,18,16,24,23,0,:utc)))
end
time_types_test(:offset) do |h|
assert_equal_with_offset_and_timezone_offset(h.time_with_offset(o2,2005,6,18,14,24,23,0),
TestTimezone.new('Europe/London', period, [], Timestamp.create(2005,6,18,16,24,23,0,10800)).to_local(h.time(2005,6,18,16,24,23,0,10800)))
end
time_types_test do |h|
assert_equal_with_offset_and_timezone_offset(h.time_with_offset(o2,2005,6,18,17,24,23,0),
TestTimezone.new('Europe/London', period, [], Timestamp.create(2005,6,18,16,24,23,0,h.expected_zero_offset)).to_local(h.time(2005,6,18,16,24,23,0,0)))
assert_equal_with_offset_and_timezone_offset(h.time_with_offset(o2,2005,6,18,17,24,23,Rational(567,1000)),
TestTimezone.new('Europe/London', period, [], Timestamp.create(2005,6,18,16,24,23,Rational(567,1000),h.expected_zero_offset)).to_local(h.time(2005,6,18,16,24,23,Rational(567,1000),0)))
end
end
def test_to_local_zero_offset_not_utc
o1 = TimezoneOffset.new(0, 0, 'GMT')
o2 = TimezoneOffset.new(0, 3600, 'BST')
period = TransitionsTimezonePeriod.new(
TimezoneTransition.new(o1, o2, Time.utc(2016,10,30,1,0,0).to_i),
TimezoneTransition.new(o2, o1, Time.utc(2017, 3,26,1,0,0).to_i))
time_types_test(:utc) do |h|
# test that the result doesn't have utc? set to true
assert_equal_with_offset_and_timezone_offset(h.time_with_offset(o1,2016,12,31,0,0,0,0),
TestTimezone.new('Europe/London', period, [], Timestamp.create(2016,12,31,0,0,0,0,:utc)).to_local(h.time(2016,12,31,0,0,0,0,:utc)))
end
end
def test_to_local_local_time_input
o1 = TimezoneOffset.new(0, 0, 'GMT')
o2 = TimezoneOffset.new(0, 3600, 'BST')
period = TransitionsTimezonePeriod.new(
TimezoneTransition.new(o2, o1, Time.utc(2005,3,27,2,0,0).to_i),
TimezoneTransition.new(o1, o2, Time.utc(2005,10,30,1,0,0).to_i))
time_types_test do |h|
assert_equal_with_offset_and_timezone_offset(h.time_with_offset(o2,2005,6,18,14,24,23,0),
TestTimezone.new('Europe/London', period, [], Timestamp.create(2005,6,18,16,24,23,0,10800)).to_local(
h.time_with_offset(TimezoneOffset.new(10800, 0, 'TEST'),2005,6,18,16,24,23,0)))
end
end
time_with_unspecified_offset_test(:to_local)
nil_time_test(:to_local)
def test_to_local_unsupported
tz = Timezone.get('Europe/London')
t = Time.utc(2004,10,31,1,30,0).to_i
error = assert_raises(ArgumentError) { tz.to_local(t) }
assert_match(Regexp.new("\\b#{Regexp.escape(t.class.name)}\\b"), error.message)
end
def test_local_to_utc
o1 = TimezoneOffset.new(0, 0, 'GMT')
o2 = TimezoneOffset.new(0, 3600, 'BST')
period = TransitionsTimezonePeriod.new(
TimezoneTransition.new(o2, o1, Time.utc(2005,3,27,2,0,0).to_i),
TimezoneTransition.new(o1, o2, Time.utc(2005,10,30,1,0,0).to_i))
time_types_test(:unspecified_offset) do |h|
assert_equal_with_offset_and_class(h.output_time(2005,6,18,15,24,23,0,:utc),
TestTimezone.new('Europe/London', nil, [period], Timestamp.create(2005,6,18,16,24,23,0)).local_to_utc(h.time(2005,6,18,16,24,23,0)))
end
time_types_test(:utc) do |h|
assert_equal_with_offset_and_class(h.output_time(2005,6,18,15,24,23,0,:utc),
TestTimezone.new('Europe/London', nil, [period], Timestamp.create(2005,6,18,16,24,23,0)).local_to_utc(h.time(2005,6,18,16,24,23,0,:utc)))
end
time_types_test(:offset) do |h|
assert_equal_with_offset_and_class(h.output_time(2005,6,18,15,24,23,0,:utc),
TestTimezone.new('Europe/London', nil, [period], Timestamp.create(2005,6,18,16,24,23,0)).local_to_utc(h.time(2005,6,18,16,24,23,0,10800)))
end
time_types_test do |h|
assert_equal_with_offset_and_class(h.output_time(2005,6,18,15,24,23,0,:utc),
TestTimezone.new('Europe/London', nil, [period], Timestamp.create(2005,6,18,16,24,23,0)).local_to_utc(h.time(2005,6,18,16,24,23,0,0)))
assert_equal_with_offset_and_class(h.output_time(2005,6,18,15,24,23,Rational(567,1000),:utc),
TestTimezone.new('Europe/London', nil, [period], Timestamp.create(2005,6,18,16,24,23,Rational(567,1000))).local_to_utc(h.time(2005,6,18,16,24,23,Rational(567,1000))))
end
end
def test_local_to_utc_local_time_input
o1 = TimezoneOffset.new(0, 0, 'GMT')
o2 = TimezoneOffset.new(0, 3600, 'BST')
period = TransitionsTimezonePeriod.new(
TimezoneTransition.new(o2, o1, Time.utc(2005,3,27,2,0,0).to_i),
TimezoneTransition.new(o1, o2, Time.utc(2005,10,30,1,0,0).to_i))
time_types_test do |h|
assert_equal_with_offset_and_class(h.output_time(2005,6,18,15,24,23,0,:utc),
TestTimezone.new('Europe/London', nil, [period], Timestamp.create(2005,6,18,16,24,23,0)).local_to_utc(
h.time_with_offset(TimezoneOffset.new(10800,0,'TEST'),2005,6,18,16,24,23,0)))
end
end
def test_local_to_utc_local_timestamp_without_offset_input
o1 = TimezoneOffset.new(0, 0, 'GMT')
o2 = TimezoneOffset.new(0, 3600, 'BST')
period = TransitionsTimezonePeriod.new(
TimezoneTransition.new(o2, o1, Time.utc(2005,3,27,2,0,0).to_i),
TimezoneTransition.new(o1, o2, Time.utc(2005,10,30,1,0,0).to_i))
assert_equal_with_offset_and_class(Timestamp.create(2005,6,18,15,24,23,0,:utc),
TestTimezone.new('Europe/London', nil, [period], Timestamp.create(2005,6,18,16,24,23,0)).local_to_utc(
TimestampWithOffset.create(2005,6,18,16,24,23)))
end
def test_local_to_utc_ambiguous
o1 = TimezoneOffset.new(-18000, 0, 'EST')
o2 = TimezoneOffset.new(-18000, 3600, 'EDT')
tt1 = TimezoneTransition.new(o2, o1, Time.utc(2004,4,4,8,0,0).to_i)
tt2 = TimezoneTransition.new(o1, o2, Time.utc(2004,10,31,6,0,0).to_i)
tt3 = TimezoneTransition.new(o2, o1, Time.utc(2005,4,3,8,0,0).to_i)
p1 = TransitionsTimezonePeriod.new(tt1, tt2)
p2 = TransitionsTimezonePeriod.new(tt2, tt3)
time_types_test do |h|
t1 = h.time(2004,10,31,1,30,0)
t2 = h.time(2004,10,31,1,30,0,Rational(501,1000000))
tz1 = TestTimezone.new('America/New_York', nil, [p1, p2], Timestamp.create(2004,10,31,1,30,0))
tz2 = TestTimezone.new('America/New_York', nil, [p1, p2], Timestamp.create(2004,10,31,1,30,0,Rational(501,1000000)))
assert_raises_ambiguous_time(t1) { tz1.local_to_utc(t1) }
assert_raises_ambiguous_time(t2) { tz2.local_to_utc(t2) }
end
end
def test_local_to_utc_not_found
time_types_test do |h|
t = h.time(2004,4,4,2,0,0)
tz = TestTimezone.new('America/New_York', nil, [], Timestamp.create(2004,4,4,2,0,0))
assert_raises_period_not_found(t) { tz.local_to_utc(t) }
end
end
def test_local_to_utc_default_dst_set_true
Timezone.default_dst = true
o1 = TimezoneOffset.new(-18000, 0, 'EST')
o2 = TimezoneOffset.new(-18000, 3600, 'EDT')
t1 = TimezoneTransition.new(o2, o1, Time.utc(2004,4,4,8,0,0).to_i)
t2 = TimezoneTransition.new(o1, o2, Time.utc(2004,10,31,6,0,0).to_i)
t3 = TimezoneTransition.new(o2, o1, Time.utc(2005,4,3,8,0,0).to_i)
p1 = TransitionsTimezonePeriod.new(t1, t2)
p2 = TransitionsTimezonePeriod.new(t2, t3)
time_types_test do |h|
t = h.time(2004,10,31,1,30,0)
tz = TestTimezone.new('America/New_York', nil, [p1, p2], Timestamp.create(2004,10,31,1,30,0))
assert_equal_with_offset_and_class(h.output_time(2004,10,31,5,30,0,0,:utc), tz.local_to_utc(t))
assert_equal_with_offset_and_class(h.output_time(2004,10,31,5,30,0,0,:utc), tz.local_to_utc(t, true))
assert_equal_with_offset_and_class(h.output_time(2004,10,31,6,30,0,0,:utc), tz.local_to_utc(t, false))
assert_raises_ambiguous_time(t) { tz.local_to_utc(t, nil) }
assert_equal_with_offset_and_class(h.output_time(2004,10,31,5,30,0,0,:utc), tz.local_to_utc(t) {|periods| raise BlockCalled, 'should not be called' })
end
end
def test_local_to_utc_default_dst_set_false
Timezone.default_dst = false
o1 = TimezoneOffset.new(-18000, 0, 'EST')
o2 = TimezoneOffset.new(-18000, 3600, 'EDT')
t1 = TimezoneTransition.new(o2, o1, Time.utc(2004,4,4,8,0,0).to_i)
t2 = TimezoneTransition.new(o1, o2, Time.utc(2004,10,31,6,0,0).to_i)
t3 = TimezoneTransition.new(o2, o1, Time.utc(2005,4,3,8,0,0).to_i)
p1 = TransitionsTimezonePeriod.new(t1, t2)
p2 = TransitionsTimezonePeriod.new(t2, t3)
time_types_test do |h|
t = h.time(2004,10,31,1,30,0)
tz = TestTimezone.new('America/New_York', nil, [p1, p2], Timestamp.create(2004,10,31,1,30,0))
assert_equal_with_offset_and_class(h.output_time(2004,10,31,6,30,0,0,:utc), tz.local_to_utc(t))
assert_equal_with_offset_and_class(h.output_time(2004,10,31,6,30,0,0,:utc), tz.local_to_utc(t, false))
assert_equal_with_offset_and_class(h.output_time(2004,10,31,5,30,0,0,:utc), tz.local_to_utc(t, true))