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
|
/*
*******************************************************************************
* Copyright (C) 2007, International Business Machines Corporation and *
* others. All Rights Reserved. *
*******************************************************************************
*/
#include "unicode/utypes.h"
#if !UCONFIG_NO_FORMATTING
#include "tzfmttst.h"
#include "unicode/timezone.h"
#include "unicode/simpletz.h"
#include "unicode/calendar.h"
#include "unicode/strenum.h"
#include "unicode/smpdtfmt.h"
#include "unicode/uchar.h"
#include "unicode/basictz.h"
#include "cstring.h"
#include "zonemeta.h"
#define DEBUG_ALL 0
static const char* PATTERNS[] = {"z", "zzzz", "Z", "ZZZZ", "v", "vvvv", "V", "VVVV"};
static const int NUM_PATTERNS = sizeof(PATTERNS)/sizeof(const char*);
void
TimeZoneFormatTest::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par*/ )
{
if (exec) {
logln("TestSuite TimeZoneFormatTest");
}
switch (index) {
TESTCASE(0, TestTimeZoneRoundTrip);
TESTCASE(1, TestTimeRoundTrip);
default: name = ""; break;
}
}
void
TimeZoneFormatTest::TestTimeZoneRoundTrip(void) {
UErrorCode status = U_ZERO_ERROR;
SimpleTimeZone unknownZone(-31415, (UnicodeString)"Etc/Unknown");
int32_t badDstOffset = -1234;
int32_t badZoneOffset = -2345;
int32_t testDateData[][3] = {
{2007, 1, 15},
{2007, 6, 15},
{1990, 1, 15},
{1990, 6, 15},
{1960, 1, 15},
{1960, 6, 15},
};
Calendar *cal = Calendar::createInstance(TimeZone::createTimeZone((UnicodeString)"UTC"), status);
if (U_FAILURE(status)) {
errln("Calendar::createInstance failed");
return;
}
// Set up rule equivalency test range
UDate low, high;
cal->set(1900, UCAL_JANUARY, 1);
low = cal->getTime(status);
cal->set(2040, UCAL_JANUARY, 1);
high = cal->getTime(status);
if (U_FAILURE(status)) {
errln("getTime failed");
return;
}
// Set up test dates
UDate DATES[(sizeof(testDateData)/sizeof(int32_t))/3];
const int32_t nDates = (sizeof(testDateData)/sizeof(int32_t))/3;
cal->clear();
for (int32_t i = 0; i < nDates; i++) {
cal->set(testDateData[i][0], testDateData[i][1], testDateData[i][2]);
DATES[i] = cal->getTime(status);
if (U_FAILURE(status)) {
errln("getTime failed");
return;
}
}
// Set up test locales
const Locale locales1[] = {
Locale("en_US")
};
const Locale locales2[] = {
Locale("en_US"),
Locale("en"),
Locale("en_CA"),
Locale("fr"),
Locale("zh_Hant")
};
const Locale *LOCALES;
int32_t nLocales;
if (DEBUG_ALL) {
LOCALES = Locale::getAvailableLocales(nLocales);
} else if (quick) {
LOCALES = locales1;
nLocales = sizeof(locales1)/sizeof(Locale);
} else {
LOCALES = locales2;
nLocales = sizeof(locales2)/sizeof(Locale);
}
StringEnumeration *tzids = TimeZone::createEnumeration();
if (U_FAILURE(status)) {
errln("tzids->count failed");
return;
}
int32_t inRaw, inDst;
int32_t outRaw, outDst;
// Run the roundtrip test
for (int32_t locidx = 0; locidx < nLocales; locidx++) {
for (int32_t patidx = 0; patidx < NUM_PATTERNS; patidx++) {
//DEBUG static const char* PATTERNS[] = {"z", "zzzz", "Z", "ZZZZ", "v", "vvvv", "V", "VVVV"};
//if (patidx != 1) continue;
SimpleDateFormat *sdf = new SimpleDateFormat((UnicodeString)PATTERNS[patidx], LOCALES[locidx], status);
if (U_FAILURE(status)) {
errln((UnicodeString)"new SimpleDateFormat failed for pattern " +
PATTERNS[patidx] + " for locale " + LOCALES[locidx].getName());
status = U_ZERO_ERROR;
continue;
}
tzids->reset(status);
const UnicodeString *tzid;
while ((tzid = tzids->snext(status))) {
TimeZone *tz = TimeZone::createTimeZone(*tzid);
for (int32_t datidx = 0; datidx < nDates; datidx++) {
UnicodeString tzstr;
FieldPosition fpos(0);
// Format
sdf->setTimeZone(*tz);
sdf->format(DATES[datidx], tzstr, fpos);
// Before parse, set unknown zone to SimpleDateFormat instance
// just for making sure that it does not depends on the time zone
// originally set.
sdf->setTimeZone(unknownZone);
// Parse
ParsePosition pos(0);
Calendar *outcal = Calendar::createInstance(unknownZone, status);
if (U_FAILURE(status)) {
errln("Failed to create an instance of calendar for receiving parse result.");
status = U_ZERO_ERROR;
continue;
}
outcal->set(UCAL_DST_OFFSET, badDstOffset);
outcal->set(UCAL_ZONE_OFFSET, badZoneOffset);
sdf->parse(tzstr, *outcal, pos);
// Check the result
const TimeZone &outtz = outcal->getTimeZone();
UnicodeString outtzid;
outtz.getID(outtzid);
tz->getOffset(DATES[datidx], false, inRaw, inDst, status);
if (U_FAILURE(status)) {
errln((UnicodeString)"Failed to get offsets from time zone" + *tzid);
status = U_ZERO_ERROR;
}
outtz.getOffset(DATES[datidx], false, outRaw, outDst, status);
if (U_FAILURE(status)) {
errln((UnicodeString)"Failed to get offsets from time zone" + outtzid);
status = U_ZERO_ERROR;
}
// Check if localized GMT format or RFC format is used.
int32_t numDigits = 0;
for (int n = 0; n < tzstr.length(); n++) {
if (u_isdigit(tzstr.charAt(n))) {
numDigits++;
}
}
if (numDigits >= 4) {
// Localized GMT or RFC: total offset (raw + dst) must be preserved.
int32_t inOffset = inRaw + inDst;
int32_t outOffset = outRaw + outDst;
if (inOffset != outOffset) {
errln((UnicodeString)"Offset round trip failed; tz=" + *tzid
+ ", locale=" + LOCALES[locidx].getName() + ", pattern=" + PATTERNS[patidx]
+ ", time=" + DATES[datidx] + ", str=" + tzstr
+ ", inOffset=" + inOffset + ", outOffset=" + outOffset);
}
} else if (uprv_strcmp(PATTERNS[patidx], "z") == 0 || uprv_strcmp(PATTERNS[patidx], "zzzz") == 0
|| uprv_strcmp(PATTERNS[patidx], "v") == 0 || uprv_strcmp(PATTERNS[patidx], "vvvv") == 0
|| uprv_strcmp(PATTERNS[patidx], "V") == 0) {
// Specific or generic: raw offset must be preserved.
if (inRaw != outRaw) {
errln((UnicodeString)"Raw offset round trip failed; tz=" + *tzid
+ ", locale=" + LOCALES[locidx].getName() + ", pattern=" + PATTERNS[patidx]
+ ", time=" + DATES[datidx] + ", str=" + tzstr
+ ", inRawOffset=" + inRaw + ", outRawOffset=" + outRaw);
}
} else { // "VVVV"
// Location: time zone rule must be preserved.
UnicodeString canonical;
ZoneMeta::getCanonicalID(*tzid, canonical);
if (outtzid != canonical) {
// Canonical ID did not match - check the rules
if (!((BasicTimeZone*)&outtz)->hasEquivalentTransitions((BasicTimeZone&)*tz, low, high, TRUE, status)) {
errln("Canonical round trip failed; tz=" + *tzid
+ ", locale=" + LOCALES[locidx].getName() + ", pattern=" + PATTERNS[patidx]
+ ", time=" + DATES[datidx] + ", str=" + tzstr
+ ", outtz=" + outtzid);
}
if (U_FAILURE(status)) {
errln("hasEquivalentTransitions failed");
status = U_ZERO_ERROR;
}
}
}
delete outcal;
}
delete tz;
}
delete sdf;
}
}
delete cal;
delete tzids;
}
void
TimeZoneFormatTest::TestTimeRoundTrip(void) {
UErrorCode status = U_ZERO_ERROR;
Calendar *cal = Calendar::createInstance(TimeZone::createTimeZone((UnicodeString)"UTC"), status);
if (U_FAILURE(status)) {
errln("Calendar::createInstance failed");
return;
}
UDate START_TIME, END_TIME;
if (DEBUG_ALL) {
cal->set(1900, UCAL_JANUARY, 1);
} else {
cal->set(1965, UCAL_JANUARY, 1);
}
START_TIME = cal->getTime(status);
cal->set(2015, UCAL_JANUARY, 1);
END_TIME = cal->getTime(status);
if (U_FAILURE(status)) {
errln("getTime failed");
return;
}
// Whether each pattern is ambiguous at DST->STD local time overlap
UBool AMBIGUOUS_DST_DECESSION[] = {FALSE, FALSE, FALSE, FALSE, TRUE, TRUE, FALSE, TRUE};
// Whether each pattern is ambiguous at STD->STD/DST->DST local time overlap
UBool AMBIGUOUS_NEGATIVE_SHIFT[] = {TRUE, TRUE, FALSE, FALSE, TRUE, TRUE, TRUE, TRUE};
UnicodeString BASEPATTERN("yyyy-MM-dd'T'HH:mm:ss.SSS");
// timer for performance analysis
UDate timer;
UDate times[NUM_PATTERNS];
for (int32_t i = 0; i < NUM_PATTERNS; i++) {
times[i] = 0;
}
UBool REALLY_VERBOSE = FALSE;
// Set up test locales
const Locale locales1[] = {
Locale("en_US")
};
const Locale locales2[] = {
Locale("en_US"),
Locale("en"),
Locale("de_DE"),
Locale("es_ES"),
Locale("fr_FR"),
Locale("it_IT"),
Locale("ja_JP"),
Locale("ko_KR"),
Locale("pt_BR"),
Locale("zh_Hans_CN"),
Locale("zh_Hant_TW")
};
const Locale *LOCALES;
int32_t nLocales;
if (DEBUG_ALL) {
LOCALES = Locale::getAvailableLocales(nLocales);
} else if (quick) {
LOCALES = locales1;
nLocales = sizeof(locales1)/sizeof(Locale);
} else {
LOCALES = locales2;
nLocales = sizeof(locales2)/sizeof(Locale);
}
StringEnumeration *tzids = TimeZone::createEnumeration();
if (U_FAILURE(status)) {
errln("tzids->count failed");
return;
}
int32_t testCounts = 0;
UDate testTimes[4];
UBool expectedRoundTrip[4];
int32_t testLen = 0;
for (int32_t locidx = 0; locidx < nLocales; locidx++) {
logln((UnicodeString)"Locale: " + LOCALES[locidx].getName());
for (int32_t patidx = 0; patidx < NUM_PATTERNS; patidx++) {
logln((UnicodeString)" pattern: " + PATTERNS[patidx]);
//DEBUG static const char* PATTERNS[] = {"z", "zzzz", "Z", "ZZZZ", "v", "vvvv", "V", "VVVV"};
//if (patidx != 1) continue;
UnicodeString pattern(BASEPATTERN);
pattern.append(" ").append(PATTERNS[patidx]);
SimpleDateFormat *sdf = new SimpleDateFormat(pattern, LOCALES[locidx], status);
if (U_FAILURE(status)) {
errln((UnicodeString)"new SimpleDateFormat failed for pattern " +
pattern + " for locale " + LOCALES[locidx].getName());
status = U_ZERO_ERROR;
continue;
}
tzids->reset(status);
const UnicodeString *tzid;
timer = Calendar::getNow();
while ((tzid = tzids->snext(status))) {
UnicodeString canonical;
ZoneMeta::getCanonicalID(*tzid, canonical);
if (*tzid != canonical) {
// Skip aliases
continue;
}
BasicTimeZone *tz = (BasicTimeZone*)TimeZone::createTimeZone(*tzid);
sdf->setTimeZone(*tz);
UDate t = START_TIME;
TimeZoneTransition tzt;
UBool tztAvail = FALSE;
UBool middle = TRUE;
while (t < END_TIME) {
if (!tztAvail) {
testTimes[0] = t;
expectedRoundTrip[0] = TRUE;
testLen = 1;
} else {
int32_t fromOffset = tzt.getFrom()->getRawOffset() + tzt.getFrom()->getDSTSavings();
int32_t toOffset = tzt.getTo()->getRawOffset() + tzt.getTo()->getDSTSavings();
int32_t delta = toOffset - fromOffset;
if (delta < 0) {
UBool isDstDecession = tzt.getFrom()->getDSTSavings() > 0 && tzt.getTo()->getDSTSavings() == 0;
testTimes[0] = t + delta - 1;
expectedRoundTrip[0] = TRUE;
testTimes[1] = t + delta;
expectedRoundTrip[1] = isDstDecession ?
!AMBIGUOUS_DST_DECESSION[patidx] : !AMBIGUOUS_NEGATIVE_SHIFT[patidx];
testTimes[2] = t - 1;
expectedRoundTrip[2] = isDstDecession ?
!AMBIGUOUS_DST_DECESSION[patidx] : !AMBIGUOUS_NEGATIVE_SHIFT[patidx];
testTimes[3] = t;
expectedRoundTrip[3] = TRUE;
testLen = 4;
} else {
testTimes[0] = t - 1;
expectedRoundTrip[0] = TRUE;
testTimes[1] = t;
expectedRoundTrip[1] = TRUE;
testLen = 2;
}
}
for (int32_t testidx = 0; testidx < testLen; testidx++) {
if (quick) {
// reduce regular test time
if (!expectedRoundTrip[testidx]) {
continue;
}
}
testCounts++;
UnicodeString text;
FieldPosition fpos(0);
sdf->format(testTimes[testidx], text, fpos);
UDate parsedDate = sdf->parse(text, status);
if (U_FAILURE(status)) {
errln((UnicodeString)"Failed to parse " + text);
status = U_ZERO_ERROR;
continue;
}
if (parsedDate != testTimes[testidx]) {
UnicodeString msg = (UnicodeString)"Time round trip failed for "
+ "tzid=" + *tzid
+ ", locale=" + LOCALES[locidx].getName()
+ ", pattern=" + PATTERNS[patidx]
+ ", text=" + text
+ ", time=" + testTimes[testidx]
+ ", restime=" + parsedDate
+ ", diff=" + (parsedDate - testTimes[testidx]);
if (expectedRoundTrip[testidx]) {
errln((UnicodeString)"FAIL: " + msg);
} else if (REALLY_VERBOSE) {
logln(msg);
}
}
}
tztAvail = tz->getNextTransition(t, FALSE, tzt);
if (!tztAvail) {
break;
}
if (middle) {
// Test the date in the middle of two transitions.
t += (int64_t)((tzt.getTime() - t)/2);
middle = FALSE;
tztAvail = FALSE;
} else {
t = tzt.getTime();
}
}
delete tz;
}
times[patidx] += (Calendar::getNow() - timer);
delete sdf;
}
}
UDate total = 0;
logln("### Elapsed time by patterns ###");
for (int32_t i = 0; i < NUM_PATTERNS; i++) {
logln(UnicodeString("") + times[i] + "ms (" + PATTERNS[i] + ")");
total += times[i];
}
logln((UnicodeString)"Total: " + total + "ms");
logln((UnicodeString)"Iteration: " + testCounts);
delete cal;
delete tzids;
}
#endif /* #if !UCONFIG_NO_FORMATTING */
|