-
-
Notifications
You must be signed in to change notification settings - Fork 875
Expand file tree
/
Copy pathtzrulets.cpp
More file actions
2682 lines (2459 loc) · 105 KB
/
tzrulets.cpp
File metadata and controls
2682 lines (2459 loc) · 105 KB
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
// © 2016 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html
/*
*******************************************************************************
* Copyright (C) 2007-2016, International Business Machines Corporation and
* others. All Rights Reserved.
*******************************************************************************
*/
#include "unicode/utypes.h"
#if !UCONFIG_NO_FORMATTING
#include "unicode/dtrule.h"
#include "unicode/tzrule.h"
#include "unicode/rbtz.h"
#include "unicode/simpletz.h"
#include "unicode/tzrule.h"
#include "unicode/calendar.h"
#include "unicode/gregocal.h"
#include "unicode/strenum.h"
#include "unicode/ucal.h"
#include "unicode/unistr.h"
#include "unicode/ustring.h"
#include "unicode/tztrans.h"
#include "unicode/vtzone.h"
#include "tzrulets.h"
#include "zrule.h"
#include "ztrans.h"
#include "vzone.h"
#include "cmemory.h"
#define CASE(id,test) case id: name = #test; if (exec) { logln(#test "---"); logln((UnicodeString)""); test(); } break
#define HOUR (60*60*1000)
static const char *const TESTZIDS[] = {
"AGT",
"America/New_York",
"America/Los_Angeles",
"America/Indiana/Indianapolis",
"America/Havana",
"Europe/Lisbon",
"Europe/Paris",
"Asia/Tokyo",
"Asia/Sakhalin",
"Africa/Cairo",
"Africa/Windhoek",
"Australia/Sydney",
"Etc/GMT+8"
};
static UBool hasEquivalentTransitions(/*const*/ BasicTimeZone& tz1, /*const*/BasicTimeZone& tz2,
UDate start, UDate end,
UBool ignoreDstAmount, int32_t maxTransitionTimeDelta,
UErrorCode& status);
class TestZIDEnumeration : public StringEnumeration {
public:
TestZIDEnumeration(UBool all = false);
~TestZIDEnumeration();
virtual int32_t count(UErrorCode& /*status*/) const override {
return len;
}
virtual const UnicodeString *snext(UErrorCode& status) override;
virtual void reset(UErrorCode& status) override;
static inline UClassID getStaticClassID() {
return (UClassID)&fgClassID;
}
virtual UClassID getDynamicClassID() const override {
return getStaticClassID();
}
private:
static const char fgClassID;
int32_t idx;
int32_t len;
StringEnumeration *tzenum;
};
const char TestZIDEnumeration::fgClassID = 0;
TestZIDEnumeration::TestZIDEnumeration(UBool all)
: idx(0) {
UErrorCode status = U_ZERO_ERROR;
if (all) {
tzenum = TimeZone::createEnumeration(status);
len = tzenum->count(status);
} else {
tzenum = nullptr;
len = UPRV_LENGTHOF(TESTZIDS);
}
}
TestZIDEnumeration::~TestZIDEnumeration() {
delete tzenum;
}
const UnicodeString*
TestZIDEnumeration::snext(UErrorCode& status) {
if (tzenum != nullptr) {
return tzenum->snext(status);
} else if (U_SUCCESS(status) && idx < len) {
unistr = UnicodeString(TESTZIDS[idx++], "");
return &unistr;
}
return nullptr;
}
void
TestZIDEnumeration::reset(UErrorCode& status) {
if (tzenum != nullptr) {
tzenum->reset(status);
} else {
idx = 0;
}
}
void TimeZoneRuleTest::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par*/ )
{
if (exec) {
logln("TestSuite TestTimeZoneRule");
}
switch (index) {
CASE(0, TestSimpleRuleBasedTimeZone);
CASE(1, TestHistoricalRuleBasedTimeZone);
CASE(2, TestOlsonTransition);
CASE(3, TestRBTZTransition);
CASE(4, TestHasEquivalentTransitions);
CASE(5, TestVTimeZoneRoundTrip);
CASE(6, TestVTimeZoneRoundTripPartial);
CASE(7, TestVTimeZoneSimpleWrite);
CASE(8, TestVTimeZoneHeaderProps);
CASE(9, TestGetSimpleRules);
CASE(10, TestTimeZoneRuleCoverage);
CASE(11, TestSimpleTimeZoneCoverage);
CASE(12, TestVTimeZoneCoverage);
CASE(13, TestVTimeZoneParse);
CASE(14, TestT6216);
CASE(15, TestT6669);
CASE(16, TestVTimeZoneWrapper);
CASE(17, TestT8943);
CASE(18, TestVTimeZoneRruleUntil);
default: name = ""; break;
}
}
/*
* Compare SimpleTimeZone with equivalent RBTZ
*/
void
TimeZoneRuleTest::TestSimpleRuleBasedTimeZone() {
UErrorCode status = U_ZERO_ERROR;
SimpleTimeZone stz(-1*HOUR, "TestSTZ",
UCAL_SEPTEMBER, -30, -UCAL_SATURDAY, 1*HOUR, SimpleTimeZone::WALL_TIME,
UCAL_FEBRUARY, 2, UCAL_SUNDAY, 1*HOUR, SimpleTimeZone::WALL_TIME,
1*HOUR, status);
if (U_FAILURE(status)) {
errln("FAIL: Couldn't create SimpleTimezone.");
}
DateTimeRule *dtr;
AnnualTimeZoneRule *atzr;
int32_t STARTYEAR = 2000;
InitialTimeZoneRule *ir = new InitialTimeZoneRule(
"RBTZ_Initial", // Initial time Name
-1*HOUR, // Raw offset
1*HOUR); // DST saving amount
// Original rules
RuleBasedTimeZone *rbtz1 = new RuleBasedTimeZone("RBTZ1", ir->clone());
dtr = new DateTimeRule(UCAL_SEPTEMBER, 30, UCAL_SATURDAY, false,
1*HOUR, DateTimeRule::WALL_TIME); // SUN<=30 in September, at 1AM wall time
atzr = new AnnualTimeZoneRule("RBTZ_DST1",
-1*HOUR /*rawOffset*/, 1*HOUR /*dstSavings*/, dtr,
STARTYEAR, AnnualTimeZoneRule::MAX_YEAR);
rbtz1->addTransitionRule(atzr, status);
if (U_FAILURE(status)) {
errln("FAIL: couldn't add AnnualTimeZoneRule 1-1.");
}
dtr = new DateTimeRule(UCAL_FEBRUARY, 2, UCAL_SUNDAY,
1*HOUR, DateTimeRule::WALL_TIME); // 2nd Sunday in February, at 1AM wall time
atzr = new AnnualTimeZoneRule("RBTZ_STD1",
-1*HOUR /*rawOffset*/, 0 /*dstSavings*/, dtr,
STARTYEAR, AnnualTimeZoneRule::MAX_YEAR);
rbtz1->addTransitionRule(atzr, status);
if (U_FAILURE(status)) {
errln("FAIL: couldn't add AnnualTimeZoneRule 1-2.");
}
rbtz1->complete(status);
if (U_FAILURE(status)) {
errln("FAIL: couldn't complete RBTZ 1.");
}
// Equivalent, but different date rule type
RuleBasedTimeZone *rbtz2 = new RuleBasedTimeZone("RBTZ2", ir->clone());
dtr = new DateTimeRule(UCAL_SEPTEMBER, -1, UCAL_SATURDAY,
1*HOUR, DateTimeRule::WALL_TIME); // Last Sunday in September at 1AM wall time
atzr = new AnnualTimeZoneRule("RBTZ_DST2", -1*HOUR, 1*HOUR, dtr, STARTYEAR, AnnualTimeZoneRule::MAX_YEAR);
rbtz2->addTransitionRule(atzr, status);
if (U_FAILURE(status)) {
errln("FAIL: couldn't add AnnualTimeZoneRule 2-1.");
}
dtr = new DateTimeRule(UCAL_FEBRUARY, 8, UCAL_SUNDAY, true,
1*HOUR, DateTimeRule::WALL_TIME); // SUN>=8 in February, at 1AM wall time
atzr = new AnnualTimeZoneRule("RBTZ_STD2", -1*HOUR, 0, dtr, STARTYEAR, AnnualTimeZoneRule::MAX_YEAR);
rbtz2->addTransitionRule(atzr, status);
if (U_FAILURE(status)) {
errln("FAIL: couldn't add AnnualTimeZoneRule 2-2.");
}
rbtz2->complete(status);
if (U_FAILURE(status)) {
errln("FAIL: couldn't complete RBTZ 2");
}
// Equivalent, but different time rule type
RuleBasedTimeZone *rbtz3 = new RuleBasedTimeZone("RBTZ3", ir->clone());
dtr = new DateTimeRule(UCAL_SEPTEMBER, 30, UCAL_SATURDAY, false,
2*HOUR, DateTimeRule::UTC_TIME);
atzr = new AnnualTimeZoneRule("RBTZ_DST3", -1*HOUR, 1*HOUR, dtr, STARTYEAR, AnnualTimeZoneRule::MAX_YEAR);
rbtz3->addTransitionRule(atzr, status);
if (U_FAILURE(status)) {
errln("FAIL: couldn't add AnnualTimeZoneRule 3-1.");
}
dtr = new DateTimeRule(UCAL_FEBRUARY, 2, UCAL_SUNDAY,
0*HOUR, DateTimeRule::STANDARD_TIME);
atzr = new AnnualTimeZoneRule("RBTZ_STD3", -1*HOUR, 0, dtr, STARTYEAR, AnnualTimeZoneRule::MAX_YEAR);
rbtz3->addTransitionRule(atzr, status);
if (U_FAILURE(status)) {
errln("FAIL: couldn't add AnnualTimeZoneRule 3-2.");
}
rbtz3->complete(status);
if (U_FAILURE(status)) {
errln("FAIL: couldn't complete RBTZ 3");
}
// Check equivalency for 10 years
UDate start = getUTCMillis(STARTYEAR, UCAL_JANUARY, 1);
UDate until = getUTCMillis(STARTYEAR + 10, UCAL_JANUARY, 1);
if (!(stz.hasEquivalentTransitions(*rbtz1, start, until, true, status))) {
errln("FAIL: rbtz1 must be equivalent to the SimpleTimeZone in the time range.");
}
if (U_FAILURE(status)) {
errln("FAIL: error returned from hasEquivalentTransitions");
}
if (!(stz.hasEquivalentTransitions(*rbtz2, start, until, true, status))) {
errln("FAIL: rbtz2 must be equivalent to the SimpleTimeZone in the time range.");
}
if (U_FAILURE(status)) {
errln("FAIL: error returned from hasEquivalentTransitions");
}
if (!(stz.hasEquivalentTransitions(*rbtz3, start, until, true, status))) {
errln("FAIL: rbtz3 must be equivalent to the SimpleTimeZone in the time range.");
}
if (U_FAILURE(status)) {
errln("FAIL: error returned from hasEquivalentTransitions");
}
// hasSameRules
if (rbtz1->hasSameRules(*rbtz2)) {
errln("FAIL: rbtz1 and rbtz2 have different rules, but returned true.");
}
if (rbtz1->hasSameRules(*rbtz3)) {
errln("FAIL: rbtz1 and rbtz3 have different rules, but returned true.");
}
RuleBasedTimeZone *rbtz1c = rbtz1->clone();
if (!rbtz1->hasSameRules(*rbtz1c)) {
errln("FAIL: Cloned RuleBasedTimeZone must have the same rules with the original.");
}
// getOffset
int32_t era, year, month, dayOfMonth, dayOfWeek, millisInDay;
UDate time;
int32_t offset, dstSavings;
UBool dst;
GregorianCalendar *cal = new GregorianCalendar(status);
if (U_FAILURE(status)) {
dataerrln("FAIL: Could not create a Gregorian calendar instance.: %s", u_errorName(status));
delete rbtz1;
delete rbtz2;
delete rbtz3;
delete rbtz1c;
return;
}
cal->setTimeZone(*rbtz1);
cal->clear();
// Jan 1, 1000 BC
cal->set(UCAL_ERA, GregorianCalendar::BC);
cal->set(1000, UCAL_JANUARY, 1);
era = cal->get(UCAL_ERA, status);
year = cal->get(UCAL_YEAR, status);
month = cal->get(UCAL_MONTH, status);
dayOfMonth = cal->get(UCAL_DAY_OF_MONTH, status);
dayOfWeek = cal->get(UCAL_DAY_OF_WEEK, status);
millisInDay = cal->get(UCAL_MILLISECONDS_IN_DAY, status);
time = cal->getTime(status);
if (U_FAILURE(status)) {
errln("FAIL: Could not get calendar field values.");
}
offset = rbtz1->getOffset(era, year, month, dayOfMonth, dayOfWeek, millisInDay, status);
if (U_FAILURE(status)) {
errln("FAIL: getOffset(7 args) failed.");
}
if (offset != 0) {
errln(UnicodeString("FAIL: Invalid time zone offset: ") + offset + " /expected: 0");
}
dst = rbtz1->inDaylightTime(time, status);
if (U_FAILURE(status)) {
errln("FAIL: inDaylightTime failed.");
}
if (!dst) {
errln("FAIL: Invalid daylight saving time");
}
rbtz1->getOffset(time, true, offset, dstSavings, status);
if (U_FAILURE(status)) {
errln("FAIL: getOffset(5 args) failed.");
}
if (offset != -3600000) {
errln(UnicodeString("FAIL: Invalid time zone raw offset: ") + offset + " /expected: -3600000");
}
if (dstSavings != 3600000) {
errln(UnicodeString("FAIL: Invalid DST amount: ") + dstSavings + " /expected: 3600000");
}
// July 1, 2000, AD
cal->set(UCAL_ERA, GregorianCalendar::AD);
cal->set(2000, UCAL_JULY, 1);
era = cal->get(UCAL_ERA, status);
year = cal->get(UCAL_YEAR, status);
month = cal->get(UCAL_MONTH, status);
dayOfMonth = cal->get(UCAL_DAY_OF_MONTH, status);
dayOfWeek = cal->get(UCAL_DAY_OF_WEEK, status);
millisInDay = cal->get(UCAL_MILLISECONDS_IN_DAY, status);
time = cal->getTime(status);
if (U_FAILURE(status)) {
errln("FAIL: Could not get calendar field values.");
}
offset = rbtz1->getOffset(era, year, month, dayOfMonth, dayOfWeek, millisInDay, status);
if (U_FAILURE(status)) {
errln("FAIL: getOffset(7 args) failed.");
}
if (offset != -3600000) {
errln(UnicodeString("FAIL: Invalid time zone offset: ") + offset + " /expected: -3600000");
}
dst = rbtz1->inDaylightTime(time, status);
if (U_FAILURE(status)) {
errln("FAIL: inDaylightTime failed.");
}
if (dst) {
errln("FAIL: Invalid daylight saving time");
}
rbtz1->getOffset(time, true, offset, dstSavings, status);
if (U_FAILURE(status)) {
errln("FAIL: getOffset(5 args) failed.");
}
if (offset != -3600000) {
errln(UnicodeString("FAIL: Invalid time zone raw offset: ") + offset + " /expected: -3600000");
}
if (dstSavings != 0) {
errln(UnicodeString("FAIL: Invalid DST amount: ") + dstSavings + " /expected: 0");
}
// getRawOffset
offset = rbtz1->getRawOffset();
if (offset != -1*HOUR) {
errln(UnicodeString("FAIL: Invalid time zone raw offset returned by getRawOffset: ")
+ offset + " /expected: -3600000");
}
// operator=/==/!=
RuleBasedTimeZone rbtz0("RBTZ1", ir->clone());
if (rbtz0 == *rbtz1 || !(rbtz0 != *rbtz1)) {
errln("FAIL: RuleBasedTimeZone rbtz0 is not equal to rbtz1, but got wrong result");
}
rbtz0 = *rbtz1;
if (rbtz0 != *rbtz1 || !(rbtz0 == *rbtz1)) {
errln("FAIL: RuleBasedTimeZone rbtz0 is equal to rbtz1, but got wrong result");
}
// setRawOffset
const int32_t RAW = -10*HOUR;
rbtz0.setRawOffset(RAW);
if (rbtz0.getRawOffset() != RAW) {
logln("setRawOffset is implemented in RuleBasedTimeZone");
}
// useDaylightTime
if (!rbtz1->useDaylightTime()) {
errln("FAIL: useDaylightTime returned false");
}
// Try to add 3rd final rule
dtr = new DateTimeRule(UCAL_OCTOBER, 15, 1*HOUR, DateTimeRule::WALL_TIME);
atzr = new AnnualTimeZoneRule("3RD_ATZ", -1*HOUR, 2*HOUR, dtr, STARTYEAR, AnnualTimeZoneRule::MAX_YEAR);
rbtz1->addTransitionRule(atzr, status);
if (U_SUCCESS(status)) {
errln("FAIL: 3rd final rule must be rejected");
}
// Try to add an initial rule
InitialTimeZoneRule *ir1 = new InitialTimeZoneRule("Test Initial", 2*HOUR, 0);
rbtz1->addTransitionRule(ir1, status);
if (U_SUCCESS(status)) {
errln("FAIL: InitialTimeZoneRule must be rejected");
}
delete ir;
delete rbtz1;
delete rbtz2;
delete rbtz3;
delete rbtz1c;
delete cal;
}
/*
* Test equivalency between OlsonTimeZone and custom RBTZ representing the
* equivalent rules in a certain time range
*/
void
TimeZoneRuleTest::TestHistoricalRuleBasedTimeZone() {
UErrorCode status = U_ZERO_ERROR;
// Compare to America/New_York with equivalent RBTZ
BasicTimeZone *ny = dynamic_cast<BasicTimeZone*>(TimeZone::createTimeZone("America/New_York"));
//RBTZ
InitialTimeZoneRule *ir = new InitialTimeZoneRule("EST", -5*HOUR, 0);
RuleBasedTimeZone *rbtz = new RuleBasedTimeZone("EST5EDT", ir);
DateTimeRule *dtr;
AnnualTimeZoneRule *tzr;
// Standard time
dtr = new DateTimeRule(UCAL_OCTOBER, -1, UCAL_SUNDAY,
2*HOUR, DateTimeRule::WALL_TIME); // Last Sunday in October, at 2AM wall time
tzr = new AnnualTimeZoneRule("EST", -5*HOUR /*rawOffset*/, 0 /*dstSavings*/, dtr, 1967, 2006);
rbtz->addTransitionRule(tzr, status);
if (U_FAILURE(status)) {
errln("FAIL: couldn't add AnnualTimeZoneRule 1.");
}
dtr = new DateTimeRule(UCAL_NOVEMBER, 1, UCAL_SUNDAY,
true, 2*HOUR, DateTimeRule::WALL_TIME); // SUN>=1 in November, at 2AM wall time
tzr = new AnnualTimeZoneRule("EST", -5*HOUR, 0, dtr, 2007, AnnualTimeZoneRule::MAX_YEAR);
rbtz->addTransitionRule(tzr, status);
if (U_FAILURE(status)) {
errln("FAIL: couldn't add AnnualTimeZoneRule 2.");
}
// Daylight saving time
dtr = new DateTimeRule(UCAL_APRIL, -1, UCAL_SUNDAY,
2*HOUR, DateTimeRule::WALL_TIME); // Last Sunday in April, at 2AM wall time
tzr = new AnnualTimeZoneRule("EDT", -5*HOUR, 1*HOUR, dtr, 1967, 1973);
rbtz->addTransitionRule(tzr, status);
if (U_FAILURE(status)) {
errln("FAIL: couldn't add AnnualTimeZoneRule 3.");
}
dtr = new DateTimeRule(UCAL_JANUARY, 6,
2*HOUR, DateTimeRule::WALL_TIME); // January 6, at 2AM wall time
tzr = new AnnualTimeZoneRule("EDT", -5*HOUR, 1*HOUR, dtr, 1974, 1974);
rbtz->addTransitionRule(tzr, status);
if (U_FAILURE(status)) {
errln("FAIL: couldn't add AnnualTimeZoneRule 4.");
}
dtr = new DateTimeRule(UCAL_FEBRUARY, 23,
2*HOUR, DateTimeRule::WALL_TIME); // February 23, at 2AM wall time
tzr = new AnnualTimeZoneRule("EDT", -5*HOUR, 1*HOUR, dtr, 1975, 1975);
rbtz->addTransitionRule(tzr, status);
if (U_FAILURE(status)) {
errln("FAIL: couldn't add AnnualTimeZoneRule 5.");
}
dtr = new DateTimeRule(UCAL_APRIL, -1, UCAL_SUNDAY,
2*HOUR, DateTimeRule::WALL_TIME); // Last Sunday in April, at 2AM wall time
tzr = new AnnualTimeZoneRule("EDT", -5*HOUR, 1*HOUR, dtr, 1976, 1986);
rbtz->addTransitionRule(tzr, status);
if (U_FAILURE(status)) {
errln("FAIL: couldn't add AnnualTimeZoneRule 6.");
}
dtr = new DateTimeRule(UCAL_APRIL, 1, UCAL_SUNDAY,
true, 2*HOUR, DateTimeRule::WALL_TIME); // SUN>=1 in April, at 2AM wall time
tzr = new AnnualTimeZoneRule("EDT", -5*HOUR, 1*HOUR, dtr, 1987, 2006);
rbtz->addTransitionRule(tzr, status);
if (U_FAILURE(status)) {
errln("FAIL: couldn't add AnnualTimeZoneRule 7.");
}
dtr = new DateTimeRule(UCAL_MARCH, 8, UCAL_SUNDAY,
true, 2*HOUR, DateTimeRule::WALL_TIME); // SUN>=8 in March, at 2AM wall time
tzr = new AnnualTimeZoneRule("EDT", -5*HOUR, 1*HOUR, dtr, 2007, AnnualTimeZoneRule::MAX_YEAR);
rbtz->addTransitionRule(tzr, status);
if (U_FAILURE(status)) {
errln("FAIL: couldn't add AnnualTimeZoneRule 7.");
}
rbtz->complete(status);
if (U_FAILURE(status)) {
errln("FAIL: couldn't complete RBTZ.");
}
// hasEquivalentTransitions
UDate jan1_1950 = getUTCMillis(1950, UCAL_JANUARY, 1);
UDate jan1_1967 = getUTCMillis(1971, UCAL_JANUARY, 1);
UDate jan1_2010 = getUTCMillis(2010, UCAL_JANUARY, 1);
if (!ny->hasEquivalentTransitions(*rbtz, jan1_1967, jan1_2010, true, status)) {
dataerrln("FAIL: The RBTZ must be equivalent to America/New_York between 1967 and 2010");
}
if (U_FAILURE(status)) {
errln("FAIL: error returned from hasEquivalentTransitions for ny/rbtz 1967-2010");
}
if (ny->hasEquivalentTransitions(*rbtz, jan1_1950, jan1_2010, true, status)) {
errln("FAIL: The RBTZ must not be equivalent to America/New_York between 1950 and 2010");
}
if (U_FAILURE(status)) {
errln("FAIL: error returned from hasEquivalentTransitions for ny/rbtz 1950-2010");
}
// Same with above, but calling RBTZ#hasEquivalentTransitions against OlsonTimeZone
if (!rbtz->hasEquivalentTransitions(*ny, jan1_1967, jan1_2010, true, status)) {
dataerrln("FAIL: The RBTZ must be equivalent to America/New_York between 1967 and 2010 ");
}
if (U_FAILURE(status)) {
errln("FAIL: error returned from hasEquivalentTransitions for rbtz/ny 1967-2010");
}
if (rbtz->hasEquivalentTransitions(*ny, jan1_1950, jan1_2010, true, status)) {
errln("FAIL: The RBTZ must not be equivalent to America/New_York between 1950 and 2010");
}
if (U_FAILURE(status)) {
errln("FAIL: error returned from hasEquivalentTransitions for rbtz/ny 1950-2010");
}
// TimeZone APIs
if (ny->hasSameRules(*rbtz) || rbtz->hasSameRules(*ny)) {
errln("FAIL: hasSameRules must return false");
}
RuleBasedTimeZone *rbtzc = rbtz->clone();
if (!rbtz->hasSameRules(*rbtzc) || !rbtz->hasEquivalentTransitions(*rbtzc, jan1_1950, jan1_2010, true, status)) {
errln("FAIL: hasSameRules/hasEquivalentTransitions must return true for cloned RBTZs");
}
if (U_FAILURE(status)) {
errln("FAIL: error returned from hasEquivalentTransitions for rbtz/rbtzc 1950-2010");
}
UDate times[] = {
getUTCMillis(2006, UCAL_MARCH, 15),
getUTCMillis(2006, UCAL_NOVEMBER, 1),
getUTCMillis(2007, UCAL_MARCH, 15),
getUTCMillis(2007, UCAL_NOVEMBER, 1),
getUTCMillis(2008, UCAL_MARCH, 15),
getUTCMillis(2008, UCAL_NOVEMBER, 1),
0
};
int32_t offset1, dst1;
int32_t offset2, dst2;
for (int i = 0; times[i] != 0; i++) {
// Check getOffset - must return the same results for these time data
rbtz->getOffset(times[i], false, offset1, dst1, status);
if (U_FAILURE(status)) {
errln("FAIL: rbtz->getOffset failed");
}
ny->getOffset(times[i], false, offset2, dst2, status);
if (U_FAILURE(status)) {
errln("FAIL: ny->getOffset failed");
}
if (offset1 != offset2 || dst1 != dst2) {
dataerrln("FAIL: Incompatible time zone offset/dstSavings for ny and rbtz");
}
// Check inDaylightTime
if (rbtz->inDaylightTime(times[i], status) != ny->inDaylightTime(times[i], status)) {
dataerrln("FAIL: Incompatible daylight saving time for ny and rbtz");
}
if (U_FAILURE(status)) {
errln("FAIL: inDaylightTime failed");
}
}
delete ny;
delete rbtz;
delete rbtzc;
}
/*
* Check if transitions returned by getNextTransition/getPreviousTransition
* are actual time transitions.
*/
void
TimeZoneRuleTest::TestOlsonTransition() {
const int32_t TESTYEARS[][2] = {
{1895, 1905}, // including int32 minimum second
{1965, 1975}, // including the epoch
{1995, 2015}, // practical year range
{0,0}
};
UErrorCode status = U_ZERO_ERROR;
TestZIDEnumeration tzenum(!quick);
while (true) {
const UnicodeString *tzid = tzenum.snext(status);
if (tzid == nullptr) {
break;
}
if (U_FAILURE(status)) {
errln("FAIL: error returned while enumerating timezone IDs.");
break;
}
BasicTimeZone *tz = dynamic_cast<BasicTimeZone*>(TimeZone::createTimeZone(*tzid));
for (int32_t i = 0; TESTYEARS[i][0] != 0 || TESTYEARS[i][1] != 0; i++) {
UDate lo = getUTCMillis(TESTYEARS[i][0], UCAL_JANUARY, 1);
UDate hi = getUTCMillis(TESTYEARS[i][1], UCAL_JANUARY, 1);
verifyTransitions(*tz, lo, hi);
}
delete tz;
}
}
/*
* Check if an OlsonTimeZone and its equivalent RBTZ have the exact same
* transitions.
*/
void
TimeZoneRuleTest::TestRBTZTransition() {
const int32_t STARTYEARS[] = {
1900,
1960,
1990,
2010,
0
};
UErrorCode status = U_ZERO_ERROR;
TestZIDEnumeration tzenum(!quick);
while (true) {
const UnicodeString *tzid = tzenum.snext(status);
if (tzid == nullptr) {
break;
}
if (U_FAILURE(status)) {
errln("FAIL: error returned while enumerating timezone IDs.");
break;
}
BasicTimeZone *tz = dynamic_cast<BasicTimeZone*>(TimeZone::createTimeZone(*tzid));
int32_t ruleCount = tz->countTransitionRules(status);
const InitialTimeZoneRule *initial;
const TimeZoneRule **trsrules = new const TimeZoneRule*[ruleCount];
tz->getTimeZoneRules(initial, trsrules, ruleCount, status);
if (U_FAILURE(status)) {
errln(UnicodeString("FAIL: failed to get the TimeZoneRules from time zone ") + *tzid);
}
RuleBasedTimeZone *rbtz = new RuleBasedTimeZone(*tzid, initial->clone());
if (U_FAILURE(status)) {
errln(UnicodeString("FAIL: failed to get the transition rule count from time zone ") + *tzid);
}
for (int32_t i = 0; i < ruleCount; i++) {
rbtz->addTransitionRule(trsrules[i]->clone(), status);
if (U_FAILURE(status)) {
errln(UnicodeString("FAIL: failed to add a transition rule at index ") + i + " to the RBTZ for " + *tzid);
}
}
rbtz->complete(status);
if (U_FAILURE(status)) {
errln(UnicodeString("FAIL: complete() failed for the RBTZ for ") + *tzid);
}
for (int32_t idx = 0; STARTYEARS[idx] != 0; idx++) {
UDate start = getUTCMillis(STARTYEARS[idx], UCAL_JANUARY, 1);
UDate until = getUTCMillis(STARTYEARS[idx] + 20, UCAL_JANUARY, 1);
// Compare the original OlsonTimeZone with the RBTZ starting the startTime for 20 years
// Ascending
compareTransitionsAscending(*tz, *rbtz, start, until, false);
// Ascending/inclusive
compareTransitionsAscending(*tz, *rbtz, start + 1, until, true);
// Descending
compareTransitionsDescending(*tz, *rbtz, start, until, false);
// Descending/inclusive
compareTransitionsDescending(*tz, *rbtz, start + 1, until, true);
}
delete [] trsrules;
delete rbtz;
delete tz;
}
}
void
TimeZoneRuleTest::TestHasEquivalentTransitions() {
// America/New_York and America/Indiana/Indianapolis are equivalent
// since 2006
UErrorCode status = U_ZERO_ERROR;
BasicTimeZone *newyork = dynamic_cast<BasicTimeZone*>(TimeZone::createTimeZone("America/New_York"));
BasicTimeZone *indianapolis = dynamic_cast<BasicTimeZone*>(TimeZone::createTimeZone("America/Indiana/Indianapolis"));
BasicTimeZone *gmt_5 = dynamic_cast<BasicTimeZone*>(TimeZone::createTimeZone("Etc/GMT+5"));
UDate jan1_1971 = getUTCMillis(1971, UCAL_JANUARY, 1);
UDate jan1_2005 = getUTCMillis(2005, UCAL_JANUARY, 1);
UDate jan1_2006 = getUTCMillis(2006, UCAL_JANUARY, 1);
UDate jan1_2007 = getUTCMillis(2007, UCAL_JANUARY, 1);
UDate jan1_2011 = getUTCMillis(2010, UCAL_JANUARY, 1);
if (newyork->hasEquivalentTransitions(*indianapolis, jan1_2005, jan1_2011, true, status)) {
dataerrln("FAIL: New_York is not equivalent to Indianapolis between 2005 and 2010");
}
if (U_FAILURE(status)) {
errln("FAIL: error status is returned from hasEquivalentTransition");
}
if (!newyork->hasEquivalentTransitions(*indianapolis, jan1_2006, jan1_2011, true, status)) {
errln("FAIL: New_York is equivalent to Indianapolis between 2006 and 2010");
}
if (U_FAILURE(status)) {
errln("FAIL: error status is returned from hasEquivalentTransition");
}
if (!indianapolis->hasEquivalentTransitions(*gmt_5, jan1_1971, jan1_2006, true, status)) {
errln("FAIL: Indianapolis is equivalent to GMT+5 between 1971 and 2005");
}
if (U_FAILURE(status)) {
errln("FAIL: error status is returned from hasEquivalentTransition");
}
if (indianapolis->hasEquivalentTransitions(*gmt_5, jan1_1971, jan1_2007, true, status)) {
dataerrln("FAIL: Indianapolis is not equivalent to GMT+5 between 1971 and 2006");
}
if (U_FAILURE(status)) {
errln("FAIL: error status is returned from hasEquivalentTransition");
}
// Cloned TimeZone
BasicTimeZone *newyork2 = newyork->clone();
if (!newyork->hasEquivalentTransitions(*newyork2, jan1_1971, jan1_2011, false, status)) {
errln("FAIL: Cloned TimeZone must have the same transitions");
}
if (U_FAILURE(status)) {
errln("FAIL: error status is returned from hasEquivalentTransition for newyork/newyork2");
}
if (!newyork->hasEquivalentTransitions(*newyork2, jan1_1971, jan1_2011, true, status)) {
errln("FAIL: Cloned TimeZone must have the same transitions");
}
if (U_FAILURE(status)) {
errln("FAIL: error status is returned from hasEquivalentTransition for newyork/newyork2");
}
// America/New_York and America/Los_Angeles has same DST start rules, but
// raw offsets are different
BasicTimeZone *losangeles = dynamic_cast<BasicTimeZone*>(TimeZone::createTimeZone("America/Los_Angeles"));
if (newyork->hasEquivalentTransitions(*losangeles, jan1_2006, jan1_2011, true, status)) {
dataerrln("FAIL: New_York is not equivalent to Los Angeles, but returned true");
}
if (U_FAILURE(status)) {
errln("FAIL: error status is returned from hasEquivalentTransition for newyork/losangeles");
}
delete newyork;
delete newyork2;
delete indianapolis;
delete gmt_5;
delete losangeles;
}
/*
* Write out time zone rules of OlsonTimeZone into VTIMEZONE format, create a new
* VTimeZone from the VTIMEZONE data, then compare transitions
*/
void
TimeZoneRuleTest::TestVTimeZoneRoundTrip() {
UDate startTime = getUTCMillis(1850, UCAL_JANUARY, 1);
UDate endTime = getUTCMillis(2050, UCAL_JANUARY, 1);
UErrorCode status = U_ZERO_ERROR;
TestZIDEnumeration tzenum(!quick);
while (true) {
const UnicodeString *tzid = tzenum.snext(status);
if (tzid == nullptr) {
break;
}
if (U_FAILURE(status)) {
errln("FAIL: error returned while enumerating timezone IDs.");
break;
}
BasicTimeZone *tz = dynamic_cast<BasicTimeZone*>(TimeZone::createTimeZone(*tzid));
VTimeZone *vtz_org = VTimeZone::createVTimeZoneByID(*tzid);
vtz_org->setTZURL("http://source.icu-project.org/timezone");
vtz_org->setLastModified(Calendar::getNow());
VTimeZone *vtz_new = nullptr;
UnicodeString vtzdata;
// Write out VTIMEZONE data
vtz_org->write(vtzdata, status);
if (U_FAILURE(status)) {
errln(UnicodeString("FAIL: error returned while writing time zone rules for ") +
*tzid + " into VTIMEZONE format.");
} else {
// Read VTIMEZONE data
vtz_new = VTimeZone::createVTimeZone(vtzdata, status);
if (U_FAILURE(status)) {
errln(UnicodeString("FAIL: error returned while reading VTIMEZONE data for ") + *tzid);
} else {
// Write out VTIMEZONE one more time
UnicodeString vtzdata1;
vtz_new->write(vtzdata1, status);
if (U_FAILURE(status)) {
errln(UnicodeString("FAIL: error returned while writing time zone rules for ") +
*tzid + "(vtz_new) into VTIMEZONE format.");
} else {
// Make sure VTIMEZONE data is exactly same with the first one
if (vtzdata != vtzdata1) {
errln(UnicodeString("FAIL: different VTIMEZONE data after round trip for ") + *tzid);
}
}
// Check equivalency after the first transition.
// The DST information before the first transition might be lost
// because there is no good way to represent the initial time with
// VTIMEZONE.
int32_t raw1, raw2, dst1, dst2;
tz->getOffset(startTime, false, raw1, dst1, status);
vtz_new->getOffset(startTime, false, raw2, dst2, status);
if (U_FAILURE(status)) {
errln("FAIL: error status is returned from getOffset");
} else {
if (raw1 + dst1 != raw2 + dst2) {
errln("FAIL: VTimeZone for " + *tzid +
" is not equivalent to its OlsonTimeZone corresponding at "
+ dateToString(startTime));
}
TimeZoneTransition trans;
UBool avail = tz->getNextTransition(startTime, false, trans);
if (avail) {
if (!vtz_new->hasEquivalentTransitions(*tz, trans.getTime(),
endTime, true, status)) {
int32_t maxDelta = 1000;
if (!hasEquivalentTransitions(*vtz_new, *tz, trans.getTime() + maxDelta,
endTime, true, maxDelta, status)) {
errln("FAIL: VTimeZone for " + *tzid +
" is not equivalent to its OlsonTimeZone corresponding.");
} else {
logln("VTimeZone for " + *tzid +
" differs from its OlsonTimeZone corresponding with maximum transition time delta - " + maxDelta);
}
}
if (U_FAILURE(status)) {
errln("FAIL: error status is returned from hasEquivalentTransition");
}
}
}
}
if (vtz_new != nullptr) {
delete vtz_new;
vtz_new = nullptr;
}
}
delete tz;
delete vtz_org;
}
}
/*
* Write out time zone rules of OlsonTimeZone after a cutover date into VTIMEZONE format,
* create a new VTimeZone from the VTIMEZONE data, then compare transitions
*/
void
TimeZoneRuleTest::TestVTimeZoneRoundTripPartial() {
const int32_t STARTYEARS[] = {
1900,
1950,
2020,
0
};
UDate endTime = getUTCMillis(2050, UCAL_JANUARY, 1);
UErrorCode status = U_ZERO_ERROR;
TestZIDEnumeration tzenum(!quick);
while (true) {
const UnicodeString *tzid = tzenum.snext(status);
if (tzid == nullptr) {
break;
}
if (U_FAILURE(status)) {
errln("FAIL: error returned while enumerating timezone IDs.");
break;
}
BasicTimeZone *tz = dynamic_cast<BasicTimeZone*>(TimeZone::createTimeZone(*tzid));
VTimeZone *vtz_org = VTimeZone::createVTimeZoneByID(*tzid);
VTimeZone *vtz_new = nullptr;
UnicodeString vtzdata;
for (int32_t i = 0; STARTYEARS[i] != 0; i++) {
// Write out VTIMEZONE
UDate startTime = getUTCMillis(STARTYEARS[i], UCAL_JANUARY, 1);
vtz_org->write(startTime, vtzdata, status);
if (U_FAILURE(status)) {
errln(UnicodeString("FAIL: error returned while writing time zone rules for ") +
*tzid + " into VTIMEZONE format since " + dateToString(startTime));
} else {
// Read VTIMEZONE data
vtz_new = VTimeZone::createVTimeZone(vtzdata, status);
if (U_FAILURE(status)) {
errln(UnicodeString("FAIL: error returned while reading VTIMEZONE data for ") + *tzid
+ " since " + dateToString(startTime));
} else {
// Check equivalency after the first transition.
// The DST information before the first transition might be lost
// because there is no good way to represent the initial time with
// VTIMEZONE.
int32_t raw1, raw2, dst1, dst2;
tz->getOffset(startTime, false, raw1, dst1, status);
vtz_new->getOffset(startTime, false, raw2, dst2, status);
if (U_FAILURE(status)) {
errln("FAIL: error status is returned from getOffset");
} else {
if (raw1 + dst1 != raw2 + dst2) {
errln("FAIL: VTimeZone for " + *tzid +
" is not equivalent to its OlsonTimeZone corresponding at "
+ dateToString(startTime));
}
TimeZoneTransition trans;
UBool avail = tz->getNextTransition(startTime, false, trans);
if (avail) {
if (!vtz_new->hasEquivalentTransitions(*tz, trans.getTime(),
endTime, true, status)) {
int32_t maxDelta = 1000;
if (!hasEquivalentTransitions(*vtz_new, *tz, trans.getTime() + maxDelta,
endTime, true, maxDelta, status)) {
errln("FAIL: VTimeZone for " + *tzid +
" is not equivalent to its OlsonTimeZone corresponding.");
} else {
logln("VTimeZone for " + *tzid +
" differs from its OlsonTimeZone corresponding with maximum transition time delta - " + maxDelta);
}
}
if (U_FAILURE(status)) {
errln("FAIL: error status is returned from hasEquivalentTransition");
}
}
}
}
}
if (vtz_new != nullptr) {
delete vtz_new;
vtz_new = nullptr;
}
}
delete tz;
delete vtz_org;
}
}
/*
* Write out simple time zone rules from an OlsonTimeZone at various time into VTIMEZONE
* format and create a new VTimeZone from the VTIMEZONE data, then make sure the raw offset
* and DST savings are same in these two time zones.
*/
void
TimeZoneRuleTest::TestVTimeZoneSimpleWrite() {
const int32_t TESTDATES[][3] = {
{2006, UCAL_JANUARY, 1},
{2006, UCAL_MARCH, 15},
{2006, UCAL_MARCH, 31},
{2006, UCAL_OCTOBER, 25},
{2006, UCAL_NOVEMBER, 1},
{2006, UCAL_NOVEMBER, 5},
{2007, UCAL_JANUARY, 1},
{0, 0, 0}
};
UErrorCode status = U_ZERO_ERROR;
TestZIDEnumeration tzenum(!quick);
while (true) {
const UnicodeString *tzid = tzenum.snext(status);
if (tzid == nullptr) {
break;
}
if (U_FAILURE(status)) {
errln("FAIL: error returned while enumerating timezone IDs.");
break;
}
VTimeZone *vtz_org = VTimeZone::createVTimeZoneByID(*tzid);
VTimeZone *vtz_new = nullptr;
UnicodeString vtzdata;
for (int32_t i = 0; TESTDATES[i][0] != 0; i++) {
// Write out VTIMEZONE
UDate time = getUTCMillis(TESTDATES[i][0], TESTDATES[i][1], TESTDATES[i][2]);
vtz_org->writeSimple(time, vtzdata, status);
if (U_FAILURE(status)) {
errln(UnicodeString("FAIL: error returned while writing simple time zone rules for ") +
*tzid + " into VTIMEZONE format at " + dateToString(time));
} else {
// Read VTIMEZONE data
vtz_new = VTimeZone::createVTimeZone(vtzdata, status);