1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /********************************************************************
4 * Copyright (c) 1997-2016, International Business Machines
5 * Corporation and others. All Rights Reserved.
6 ********************************************************************
7 *
8 * File CCALTST.C
9 *
10 * Modification History:
11 * Name Description
12 * Madhu Katragadda Creation
13 ********************************************************************
14 */
15
16 /* C API AND FUNCTIONALITY TEST FOR CALENDAR (ucol.h)*/
17
18 #include "unicode/utypes.h"
19
20 #if !UCONFIG_NO_FORMATTING
21
22 #include <stdbool.h>
23 #include <stdlib.h>
24 #include <string.h>
25
26 #include "unicode/uloc.h"
27 #include "unicode/ucal.h"
28 #include "unicode/udat.h"
29 #include "unicode/ustring.h"
30 #include "cintltst.h"
31 #include "ccaltst.h"
32 #include "cformtst.h"
33 #include "cmemory.h"
34 #include "cstring.h"
35 #include "ulist.h"
36
37 void TestGregorianChange(void);
38 void TestFieldDifference(void);
39 void TestAddRollEra0AndEraBounds(void);
40 void TestGetTZTransition(void);
41 void TestGetWindowsTimeZoneID(void);
42 void TestGetTimeZoneIDByWindowsID(void);
43 void TestJpnCalAddSetNextEra(void);
44 void TestUcalOpenBufferRead(void);
45 void TestGetTimeZoneOffsetFromLocal(void);
46
47 void TestFWWithISO8601(void);
48
49 void addCalTest(TestNode** root);
50
addCalTest(TestNode ** root)51 void addCalTest(TestNode** root)
52 {
53
54 addTest(root, &TestCalendar, "tsformat/ccaltst/TestCalendar");
55 addTest(root, &TestGetSetDateAPI, "tsformat/ccaltst/TestGetSetDateAPI");
56 addTest(root, &TestFieldGetSet, "tsformat/ccaltst/TestFieldGetSet");
57 addTest(root, &TestAddRollExtensive, "tsformat/ccaltst/TestAddRollExtensive");
58 addTest(root, &TestGetLimits, "tsformat/ccaltst/TestGetLimits");
59 addTest(root, &TestDOWProgression, "tsformat/ccaltst/TestDOWProgression");
60 addTest(root, &TestGMTvsLocal, "tsformat/ccaltst/TestGMTvsLocal");
61 addTest(root, &TestGregorianChange, "tsformat/ccaltst/TestGregorianChange");
62 addTest(root, &TestGetKeywordValuesForLocale, "tsformat/ccaltst/TestGetKeywordValuesForLocale");
63 addTest(root, &TestWeekend, "tsformat/ccaltst/TestWeekend");
64 addTest(root, &TestFieldDifference, "tsformat/ccaltst/TestFieldDifference");
65 addTest(root, &TestAmbiguousWallTime, "tsformat/ccaltst/TestAmbiguousWallTime");
66 addTest(root, &TestAddRollEra0AndEraBounds, "tsformat/ccaltst/TestAddRollEra0AndEraBounds");
67 addTest(root, &TestGetTZTransition, "tsformat/ccaltst/TestGetTZTransition");
68 addTest(root, &TestGetWindowsTimeZoneID, "tsformat/ccaltst/TestGetWindowsTimeZoneID");
69 addTest(root, &TestGetTimeZoneIDByWindowsID, "tsformat/ccaltst/TestGetTimeZoneIDByWindowsID");
70 addTest(root, &TestJpnCalAddSetNextEra, "tsformat/ccaltst/TestJpnCalAddSetNextEra");
71 addTest(root, &TestUcalOpenBufferRead, "tsformat/ccaltst/TestUcalOpenBufferRead");
72 addTest(root, &TestGetTimeZoneOffsetFromLocal, "tsformat/ccaltst/TestGetTimeZoneOffsetFromLocal");
73 addTest(root, &TestFWWithISO8601, "tsformat/ccaltst/TestFWWithISO8601");
74 addTest(root, &TestGetIanaTimeZoneID, "tstformat/ccaltst/TestGetIanaTimeZoneID");
75 }
76
77 /* "GMT" */
78 static const UChar fgGMTID [] = { 0x0047, 0x004d, 0x0054, 0x0000 };
79
80 /* "PST" */
81 static const UChar PST[] = {0x50, 0x53, 0x54, 0x00}; /* "PST" */
82
83 static const UChar EUROPE_PARIS[] = {0x45, 0x75, 0x72, 0x6F, 0x70, 0x65, 0x2F, 0x50, 0x61, 0x72, 0x69, 0x73, 0x00}; /* "Europe/Paris" */
84
85 static const UChar AMERICA_LOS_ANGELES[] = {0x41, 0x6D, 0x65, 0x72, 0x69, 0x63, 0x61, 0x2F,
86 0x4C, 0x6F, 0x73, 0x5F, 0x41, 0x6E, 0x67, 0x65, 0x6C, 0x65, 0x73, 0x00}; /* America/Los_Angeles */
87
88 typedef struct {
89 const char * locale;
90 UCalendarType calType;
91 const char * expectedResult;
92 } UCalGetTypeTest;
93
94 static const UCalGetTypeTest ucalGetTypeTests[] = {
95 { "en_US", UCAL_GREGORIAN, "gregorian" },
96 { "ja_JP@calendar=japanese", UCAL_DEFAULT, "japanese" },
97 { "th_TH", UCAL_GREGORIAN, "gregorian" },
98 { "th_TH", UCAL_DEFAULT, "buddhist" },
99 { "th-TH-u-ca-gregory", UCAL_DEFAULT, "gregorian" },
100 { "ja_JP@calendar=japanese", UCAL_GREGORIAN, "gregorian" },
101 { "fr_CH", UCAL_DEFAULT, "gregorian" },
102 { "fr_SA", UCAL_DEFAULT, "islamic-umalqura" },
103 { "fr_CH@rg=sazzzz", UCAL_DEFAULT, "islamic-umalqura" },
104 { "fr_CH@rg=sa14", UCAL_DEFAULT, "islamic-umalqura" },
105 { "fr_CH@calendar=japanese;rg=sazzzz", UCAL_DEFAULT, "japanese" },
106 { "fr_CH@rg=twcyi", UCAL_DEFAULT, "gregorian" }, // test for ICU-22364
107 { "fr_CH@rg=ugw", UCAL_DEFAULT, "gregorian" }, // test for ICU-22364
108 { "fr_TH@rg=SA", UCAL_DEFAULT, "buddhist" }, /* ignore malformed rg tag */
109 { "th@rg=SA", UCAL_DEFAULT, "buddhist" }, /* ignore malformed rg tag */
110 { "", UCAL_GREGORIAN, "gregorian" },
111 { NULL, UCAL_GREGORIAN, "gregorian" },
112 { NULL, 0, NULL } /* terminator */
113 };
114
TestCalendar()115 static void TestCalendar()
116 {
117 UCalendar *caldef = 0, *caldef2 = 0, *calfr = 0, *calit = 0, *calfrclone = 0;
118 UEnumeration* uenum = NULL;
119 int32_t count, count2, i,j;
120 UChar tzID[4];
121 UChar *tzdname = 0;
122 UErrorCode status = U_ZERO_ERROR;
123 UDate now;
124 UDateFormat *datdef = 0;
125 UChar *result = 0;
126 int32_t resultlength, resultlengthneeded;
127 char tempMsgBuf[1024]; // u_austrcpy() of some formatted dates & times.
128 char tempMsgBuf2[256]; // u_austrcpy() of some formatted dates & times.
129 UChar zone1[64], zone2[64];
130 const char *tzver = 0;
131 int32_t tzverLen = 0;
132 UChar canonicalID[64];
133 UBool isSystemID = false;
134 const UCalGetTypeTest * ucalGetTypeTestPtr;
135
136 #ifdef U_USE_UCAL_OBSOLETE_2_8
137 /*Testing countAvailableTimeZones*/
138 int32_t offset=0;
139 log_verbose("\nTesting ucal_countAvailableTZIDs\n");
140 count=ucal_countAvailableTZIDs(offset);
141 log_verbose("The number of timezone id's present with offset 0 are %d:\n", count);
142 if(count < 5) /* Don't hard code an exact == test here! */
143 log_err("FAIL: error in the ucal_countAvailableTZIDs - got %d expected at least 5 total\n", count);
144
145 /*Testing getAvailableTZIDs*/
146 log_verbose("\nTesting ucal_getAvailableTZIDs");
147 for(i=0;i<count;i++){
148 ucal_getAvailableTZIDs(offset, i, &status);
149 if(U_FAILURE(status)){
150 log_err("FAIL: ucal_getAvailableTZIDs returned %s\n", u_errorName(status));
151 }
152 log_verbose("%s\n", u_austrcpy(tempMsgBuf, ucal_getAvailableTZIDs(offset, i, &status)));
153 }
154 /*get Illegal TZID where index >= count*/
155 ucal_getAvailableTZIDs(offset, i, &status);
156 if(status != U_INDEX_OUTOFBOUNDS_ERROR){
157 log_err("FAIL:for TZID index >= count Expected INDEX_OUTOFBOUNDS_ERROR Got %s\n", u_errorName(status));
158 }
159 status=U_ZERO_ERROR;
160 #endif
161
162 /*Test ucal_openTimeZones, ucal_openCountryTimeZones and ucal_openTimeZoneIDEnumeration */
163 for (j=0; j<6; ++j) {
164 const char *api = "?";
165 const int32_t offsetMinus5 = -5*60*60*1000;
166 switch (j) {
167 case 0:
168 api = "ucal_openTimeZones()";
169 uenum = ucal_openTimeZones(&status);
170 break;
171 case 1:
172 api = "ucal_openCountryTimeZones(US)";
173 uenum = ucal_openCountryTimeZones("US", &status);
174 break;
175 case 2:
176 api = "ucal_openTimeZoneIDEnumerarion(UCAL_ZONE_TYPE_CANONICAL, NULL, NULL)";
177 uenum = ucal_openTimeZoneIDEnumeration(UCAL_ZONE_TYPE_CANONICAL, NULL, NULL, &status);
178 break;
179 case 3:
180 api = "ucal_openTimeZoneIDEnumerarion(UCAL_ZONE_TYPE_CANONICAL_LOCATION, CA, NULL)";
181 uenum = ucal_openTimeZoneIDEnumeration(UCAL_ZONE_TYPE_CANONICAL_LOCATION, "CA", NULL, &status);
182 break;
183 case 4:
184 api = "ucal_openTimeZoneIDEnumerarion(UCAL_ZONE_TYPE_ANY, NULL, -5 hour)";
185 uenum = ucal_openTimeZoneIDEnumeration(UCAL_ZONE_TYPE_ANY, NULL, &offsetMinus5, &status);
186 break;
187 case 5:
188 api = "ucal_openTimeZoneIDEnumerarion(UCAL_ZONE_TYPE_ANY, US, -5 hour)";
189 uenum = ucal_openTimeZoneIDEnumeration(UCAL_ZONE_TYPE_ANY, "US", &offsetMinus5, &status);
190 break;
191 }
192 if (U_FAILURE(status)) {
193 log_err_status(status, "FAIL: %s failed with %s\n", api,
194 u_errorName(status));
195 } else {
196 const char* id;
197 int32_t len;
198 count = uenum_count(uenum, &status);
199 log_verbose("%s returned %d timezone id's:\n", api, count);
200 if (count < 5) { /* Don't hard code an exact == test here! */
201 log_data_err("FAIL: in %s, got %d, expected at least 5 -> %s (Are you missing data?)\n", api, count, u_errorName(status));
202 }
203 uenum_reset(uenum, &status);
204 if (U_FAILURE(status)){
205 log_err("FAIL: uenum_reset for %s returned %s\n",
206 api, u_errorName(status));
207 }
208 for (i=0; i<count; i++) {
209 id = uenum_next(uenum, &len, &status);
210 if (U_FAILURE(status)){
211 log_err("FAIL: uenum_next for %s returned %s\n",
212 api, u_errorName(status));
213 } else {
214 log_verbose("%s\n", id);
215 }
216 }
217 /* Next one should be NULL */
218 id = uenum_next(uenum, &len, &status);
219 if (id != NULL) {
220 log_err("FAIL: uenum_next for %s returned %s, expected NULL\n",
221 api, id);
222 }
223 }
224 uenum_close(uenum);
225 }
226
227 /*Test ucal_getDSTSavings*/
228 status = U_ZERO_ERROR;
229 i = ucal_getDSTSavings(fgGMTID, &status);
230 if (U_FAILURE(status)) {
231 log_err("FAIL: ucal_getDSTSavings(GMT) => %s\n",
232 u_errorName(status));
233 } else if (i != 0) {
234 log_data_err("FAIL: ucal_getDSTSavings(GMT) => %d, expect 0 (Are you missing data?)\n", i);
235 }
236 i = ucal_getDSTSavings(PST, &status);
237 if (U_FAILURE(status)) {
238 log_err("FAIL: ucal_getDSTSavings(PST) => %s\n",
239 u_errorName(status));
240 } else if (i != 1*60*60*1000) {
241 log_err("FAIL: ucal_getDSTSavings(PST) => %d, expect %d\n", i, 1*60*60*1000);
242 }
243
244 /*Test ucal_set/getDefaultTimeZone and ucal_getHostTimeZone */
245 status = U_ZERO_ERROR;
246 i = ucal_getDefaultTimeZone(zone1, UPRV_LENGTHOF(zone1), &status);
247 if (U_FAILURE(status) || status == U_STRING_NOT_TERMINATED_WARNING) {
248 log_err("FAIL: ucal_getDefaultTimeZone() => %s\n",
249 u_errorName(status));
250 } else {
251 ucal_setDefaultTimeZone(EUROPE_PARIS, &status);
252 if (U_FAILURE(status)) {
253 log_err("FAIL: ucal_setDefaultTimeZone(Europe/Paris) => %s\n",
254 u_errorName(status));
255 } else {
256 i = ucal_getDefaultTimeZone(zone2, UPRV_LENGTHOF(zone2), &status);
257 if (U_FAILURE(status)) {
258 log_err("FAIL: ucal_getDefaultTimeZone() => %s\n",
259 u_errorName(status));
260 } else {
261 if (u_strcmp(zone2, EUROPE_PARIS) != 0) {
262 log_data_err("FAIL: ucal_getDefaultTimeZone() did not return Europe/Paris (Are you missing data?)\n");
263 } else {
264 // Redetect the host timezone, it should be the same as zone1 even though ICU's default timezone has been changed.
265 i = ucal_getHostTimeZone(zone2, UPRV_LENGTHOF(zone2), &status);
266 if (U_FAILURE(status) || status == U_STRING_NOT_TERMINATED_WARNING) {
267 log_err("FAIL: ucal_getHostTimeZone() => %s\n", u_errorName(status));
268 } else {
269 if (u_strcmp(zone1, zone2) != 0) {
270 log_err("FAIL: ucal_getHostTimeZone() should give the same host timezone even if the default changed. (Got '%s', Expected '%s').\n",
271 u_austrcpy(tempMsgBuf, zone2), u_austrcpy(tempMsgBuf2, zone1));
272 }
273 }
274 }
275 }
276 }
277 status = U_ZERO_ERROR;
278 ucal_setDefaultTimeZone(zone1, &status);
279 }
280
281 /*Test ucal_getTZDataVersion*/
282 status = U_ZERO_ERROR;
283 tzver = ucal_getTZDataVersion(&status);
284 if (U_FAILURE(status)) {
285 log_err_status(status, "FAIL: ucal_getTZDataVersion() => %s\n", u_errorName(status));
286 } else {
287 tzverLen = uprv_strlen(tzver);
288 if (tzverLen == 5 || tzverLen == 6 /* 4 digits + 1 or 2 letters */) {
289 log_verbose("PASS: ucal_getTZDataVersion returned %s\n", tzver);
290 } else {
291 log_err("FAIL: Bad version string was returned by ucal_getTZDataVersion\n");
292 }
293 }
294
295 /*Testing ucal_getCanonicalTimeZoneID*/
296 status = U_ZERO_ERROR;
297 resultlength = ucal_getCanonicalTimeZoneID(PST, -1,
298 canonicalID, UPRV_LENGTHOF(canonicalID), &isSystemID, &status);
299 if (U_FAILURE(status)) {
300 log_data_err("FAIL: error in ucal_getCanonicalTimeZoneID : %s\n", u_errorName(status));
301 } else {
302 if (u_strcmp(AMERICA_LOS_ANGELES, canonicalID) != 0) {
303 log_data_err("FAIL: ucal_getCanonicalTimeZoneID(%s) returned %s : expected - %s (Are you missing data?)\n",
304 PST, canonicalID, AMERICA_LOS_ANGELES);
305 }
306 if (!isSystemID) {
307 log_data_err("FAIL: ucal_getCanonicalTimeZoneID(%s) set %d to isSystemID (Are you missing data?)\n",
308 PST, isSystemID);
309 }
310 }
311
312 /*Testing the ucal_open() function*/
313 status = U_ZERO_ERROR;
314 log_verbose("\nTesting the ucal_open()\n");
315 u_uastrcpy(tzID, "PST");
316 caldef=ucal_open(tzID, u_strlen(tzID), "en_US", UCAL_TRADITIONAL, &status);
317 if(U_FAILURE(status)){
318 log_data_err("FAIL: error in ucal_open caldef : %s\n - (Are you missing data?)", u_errorName(status));
319 }
320
321 caldef2=ucal_open(tzID, u_strlen(tzID), "en_US", UCAL_TRADITIONAL, &status);
322 if(U_FAILURE(status)){
323 log_data_err("FAIL: error in ucal_open caldef : %s - (Are you missing data?)\n", u_errorName(status));
324 }
325 u_strcpy(tzID, fgGMTID);
326 calfr=ucal_open(tzID, u_strlen(tzID), "fr_FR", UCAL_TRADITIONAL, &status);
327 if(U_FAILURE(status)){
328 log_data_err("FAIL: error in ucal_open calfr : %s - (Are you missing data?)\n", u_errorName(status));
329 }
330 calit=ucal_open(tzID, u_strlen(tzID), "it_IT", UCAL_TRADITIONAL, &status);
331 if(U_FAILURE(status)) {
332 log_data_err("FAIL: error in ucal_open calit : %s - (Are you missing data?)\n", u_errorName(status));
333 }
334
335 /*Testing the clone() function*/
336 calfrclone = ucal_clone(calfr, &status);
337 if(U_FAILURE(status)){
338 log_data_err("FAIL: error in ucal_clone calfr : %s - (Are you missing data?)\n", u_errorName(status));
339 }
340
341 /*Testing udat_getAvailable() and udat_countAvailable()*/
342 log_verbose("\nTesting getAvailableLocales and countAvailable()\n");
343 count=ucal_countAvailable();
344 /* use something sensible w/o hardcoding the count */
345 if(count > 0) {
346 log_verbose("PASS: ucal_countAvailable() works fine\n");
347 log_verbose("The no: of locales for which calendars are available are %d\n", count);
348 } else {
349 log_data_err("FAIL: Error in countAvailable()\n");
350 }
351
352 for(i=0;i<count;i++) {
353 log_verbose("%s\n", ucal_getAvailable(i));
354 }
355
356
357 /*Testing the equality between calendar's*/
358 log_verbose("\nTesting ucal_equivalentTo()\n");
359 if(caldef && caldef2 && calfr && calit) {
360 if(ucal_equivalentTo(caldef, caldef2) == false || ucal_equivalentTo(caldef, calfr)== true ||
361 ucal_equivalentTo(caldef, calit)== true || ucal_equivalentTo(calfr, calfrclone) == false) {
362 log_data_err("FAIL: Error. equivalentTo test failed (Are you missing data?)\n");
363 } else {
364 log_verbose("PASS: equivalentTo test passed\n");
365 }
366 }
367
368
369 /*Testing the current time and date using ucal_getnow()*/
370 log_verbose("\nTesting the ucal_getNow function to check if it is fetching tbe current time\n");
371 now=ucal_getNow();
372 /* open the date format and format the date to check the output */
373 datdef=udat_open(UDAT_FULL,UDAT_FULL ,NULL, NULL, 0,NULL,0,&status);
374 if(U_FAILURE(status)){
375 log_data_err("FAIL: error in creating the dateformat : %s (Are you missing data?)\n", u_errorName(status));
376 ucal_close(caldef2);
377 ucal_close(calfr);
378 ucal_close(calit);
379 ucal_close(calfrclone);
380 ucal_close(caldef);
381 return;
382 }
383 log_verbose("PASS: The current date and time fetched is %s\n", u_austrcpy(tempMsgBuf, myDateFormat(datdef, now)) );
384
385
386 /*Testing the TimeZoneDisplayName */
387 log_verbose("\nTesting the fetching of time zone display name\n");
388 /*for the US locale */
389 resultlength=0;
390 resultlengthneeded=ucal_getTimeZoneDisplayName(caldef, UCAL_DST, "en_US", NULL, resultlength, &status);
391
392 if(status==U_BUFFER_OVERFLOW_ERROR)
393 {
394 status=U_ZERO_ERROR;
395 resultlength=resultlengthneeded+1;
396 result=(UChar*)malloc(sizeof(UChar) * resultlength);
397 ucal_getTimeZoneDisplayName(caldef, UCAL_DST, "en_US", result, resultlength, &status);
398 }
399 if(U_FAILURE(status)) {
400 log_err("FAIL: Error in getting the timezone display name : %s\n", u_errorName(status));
401 }
402 else{
403 log_verbose("PASS: getting the time zone display name successful : %s, %d needed \n",
404 u_errorName(status), resultlengthneeded);
405 }
406
407
408 #define expectPDT "Pacific Daylight Time"
409
410 tzdname=(UChar*)malloc(sizeof(UChar) * (sizeof(expectPDT)+1));
411 u_uastrcpy(tzdname, expectPDT);
412 if(u_strcmp(tzdname, result)==0){
413 log_verbose("PASS: got the correct time zone display name %s\n", u_austrcpy(tempMsgBuf, result) );
414 }
415 else{
416 log_err("FAIL: got the wrong time zone(DST) display name %s, wanted %s\n", austrdup(result) , expectPDT);
417 }
418
419 ucal_getTimeZoneDisplayName(caldef, UCAL_SHORT_DST, "en_US", result, resultlength, &status);
420 u_uastrcpy(tzdname, "PDT");
421 if(u_strcmp(tzdname, result) != 0){
422 log_err("FAIL: got the wrong time zone(SHORT_DST) display name %s, wanted %s\n", austrdup(result), austrdup(tzdname));
423 }
424
425 ucal_getTimeZoneDisplayName(caldef, UCAL_STANDARD, "en_US", result, resultlength, &status);
426 u_uastrcpy(tzdname, "Pacific Standard Time");
427 if(u_strcmp(tzdname, result) != 0){
428 log_err("FAIL: got the wrong time zone(STANDARD) display name %s, wanted %s\n", austrdup(result), austrdup(tzdname));
429 }
430
431 ucal_getTimeZoneDisplayName(caldef, UCAL_SHORT_STANDARD, "en_US", result, resultlength, &status);
432 u_uastrcpy(tzdname, "PST");
433 if(u_strcmp(tzdname, result) != 0){
434 log_err("FAIL: got the wrong time zone(SHORT_STANDARD) display name %s, wanted %s\n", austrdup(result), austrdup(tzdname));
435 }
436
437
438 /*testing the setAttributes and getAttributes of a UCalendar*/
439 log_verbose("\nTesting the getAttributes and set Attributes\n");
440 count=ucal_getAttribute(calit, UCAL_LENIENT);
441 count2=ucal_getAttribute(calfr, UCAL_LENIENT);
442 ucal_setAttribute(calit, UCAL_LENIENT, 0);
443 ucal_setAttribute(caldef, UCAL_LENIENT, count2);
444 if( ucal_getAttribute(calit, UCAL_LENIENT) !=0 ||
445 ucal_getAttribute(calfr, UCAL_LENIENT)!=ucal_getAttribute(caldef, UCAL_LENIENT) )
446 log_err("FAIL: there is an error in getAttributes or setAttributes\n");
447 else
448 log_verbose("PASS: attribute set and got successfully\n");
449 /*set it back to original value */
450 log_verbose("Setting it back to normal\n");
451 ucal_setAttribute(calit, UCAL_LENIENT, count);
452 if(ucal_getAttribute(calit, UCAL_LENIENT)!=count)
453 log_err("FAIL: Error in setting the attribute back to normal\n");
454
455 /*setting the first day of the week to other values */
456 count=ucal_getAttribute(calit, UCAL_FIRST_DAY_OF_WEEK);
457 for (i=1; i<=7; ++i) {
458 ucal_setAttribute(calit, UCAL_FIRST_DAY_OF_WEEK,i);
459 if (ucal_getAttribute(calit, UCAL_FIRST_DAY_OF_WEEK) != i)
460 log_err("FAIL: set/getFirstDayOfWeek failed\n");
461 }
462 /*get bogus Attribute*/
463 count=ucal_getAttribute(calit, (UCalendarAttribute)99); /* BOGUS_ATTRIBUTE */
464 if(count != -1){
465 log_err("FAIL: get/bogus attribute should return -1\n");
466 }
467
468 /*set it back to normal */
469 ucal_setAttribute(calit, UCAL_FIRST_DAY_OF_WEEK,count);
470 /*setting minimal days of the week to other values */
471 count=ucal_getAttribute(calit, UCAL_MINIMAL_DAYS_IN_FIRST_WEEK);
472 for (i=1; i<=7; ++i) {
473 ucal_setAttribute(calit, UCAL_MINIMAL_DAYS_IN_FIRST_WEEK,i);
474 if (ucal_getAttribute(calit, UCAL_MINIMAL_DAYS_IN_FIRST_WEEK) != i)
475 log_err("FAIL: set/getMinimalDaysInFirstWeek failed\n");
476 }
477 /*set it back to normal */
478 ucal_setAttribute(calit, UCAL_MINIMAL_DAYS_IN_FIRST_WEEK,count);
479
480
481 /*testing if the UCalendar's timezone is currently in day light saving's time*/
482 log_verbose("\nTesting if the UCalendar is currently in daylight saving's time\n");
483 ucal_setDateTime(caldef, 1999, UCAL_MARCH, 3, 10, 45, 20, &status);
484 ucal_inDaylightTime(caldef, &status );
485 if(U_FAILURE(status)) {
486 log_err("Error in ucal_inDaylightTime: %s\n", u_errorName(status));
487 }
488 if(!ucal_inDaylightTime(caldef, &status))
489 log_verbose("PASS: It is not in daylight saving's time\n");
490 else
491 log_err("FAIL: It is not in daylight saving's time\n");
492
493 /*closing the UCalendar*/
494 ucal_close(caldef);
495 ucal_close(caldef2);
496 ucal_close(calfr);
497 ucal_close(calit);
498 ucal_close(calfrclone);
499
500 /*testing ucal_getType, and ucal_open with UCAL_GREGORIAN*/
501 for (ucalGetTypeTestPtr = ucalGetTypeTests; ucalGetTypeTestPtr->expectedResult != NULL; ++ucalGetTypeTestPtr) {
502 const char * localeToDisplay = (ucalGetTypeTestPtr->locale != NULL)? ucalGetTypeTestPtr->locale: "<NULL>";
503 status = U_ZERO_ERROR;
504 caldef = ucal_open(NULL, 0, ucalGetTypeTestPtr->locale, ucalGetTypeTestPtr->calType, &status);
505 if ( U_SUCCESS(status) ) {
506 const char * calType = ucal_getType(caldef, &status);
507 if ( U_SUCCESS(status) && calType != NULL ) {
508 if ( uprv_strcmp( calType, ucalGetTypeTestPtr->expectedResult ) != 0 ) {
509 log_err("FAIL: ucal_open %s type %d does not return %s calendar\n", localeToDisplay,
510 ucalGetTypeTestPtr->calType, ucalGetTypeTestPtr->expectedResult);
511 }
512 } else {
513 log_err("FAIL: ucal_open %s type %d, then ucal_getType fails\n", localeToDisplay, ucalGetTypeTestPtr->calType);
514 }
515 ucal_close(caldef);
516 } else {
517 log_err("FAIL: ucal_open %s type %d fails\n", localeToDisplay, ucalGetTypeTestPtr->calType);
518 }
519 }
520
521 /*closing the UDateFormat used */
522 udat_close(datdef);
523 free(result);
524 free(tzdname);
525 }
526
527 /*------------------------------------------------------*/
528 /*Testing the getMillis, setMillis, setDate and setDateTime functions extensively*/
529
TestGetSetDateAPI()530 static void TestGetSetDateAPI()
531 {
532 UCalendar *caldef = 0, *caldef2 = 0, *caldef3 = 0;
533 UChar tzID[4];
534 UDate d1;
535 int32_t hour;
536 int32_t zoneOffset;
537 UDateFormat *datdef = 0;
538 UErrorCode status=U_ZERO_ERROR;
539 UDate d2= 837039928046.0;
540 UChar temp[30];
541 double testMillis;
542 int32_t dateBit;
543 UChar id[4];
544 int32_t idLen;
545
546 log_verbose("\nOpening the calendars()\n");
547 u_strcpy(tzID, fgGMTID);
548 /*open the calendars used */
549 caldef=ucal_open(tzID, u_strlen(tzID), "en_US", UCAL_TRADITIONAL, &status);
550 caldef2=ucal_open(tzID, u_strlen(tzID), "en_US", UCAL_TRADITIONAL, &status);
551 caldef3=ucal_open(tzID, u_strlen(tzID), "en_US", UCAL_TRADITIONAL, &status);
552 /*open the dateformat */
553 /* this is supposed to open default date format, but later on it treats it like it is "en_US"
554 - very bad if you try to run the tests on machine where default locale is NOT "en_US" */
555 /*datdef=udat_open(UDAT_DEFAULT,UDAT_DEFAULT ,NULL,fgGMTID,-1, &status);*/
556 datdef=udat_open(UDAT_DEFAULT,UDAT_DEFAULT ,"en_US",fgGMTID,-1,NULL,0, &status);
557 if(U_FAILURE(status))
558 {
559 log_data_err("error in creating the dateformat : %s (Are you missing data?)\n", u_errorName(status));
560 ucal_close(caldef);
561 ucal_close(caldef2);
562 ucal_close(caldef3);
563 udat_close(datdef);
564 return;
565 }
566
567 /*Testing getMillis and setMillis */
568 log_verbose("\nTesting the date and time fetched in millis for a calendar using getMillis\n");
569 d1=ucal_getMillis(caldef, &status);
570 if(U_FAILURE(status)){
571 log_err("Error in getMillis : %s\n", u_errorName(status));
572 }
573
574 /*testing setMillis */
575 log_verbose("\nTesting the set date and time function using setMillis\n");
576 ucal_setMillis(caldef, d2, &status);
577 if(U_FAILURE(status)){
578 log_err("Error in setMillis : %s\n", u_errorName(status));
579 }
580
581 /*testing if the calendar date is set properly or not */
582 d1=ucal_getMillis(caldef, &status);
583 if(u_strcmp(myDateFormat(datdef, d1), myDateFormat(datdef, d2))!=0)
584 log_err("error in setMillis or getMillis\n");
585 /*-------------------*/
586
587 /*testing large negative millis*/
588 /*test a previously failed millis and beyond the lower bounds - ICU trac #9403 */
589 // -184303902611600000.0 - just beyond lower bounds (#9403 sets U_ILLEGAL_ARGUMENT_ERROR in strict mode)
590 // -46447814188001000.0 - fixed by #9403
591
592 log_verbose("\nTesting very large valid millis & invalid setMillis values (in both strict & lienent modes) detected\n");
593
594 testMillis = -46447814188001000.0; // point where floorDivide in handleComputeFields failed as per #9403
595 log_verbose("using value[%lf]\n", testMillis);
596 ucal_setAttribute(caldef3, UCAL_LENIENT, 0);
597 ucal_setMillis(caldef3, testMillis, &status);
598 if(U_FAILURE(status)){
599 log_err("Fail: setMillis incorrectly detected invalid value : for millis : %e : returned : %s\n", testMillis, u_errorName(status));
600 status = U_ZERO_ERROR;
601 }
602
603 log_verbose("\nTesting invalid setMillis values detected\n");
604 testMillis = -184303902611600000.0;
605 log_verbose("using value[%lf]\n", testMillis);
606 ucal_setAttribute(caldef3, UCAL_LENIENT, 1);
607 ucal_setMillis(caldef3, testMillis, &status);
608 if(U_FAILURE(status)){
609 log_err("Fail: setMillis incorrectly detected invalid value : for millis : %e : returned : %s\n", testMillis, u_errorName(status));
610 status = U_ZERO_ERROR;
611 } else {
612 dateBit = ucal_get(caldef2, UCAL_MILLISECOND, &status);
613 if(testMillis == dateBit)
614 {
615 log_err("Fail: error in setMillis, allowed invalid value %e : returns millisecond : %d", testMillis, dateBit);
616 } else {
617 log_verbose("Pass: setMillis correctly pinned min, returned : %d", dateBit);
618 }
619 }
620
621 log_verbose("\nTesting invalid setMillis values detected\n");
622 testMillis = -184303902611600000.0;
623 log_verbose("using value[%lf]\n", testMillis);
624 ucal_setAttribute(caldef3, UCAL_LENIENT, 0);
625 ucal_setMillis(caldef3, testMillis, &status);
626 if(U_FAILURE(status)){
627 log_verbose("Pass: Illegal argument error as expected : for millis : %e : returned : %s\n", testMillis, u_errorName(status));
628 status = U_ZERO_ERROR;
629 } else {
630 dateBit = ucal_get(caldef3, UCAL_DAY_OF_MONTH, &status);
631 log_err("Fail: error in setMillis, allowed invalid value %e : returns DayOfMonth : %d", testMillis, dateBit);
632 }
633 /*-------------------*/
634
635
636 ctest_setTimeZone(NULL, &status);
637
638 /*testing ucal_setTimeZone() and ucal_getTimeZoneID function*/
639 log_verbose("\nTesting if the function ucal_setTimeZone() and ucal_getTimeZoneID work fine\n");
640 idLen = ucal_getTimeZoneID(caldef2, id, UPRV_LENGTHOF(id), &status);
641 (void)idLen; /* Suppress set but not used warning. */
642 if (U_FAILURE(status)) {
643 log_err("Error in getTimeZoneID : %s\n", u_errorName(status));
644 } else if (u_strcmp(id, fgGMTID) != 0) {
645 log_err("FAIL: getTimeZoneID returns a wrong ID: actual=%d, expected=%s\n", austrdup(id), austrdup(fgGMTID));
646 } else {
647 log_verbose("PASS: getTimeZoneID works fine\n");
648 }
649
650 ucal_setMillis(caldef2, d2, &status);
651 if(U_FAILURE(status)){
652 log_err("Error in getMillis : %s\n", u_errorName(status));
653 }
654 hour=ucal_get(caldef2, UCAL_HOUR_OF_DAY, &status);
655
656 u_uastrcpy(tzID, "PST");
657 ucal_setTimeZone(caldef2,tzID, 3, &status);
658 if(U_FAILURE(status)){
659 log_err("Error in setting the time zone using ucal_setTimeZone(): %s\n", u_errorName(status));
660 }
661 else
662 log_verbose("ucal_setTimeZone worked fine\n");
663
664 idLen = ucal_getTimeZoneID(caldef2, id, UPRV_LENGTHOF(id), &status);
665 if (U_FAILURE(status)) {
666 log_err("Error in getTimeZoneID : %s\n", u_errorName(status));
667 } else if (u_strcmp(id, tzID) != 0) {
668 log_err("FAIL: getTimeZoneID returns a wrong ID: actual=%d, expected=%s\n", austrdup(id), austrdup(tzID));
669 } else {
670 log_verbose("PASS: getTimeZoneID works fine\n");
671 }
672
673 if(hour == ucal_get(caldef2, UCAL_HOUR_OF_DAY, &status))
674 log_err("FAIL: Error setting the time zone doesn't change the represented time\n");
675 else if((hour-8 + 1) != ucal_get(caldef2, UCAL_HOUR_OF_DAY, &status)) /*because it is not in daylight savings time */
676 log_err("FAIL: Error setTimeZone doesn't change the represented time correctly with 8 hour offset\n");
677 else
678 log_verbose("PASS: setTimeZone works fine\n");
679
680 /*testing setTimeZone roundtrip */
681 log_verbose("\nTesting setTimeZone() roundtrip\n");
682 u_strcpy(tzID, fgGMTID);
683 ucal_setTimeZone(caldef2, tzID, 3, &status);
684 if(U_FAILURE(status)){
685 log_err("Error in setting the time zone using ucal_setTimeZone(): %s\n", u_errorName(status));
686 }
687 if(d2==ucal_getMillis(caldef2, &status))
688 log_verbose("PASS: setTimeZone roundtrip test passed\n");
689 else
690 log_err("FAIL: setTimeZone roundtrip test failed\n");
691
692 zoneOffset = ucal_get(caldef2, UCAL_ZONE_OFFSET, &status);
693 if(U_FAILURE(status)){
694 log_err("Error in getting the time zone using ucal_get() after using ucal_setTimeZone(): %s\n", u_errorName(status));
695 }
696 else if (zoneOffset != 0) {
697 log_err("Error in getting the time zone using ucal_get() after using ucal_setTimeZone() offset=%d\n", zoneOffset);
698 }
699
700 ucal_setTimeZone(caldef2, NULL, -1, &status);
701 if(U_FAILURE(status)){
702 log_err("Error in setting the time zone using ucal_setTimeZone(): %s\n", u_errorName(status));
703 }
704 if(ucal_getMillis(caldef2, &status))
705 log_verbose("PASS: setTimeZone roundtrip test passed\n");
706 else
707 log_err("FAIL: setTimeZone roundtrip test failed\n");
708
709 zoneOffset = ucal_get(caldef2, UCAL_ZONE_OFFSET, &status);
710 if(U_FAILURE(status)){
711 log_err("Error in getting the time zone using ucal_get() after using ucal_setTimeZone(): %s\n", u_errorName(status));
712 }
713 else if (zoneOffset != -28800000) {
714 log_err("Error in getting the time zone using ucal_get() after using ucal_setTimeZone() offset=%d\n", zoneOffset);
715 }
716
717 ctest_resetTimeZone();
718
719 /*----------------------------* */
720
721
722
723 /*Testing if setDate works fine */
724 log_verbose("\nTesting the ucal_setDate() function \n");
725 u_strcpy(temp, u"Dec 17, 1971, 11:05:28\u202FPM");
726 ucal_setDate(caldef,1971, UCAL_DECEMBER, 17, &status);
727 if(U_FAILURE(status)){
728 log_err("error in setting the calendar date : %s\n", u_errorName(status));
729 }
730 /*checking if the calendar date is set properly or not */
731 d1=ucal_getMillis(caldef, &status);
732 if(u_strcmp(myDateFormat(datdef, d1), temp)==0)
733 log_verbose("PASS:setDate works fine\n");
734 else
735 log_err("FAIL:Error in setDate()\n");
736
737
738 /* Testing setDate Extensively with various input values */
739 log_verbose("\nTesting ucal_setDate() extensively\n");
740 ucal_setDate(caldef, 1999, UCAL_JANUARY, 10, &status);
741 verify1("1999 10th day of January is :", caldef, datdef, 1999, UCAL_JANUARY, 10);
742 ucal_setDate(caldef, 1999, UCAL_DECEMBER, 3, &status);
743 verify1("1999 3rd day of December is :", caldef, datdef, 1999, UCAL_DECEMBER, 3);
744 ucal_setDate(caldef, 2000, UCAL_MAY, 3, &status);
745 verify1("2000 3rd day of May is :", caldef, datdef, 2000, UCAL_MAY, 3);
746 ucal_setDate(caldef, 1999, UCAL_AUGUST, 32, &status);
747 verify1("1999 32th day of August is :", caldef, datdef, 1999, UCAL_SEPTEMBER, 1);
748 ucal_setDate(caldef, 1999, UCAL_MARCH, 0, &status);
749 verify1("1999 0th day of March is :", caldef, datdef, 1999, UCAL_FEBRUARY, 28);
750 ucal_setDate(caldef, 0, UCAL_MARCH, 12, &status);
751
752 /*--------------------*/
753
754 /*Testing if setDateTime works fine */
755 log_verbose("\nTesting the ucal_setDateTime() function \n");
756 u_strcpy(temp, u"May 3, 1972, 4:30:42\u202FPM");
757 ucal_setDateTime(caldef,1972, UCAL_MAY, 3, 16, 30, 42, &status);
758 if(U_FAILURE(status)){
759 log_err("error in setting the calendar date : %s\n", u_errorName(status));
760 }
761 /*checking if the calendar date is set properly or not */
762 d1=ucal_getMillis(caldef, &status);
763 if(u_strcmp(myDateFormat(datdef, d1), temp)==0)
764 log_verbose("PASS: setDateTime works fine\n");
765 else
766 log_err("FAIL: Error in setDateTime\n");
767
768
769
770 /*Testing setDateTime extensively with various input values*/
771 log_verbose("\nTesting ucal_setDateTime() function extensively\n");
772 ucal_setDateTime(caldef, 1999, UCAL_OCTOBER, 10, 6, 45, 30, &status);
773 verify2("1999 10th day of October at 6:45:30 is :", caldef, datdef, 1999, UCAL_OCTOBER, 10, 6, 45, 30, 0 );
774 ucal_setDateTime(caldef, 1999, UCAL_MARCH, 3, 15, 10, 55, &status);
775 verify2("1999 3rd day of March at 15:10:55 is :", caldef, datdef, 1999, UCAL_MARCH, 3, 3, 10, 55, 1);
776 ucal_setDateTime(caldef, 1999, UCAL_MAY, 3, 25, 30, 45, &status);
777 verify2("1999 3rd day of May at 25:30:45 is :", caldef, datdef, 1999, UCAL_MAY, 4, 1, 30, 45, 0);
778 ucal_setDateTime(caldef, 1999, UCAL_AUGUST, 32, 22, 65, 40, &status);
779 verify2("1999 32th day of August at 22:65:40 is :", caldef, datdef, 1999, UCAL_SEPTEMBER, 1, 11, 5, 40,1);
780 ucal_setDateTime(caldef, 1999, UCAL_MARCH, 12, 0, 0, 0,&status);
781 verify2("1999 12th day of March at 0:0:0 is :", caldef, datdef, 1999, UCAL_MARCH, 12, 0, 0, 0, 0);
782 ucal_setDateTime(caldef, 1999, UCAL_MARCH, 12, -10, -10,0, &status);
783 verify2("1999 12th day of March is at -10:-10:0 :", caldef, datdef, 1999, UCAL_MARCH, 11, 1, 50, 0, 1);
784
785
786
787 /*close caldef and datdef*/
788 ucal_close(caldef);
789 ucal_close(caldef2);
790 ucal_close(caldef3);
791 udat_close(datdef);
792 }
793
794 /*----------------------------------------------------------- */
795 /**
796 * Confirm the functioning of the calendar field related functions.
797 */
TestFieldGetSet()798 static void TestFieldGetSet()
799 {
800 UCalendar *cal = 0;
801 UChar tzID[4];
802 UDateFormat *datdef = 0;
803 UDate d1 = 0;
804 UErrorCode status=U_ZERO_ERROR;
805 (void)d1; /* Suppress set but not used warning. */
806 log_verbose("\nFetching pointer to UCalendar using the ucal_open()\n");
807 u_strcpy(tzID, fgGMTID);
808 /*open the calendar used */
809 cal=ucal_open(tzID, u_strlen(tzID), "en_US", UCAL_TRADITIONAL, &status);
810 if (U_FAILURE(status)) {
811 log_data_err("ucal_open failed: %s - (Are you missing data?)\n", u_errorName(status));
812 return;
813 }
814 datdef=udat_open(UDAT_SHORT,UDAT_SHORT ,NULL,fgGMTID,-1,NULL, 0, &status);
815 if(U_FAILURE(status))
816 {
817 log_data_err("error in creating the dateformat : %s (Are you missing data?)\n", u_errorName(status));
818 }
819
820 /*Testing ucal_get()*/
821 log_verbose("\nTesting the ucal_get() function of Calendar\n");
822 ucal_setDateTime(cal, 1999, UCAL_MARCH, 12, 5, 25, 30, &status);
823 if(U_FAILURE(status)){
824 log_data_err("error in the setDateTime() : %s (Are you missing data?)\n", u_errorName(status));
825 }
826 if(ucal_get(cal, UCAL_YEAR, &status)!=1999 || ucal_get(cal, UCAL_MONTH, &status)!=2 ||
827 ucal_get(cal, UCAL_DATE, &status)!=12 || ucal_get(cal, UCAL_HOUR, &status)!=5)
828 log_data_err("error in ucal_get() -> %s (Are you missing data?)\n", u_errorName(status));
829 else if(ucal_get(cal, UCAL_DAY_OF_WEEK_IN_MONTH, &status)!=2 || ucal_get(cal, UCAL_DAY_OF_WEEK, &status)!=6
830 || ucal_get(cal, UCAL_WEEK_OF_MONTH, &status)!=2 || ucal_get(cal, UCAL_WEEK_OF_YEAR, &status)!= 11)
831 log_err("FAIL: error in ucal_get()\n");
832 else
833 log_verbose("PASS: ucal_get() works fine\n");
834
835 /*Testing the ucal_set() , ucal_clear() functions of calendar*/
836 log_verbose("\nTesting the set, and clear field functions of calendar\n");
837 ucal_setAttribute(cal, UCAL_LENIENT, 0);
838 ucal_clear(cal);
839 ucal_set(cal, UCAL_YEAR, 1997);
840 ucal_set(cal, UCAL_MONTH, UCAL_JUNE);
841 ucal_set(cal, UCAL_DATE, 3);
842 verify1("1997 third day of June = ", cal, datdef, 1997, UCAL_JUNE, 3);
843 ucal_clear(cal);
844 ucal_set(cal, UCAL_YEAR, 1997);
845 ucal_set(cal, UCAL_DAY_OF_WEEK, UCAL_TUESDAY);
846 ucal_set(cal, UCAL_MONTH, UCAL_JUNE);
847 ucal_set(cal, UCAL_DAY_OF_WEEK_IN_MONTH, 1);
848 verify1("1997 first Tuesday in June = ", cal, datdef, 1997, UCAL_JUNE, 3);
849 ucal_clear(cal);
850 ucal_set(cal, UCAL_YEAR, 1997);
851 ucal_set(cal, UCAL_DAY_OF_WEEK, UCAL_TUESDAY);
852 ucal_set(cal, UCAL_MONTH, UCAL_JUNE);
853 ucal_set(cal, UCAL_DAY_OF_WEEK_IN_MONTH, - 1);
854 verify1("1997 last Tuesday in June = ", cal, datdef,1997, UCAL_JUNE, 24);
855 /*give undesirable input */
856 status = U_ZERO_ERROR;
857 ucal_clear(cal);
858 ucal_set(cal, UCAL_YEAR, 1997);
859 ucal_set(cal, UCAL_DAY_OF_WEEK, UCAL_TUESDAY);
860 ucal_set(cal, UCAL_MONTH, UCAL_JUNE);
861 ucal_set(cal, UCAL_DAY_OF_WEEK_IN_MONTH, 0);
862 d1 = ucal_getMillis(cal, &status);
863 if (status != U_ILLEGAL_ARGUMENT_ERROR) {
864 log_err("FAIL: U_ILLEGAL_ARGUMENT_ERROR was not returned for : 1997 zero-th Tuesday in June\n");
865 } else {
866 log_verbose("PASS: U_ILLEGAL_ARGUMENT_ERROR as expected\n");
867 }
868 status = U_ZERO_ERROR;
869 ucal_clear(cal);
870 ucal_set(cal, UCAL_YEAR, 1997);
871 ucal_set(cal, UCAL_DAY_OF_WEEK, UCAL_TUESDAY);
872 ucal_set(cal, UCAL_MONTH, UCAL_JUNE);
873 ucal_set(cal, UCAL_WEEK_OF_MONTH, 1);
874 verify1("1997 Tuesday in week 1 of June = ", cal,datdef, 1997, UCAL_JUNE, 3);
875 ucal_clear(cal);
876 ucal_set(cal, UCAL_YEAR, 1997);
877 ucal_set(cal, UCAL_DAY_OF_WEEK, UCAL_TUESDAY);
878 ucal_set(cal, UCAL_MONTH, UCAL_JUNE);
879 ucal_set(cal, UCAL_WEEK_OF_MONTH, 5);
880 verify1("1997 Tuesday in week 5 of June = ", cal,datdef, 1997, UCAL_JULY, 1);
881 status = U_ZERO_ERROR;
882 ucal_clear(cal);
883 ucal_set(cal, UCAL_YEAR, 1997);
884 ucal_set(cal, UCAL_DAY_OF_WEEK, UCAL_TUESDAY);
885 ucal_set(cal, UCAL_MONTH, UCAL_JUNE);
886 ucal_set(cal, UCAL_WEEK_OF_MONTH, 0);
887 ucal_setAttribute(cal, UCAL_MINIMAL_DAYS_IN_FIRST_WEEK,1);
888 d1 = ucal_getMillis(cal,&status);
889 if (status != U_ILLEGAL_ARGUMENT_ERROR){
890 log_err("FAIL: U_ILLEGAL_ARGUMENT_ERROR was not returned for : 1997 Tuesday zero-th week in June\n");
891 } else {
892 log_verbose("PASS: U_ILLEGAL_ARGUMENT_ERROR as expected\n");
893 }
894 status = U_ZERO_ERROR;
895 ucal_clear(cal);
896 ucal_set(cal, UCAL_YEAR_WOY, 1997);
897 ucal_set(cal, UCAL_DAY_OF_WEEK, UCAL_TUESDAY);
898 ucal_set(cal, UCAL_WEEK_OF_YEAR, 1);
899 verify1("1997 Tuesday in week 1 of year = ", cal, datdef,1996, UCAL_DECEMBER, 31);
900 ucal_clear(cal);
901 ucal_set(cal, UCAL_YEAR, 1997);
902 ucal_set(cal, UCAL_DAY_OF_WEEK, UCAL_TUESDAY);
903 ucal_set(cal, UCAL_WEEK_OF_YEAR, 10);
904 verify1("1997 Tuesday in week 10 of year = ", cal,datdef, 1997, UCAL_MARCH, 4);
905 ucal_clear(cal);
906 ucal_set(cal, UCAL_YEAR, 1999);
907 ucal_set(cal, UCAL_DAY_OF_YEAR, 1);
908 verify1("1999 1st day of the year =", cal, datdef, 1999, UCAL_JANUARY, 1);
909 ucal_set(cal, UCAL_MONTH, -3);
910 d1 = ucal_getMillis(cal,&status);
911 if (status != U_ILLEGAL_ARGUMENT_ERROR){
912 log_err("FAIL: U_ILLEGAL_ARGUMENT_ERROR was not returned for : 1999 -3th month\n");
913 } else {
914 log_verbose("PASS: U_ILLEGAL_ARGUMENT_ERROR as expected\n");
915 }
916
917 ucal_setAttribute(cal, UCAL_LENIENT, 1);
918
919 ucal_set(cal, UCAL_MONTH, -3);
920 verify1("1999 -3th month should be", cal, datdef, 1998, UCAL_OCTOBER, 1);
921
922
923 /*testing isSet and clearField()*/
924 if(!ucal_isSet(cal, UCAL_WEEK_OF_YEAR))
925 log_err("FAIL: error in isSet\n");
926 else
927 log_verbose("PASS: isSet working fine\n");
928 ucal_clearField(cal, UCAL_WEEK_OF_YEAR);
929 if(ucal_isSet(cal, UCAL_WEEK_OF_YEAR))
930 log_err("FAIL: there is an error in clearField or isSet\n");
931 else
932 log_verbose("PASS :clearField working fine\n");
933
934 /*-------------------------------*/
935
936 ucal_close(cal);
937 udat_close(datdef);
938 }
939
940 typedef struct {
941 const char * zone;
942 int32_t year;
943 int32_t month;
944 int32_t day;
945 int32_t hour;
946 } TransitionItem;
947
948 static const TransitionItem transitionItems[] = {
949 { "America/Caracas", 2007, UCAL_DECEMBER, 8, 10 }, /* day before change in UCAL_ZONE_OFFSET */
950 { "US/Pacific", 2011, UCAL_MARCH, 12, 10 }, /* day before change in UCAL_DST_OFFSET */
951 { NULL, 0, 0, 0, 0 }
952 };
953
954 /* ------------------------------------- */
955 /**
956 * Execute adding and rolling in Calendar extensively,
957 */
TestAddRollExtensive()958 static void TestAddRollExtensive()
959 {
960 const TransitionItem * itemPtr;
961 UCalendar *cal = 0;
962 int32_t i,limit;
963 UChar tzID[32];
964 UCalendarDateFields e;
965 int32_t y,m,d,hr,min,sec,ms;
966 int32_t maxlimit = 40;
967 UErrorCode status = U_ZERO_ERROR;
968 y = 1997; m = UCAL_FEBRUARY; d = 1; hr = 1; min = 1; sec = 0; ms = 0;
969
970 log_verbose("Testing add and roll extensively\n");
971
972 u_uastrcpy(tzID, "PST");
973 /*open the calendar used */
974 cal=ucal_open(tzID, u_strlen(tzID), "en_US", UCAL_GREGORIAN, &status);
975 if (U_FAILURE(status)) {
976 log_data_err("ucal_open() failed : %s - (Are you missing data?)\n", u_errorName(status));
977 return;
978 }
979
980 ucal_set(cal, UCAL_YEAR, y);
981 ucal_set(cal, UCAL_MONTH, m);
982 ucal_set(cal, UCAL_DATE, d);
983 ucal_setAttribute(cal, UCAL_MINIMAL_DAYS_IN_FIRST_WEEK,1);
984
985 /* Confirm that adding to various fields works.*/
986 log_verbose("\nTesting to confirm that adding to various fields works with ucal_add()\n");
987 checkDate(cal, y, m, d);
988 ucal_add(cal,UCAL_YEAR, 1, &status);
989 if (U_FAILURE(status)) { log_err("ucal_add failed: %s\n", u_errorName(status)); return; }
990 y++;
991 checkDate(cal, y, m, d);
992 ucal_add(cal,UCAL_MONTH, 12, &status);
993 if (U_FAILURE(status)) { log_err("ucal_add failed: %s\n", u_errorName(status) ); return; }
994 y+=1;
995 checkDate(cal, y, m, d);
996 ucal_add(cal,UCAL_DATE, 1, &status);
997 if (U_FAILURE(status)) { log_err("ucal_add failed: %s\n", u_errorName(status) ); return; }
998 d++;
999 checkDate(cal, y, m, d);
1000 ucal_add(cal,UCAL_DATE, 2, &status);
1001 if (U_FAILURE(status)) { log_err("ucal_add failed: %s\n", u_errorName(status) ); return; }
1002 d += 2;
1003 checkDate(cal, y, m, d);
1004 ucal_add(cal,UCAL_DATE, 28, &status);
1005 if (U_FAILURE(status)) { log_err("ucal_add failed: %s\n", u_errorName(status) ); return; }
1006 ++m;
1007 checkDate(cal, y, m, d);
1008 ucal_add(cal, (UCalendarDateFields)-1, 10, &status);
1009 if(status==U_ILLEGAL_ARGUMENT_ERROR)
1010 log_verbose("Pass: Illegal argument error as expected\n");
1011 else{
1012 log_err("Fail: No, illegal argument error as expected. Got....: %s\n", u_errorName(status));
1013 }
1014 status=U_ZERO_ERROR;
1015
1016
1017 /*confirm that applying roll to various fields works fine*/
1018 log_verbose("\nTesting to confirm that ucal_roll() works\n");
1019 ucal_roll(cal, UCAL_DATE, -1, &status);
1020 if (U_FAILURE(status)) { log_err("ucal_roll failed: %s\n", u_errorName(status) ); return; }
1021 d -=1;
1022 checkDate(cal, y, m, d);
1023 ucal_roll(cal, UCAL_MONTH, -2, &status);
1024 if (U_FAILURE(status)) { log_err("ucal_roll failed: %s\n", u_errorName(status) ); return; }
1025 m -=2;
1026 checkDate(cal, y, m, d);
1027 ucal_roll(cal, UCAL_DATE, 1, &status);
1028 if (U_FAILURE(status)) { log_err("ucal_roll failed: %s\n", u_errorName(status) ); return; }
1029 d +=1;
1030 checkDate(cal, y, m, d);
1031 ucal_roll(cal, UCAL_MONTH, -12, &status);
1032 if (U_FAILURE(status)) { log_err("ucal_roll failed: %s\n", u_errorName(status) ); return; }
1033 checkDate(cal, y, m, d);
1034 ucal_roll(cal, UCAL_YEAR, -1, &status);
1035 if (U_FAILURE(status)) { log_err("ucal_roll failed: %s\n", u_errorName(status) ); return; }
1036 y -=1;
1037 checkDate(cal, y, m, d);
1038 ucal_roll(cal, UCAL_DATE, 29, &status);
1039 if (U_FAILURE(status)) { log_err("ucal_roll failed: %s\n", u_errorName(status) ); return; }
1040 d = 2;
1041 checkDate(cal, y, m, d);
1042 ucal_roll(cal, (UCalendarDateFields)-1, 10, &status);
1043 if(status==U_ILLEGAL_ARGUMENT_ERROR)
1044 log_verbose("Pass: illegal argument error as expected\n");
1045 else{
1046 log_err("Fail: no illegal argument error got..: %s\n", u_errorName(status));
1047 return;
1048 }
1049 status=U_ZERO_ERROR;
1050 ucal_clear(cal);
1051 ucal_setDateTime(cal, 1999, UCAL_FEBRUARY, 28, 10, 30, 45, &status);
1052 if(U_FAILURE(status)){
1053 log_err("error is setting the datetime: %s\n", u_errorName(status));
1054 }
1055 ucal_add(cal, UCAL_MONTH, 1, &status);
1056 checkDate(cal, 1999, UCAL_MARCH, 28);
1057 ucal_add(cal, UCAL_MILLISECOND, 1000, &status);
1058 checkDateTime(cal, 1999, UCAL_MARCH, 28, 10, 30, 46, 0, UCAL_MILLISECOND);
1059
1060 ucal_close(cal);
1061 /*--------------- */
1062 status=U_ZERO_ERROR;
1063 /* Testing add and roll extensively */
1064 log_verbose("\nTesting the ucal_add() and ucal_roll() functions extensively\n");
1065 y = 1997; m = UCAL_FEBRUARY; d = 1; hr = 1; min = 1; sec = 0; ms = 0;
1066 cal=ucal_open(tzID, u_strlen(tzID), "en_US", UCAL_TRADITIONAL, &status);
1067 if (U_FAILURE(status)) {
1068 log_err("ucal_open failed: %s\n", u_errorName(status));
1069 return;
1070 }
1071 ucal_set(cal, UCAL_YEAR, y);
1072 ucal_set(cal, UCAL_MONTH, m);
1073 ucal_set(cal, UCAL_DATE, d);
1074 ucal_set(cal, UCAL_HOUR, hr);
1075 ucal_set(cal, UCAL_MINUTE, min);
1076 ucal_set(cal, UCAL_SECOND,sec);
1077 ucal_set(cal, UCAL_MILLISECOND, ms);
1078 ucal_setAttribute(cal, UCAL_MINIMAL_DAYS_IN_FIRST_WEEK,1);
1079 status=U_ZERO_ERROR;
1080
1081 log_verbose("\nTesting UCalendar add...\n");
1082 for(e = UCAL_YEAR;e < UCAL_FIELD_COUNT; e=(UCalendarDateFields)((int32_t)e + 1)) {
1083 limit = maxlimit;
1084 status = U_ZERO_ERROR;
1085 for (i = 0; i < limit; i++) {
1086 ucal_add(cal, e, 1, &status);
1087 if (U_FAILURE(status)) { limit = i; status = U_ZERO_ERROR; }
1088 }
1089 for (i = 0; i < limit; i++) {
1090 ucal_add(cal, e, -1, &status);
1091 if (U_FAILURE(status)) {
1092 log_err("ucal_add -1 failed: %s\n", u_errorName(status));
1093 return;
1094 }
1095 }
1096 checkDateTime(cal, y, m, d, hr, min, sec, ms, e);
1097 }
1098 log_verbose("\nTesting calendar ucal_roll()...\n");
1099 for(e = UCAL_YEAR;e < UCAL_FIELD_COUNT; e=(UCalendarDateFields)((int32_t)e + 1)) {
1100 limit = maxlimit;
1101 status = U_ZERO_ERROR;
1102 for (i = 0; i < limit; i++) {
1103 ucal_roll(cal, e, 1, &status);
1104 if (U_FAILURE(status)) {
1105 limit = i;
1106 status = U_ZERO_ERROR;
1107 }
1108 }
1109 for (i = 0; i < limit; i++) {
1110 ucal_roll(cal, e, -1, &status);
1111 if (U_FAILURE(status)) {
1112 log_err("ucal_roll -1 failed: %s\n", u_errorName(status));
1113 return;
1114 }
1115 }
1116 checkDateTime(cal, y, m, d, hr, min, sec, ms, e);
1117 }
1118
1119 ucal_close(cal);
1120 /*--------------- */
1121 log_verbose("\nTesting ucal_add() across ZONE_OFFSET and DST_OFFSE transitions.\n");
1122 for (itemPtr = transitionItems; itemPtr->zone != NULL; itemPtr++) {
1123 status=U_ZERO_ERROR;
1124 u_uastrcpy(tzID, itemPtr->zone);
1125 cal=ucal_open(tzID, u_strlen(tzID), "en_US", UCAL_GREGORIAN, &status);
1126 if (U_FAILURE(status)) {
1127 log_err("ucal_open failed for zone %s: %s\n", itemPtr->zone, u_errorName(status));
1128 continue;
1129 }
1130 ucal_setDateTime(cal, itemPtr->year, itemPtr->month, itemPtr->day, itemPtr->hour, 0, 0, &status);
1131 ucal_add(cal, UCAL_DATE, 1, &status);
1132 hr = ucal_get(cal, UCAL_HOUR_OF_DAY, &status);
1133 if ( U_FAILURE(status) ) {
1134 log_err("ucal_add failed adding day across transition for zone %s: %s\n", itemPtr->zone, u_errorName(status));
1135 } else if ( hr != itemPtr->hour ) {
1136 log_err("ucal_add produced wrong hour %d when adding day across transition for zone %s\n", hr, itemPtr->zone);
1137 } else {
1138 ucal_add(cal, UCAL_DATE, -1, &status);
1139 hr = ucal_get(cal, UCAL_HOUR_OF_DAY, &status);
1140 if ( U_FAILURE(status) ) {
1141 log_err("ucal_add failed subtracting day across transition for zone %s: %s\n", itemPtr->zone, u_errorName(status));
1142 } else if ( hr != itemPtr->hour ) {
1143 log_err("ucal_add produced wrong hour %d when subtracting day across transition for zone %s\n", hr, itemPtr->zone);
1144 }
1145 }
1146 ucal_close(cal);
1147 }
1148 }
1149
1150 /*------------------------------------------------------ */
1151 /*Testing the Limits for various Fields of Calendar*/
TestGetLimits()1152 static void TestGetLimits()
1153 {
1154 UCalendar *cal = 0;
1155 int32_t min, max, gr_min, le_max, ac_min, ac_max, val;
1156 UChar tzID[4];
1157 UErrorCode status = U_ZERO_ERROR;
1158
1159
1160 u_uastrcpy(tzID, "PST");
1161 /*open the calendar used */
1162 cal=ucal_open(tzID, u_strlen(tzID), "en_US", UCAL_GREGORIAN, &status);
1163 if (U_FAILURE(status)) {
1164 log_data_err("ucal_open() for gregorian calendar failed in TestGetLimits: %s - (Are you missing data?)\n", u_errorName(status));
1165 return;
1166 }
1167
1168 log_verbose("\nTesting the getLimits function for various fields\n");
1169
1170
1171
1172 ucal_setDate(cal, 1999, UCAL_MARCH, 5, &status); /* Set the date to be March 5, 1999 */
1173 val = ucal_get(cal, UCAL_DAY_OF_WEEK, &status);
1174 min = ucal_getLimit(cal, UCAL_DAY_OF_WEEK, UCAL_MINIMUM, &status);
1175 max = ucal_getLimit(cal, UCAL_DAY_OF_WEEK, UCAL_MAXIMUM, &status);
1176 if ( (min != UCAL_SUNDAY || max != UCAL_SATURDAY ) && (min > val && val > max) && (val != UCAL_FRIDAY)){
1177 log_err("FAIL: Min/max bad\n");
1178 log_err("FAIL: Day of week %d out of range\n", val);
1179 log_err("FAIL: FAIL: Day of week should be SUNDAY Got %d\n", val);
1180 }
1181 else
1182 log_verbose("getLimits successful\n");
1183
1184 val = ucal_get(cal, UCAL_DAY_OF_WEEK_IN_MONTH, &status);
1185 min = ucal_getLimit(cal, UCAL_DAY_OF_WEEK_IN_MONTH, UCAL_MINIMUM, &status);
1186 max = ucal_getLimit(cal, UCAL_DAY_OF_WEEK_IN_MONTH, UCAL_MAXIMUM, &status);
1187 if ( (min != 0 || max != 5 ) && (min > val && val > max) && (val != 1)){
1188 log_err("FAIL: Min/max bad\n");
1189 log_err("FAIL: Day of week in month %d out of range\n", val);
1190 log_err("FAIL: FAIL: Day of week in month should be SUNDAY Got %d\n", val);
1191
1192 }
1193 else
1194 log_verbose("getLimits successful\n");
1195
1196 min=ucal_getLimit(cal, UCAL_MONTH, UCAL_MINIMUM, &status);
1197 max=ucal_getLimit(cal, UCAL_MONTH, UCAL_MAXIMUM, &status);
1198 gr_min=ucal_getLimit(cal, UCAL_MONTH, UCAL_GREATEST_MINIMUM, &status);
1199 le_max=ucal_getLimit(cal, UCAL_MONTH, UCAL_LEAST_MAXIMUM, &status);
1200 ac_min=ucal_getLimit(cal, UCAL_MONTH, UCAL_ACTUAL_MINIMUM, &status);
1201 ac_max=ucal_getLimit(cal, UCAL_MONTH, UCAL_ACTUAL_MAXIMUM, &status);
1202 if(U_FAILURE(status)){
1203 log_err("Error in getLimits: %s\n", u_errorName(status));
1204 }
1205 if(min!=0 || max!=11 || gr_min!=0 || le_max!=11 || ac_min!=0 || ac_max!=11)
1206 log_err("There is and error in getLimits in fetching the values\n");
1207 else
1208 log_verbose("getLimits successful\n");
1209
1210 ucal_setDateTime(cal, 1999, UCAL_MARCH, 5, 4, 10, 35, &status);
1211 val=ucal_get(cal, UCAL_HOUR_OF_DAY, &status);
1212 min=ucal_getLimit(cal, UCAL_HOUR_OF_DAY, UCAL_MINIMUM, &status);
1213 max=ucal_getLimit(cal, UCAL_HOUR_OF_DAY, UCAL_MAXIMUM, &status);
1214 gr_min=ucal_getLimit(cal, UCAL_MINUTE, UCAL_GREATEST_MINIMUM, &status);
1215 le_max=ucal_getLimit(cal, UCAL_MINUTE, UCAL_LEAST_MAXIMUM, &status);
1216 ac_min=ucal_getLimit(cal, UCAL_MINUTE, UCAL_ACTUAL_MINIMUM, &status);
1217 ac_max=ucal_getLimit(cal, UCAL_SECOND, UCAL_ACTUAL_MAXIMUM, &status);
1218 if( (min!=0 || max!= 11 || gr_min!=0 || le_max!=60 || ac_min!=0 || ac_max!=60) &&
1219 (min>val && val>max) && val!=4){
1220
1221 log_err("FAIL: Min/max bad\n");
1222 log_err("FAIL: Hour of Day %d out of range\n", val);
1223 log_err("FAIL: HOUR_OF_DAY should be 4 Got %d\n", val);
1224 }
1225 else
1226 log_verbose("getLimits successful\n");
1227
1228
1229 /*get BOGUS_LIMIT type*/
1230 val=ucal_getLimit(cal, UCAL_SECOND, (UCalendarLimitType)99, &status);
1231 if(val != -1){
1232 log_err("FAIL: ucal_getLimit() with BOGUS type should return -1\n");
1233 }
1234 status=U_ZERO_ERROR;
1235
1236
1237 ucal_close(cal);
1238 }
1239
1240
1241
1242 /* ------------------------------------- */
1243
1244 /**
1245 * Test that the days of the week progress properly when add is called repeatedly
1246 * for increments of 24 days.
1247 */
TestDOWProgression()1248 static void TestDOWProgression()
1249 {
1250 int32_t initialDOW, DOW, newDOW, expectedDOW;
1251 UCalendar *cal = 0;
1252 UDateFormat *datfor = 0;
1253 UDate date1;
1254 int32_t delta=24;
1255 UErrorCode status = U_ZERO_ERROR;
1256 UChar tzID[4];
1257 char tempMsgBuf[256];
1258 u_strcpy(tzID, fgGMTID);
1259 /*open the calendar used */
1260 cal=ucal_open(tzID, u_strlen(tzID), "en_US", UCAL_TRADITIONAL, &status);
1261 if (U_FAILURE(status)) {
1262 log_data_err("ucal_open failed: %s - (Are you missing data?)\n", u_errorName(status));
1263 return;
1264 }
1265
1266 datfor=udat_open(UDAT_MEDIUM,UDAT_MEDIUM ,NULL, fgGMTID,-1,NULL, 0, &status);
1267 if(U_FAILURE(status)){
1268 log_data_err("error in creating the dateformat : %s (Are you missing data?)\n", u_errorName(status));
1269 }
1270
1271
1272 ucal_setDate(cal, 1999, UCAL_JANUARY, 1, &status);
1273
1274 log_verbose("\nTesting the DOW progression\n");
1275
1276 initialDOW = ucal_get(cal, UCAL_DAY_OF_WEEK, &status);
1277 if (U_FAILURE(status)) {
1278 log_data_err("ucal_get() failed: %s (Are you missing data?)\n", u_errorName(status) );
1279 } else {
1280 newDOW = initialDOW;
1281 do {
1282 DOW = newDOW;
1283 log_verbose("DOW = %d...\n", DOW);
1284 date1=ucal_getMillis(cal, &status);
1285 if(U_FAILURE(status)){ log_err("ucal_getMiilis() failed: %s\n", u_errorName(status)); break;}
1286 log_verbose("%s\n", u_austrcpy(tempMsgBuf, myDateFormat(datfor, date1)));
1287
1288 ucal_add(cal,UCAL_DAY_OF_WEEK, delta, &status);
1289 if (U_FAILURE(status)) { log_err("ucal_add() failed: %s\n", u_errorName(status)); break; }
1290
1291 newDOW = ucal_get(cal, UCAL_DAY_OF_WEEK, &status);
1292 if (U_FAILURE(status)) { log_err("ucal_get() failed: %s\n", u_errorName(status)); break; }
1293 expectedDOW = 1 + (DOW + delta - 1) % 7;
1294 date1=ucal_getMillis(cal, &status);
1295 if(U_FAILURE(status)){ log_err("ucal_getMiilis() failed: %s\n", u_errorName(status)); break;}
1296 if (newDOW != expectedDOW) {
1297 log_err("Day of week should be %d instead of %d on %s", expectedDOW, newDOW,
1298 u_austrcpy(tempMsgBuf, myDateFormat(datfor, date1)) );
1299 break;
1300 }
1301 }
1302 while (newDOW != initialDOW);
1303 }
1304
1305 ucal_close(cal);
1306 udat_close(datfor);
1307 }
1308
1309 /* ------------------------------------- */
1310
1311 /**
1312 * Confirm that the offset between local time and GMT behaves as expected.
1313 */
TestGMTvsLocal()1314 static void TestGMTvsLocal()
1315 {
1316 log_verbose("\nTesting the offset between the GMT and local time\n");
1317 testZones(1999, 1, 1, 12, 0, 0);
1318 testZones(1999, 4, 16, 18, 30, 0);
1319 testZones(1998, 12, 17, 19, 0, 0);
1320 }
1321
1322 /* ------------------------------------- */
1323
testZones(int32_t yr,int32_t mo,int32_t dt,int32_t hr,int32_t mn,int32_t sc)1324 static void testZones(int32_t yr, int32_t mo, int32_t dt, int32_t hr, int32_t mn, int32_t sc)
1325 {
1326 int32_t offset,utc, expected;
1327 UCalendar *gmtcal = 0, *cal = 0;
1328 UDate date1;
1329 double temp;
1330 UDateFormat *datfor = 0;
1331 UErrorCode status = U_ZERO_ERROR;
1332 UChar tzID[4];
1333 char tempMsgBuf[256];
1334
1335 u_strcpy(tzID, fgGMTID);
1336 gmtcal=ucal_open(tzID, 3, "en_US", UCAL_TRADITIONAL, &status);
1337 if (U_FAILURE(status)) {
1338 log_data_err("ucal_open failed: %s - (Are you missing data?)\n", u_errorName(status));
1339 goto cleanup;
1340 }
1341 u_uastrcpy(tzID, "PST");
1342 cal = ucal_open(tzID, 3, "en_US", UCAL_TRADITIONAL, &status);
1343 if (U_FAILURE(status)) {
1344 log_err("ucal_open failed: %s\n", u_errorName(status));
1345 goto cleanup;
1346 }
1347
1348 datfor=udat_open(UDAT_MEDIUM,UDAT_MEDIUM ,NULL, fgGMTID,-1,NULL, 0, &status);
1349 if(U_FAILURE(status)){
1350 log_data_err("error in creating the dateformat : %s (Are you missing data?)\n", u_errorName(status));
1351 }
1352
1353 ucal_setDateTime(gmtcal, yr, mo - 1, dt, hr, mn, sc, &status);
1354 if (U_FAILURE(status)) {
1355 log_data_err("ucal_setDateTime failed: %s (Are you missing data?)\n", u_errorName(status));
1356 goto cleanup;
1357 }
1358 ucal_set(gmtcal, UCAL_MILLISECOND, 0);
1359 date1 = ucal_getMillis(gmtcal, &status);
1360 if (U_FAILURE(status)) {
1361 log_err("ucal_getMillis failed: %s\n", u_errorName(status));
1362 goto cleanup;
1363 }
1364 log_verbose("date = %s\n", u_austrcpy(tempMsgBuf, myDateFormat(datfor, date1)) );
1365
1366
1367 ucal_setMillis(cal, date1, &status);
1368 if (U_FAILURE(status)) {
1369 log_err("ucal_setMillis() failed: %s\n", u_errorName(status));
1370 goto cleanup;
1371 }
1372
1373 offset = ucal_get(cal, UCAL_ZONE_OFFSET, &status);
1374 offset += ucal_get(cal, UCAL_DST_OFFSET, &status);
1375
1376 if (U_FAILURE(status)) {
1377 log_err("ucal_get() failed: %s\n", u_errorName(status));
1378 goto cleanup;
1379 }
1380 temp=(double)((double)offset / 1000.0 / 60.0 / 60.0);
1381 /*printf("offset for %s %f hr\n", austrdup(myDateFormat(datfor, date1)), temp);*/
1382
1383 utc = ((ucal_get(cal, UCAL_HOUR_OF_DAY, &status) * 60 +
1384 ucal_get(cal, UCAL_MINUTE, &status)) * 60 +
1385 ucal_get(cal, UCAL_SECOND, &status)) * 1000 +
1386 ucal_get(cal, UCAL_MILLISECOND, &status) - offset;
1387 if (U_FAILURE(status)) {
1388 log_err("ucal_get() failed: %s\n", u_errorName(status));
1389 goto cleanup;
1390 }
1391
1392 expected = ((hr * 60 + mn) * 60 + sc) * 1000;
1393 if (utc != expected) {
1394 temp=(double)(utc - expected)/ 1000 / 60 / 60.0;
1395 log_err("FAIL: Discrepancy of %d millis = %fhr\n", utc-expected, temp );
1396 }
1397 else
1398 log_verbose("PASS: the offset between local and GMT is correct\n");
1399
1400 cleanup:
1401 ucal_close(gmtcal);
1402 ucal_close(cal);
1403 udat_close(datfor);
1404 }
1405
1406 /* ------------------------------------- */
1407
1408
1409
1410
1411 /* INTERNAL FUNCTIONS USED */
1412 /*------------------------------------------------------------------------------------------- */
1413
1414 /* ------------------------------------- */
checkDateTime(UCalendar * c,int32_t y,int32_t m,int32_t d,int32_t hr,int32_t min,int32_t sec,int32_t ms,UCalendarDateFields field)1415 static void checkDateTime(UCalendar* c,
1416 int32_t y, int32_t m, int32_t d,
1417 int32_t hr, int32_t min, int32_t sec,
1418 int32_t ms, UCalendarDateFields field)
1419
1420 {
1421 UErrorCode status = U_ZERO_ERROR;
1422 if (ucal_get(c, UCAL_YEAR, &status) != y ||
1423 ucal_get(c, UCAL_MONTH, &status) != m ||
1424 ucal_get(c, UCAL_DATE, &status) != d ||
1425 ucal_get(c, UCAL_HOUR, &status) != hr ||
1426 ucal_get(c, UCAL_MINUTE, &status) != min ||
1427 ucal_get(c, UCAL_SECOND, &status) != sec ||
1428 ucal_get(c, UCAL_MILLISECOND, &status) != ms) {
1429 log_err("U_FAILURE for field %d, Expected y/m/d h:m:s:ms of %d/%d/%d %d:%d:%d:%d got %d/%d/%d %d:%d:%d:%d\n",
1430 (int32_t)field, y, m + 1, d, hr, min, sec, ms,
1431 ucal_get(c, UCAL_YEAR, &status),
1432 ucal_get(c, UCAL_MONTH, &status) + 1,
1433 ucal_get(c, UCAL_DATE, &status),
1434 ucal_get(c, UCAL_HOUR, &status),
1435 ucal_get(c, UCAL_MINUTE, &status) + 1,
1436 ucal_get(c, UCAL_SECOND, &status),
1437 ucal_get(c, UCAL_MILLISECOND, &status) );
1438
1439 if (U_FAILURE(status)){
1440 log_err("ucal_get failed: %s\n", u_errorName(status));
1441 return;
1442 }
1443
1444 }
1445 else
1446 log_verbose("Confirmed: %d/%d/%d %d:%d:%d:%d\n", y, m + 1, d, hr, min, sec, ms);
1447
1448 }
1449
1450 /* ------------------------------------- */
checkDate(UCalendar * c,int32_t y,int32_t m,int32_t d)1451 static void checkDate(UCalendar* c, int32_t y, int32_t m, int32_t d)
1452 {
1453 UErrorCode status = U_ZERO_ERROR;
1454 if (ucal_get(c,UCAL_YEAR, &status) != y ||
1455 ucal_get(c, UCAL_MONTH, &status) != m ||
1456 ucal_get(c, UCAL_DATE, &status) != d) {
1457
1458 log_err("FAILURE: Expected y/m/d of %d/%d/%d got %d/%d/%d\n", y, m + 1, d,
1459 ucal_get(c, UCAL_YEAR, &status),
1460 ucal_get(c, UCAL_MONTH, &status) + 1,
1461 ucal_get(c, UCAL_DATE, &status) );
1462
1463 if (U_FAILURE(status)) {
1464 log_err("ucal_get failed: %s\n", u_errorName(status));
1465 return;
1466 }
1467 }
1468 else
1469 log_verbose("Confirmed: %d/%d/%d\n", y, m + 1, d);
1470
1471
1472 }
1473
1474 /* ------------------------------------- */
1475
1476 /* ------------------------------------- */
1477
verify1(const char * msg,UCalendar * c,UDateFormat * dat,int32_t year,int32_t month,int32_t day)1478 static void verify1(const char* msg, UCalendar* c, UDateFormat* dat, int32_t year, int32_t month, int32_t day)
1479 {
1480 UDate d1;
1481 UErrorCode status = U_ZERO_ERROR;
1482 if (ucal_get(c, UCAL_YEAR, &status) == year &&
1483 ucal_get(c, UCAL_MONTH, &status) == month &&
1484 ucal_get(c, UCAL_DATE, &status) == day) {
1485 if (U_FAILURE(status)) {
1486 log_err("FAIL: Calendar::get failed: %s\n", u_errorName(status));
1487 return;
1488 }
1489 log_verbose("PASS: %s\n", msg);
1490 d1=ucal_getMillis(c, &status);
1491 if (U_FAILURE(status)) {
1492 log_err("ucal_getMillis failed: %s\n", u_errorName(status));
1493 return;
1494 }
1495 /*log_verbose(austrdup(myDateFormat(dat, d1)) );*/
1496 }
1497 else {
1498 log_err("FAIL: %s\n", msg);
1499 d1=ucal_getMillis(c, &status);
1500 if (U_FAILURE(status)) {
1501 log_err("ucal_getMillis failed: %s\n", u_errorName(status) );
1502 return;
1503 }
1504 log_err("Got %s Expected %d/%d/%d \n", austrdup(myDateFormat(dat, d1)), year, month + 1, day );
1505 return;
1506 }
1507
1508
1509 }
1510
1511 /* ------------------------------------ */
verify2(const char * msg,UCalendar * c,UDateFormat * dat,int32_t year,int32_t month,int32_t day,int32_t hour,int32_t min,int32_t sec,int32_t am_pm)1512 static void verify2(const char* msg, UCalendar* c, UDateFormat* dat, int32_t year, int32_t month, int32_t day,
1513 int32_t hour, int32_t min, int32_t sec, int32_t am_pm)
1514 {
1515 UDate d1;
1516 UErrorCode status = U_ZERO_ERROR;
1517 char tempMsgBuf[256];
1518
1519 if (ucal_get(c, UCAL_YEAR, &status) == year &&
1520 ucal_get(c, UCAL_MONTH, &status) == month &&
1521 ucal_get(c, UCAL_DATE, &status) == day &&
1522 ucal_get(c, UCAL_HOUR, &status) == hour &&
1523 ucal_get(c, UCAL_MINUTE, &status) == min &&
1524 ucal_get(c, UCAL_SECOND, &status) == sec &&
1525 ucal_get(c, UCAL_AM_PM, &status) == am_pm ){
1526 if (U_FAILURE(status)) {
1527 log_err("FAIL: Calendar::get failed: %s\n", u_errorName(status));
1528 return;
1529 }
1530 log_verbose("PASS: %s\n", msg);
1531 d1=ucal_getMillis(c, &status);
1532 if (U_FAILURE(status)) {
1533 log_err("ucal_getMillis failed: %s\n", u_errorName(status));
1534 return;
1535 }
1536 log_verbose("%s\n" , u_austrcpy(tempMsgBuf, myDateFormat(dat, d1)) );
1537 }
1538 else {
1539 log_err("FAIL: %s\n", msg);
1540 d1=ucal_getMillis(c, &status);
1541 if (U_FAILURE(status)) {
1542 log_err("ucal_getMillis failed: %s\n", u_errorName(status));
1543 return;
1544 }
1545 log_err("Got %s Expected %d/%d/%d/ %d:%d:%d %s\n", austrdup(myDateFormat(dat, d1)),
1546 year, month + 1, day, hour, min, sec, (am_pm==0) ? "AM": "PM");
1547
1548 return;
1549 }
1550
1551
1552 }
1553
TestGregorianChange()1554 void TestGregorianChange() {
1555 static const UChar utc[] = { 0x45, 0x74, 0x63, 0x2f, 0x47, 0x4d, 0x54, 0 }; /* "Etc/GMT" */
1556 const int32_t dayMillis = 86400 * INT64_C(1000); /* 1 day = 86400 seconds */
1557 UCalendar *cal;
1558 UDate date;
1559 UErrorCode errorCode = U_ZERO_ERROR;
1560
1561 /* Test ucal_setGregorianChange() on a Gregorian calendar. */
1562 errorCode = U_ZERO_ERROR;
1563 cal = ucal_open(utc, -1, "", UCAL_GREGORIAN, &errorCode);
1564 if(U_FAILURE(errorCode)) {
1565 log_data_err("ucal_open(UTC) failed: %s - (Are you missing data?)\n", u_errorName(errorCode));
1566 return;
1567 }
1568 ucal_setGregorianChange(cal, -365 * (dayMillis * (UDate)1), &errorCode);
1569 if(U_FAILURE(errorCode)) {
1570 log_err("ucal_setGregorianChange(1969) failed: %s\n", u_errorName(errorCode));
1571 } else {
1572 date = ucal_getGregorianChange(cal, &errorCode);
1573 if(U_FAILURE(errorCode) || date != -365 * (dayMillis * (UDate)1)) {
1574 log_err("ucal_getGregorianChange() failed: %s, date = %f\n", u_errorName(errorCode), date);
1575 }
1576 }
1577 ucal_close(cal);
1578 /* Test ucal_setGregorianChange() on a iso8601 calendar and it should work
1579 * as Gregorian. */
1580 errorCode = U_ZERO_ERROR;
1581 cal = ucal_open(utc, -1, "en@calendar=iso8601", UCAL_TRADITIONAL, &errorCode);
1582 if(U_FAILURE(errorCode)) {
1583 log_data_err("ucal_open(UTC) failed: %s - (Are you missing data?)\n", u_errorName(errorCode));
1584 return;
1585 }
1586 ucal_setGregorianChange(cal, -365 * (dayMillis * (UDate)1), &errorCode);
1587 if(U_FAILURE(errorCode)) {
1588 log_err("ucal_setGregorianChange(1969) failed: %s\n", u_errorName(errorCode));
1589 } else {
1590 date = ucal_getGregorianChange(cal, &errorCode);
1591 if(U_FAILURE(errorCode) || date != -365 * (dayMillis * (UDate)1)) {
1592 log_err("ucal_getGregorianChange() failed: %s, date = %f\n", u_errorName(errorCode), date);
1593 }
1594 }
1595 ucal_close(cal);
1596
1597 /* Test ucal_setGregorianChange() on a non-Gregorian calendar where it should fail. */
1598 errorCode = U_ZERO_ERROR;
1599 cal = ucal_open(utc, -1, "th@calendar=buddhist", UCAL_TRADITIONAL, &errorCode);
1600 if(U_FAILURE(errorCode)) {
1601 log_err("ucal_open(UTC, non-Gregorian) failed: %s\n", u_errorName(errorCode));
1602 return;
1603 }
1604 ucal_setGregorianChange(cal, -730 * (dayMillis * (UDate)1), &errorCode);
1605 if(errorCode != U_UNSUPPORTED_ERROR) {
1606 log_err("ucal_setGregorianChange(non-Gregorian calendar) did not yield U_UNSUPPORTED_ERROR but %s\n",
1607 u_errorName(errorCode));
1608 }
1609 errorCode = U_ZERO_ERROR;
1610 date = ucal_getGregorianChange(cal, &errorCode);
1611 if(errorCode != U_UNSUPPORTED_ERROR) {
1612 log_err("ucal_getGregorianChange(non-Gregorian calendar) did not yield U_UNSUPPORTED_ERROR but %s\n",
1613 u_errorName(errorCode));
1614 }
1615 ucal_close(cal);
1616 }
1617
TestGetKeywordValuesForLocale()1618 static void TestGetKeywordValuesForLocale() {
1619 #define PREFERRED_SIZE 26
1620 #define MAX_NUMBER_OF_KEYWORDS 5
1621 const char *PREFERRED[PREFERRED_SIZE][MAX_NUMBER_OF_KEYWORDS+1] = {
1622 { "root", "gregorian", NULL, NULL, NULL, NULL },
1623 { "und", "gregorian", NULL, NULL, NULL, NULL },
1624 { "en_US", "gregorian", NULL, NULL, NULL, NULL },
1625 { "en_029", "gregorian", NULL, NULL, NULL, NULL },
1626 { "th_TH", "buddhist", "gregorian", NULL, NULL, NULL },
1627 { "und_TH", "buddhist", "gregorian", NULL, NULL, NULL },
1628 { "en_TH", "buddhist", "gregorian", NULL, NULL, NULL },
1629 { "he_IL", "gregorian", "hebrew", "islamic", "islamic-civil", "islamic-tbla" },
1630 { "ar_EG", "gregorian", "coptic", "islamic", "islamic-civil", "islamic-tbla" },
1631 { "ja", "gregorian", "japanese", NULL, NULL, NULL },
1632 { "ps_Guru_IN", "gregorian", "indian", NULL, NULL, NULL },
1633 { "th@calendar=gregorian", "buddhist", "gregorian", NULL, NULL, NULL },
1634 { "en@calendar=islamic", "gregorian", NULL, NULL, NULL, NULL },
1635 { "zh_TW", "gregorian", "roc", "chinese", NULL, NULL },
1636 { "ar_IR", "persian", "gregorian", "islamic", "islamic-civil", "islamic-tbla" },
1637 { "th@rg=SAZZZZ", "islamic-umalqura", "gregorian", "islamic", "islamic-rgsa", NULL },
1638
1639 // tests for ICU-22364
1640 { "zh_CN@rg=TW", "gregorian", "chinese", NULL, NULL, NULL }, // invalid subdivision code
1641 { "zh_CN@rg=TWzzzz", "gregorian", "roc", "chinese", NULL, NULL }, // whole region
1642 { "zh_TW@rg=TWxxxx", "gregorian", "roc", "chinese", NULL, NULL }, // invalid subdivision code (ignored)
1643 { "zh_TW@rg=ARa", "gregorian", NULL, NULL, NULL, NULL }, // single-letter subdivision code
1644 { "zh_TW@rg=AT1", "gregorian", NULL, NULL, NULL, NULL }, // single-digit subdivision code
1645 { "zh_TW@rg=USca", "gregorian", NULL, NULL, NULL, NULL }, // two-letter subdivision code
1646 { "zh_TW@rg=IT53", "gregorian", NULL, NULL, NULL, NULL }, // two-digit subdivision code
1647 { "zh_TW@rg=AUnsw", "gregorian", NULL, NULL, NULL, NULL }, // three-letter subdivision code
1648 { "zh_TW@rg=EE130", "gregorian", NULL, NULL, NULL, NULL }, // three-digit subdivision code
1649 { "zh_TW@rg=417zzzz", "gregorian", NULL, NULL, NULL, NULL }, // three-digit region code
1650 };
1651 const int32_t EXPECTED_SIZE[PREFERRED_SIZE] = { 1, 1, 1, 1, 2, 2, 2, 5, 5, 2, 2, 2, 1, 3, 5, 4, 2, 3, 3, 1, 1, 1, 1, 1, 1, 1 };
1652 UErrorCode status = U_ZERO_ERROR;
1653 int32_t i, size, j;
1654 UEnumeration *all, *pref;
1655 const char *loc = NULL;
1656 UBool matchPref, matchAll;
1657 const char *value;
1658 int32_t valueLength;
1659 UList *ALLList = NULL;
1660
1661 UEnumeration *ALL = ucal_getKeywordValuesForLocale("calendar", uloc_getDefault(), false, &status);
1662 if (U_SUCCESS(status)) {
1663 for (i = 0; i < PREFERRED_SIZE; i++) {
1664 pref = NULL;
1665 all = NULL;
1666 loc = PREFERRED[i][0];
1667 pref = ucal_getKeywordValuesForLocale("calendar", loc, true, &status);
1668 matchPref = false;
1669 matchAll = false;
1670
1671 value = NULL;
1672 valueLength = 0;
1673
1674 if (U_SUCCESS(status) && uenum_count(pref, &status) == EXPECTED_SIZE[i]) {
1675 matchPref = true;
1676 for (j = 0; j < EXPECTED_SIZE[i]; j++) {
1677 if ((value = uenum_next(pref, &valueLength, &status)) != NULL && U_SUCCESS(status)) {
1678 if (uprv_strcmp(value, PREFERRED[i][j+1]) != 0) {
1679 matchPref = false;
1680 break;
1681 }
1682 } else {
1683 matchPref = false;
1684 log_err("ERROR getting keyword value for locale \"%s\"\n", loc);
1685 break;
1686 }
1687 }
1688 }
1689
1690 if (!matchPref) {
1691 log_err("FAIL: Preferred values for locale \"%s\" does not match expected.\n", loc);
1692 break;
1693 }
1694 uenum_close(pref);
1695
1696 all = ucal_getKeywordValuesForLocale("calendar", loc, false, &status);
1697
1698 size = uenum_count(all, &status);
1699
1700 if (U_SUCCESS(status) && size == uenum_count(ALL, &status)) {
1701 matchAll = true;
1702 ALLList = ulist_getListFromEnum(ALL);
1703 for (j = 0; j < size; j++) {
1704 if ((value = uenum_next(all, &valueLength, &status)) != NULL && U_SUCCESS(status)) {
1705 if (!ulist_containsString(ALLList, value, (int32_t)uprv_strlen(value))) {
1706 log_err("Locale %s have %s not in ALL\n", loc, value);
1707 matchAll = false;
1708 break;
1709 }
1710 } else {
1711 matchAll = false;
1712 log_err("ERROR getting \"all\" keyword value for locale \"%s\"\n", loc);
1713 break;
1714 }
1715 }
1716 }
1717 if (!matchAll) {
1718 log_err("FAIL: All values for locale \"%s\" does not match expected.\n", loc);
1719 }
1720
1721 uenum_close(all);
1722 }
1723 } else {
1724 log_err_status(status, "Failed to get ALL keyword values for default locale %s: %s.\n", uloc_getDefault(), u_errorName(status));
1725 }
1726 uenum_close(ALL);
1727 }
1728
1729 /*
1730 * Weekend tests, ported from
1731 * icu4j/main/core/src/test/java/com/ibm/icu/dev/test/calendar/IBMCalendarTest.java
1732 * and extended a bit. Notes below from IBMCalendarTest.java ...
1733 * This test tests for specific locale data. This is probably okay
1734 * as far as US data is concerned, but if the Arabic/Yemen data
1735 * changes, this test will have to be updated.
1736 */
1737
1738 typedef struct {
1739 int32_t year;
1740 int32_t month;
1741 int32_t day;
1742 int32_t hour;
1743 int32_t millisecOffset;
1744 UBool isWeekend;
1745 } TestWeekendDates;
1746 typedef struct {
1747 const char * locale;
1748 const TestWeekendDates * dates;
1749 int32_t numDates;
1750 } TestWeekendDatesList;
1751
1752 static const TestWeekendDates weekendDates_en_US[] = {
1753 { 2000, UCAL_MARCH, 17, 23, 0, 0 }, /* Fri 23:00 */
1754 { 2000, UCAL_MARCH, 18, 0, -1, 0 }, /* Fri 23:59:59.999 */
1755 { 2000, UCAL_MARCH, 18, 0, 0, 1 }, /* Sat 00:00 */
1756 { 2000, UCAL_MARCH, 18, 15, 0, 1 }, /* Sat 15:00 */
1757 { 2000, UCAL_MARCH, 19, 23, 0, 1 }, /* Sun 23:00 */
1758 { 2000, UCAL_MARCH, 20, 0, -1, 1 }, /* Sun 23:59:59.999 */
1759 { 2000, UCAL_MARCH, 20, 0, 0, 0 }, /* Mon 00:00 */
1760 { 2000, UCAL_MARCH, 20, 8, 0, 0 }, /* Mon 08:00 */
1761 };
1762 static const TestWeekendDates weekendDates_ar_OM[] = {
1763 { 2000, UCAL_MARCH, 15, 23, 0, 0 }, /* Wed 23:00 */
1764 { 2000, UCAL_MARCH, 16, 0, -1, 0 }, /* Wed 23:59:59.999 */
1765 { 2000, UCAL_MARCH, 16, 0, 0, 0 }, /* Thu 00:00 */
1766 { 2000, UCAL_MARCH, 16, 15, 0, 0 }, /* Thu 15:00 */
1767 { 2000, UCAL_MARCH, 17, 23, 0, 1 }, /* Fri 23:00 */
1768 { 2000, UCAL_MARCH, 18, 0, -1, 1 }, /* Fri 23:59:59.999 */
1769 { 2000, UCAL_MARCH, 18, 0, 0, 1 }, /* Sat 00:00 */
1770 { 2000, UCAL_MARCH, 18, 8, 0, 1 }, /* Sat 08:00 */
1771 };
1772 static const TestWeekendDatesList testDates[] = {
1773 { "en_US", weekendDates_en_US, UPRV_LENGTHOF(weekendDates_en_US) },
1774 { "ar_OM", weekendDates_ar_OM, UPRV_LENGTHOF(weekendDates_ar_OM) },
1775 };
1776
1777 typedef struct {
1778 UCalendarDaysOfWeek dayOfWeek;
1779 UCalendarWeekdayType dayType;
1780 int32_t transition; /* transition time if dayType is UCAL_WEEKEND_ONSET or UCAL_WEEKEND_CEASE; else must be 0 */
1781 } TestDaysOfWeek;
1782 typedef struct {
1783 const char * locale;
1784 const TestDaysOfWeek * days;
1785 int32_t numDays;
1786 } TestDaysOfWeekList;
1787
1788 static const TestDaysOfWeek daysOfWeek_en_US[] = {
1789 { UCAL_MONDAY, UCAL_WEEKDAY, 0 },
1790 { UCAL_FRIDAY, UCAL_WEEKDAY, 0 },
1791 { UCAL_SATURDAY, UCAL_WEEKEND, 0 },
1792 { UCAL_SUNDAY, UCAL_WEEKEND, 0 },
1793 };
1794 static const TestDaysOfWeek daysOfWeek_ar_OM[] = { /* Friday:Saturday */
1795 { UCAL_WEDNESDAY,UCAL_WEEKDAY, 0 },
1796 { UCAL_THURSDAY, UCAL_WEEKDAY, 0 },
1797 { UCAL_FRIDAY, UCAL_WEEKEND, 0 },
1798 { UCAL_SATURDAY, UCAL_WEEKEND, 0 },
1799 };
1800 static const TestDaysOfWeek daysOfWeek_hi_IN[] = { /* Sunday only */
1801 { UCAL_MONDAY, UCAL_WEEKDAY, 0 },
1802 { UCAL_FRIDAY, UCAL_WEEKDAY, 0 },
1803 { UCAL_SATURDAY, UCAL_WEEKDAY, 0 },
1804 { UCAL_SUNDAY, UCAL_WEEKEND, 0 },
1805 };
1806 static const TestDaysOfWeekList testDays[] = {
1807 { "en_US", daysOfWeek_en_US, UPRV_LENGTHOF(daysOfWeek_en_US) },
1808 { "ar_OM", daysOfWeek_ar_OM, UPRV_LENGTHOF(daysOfWeek_ar_OM) },
1809 { "hi_IN", daysOfWeek_hi_IN, UPRV_LENGTHOF(daysOfWeek_hi_IN) },
1810 { "en_US@rg=OMZZZZ", daysOfWeek_ar_OM, UPRV_LENGTHOF(daysOfWeek_ar_OM) },
1811 { "hi@rg=USZZZZ", daysOfWeek_en_US, UPRV_LENGTHOF(daysOfWeek_en_US) },
1812 };
1813
1814 static const UChar logDateFormat[] = { 0x0045,0x0045,0x0045,0x0020,0x004D,0x004D,0x004D,0x0020,0x0064,0x0064,0x0020,0x0079,
1815 0x0079,0x0079,0x0079,0x0020,0x0047,0x0020,0x0048,0x0048,0x003A,0x006D,0x006D,0x003A,
1816 0x0073,0x0073,0x002E,0x0053,0x0053,0x0053,0 }; /* "EEE MMM dd yyyy G HH:mm:ss.SSS" */
1817 enum { kFormattedDateMax = 2*UPRV_LENGTHOF(logDateFormat) };
1818
TestWeekend()1819 static void TestWeekend() {
1820 const TestWeekendDatesList * testDatesPtr = testDates;
1821 const TestDaysOfWeekList * testDaysPtr = testDays;
1822 int32_t count, subCount;
1823
1824 UErrorCode fmtStatus = U_ZERO_ERROR;
1825 UDateFormat * fmt = udat_open(UDAT_NONE, UDAT_NONE, "en", NULL, 0, NULL, 0, &fmtStatus);
1826 if (U_SUCCESS(fmtStatus)) {
1827 udat_applyPattern(fmt, false, logDateFormat, -1);
1828 } else {
1829 log_data_err("Unable to create UDateFormat - %s\n", u_errorName(fmtStatus));
1830 return;
1831 }
1832 for (count = UPRV_LENGTHOF(testDates); count-- > 0; ++testDatesPtr) {
1833 UErrorCode status = U_ZERO_ERROR;
1834 UCalendar * cal = ucal_open(NULL, 0, testDatesPtr->locale, UCAL_GREGORIAN, &status);
1835 log_verbose("locale: %s\n", testDatesPtr->locale);
1836 if (U_SUCCESS(status)) {
1837 const TestWeekendDates * weekendDatesPtr = testDatesPtr->dates;
1838 for (subCount = testDatesPtr->numDates; subCount--; ++weekendDatesPtr) {
1839 UDate dateToTest;
1840 UBool isWeekend;
1841 char fmtDateBytes[kFormattedDateMax] = "<could not format test date>"; /* initialize for failure */
1842
1843 ucal_clear(cal);
1844 ucal_setDateTime(cal, weekendDatesPtr->year, weekendDatesPtr->month, weekendDatesPtr->day,
1845 weekendDatesPtr->hour, 0, 0, &status);
1846 dateToTest = ucal_getMillis(cal, &status) + weekendDatesPtr->millisecOffset;
1847 isWeekend = ucal_isWeekend(cal, dateToTest, &status);
1848 if (U_SUCCESS(fmtStatus)) {
1849 UChar fmtDate[kFormattedDateMax];
1850 (void)udat_format(fmt, dateToTest, fmtDate, kFormattedDateMax, NULL, &fmtStatus);
1851 if (U_SUCCESS(fmtStatus)) {
1852 u_austrncpy(fmtDateBytes, fmtDate, kFormattedDateMax);
1853 fmtDateBytes[kFormattedDateMax-1] = 0;
1854 } else {
1855 fmtStatus = U_ZERO_ERROR;
1856 }
1857 }
1858 if ( U_FAILURE(status) ) {
1859 log_err("FAIL: locale %s date %s isWeekend() status %s\n", testDatesPtr->locale, fmtDateBytes, u_errorName(status) );
1860 status = U_ZERO_ERROR;
1861 } else if ( (isWeekend!=0) != (weekendDatesPtr->isWeekend!=0) ) {
1862 log_err("FAIL: locale %s date %s isWeekend %d, expected the opposite\n", testDatesPtr->locale, fmtDateBytes, isWeekend );
1863 } else {
1864 log_verbose("OK: locale %s date %s isWeekend %d\n", testDatesPtr->locale, fmtDateBytes, isWeekend );
1865 }
1866 }
1867 ucal_close(cal);
1868 } else {
1869 log_data_err("FAIL: ucal_open for locale %s failed: %s - (Are you missing data?)\n", testDatesPtr->locale, u_errorName(status) );
1870 }
1871 }
1872 if (U_SUCCESS(fmtStatus)) {
1873 udat_close(fmt);
1874 }
1875
1876 for (count = UPRV_LENGTHOF(testDays); count-- > 0; ++testDaysPtr) {
1877 UErrorCode status = U_ZERO_ERROR;
1878 UCalendar * cal = ucal_open(NULL, 0, testDaysPtr->locale, UCAL_GREGORIAN, &status);
1879 log_verbose("locale: %s\n", testDaysPtr->locale);
1880 if (U_SUCCESS(status)) {
1881 const TestDaysOfWeek * daysOfWeekPtr = testDaysPtr->days;
1882 for (subCount = testDaysPtr->numDays; subCount--; ++daysOfWeekPtr) {
1883 int32_t transition = 0;
1884 UCalendarWeekdayType dayType = ucal_getDayOfWeekType(cal, daysOfWeekPtr->dayOfWeek, &status);
1885 if ( dayType == UCAL_WEEKEND_ONSET || dayType == UCAL_WEEKEND_CEASE ) {
1886 transition = ucal_getWeekendTransition(cal, daysOfWeekPtr->dayOfWeek, &status);
1887 }
1888 if ( U_FAILURE(status) ) {
1889 log_err("FAIL: locale %s DOW %d getDayOfWeekType() status %s\n", testDaysPtr->locale, daysOfWeekPtr->dayOfWeek, u_errorName(status) );
1890 status = U_ZERO_ERROR;
1891 } else if ( dayType != daysOfWeekPtr->dayType || transition != daysOfWeekPtr->transition ) {
1892 log_err("FAIL: locale %s DOW %d type %d, expected %d\n", testDaysPtr->locale, daysOfWeekPtr->dayOfWeek, dayType, daysOfWeekPtr->dayType );
1893 } else {
1894 log_verbose("OK: locale %s DOW %d type %d\n", testDaysPtr->locale, daysOfWeekPtr->dayOfWeek, dayType );
1895 }
1896 }
1897 ucal_close(cal);
1898 } else {
1899 log_data_err("FAIL: ucal_open for locale %s failed: %s - (Are you missing data?)\n", testDaysPtr->locale, u_errorName(status) );
1900 }
1901 }
1902 }
1903
1904 /**
1905 * TestFieldDifference
1906 */
1907
1908 typedef struct {
1909 const UChar * timezone;
1910 const char * locale;
1911 UDate start;
1912 UDate target;
1913 UBool progressive; /* true to compute progressive difference for each field, false to reset calendar after each call */
1914 int32_t yDiff;
1915 int32_t MDiff;
1916 int32_t dDiff;
1917 int32_t HDiff;
1918 int32_t mDiff;
1919 int32_t sDiff; /* 0x7FFFFFFF indicates overflow error expected */
1920 } TFDItem;
1921
1922 static const UChar tzUSPacific[] = { 0x55,0x53,0x2F,0x50,0x61,0x63,0x69,0x66,0x69,0x63,0 }; /* "US/Pacific" */
1923 static const UChar tzGMT[] = { 0x47,0x4D,0x54,0 }; /* "GMT" */
1924
1925 static const TFDItem tfdItems[] = {
1926 /* timezone locale start target progress yDf MDf dDf HDf mDf sDf */
1927 /* For these we compute the progressive difference for each field - not resetting the calendar after each call */
1928 { tzUSPacific, "en_US", 1267459800000.0, 1277772600000.0, true, 0, 3, 27, 9, 40, 0 }, /* 2010-Mar-01 08:10 -> 2010-Jun-28 17:50 */
1929 { tzUSPacific, "en_US", 1267459800000.0, 1299089280000.0, true, 1, 0, 1, 1, 58, 0 }, /* 2010-Mar-01 08:10 -> 2011-Mar-02 10:08 */
1930 /* For these we compute the total difference for each field - resetting the calendar after each call */
1931 { tzGMT, "en_US", 0.0, 1073692800000.0, false, 34, 408, 12427, 298248, 17894880, 1073692800 }, /* 1970-Jan-01 00:00 -> 2004-Jan-10 00:00 */
1932 { tzGMT, "en_US", 0.0, 1073779200000.0, false, 34, 408, 12428, 298272, 17896320, 1073779200 }, /* 1970-Jan-01 00:00 -> 2004-Jan-11 00:00 */
1933 { tzGMT, "en_US", 0.0, 2147472000000.0, false, 68, 816, 24855, 596520, 35791200, 2147472000 }, /* 1970-Jan-01 00:00 -> 2038-Jan-19 00:00 */
1934 { tzGMT, "en_US", 0.0, 2147558400000.0, false, 68, 816, 24856, 596544, 35792640, 0x7FFFFFFF }, /* 1970-Jan-01 00:00 -> 2038-Jan-20 00:00, seconds diff overflow */
1935 { tzGMT, "en_US", 0.0, -1073692800000.0, false, -34,-408,-12427,-298248,-17894880,-1073692800 }, /* 1970-Jan-01 00:00 -> 1935-Dec-24 00:00 */
1936 { tzGMT, "en_US", 0.0, -1073779200000.0, false, -34,-408,-12428,-298272,-17896320,-1073779200 }, /* 1970-Jan-01 00:00 -> 1935-Dec-23 00:00 */
1937 /* check fwd/backward on either side of era boundary and across era boundary */
1938 { tzGMT, "en_US", -61978089600000.0,-61820409600000.0, false, 4, 59, 1825, 43800, 2628000, 157680000 }, /* CE 5-Dec-31 00:00 -> CE 10-Dec-30 00:00 */
1939 { tzGMT, "en_US", -61820409600000.0,-61978089600000.0, false, -4, -59, -1825, -43800, -2628000, -157680000 }, /* CE 10-Dec-30 00:00 -> CE 5-Dec-31 00:00 */
1940 { tzGMT, "en_US", -62451129600000.0,-62293449600000.0, false, 4, 59, 1825, 43800, 2628000, 157680000 }, /* BCE 10-Jan-04 00:00 -> BCE 5-Jan-03 00:00 */
1941 { tzGMT, "en_US", -62293449600000.0,-62451129600000.0, false, -4, -59, -1825, -43800, -2628000, -157680000 }, /* BCE 5-Jan-03 00:00 -> BCE 10-Jan-04 00:00 */
1942 { tzGMT, "en_US", -62293449600000.0,-61978089600000.0, false, 9, 119, 3650, 87600, 5256000, 315360000 }, /* BCE 5-Jan-03 00:00 -> CE 5-Dec-31 00:00 */
1943 { tzGMT, "en_US", -61978089600000.0,-62293449600000.0, false, -9,-119, -3650, -87600, -5256000, -315360000 }, /* CE 5-Dec-31 00:00 -> BCE 5-Jan-03 00:00 */
1944 { tzGMT, "en@calendar=roc", -1672704000000.0, -1515024000000.0, false, 4, 59, 1825, 43800, 2628000, 157680000 }, /* MG 5-Dec-30 00:00 -> MG 10-Dec-29 00:00 */
1945 { tzGMT, "en@calendar=roc", -1515024000000.0, -1672704000000.0, false, -4, -59, -1825, -43800, -2628000, -157680000 }, /* MG 10-Dec-29 00:00 -> MG 5-Dec-30 00:00 */
1946 { tzGMT, "en@calendar=roc", -2145744000000.0, -1988064000000.0, false, 4, 59, 1825, 43800, 2628000, 157680000 }, /* BMG 10-Jan-03 00:00 -> BMG 5-Jan-02 00:00 */
1947 { tzGMT, "en@calendar=roc", -1988064000000.0, -2145744000000.0, false, -4, -59, -1825, -43800, -2628000, -157680000 }, /* BMG 5-Jan-02 00:00 -> BMG 10-Jan-03 00:00 */
1948 { tzGMT, "en@calendar=roc", -1988064000000.0, -1672704000000.0, false, 9, 119, 3650, 87600, 5256000, 315360000 }, /* BMG 5-Jan-02 00:00 -> MG 5-Dec-30 00:00 */
1949 { tzGMT, "en@calendar=roc", -1672704000000.0, -1988064000000.0, false, -9,-119, -3650, -87600, -5256000, -315360000 }, /* MG 5-Dec-30 00:00 -> BMG 5-Jan-02 00:00 */
1950 { tzGMT, "en@calendar=coptic",-53026531200000.0,-52868851200000.0, false, 4, 64, 1825, 43800, 2628000, 157680000 }, /* Er1 5-Nas-05 00:00 -> Er1 10-Nas-04 00:00 */
1951 { tzGMT, "en@calendar=coptic",-52868851200000.0,-53026531200000.0, false, -4, -64, -1825, -43800, -2628000, -157680000 }, /* Er1 10-Nas-04 00:00 -> Er1 5-Nas-05 00:00 */
1952 { tzGMT, "en@calendar=coptic",-53499571200000.0,-53341891200000.0, false, 4, 64, 1825, 43800, 2628000, 157680000 }, /* Er0 10-Tou-04 00:00 -> Er0 5-Tou-02 00:00 */
1953 { tzGMT, "en@calendar=coptic",-53341891200000.0,-53499571200000.0, false, -4, -64, -1825, -43800, -2628000, -157680000 }, /* Er0 5-Tou-02 00:00 -> Er0 10-Tou-04 00:00 */
1954 { tzGMT, "en@calendar=coptic",-53341891200000.0,-53026531200000.0, false, 9, 129, 3650, 87600, 5256000, 315360000 }, /* Er0 5-Tou-02 00:00 -> Er1 5-Nas-05 00:00 */
1955 { tzGMT, "en@calendar=coptic",-53026531200000.0,-53341891200000.0, false, -9,-129, -3650, -87600, -5256000, -315360000 }, /* Er1 5-Nas-05 00:00 -> Er0 5-Tou-02 00:00 */
1956 { NULL, NULL, 0.0, 0.0, false, 0, 0, 0, 0, 0, 0 } /* terminator */
1957 };
1958
TestFieldDifference()1959 void TestFieldDifference() {
1960 const TFDItem * tfdItemPtr;
1961 for (tfdItemPtr = tfdItems; tfdItemPtr->timezone != NULL; tfdItemPtr++) {
1962 UErrorCode status = U_ZERO_ERROR;
1963 UCalendar* ucal = ucal_open(tfdItemPtr->timezone, -1, tfdItemPtr->locale, UCAL_DEFAULT, &status);
1964 if (U_FAILURE(status)) {
1965 log_err("FAIL: for locale \"%s\", ucal_open had status %s\n", tfdItemPtr->locale, u_errorName(status) );
1966 } else {
1967 int32_t yDf, MDf, dDf, HDf, mDf, sDf;
1968 if (tfdItemPtr->progressive) {
1969 ucal_setMillis(ucal, tfdItemPtr->start, &status);
1970 yDf = ucal_getFieldDifference(ucal, tfdItemPtr->target, UCAL_YEAR, &status);
1971 MDf = ucal_getFieldDifference(ucal, tfdItemPtr->target, UCAL_MONTH, &status);
1972 dDf = ucal_getFieldDifference(ucal, tfdItemPtr->target, UCAL_DATE, &status);
1973 HDf = ucal_getFieldDifference(ucal, tfdItemPtr->target, UCAL_HOUR, &status);
1974 mDf = ucal_getFieldDifference(ucal, tfdItemPtr->target, UCAL_MINUTE, &status);
1975 sDf = ucal_getFieldDifference(ucal, tfdItemPtr->target, UCAL_SECOND, &status);
1976 if (U_FAILURE(status)) {
1977 log_err("FAIL: for locale \"%s\", start %.1f, target %.1f, ucal_setMillis or ucal_getFieldDifference had status %s\n",
1978 tfdItemPtr->locale, tfdItemPtr->start, tfdItemPtr->target, u_errorName(status) );
1979 } else if ( yDf != tfdItemPtr->yDiff ||
1980 MDf != tfdItemPtr->MDiff ||
1981 dDf != tfdItemPtr->dDiff ||
1982 HDf != tfdItemPtr->HDiff ||
1983 mDf != tfdItemPtr->mDiff ||
1984 sDf != tfdItemPtr->sDiff ) {
1985 log_data_err("FAIL: for locale \"%s\", start %.1f, target %.1f, expected y-M-d-H-m-s progressive diffs %d-%d-%d-%d-%d-%d, got %d-%d-%d-%d-%d-%d\n",
1986 tfdItemPtr->locale, tfdItemPtr->start, tfdItemPtr->target,
1987 tfdItemPtr->yDiff, tfdItemPtr->MDiff, tfdItemPtr->dDiff, tfdItemPtr->HDiff, tfdItemPtr->mDiff, tfdItemPtr->sDiff,
1988 yDf, MDf, dDf, HDf, mDf, sDf);
1989 }
1990 } else {
1991 ucal_setMillis(ucal, tfdItemPtr->start, &status);
1992 yDf = ucal_getFieldDifference(ucal, tfdItemPtr->target, UCAL_YEAR, &status);
1993 ucal_setMillis(ucal, tfdItemPtr->start, &status);
1994 MDf = ucal_getFieldDifference(ucal, tfdItemPtr->target, UCAL_MONTH, &status);
1995 ucal_setMillis(ucal, tfdItemPtr->start, &status);
1996 dDf = ucal_getFieldDifference(ucal, tfdItemPtr->target, UCAL_DATE, &status);
1997 ucal_setMillis(ucal, tfdItemPtr->start, &status);
1998 HDf = ucal_getFieldDifference(ucal, tfdItemPtr->target, UCAL_HOUR, &status);
1999 ucal_setMillis(ucal, tfdItemPtr->start, &status);
2000 mDf = ucal_getFieldDifference(ucal, tfdItemPtr->target, UCAL_MINUTE, &status);
2001 if (U_FAILURE(status)) {
2002 log_err("FAIL: for locale \"%s\", start %.1f, target %.1f, ucal_setMillis or ucal_getFieldDifference (y-M-d-H-m) had status %s\n",
2003 tfdItemPtr->locale, tfdItemPtr->start, tfdItemPtr->target, u_errorName(status) );
2004 } else if ( yDf != tfdItemPtr->yDiff ||
2005 MDf != tfdItemPtr->MDiff ||
2006 dDf != tfdItemPtr->dDiff ||
2007 HDf != tfdItemPtr->HDiff ||
2008 mDf != tfdItemPtr->mDiff ) {
2009 log_data_err("FAIL: for locale \"%s\", start %.1f, target %.1f, expected y-M-d-H-m total diffs %d-%d-%d-%d-%d, got %d-%d-%d-%d-%d\n",
2010 tfdItemPtr->locale, tfdItemPtr->start, tfdItemPtr->target,
2011 tfdItemPtr->yDiff, tfdItemPtr->MDiff, tfdItemPtr->dDiff, tfdItemPtr->HDiff, tfdItemPtr->mDiff,
2012 yDf, MDf, dDf, HDf, mDf);
2013 }
2014 ucal_setMillis(ucal, tfdItemPtr->start, &status);
2015 sDf = ucal_getFieldDifference(ucal, tfdItemPtr->target, UCAL_SECOND, &status);
2016 if (tfdItemPtr->sDiff != 0x7FFFFFFF) {
2017 if (U_FAILURE(status)) {
2018 log_err("FAIL: for locale \"%s\", start %.1f, target %.1f, ucal_setMillis or ucal_getFieldDifference (seconds) had status %s\n",
2019 tfdItemPtr->locale, tfdItemPtr->start, tfdItemPtr->target, u_errorName(status) );
2020 } else if (sDf != tfdItemPtr->sDiff) {
2021 log_data_err("FAIL: for locale \"%s\", start %.1f, target %.1f, expected seconds progressive diff %d, got %d\n",
2022 tfdItemPtr->locale, tfdItemPtr->start, tfdItemPtr->target, tfdItemPtr->sDiff, sDf);
2023 }
2024 } else if (!U_FAILURE(status)) {
2025 log_err("FAIL: for locale \"%s\", start %.1f, target %.1f, for ucal_getFieldDifference (seconds) expected overflow error, got none\n",
2026 tfdItemPtr->locale, tfdItemPtr->start, tfdItemPtr->target );
2027 }
2028 }
2029 ucal_close(ucal);
2030 }
2031 }
2032 }
2033
TestAmbiguousWallTime()2034 void TestAmbiguousWallTime() {
2035 UErrorCode status = U_ZERO_ERROR;
2036 UChar tzID[32];
2037 UCalendar* ucal;
2038 UDate t, expected;
2039
2040 u_uastrcpy(tzID, "America/New_York");
2041 ucal = ucal_open(tzID, -1, "en_US", UCAL_DEFAULT, &status);
2042 if (U_FAILURE(status)) {
2043 log_err("FAIL: Failed to create a calendar");
2044 return;
2045 }
2046
2047 if (ucal_getAttribute(ucal, UCAL_REPEATED_WALL_TIME) != UCAL_WALLTIME_LAST) {
2048 log_err("FAIL: Default UCAL_REPEATED_WALL_TIME value is not UCAL_WALLTIME_LAST");
2049 }
2050
2051 if (ucal_getAttribute(ucal, UCAL_SKIPPED_WALL_TIME) != UCAL_WALLTIME_LAST) {
2052 log_err("FAIL: Default UCAL_SKIPPED_WALL_TIME value is not UCAL_WALLTIME_LAST");
2053 }
2054
2055 /* UCAL_WALLTIME_FIRST on US fall transition */
2056 ucal_setAttribute(ucal, UCAL_REPEATED_WALL_TIME, UCAL_WALLTIME_FIRST);
2057 ucal_clear(ucal);
2058 ucal_setDateTime(ucal, 2011, 11-1, 6, 1, 30, 0, &status);
2059 t = ucal_getMillis(ucal, &status);
2060 expected = 1320557400000.0; /* 2011-11-06T05:30:00Z */
2061 if (U_FAILURE(status)) {
2062 log_err("FAIL: Calculating time 2011-11-06 01:30:00 with UCAL_WALLTIME_FIRST - %s\n", u_errorName(status));
2063 status = U_ZERO_ERROR;
2064 } else if (t != expected) {
2065 log_data_err("FAIL: 2011-11-06 01:30:00 with UCAL_WALLTIME_FIRST - got: %f, expected: %f\n", t, expected);
2066 }
2067
2068 /* UCAL_WALLTIME_LAST on US fall transition */
2069 ucal_setAttribute(ucal, UCAL_REPEATED_WALL_TIME, UCAL_WALLTIME_LAST);
2070 ucal_clear(ucal);
2071 ucal_setDateTime(ucal, 2011, 11-1, 6, 1, 30, 0, &status);
2072 t = ucal_getMillis(ucal, &status);
2073 expected = 1320561000000.0; /* 2011-11-06T06:30:00Z */
2074 if (U_FAILURE(status)) {
2075 log_err("FAIL: Calculating time 2011-11-06 01:30:00 with UCAL_WALLTIME_LAST - %s\n", u_errorName(status));
2076 status = U_ZERO_ERROR;
2077 } else if (t != expected) {
2078 log_data_err("FAIL: 2011-11-06 01:30:00 with UCAL_WALLTIME_LAST - got: %f, expected: %f\n", t, expected);
2079 }
2080
2081 /* UCAL_WALLTIME_FIRST on US spring transition */
2082 ucal_setAttribute(ucal, UCAL_SKIPPED_WALL_TIME, UCAL_WALLTIME_FIRST);
2083 ucal_clear(ucal);
2084 ucal_setDateTime(ucal, 2011, 3-1, 13, 2, 30, 0, &status);
2085 t = ucal_getMillis(ucal, &status);
2086 expected = 1299997800000.0; /* 2011-03-13T06:30:00Z */
2087 if (U_FAILURE(status)) {
2088 log_err("FAIL: Calculating time 2011-03-13 02:30:00 with UCAL_WALLTIME_FIRST - %s\n", u_errorName(status));
2089 status = U_ZERO_ERROR;
2090 } else if (t != expected) {
2091 log_data_err("FAIL: 2011-03-13 02:30:00 with UCAL_WALLTIME_FIRST - got: %f, expected: %f\n", t, expected);
2092 }
2093
2094 /* UCAL_WALLTIME_LAST on US spring transition */
2095 ucal_setAttribute(ucal, UCAL_SKIPPED_WALL_TIME, UCAL_WALLTIME_LAST);
2096 ucal_clear(ucal);
2097 ucal_setDateTime(ucal, 2011, 3-1, 13, 2, 30, 0, &status);
2098 t = ucal_getMillis(ucal, &status);
2099 expected = 1300001400000.0; /* 2011-03-13T07:30:00Z */
2100 if (U_FAILURE(status)) {
2101 log_err("FAIL: Calculating time 2011-03-13 02:30:00 with UCAL_WALLTIME_LAST - %s\n", u_errorName(status));
2102 status = U_ZERO_ERROR;
2103 } else if (t != expected) {
2104 log_data_err("FAIL: 2011-03-13 02:30:00 with UCAL_WALLTIME_LAST - got: %f, expected: %f\n", t, expected);
2105 }
2106
2107 /* UCAL_WALLTIME_NEXT_VALID on US spring transition */
2108 ucal_setAttribute(ucal, UCAL_SKIPPED_WALL_TIME, UCAL_WALLTIME_NEXT_VALID);
2109 ucal_clear(ucal);
2110 ucal_setDateTime(ucal, 2011, 3-1, 13, 2, 30, 0, &status);
2111 t = ucal_getMillis(ucal, &status);
2112 expected = 1299999600000.0; /* 2011-03-13T07:00:00Z */
2113 if (U_FAILURE(status)) {
2114 log_err("FAIL: Calculating time 2011-03-13 02:30:00 with UCAL_WALLTIME_NEXT_VALID - %s\n", u_errorName(status));
2115 status = U_ZERO_ERROR;
2116 } else if (t != expected) {
2117 log_data_err("FAIL: 2011-03-13 02:30:00 with UCAL_WALLTIME_NEXT_VALID - got: %f, expected: %f\n", t, expected);
2118 }
2119
2120 /* non-lenient on US spring transition */
2121 ucal_setAttribute(ucal, UCAL_LENIENT, 0);
2122 ucal_clear(ucal);
2123 ucal_setDateTime(ucal, 2011, 3-1, 13, 2, 30, 0, &status);
2124 t = ucal_getMillis(ucal, &status);
2125 if (U_SUCCESS(status)) {
2126 /* must return error */
2127 log_data_err("FAIL: Non-lenient did not fail with 2011-03-13 02:30:00\n");
2128 status = U_ZERO_ERROR;
2129 }
2130
2131 ucal_close(ucal);
2132 }
2133
2134 /**
2135 * TestAddRollEra0AndEraBounds, for #9226
2136 */
2137
2138 typedef struct {
2139 const char * locale;
2140 UBool era0YearsGoBackwards; /* until we have API to get this, per #9393 */
2141 } EraTestItem;
2142
2143 static const EraTestItem eraTestItems[] = {
2144 /* calendars with non-modern era 0 that goes backwards, max era == 1 */
2145 { "en@calendar=gregorian", true },
2146 { "en@calendar=roc", true },
2147 { "en@calendar=coptic", true },
2148 /* calendars with non-modern era 0 that goes forwards, max era > 1 */
2149 { "en@calendar=japanese", false },
2150 { "en@calendar=chinese", false },
2151 /* calendars with non-modern era 0 that goes forwards, max era == 1 */
2152 { "en@calendar=ethiopic", false },
2153 /* calendars with only one era = 0, forwards */
2154 { "en@calendar=buddhist", false },
2155 { "en@calendar=hebrew", false },
2156 { "en@calendar=islamic", false },
2157 { "en@calendar=indian", false },
2158 { "en@calendar=persian", false },
2159 { "en@calendar=ethiopic-amete-alem", false },
2160 { NULL, false }
2161 };
2162
2163 static const UChar zoneGMT[] = { 0x47,0x4D,0x54,0 };
2164
TestAddRollEra0AndEraBounds()2165 void TestAddRollEra0AndEraBounds() {
2166 const EraTestItem * eraTestItemPtr;
2167 for (eraTestItemPtr = eraTestItems; eraTestItemPtr->locale != NULL; eraTestItemPtr++) {
2168 UErrorCode status = U_ZERO_ERROR;
2169 UCalendar *ucalTest = ucal_open(zoneGMT, -1, eraTestItemPtr->locale, UCAL_DEFAULT, &status);
2170 if ( U_SUCCESS(status) ) {
2171 int32_t yrBefore, yrAfter, yrMax, eraAfter, eraMax, eraNow;
2172
2173 status = U_ZERO_ERROR;
2174 ucal_clear(ucalTest);
2175 ucal_set(ucalTest, UCAL_YEAR, 2);
2176 ucal_set(ucalTest, UCAL_ERA, 0);
2177 yrBefore = ucal_get(ucalTest, UCAL_YEAR, &status);
2178 ucal_add(ucalTest, UCAL_YEAR, 1, &status);
2179 yrAfter = ucal_get(ucalTest, UCAL_YEAR, &status);
2180 if (U_FAILURE(status)) {
2181 log_err("FAIL: set era 0 year 2 then add 1 year and get year for %s, error %s\n",
2182 eraTestItemPtr->locale, u_errorName(status));
2183 } else if ( (eraTestItemPtr->era0YearsGoBackwards && yrAfter>yrBefore) ||
2184 (!eraTestItemPtr->era0YearsGoBackwards && yrAfter<yrBefore) ) {
2185 log_err("FAIL: era 0 add 1 year does not move forward in time for %s\n", eraTestItemPtr->locale);
2186 }
2187
2188 status = U_ZERO_ERROR;
2189 ucal_clear(ucalTest);
2190 ucal_set(ucalTest, UCAL_YEAR, 2);
2191 ucal_set(ucalTest, UCAL_ERA, 0);
2192 yrBefore = ucal_get(ucalTest, UCAL_YEAR, &status);
2193 ucal_roll(ucalTest, UCAL_YEAR, 1, &status);
2194 yrAfter = ucal_get(ucalTest, UCAL_YEAR, &status);
2195 if (U_FAILURE(status)) {
2196 log_err("FAIL: set era 0 year 2 then roll 1 year and get year for %s, error %s\n",
2197 eraTestItemPtr->locale, u_errorName(status));
2198 } else if ( (eraTestItemPtr->era0YearsGoBackwards && yrAfter>yrBefore) ||
2199 (!eraTestItemPtr->era0YearsGoBackwards && yrAfter<yrBefore) ) {
2200 log_err("FAIL: era 0 roll 1 year does not move forward in time for %s\n", eraTestItemPtr->locale);
2201 }
2202
2203 status = U_ZERO_ERROR;
2204 ucal_clear(ucalTest);
2205 ucal_set(ucalTest, UCAL_YEAR, 1);
2206 ucal_set(ucalTest, UCAL_ERA, 0);
2207 if (eraTestItemPtr->era0YearsGoBackwards) {
2208 ucal_roll(ucalTest, UCAL_YEAR, 1, &status); /* roll forward in time to era 0 boundary */
2209 yrAfter = ucal_get(ucalTest, UCAL_YEAR, &status);
2210 eraAfter = ucal_get(ucalTest, UCAL_ERA, &status);
2211 if (U_FAILURE(status)) {
2212 log_err("FAIL: set era 0 year 1 then roll 1 year and get year,era for %s, error %s\n",
2213 eraTestItemPtr->locale, u_errorName(status));
2214 /* all calendars with era0YearsGoBackwards have "unbounded" era0 year values, so we should pin at yr 1 */
2215 } else if (eraAfter != 0 || yrAfter != 1) {
2216 log_err("FAIL: era 0 roll 1 year from year 1 does not stay within era or pin to year 1 for %s (get era %d year %d)\n",
2217 eraTestItemPtr->locale, eraAfter, yrAfter);
2218 }
2219 } else {
2220 /* roll backward in time to where era 0 years go negative, except for the Chinese
2221 calendar, which uses negative eras instead of having years outside the range 1-60 */
2222 const char * calType = ucal_getType(ucalTest, &status);
2223 ucal_roll(ucalTest, UCAL_YEAR, -2, &status);
2224 yrAfter = ucal_get(ucalTest, UCAL_YEAR, &status);
2225 eraAfter = ucal_get(ucalTest, UCAL_ERA, &status);
2226 if (U_FAILURE(status)) {
2227 log_err("FAIL: set era 0 year 1 then roll -2 years and get year,era for %s, error %s\n",
2228 eraTestItemPtr->locale, u_errorName(status));
2229 } else if ( uprv_strcmp(calType,"chinese")!=0 && (eraAfter != 0 || yrAfter != -1) ) {
2230 log_err("FAIL: era 0 roll -2 years from year 1 does not stay within era or produce year -1 for %s (get era %d year %d)\n",
2231 eraTestItemPtr->locale, eraAfter, yrAfter);
2232 }
2233 }
2234
2235 status = U_ZERO_ERROR;
2236 ucal_clear(ucalTest);
2237 {
2238 int32_t eraMin = ucal_getLimit(ucalTest, UCAL_ERA, UCAL_MINIMUM, &status);
2239 const char * calType = ucal_getType(ucalTest, &status);
2240 if (eraMin != 0 && uprv_strcmp(calType, "chinese") != 0) {
2241 log_err("FAIL: ucal_getLimit returns minimum era %d (should be 0) for calType %s, error %s\n", eraMin, calType, u_errorName(status));
2242 }
2243 }
2244
2245 status = U_ZERO_ERROR;
2246 ucal_clear(ucalTest);
2247 ucal_set(ucalTest, UCAL_YEAR, 1);
2248 ucal_set(ucalTest, UCAL_ERA, 0);
2249 eraMax = ucal_getLimit(ucalTest, UCAL_ERA, UCAL_MAXIMUM, &status);
2250 if ( U_SUCCESS(status) && eraMax > 0 ) {
2251 /* try similar tests for era 1 (if calendar has it), in which years always go forward */
2252 status = U_ZERO_ERROR;
2253 ucal_clear(ucalTest);
2254 ucal_set(ucalTest, UCAL_YEAR, 2);
2255 ucal_set(ucalTest, UCAL_ERA, 1);
2256 yrBefore = ucal_get(ucalTest, UCAL_YEAR, &status);
2257 ucal_add(ucalTest, UCAL_YEAR, 1, &status);
2258 yrAfter = ucal_get(ucalTest, UCAL_YEAR, &status);
2259 if (U_FAILURE(status)) {
2260 log_err("FAIL: set era 1 year 2 then add 1 year and get year for %s, error %s\n",
2261 eraTestItemPtr->locale, u_errorName(status));
2262 } else if ( yrAfter<yrBefore ) {
2263 log_err("FAIL: era 1 add 1 year does not move forward in time for %s\n", eraTestItemPtr->locale);
2264 }
2265
2266 status = U_ZERO_ERROR;
2267 ucal_clear(ucalTest);
2268 ucal_set(ucalTest, UCAL_YEAR, 2);
2269 ucal_set(ucalTest, UCAL_ERA, 1);
2270 yrBefore = ucal_get(ucalTest, UCAL_YEAR, &status);
2271 ucal_roll(ucalTest, UCAL_YEAR, 1, &status);
2272 yrAfter = ucal_get(ucalTest, UCAL_YEAR, &status);
2273 if (U_FAILURE(status)) {
2274 log_err("FAIL: set era 1 year 2 then roll 1 year and get year for %s, error %s\n",
2275 eraTestItemPtr->locale, u_errorName(status));
2276 } else if ( yrAfter<yrBefore ) {
2277 log_err("FAIL: era 1 roll 1 year does not move forward in time for %s\n", eraTestItemPtr->locale);
2278 }
2279
2280 status = U_ZERO_ERROR;
2281 ucal_clear(ucalTest);
2282 ucal_set(ucalTest, UCAL_YEAR, 1);
2283 ucal_set(ucalTest, UCAL_ERA, 1);
2284 yrMax = ucal_getLimit(ucalTest, UCAL_YEAR, UCAL_ACTUAL_MAXIMUM, &status); /* max year value for era 1 */
2285 ucal_roll(ucalTest, UCAL_YEAR, -1, &status); /* roll down which should pin or wrap to end */
2286 yrAfter = ucal_get(ucalTest, UCAL_YEAR, &status);
2287 eraAfter = ucal_get(ucalTest, UCAL_ERA, &status);
2288 if (U_FAILURE(status)) {
2289 log_err("FAIL: set era 1 year 1 then roll -1 year and get year,era for %s, error %s\n",
2290 eraTestItemPtr->locale, u_errorName(status));
2291 /* if yrMax is reasonable we should wrap to that, else we should pin at yr 1 */
2292 } else if (yrMax >= 32768) {
2293 if (eraAfter != 1 || yrAfter != 1) {
2294 log_err("FAIL: era 1 roll -1 year from year 1 does not stay within era or pin to year 1 for %s (get era %d year %d)\n",
2295 eraTestItemPtr->locale, eraAfter, yrAfter);
2296 }
2297 } else if (eraAfter != 1 || yrAfter != yrMax) {
2298 log_err("FAIL: era 1 roll -1 year from year 1 does not stay within era or wrap to year %d for %s (get era %d year %d)\n",
2299 yrMax, eraTestItemPtr->locale, eraAfter, yrAfter);
2300 } else {
2301 /* now roll up which should wrap to beginning */
2302 ucal_roll(ucalTest, UCAL_YEAR, 1, &status); /* now roll up which should wrap to beginning */
2303 yrAfter = ucal_get(ucalTest, UCAL_YEAR, &status);
2304 eraAfter = ucal_get(ucalTest, UCAL_ERA, &status);
2305 if (U_FAILURE(status)) {
2306 log_err("FAIL: era 1 roll 1 year from end and get year,era for %s, error %s\n",
2307 eraTestItemPtr->locale, u_errorName(status));
2308 } else if (eraAfter != 1 || yrAfter != 1) {
2309 log_err("FAIL: era 1 roll 1 year from year %d does not stay within era or wrap to year 1 for %s (get era %d year %d)\n",
2310 yrMax, eraTestItemPtr->locale, eraAfter, yrAfter);
2311 }
2312 }
2313
2314 /* if current era > 1, try the same roll tests for current era */
2315 ucal_setMillis(ucalTest, ucal_getNow(), &status);
2316 eraNow = ucal_get(ucalTest, UCAL_ERA, &status);
2317 if ( U_SUCCESS(status) && eraNow > 1 ) {
2318 status = U_ZERO_ERROR;
2319 ucal_clear(ucalTest);
2320 ucal_set(ucalTest, UCAL_YEAR, 1);
2321 ucal_set(ucalTest, UCAL_ERA, eraNow);
2322 yrMax = ucal_getLimit(ucalTest, UCAL_YEAR, UCAL_ACTUAL_MAXIMUM, &status); /* max year value for this era */
2323 ucal_roll(ucalTest, UCAL_YEAR, -1, &status);
2324 yrAfter = ucal_get(ucalTest, UCAL_YEAR, &status);
2325 eraAfter = ucal_get(ucalTest, UCAL_ERA, &status);
2326 if (U_FAILURE(status)) {
2327 log_err("FAIL: set era %d year 1 then roll -1 year and get year,era for %s, error %s\n",
2328 eraNow, eraTestItemPtr->locale, u_errorName(status));
2329 /* if yrMax is reasonable we should wrap to that, else we should pin at yr 1 */
2330 } else if (yrMax >= 32768) {
2331 if (eraAfter != eraNow || yrAfter != 1) {
2332 log_err("FAIL: era %d roll -1 year from year 1 does not stay within era or pin to year 1 for %s (get era %d year %d)\n",
2333 eraNow, eraTestItemPtr->locale, eraAfter, yrAfter);
2334 }
2335 } else if (eraAfter != eraNow || yrAfter != yrMax) {
2336 log_err("FAIL: era %d roll -1 year from year 1 does not stay within era or wrap to year %d for %s (get era %d year %d)\n",
2337 eraNow, yrMax, eraTestItemPtr->locale, eraAfter, yrAfter);
2338 } else {
2339 /* now roll up which should wrap to beginning */
2340 ucal_roll(ucalTest, UCAL_YEAR, 1, &status); /* now roll up which should wrap to beginning */
2341 yrAfter = ucal_get(ucalTest, UCAL_YEAR, &status);
2342 eraAfter = ucal_get(ucalTest, UCAL_ERA, &status);
2343 if (U_FAILURE(status)) {
2344 log_err("FAIL: era %d roll 1 year from end and get year,era for %s, error %s\n",
2345 eraNow, eraTestItemPtr->locale, u_errorName(status));
2346 } else if (eraAfter != eraNow || yrAfter != 1) {
2347 log_err("FAIL: era %d roll 1 year from year %d does not stay within era or wrap to year 1 for %s (get era %d year %d)\n",
2348 eraNow, yrMax, eraTestItemPtr->locale, eraAfter, yrAfter);
2349 }
2350 }
2351 }
2352 }
2353
2354 ucal_close(ucalTest);
2355 } else {
2356 log_data_err("FAIL: ucal_open fails for zone GMT, locale %s, UCAL_DEFAULT\n", eraTestItemPtr->locale);
2357 }
2358 }
2359 }
2360
2361 /**
2362 * TestGetTZTransition, for #9606
2363 */
2364
2365 typedef struct {
2366 const char *descrip; /* test description */
2367 const UChar * zoneName; /* pointer to zero-terminated zone name */
2368 int32_t year; /* starting point for test is gregorian calendar noon on day specified by y,M,d here */
2369 int32_t month;
2370 int32_t day;
2371 UBool hasPrev; /* does it have a previous transition from starting point? If so we test inclusive from that */
2372 UBool hasNext; /* does it have a next transition from starting point? If so we test inclusive from that */
2373 } TZTransitionItem;
2374
2375 /* have zoneGMT above */
2376 static const UChar zoneUSPacific[] = { 0x55,0x53,0x2F,0x50,0x61,0x63,0x69,0x66,0x69,0x63,0 }; /* "US/Pacific" */
2377 static const UChar zoneCairo[] = { 0x41,0x66,0x72,0x69,0x63,0x61,0x2F,0x43,0x61,0x69,0x72,0x6F,0 }; /* "Africa/Cairo", DST cancelled since 2011 */
2378 static const UChar zoneIceland[] = { 0x41,0x74,0x6C,0x61,0x6E,0x74,0x69,0x63,0x2F,0x52,0x65,0x79,0x6B,0x6A,0x61,0x76,0x69,0x6B,0 }; /* "Atlantic/Reykjavik", always on DST (since when?) */
2379
2380 static const TZTransitionItem tzTransitionItems[] = {
2381 { "USPacific mid 2012", zoneUSPacific, 2012, UCAL_JULY, 1, true , true },
2382 { "USPacific mid 100", zoneUSPacific, 100, UCAL_JULY, 1, false, true }, /* no transitions before 100 CE... */
2383 { "Cairo mid 2012", zoneCairo, 2012, UCAL_JULY, 1, true , true }, /* DST cancelled since 2011 (Changed since 2014c) */
2384 { "Iceland mid 2012", zoneIceland, 2012, UCAL_JULY, 1, true , false }, /* always on DST */
2385 { NULL, NULL, 0, 0, 0, false, false } /* terminator */
2386 };
2387
TestGetTZTransition()2388 void TestGetTZTransition() {
2389 UErrorCode status = U_ZERO_ERROR;
2390 UCalendar * ucal = ucal_open(zoneGMT, -1, "en", UCAL_GREGORIAN, &status);
2391 if ( U_SUCCESS(status) ) {
2392 const TZTransitionItem * itemPtr;
2393 for (itemPtr = tzTransitionItems; itemPtr->descrip != NULL; itemPtr++) {
2394 UDate curMillis;
2395 ucal_setTimeZone(ucal, itemPtr->zoneName, -1, &status);
2396 ucal_setDateTime(ucal, itemPtr->year, itemPtr->month, itemPtr->day, 12, 0, 0, &status);
2397 curMillis = ucal_getMillis(ucal, &status);
2398 (void)curMillis; /* Suppress set but not used warning. */
2399 if ( U_SUCCESS(status) ) {
2400 UDate transition1, transition2;
2401 UBool result;
2402
2403 result = ucal_getTimeZoneTransitionDate(ucal, UCAL_TZ_TRANSITION_PREVIOUS, &transition1, &status);
2404 if (U_FAILURE(status) || result != itemPtr->hasPrev) {
2405 log_data_err("FAIL: %s ucal_getTimeZoneTransitionDate prev status %s, expected result %d but got %d\n",
2406 itemPtr->descrip, u_errorName(status), itemPtr->hasPrev, result);
2407 } else if (result) {
2408 ucal_setMillis(ucal, transition1, &status);
2409 result = ucal_getTimeZoneTransitionDate(ucal, UCAL_TZ_TRANSITION_PREVIOUS_INCLUSIVE, &transition2, &status);
2410 if (U_FAILURE(status) || !result || transition2 != transition1) {
2411 log_err("FAIL: %s ucal_getTimeZoneTransitionDate prev_inc status %s, result %d, expected date %.1f but got %.1f\n",
2412 itemPtr->descrip, u_errorName(status), result, transition1, transition2);
2413 }
2414 }
2415 status = U_ZERO_ERROR;
2416
2417 result = ucal_getTimeZoneTransitionDate(ucal, UCAL_TZ_TRANSITION_NEXT, &transition1, &status);
2418 if (U_FAILURE(status) || result != itemPtr->hasNext) {
2419 log_data_err("FAIL: %s ucal_getTimeZoneTransitionDate next status %s, expected result %d but got %d\n",
2420 itemPtr->descrip, u_errorName(status), itemPtr->hasNext, result);
2421 } else if (result) {
2422 ucal_setMillis(ucal, transition1, &status);
2423 result = ucal_getTimeZoneTransitionDate(ucal, UCAL_TZ_TRANSITION_NEXT_INCLUSIVE, &transition2, &status);
2424 if (U_FAILURE(status) || !result || transition2 != transition1) {
2425 log_err("FAIL: %s ucal_getTimeZoneTransitionDate next_inc status %s, result %d, expected date %.1f but got %.1f\n",
2426 itemPtr->descrip, u_errorName(status), result, transition1, transition2);
2427 }
2428 }
2429 status = U_ZERO_ERROR;
2430 } else {
2431 log_data_err("FAIL setup: can't setup calendar for %s, status %s\n",
2432 itemPtr->descrip, u_errorName(status));
2433 status = U_ZERO_ERROR;
2434 }
2435 }
2436 ucal_close(ucal);
2437 } else {
2438 log_data_err("FAIL setup: ucal_open status %s\n", u_errorName(status));
2439 }
2440 }
2441
2442 static const UChar winEastern[] = /* Eastern Standard Time */
2443 {0x45,0x61,0x73,0x74,0x65,0x72,0x6E,0x20,0x53,0x74,0x61,0x6E,0x64,0x61,0x72,0x64,0x20,0x54,0x69,0x6D,0x65,0x00};
2444
2445 static const UChar tzNewYork[] = /* America/New_York */
2446 {0x41,0x6D,0x65,0x72,0x69,0x63,0x61,0x2F,0x4E,0x65,0x77,0x5F,0x59,0x6F,0x72,0x6B,0x00};
2447 static const UChar tzTronto[] = /* America/Toronto */
2448 {0x41,0x6D,0x65,0x72,0x69,0x63,0x61,0x2F,0x54,0x6F,0x72,0x6F,0x6E,0x74,0x6F,0x00};
2449
2450 static const UChar sBogus[] = /* Bogus */
2451 {0x42,0x6F,0x67,0x75,0x73,0x00};
2452
2453 #ifndef U_DEBUG
2454 static const UChar sBogusWithVariantCharacters[] = /* Bogus with Variant characters: Hèℓℓô Wôřℓδ */
2455 {0x48,0xE8,0x2113,0x2113,0xF4,0x20,0x57,0xF4,0x159,0x2113,0x3B4,0x00};
2456 #endif
2457
TestGetWindowsTimeZoneID()2458 void TestGetWindowsTimeZoneID() {
2459 UErrorCode status;
2460 UChar winID[64];
2461 int32_t len;
2462
2463 {
2464 status = U_ZERO_ERROR;
2465 len = ucal_getWindowsTimeZoneID(tzNewYork, u_strlen(tzNewYork), winID, UPRV_LENGTHOF(winID), &status);
2466 if (U_FAILURE(status)) {
2467 log_data_err("FAIL: Windows ID for America/New_York, status %s\n", u_errorName(status));
2468 } else if (len != u_strlen(winEastern) || u_strncmp(winID, winEastern, len) != 0) {
2469 log_data_err("FAIL: Windows ID for America/New_York\n");
2470 }
2471 }
2472 {
2473 status = U_ZERO_ERROR;
2474 len = ucal_getWindowsTimeZoneID(tzTronto, u_strlen(tzTronto), winID, UPRV_LENGTHOF(winID), &status);
2475 if (U_FAILURE(status)) {
2476 log_data_err("FAIL: Windows ID for America/Toronto, status %s\n", u_errorName(status));
2477 } else if (len != u_strlen(winEastern) || u_strncmp(winID, winEastern, len) != 0) {
2478 log_data_err("FAIL: Windows ID for America/Toronto\n");
2479 }
2480 }
2481 {
2482 status = U_ZERO_ERROR;
2483 len = ucal_getWindowsTimeZoneID(sBogus, u_strlen(sBogus), winID, UPRV_LENGTHOF(winID), &status);
2484 if (U_FAILURE(status)) {
2485 log_data_err("FAIL: Windows ID for Bogus, status %s\n", u_errorName(status));
2486 } else if (len != 0) {
2487 log_data_err("FAIL: Windows ID for Bogus\n");
2488 }
2489 }
2490 }
2491
TestGetTimeZoneIDByWindowsID()2492 void TestGetTimeZoneIDByWindowsID() {
2493 UErrorCode status;
2494 UChar tzID[64];
2495 int32_t len;
2496
2497 {
2498 status = U_ZERO_ERROR;
2499 len = ucal_getTimeZoneIDForWindowsID(winEastern, -1, NULL, tzID, UPRV_LENGTHOF(tzID), &status);
2500 if (U_FAILURE(status)) {
2501 log_data_err("FAIL: TZ ID for Eastern Standard Time, status %s\n", u_errorName(status));
2502 } else if (len != u_strlen(tzNewYork) || u_strncmp(tzID, tzNewYork, len) != 0) {
2503 log_err("FAIL: TZ ID for Eastern Standard Time\n");
2504 }
2505 }
2506 {
2507 status = U_ZERO_ERROR;
2508 len = ucal_getTimeZoneIDForWindowsID(winEastern, u_strlen(winEastern), "US", tzID, UPRV_LENGTHOF(tzID), &status);
2509 if (U_FAILURE(status)) {
2510 log_data_err("FAIL: TZ ID for Eastern Standard Time - US, status %s\n", u_errorName(status));
2511 } else if (len != u_strlen(tzNewYork) || u_strncmp(tzID, tzNewYork, len) != 0) {
2512 log_err("FAIL: TZ ID for Eastern Standard Time - US\n");
2513 }
2514 }
2515 {
2516 status = U_ZERO_ERROR;
2517 len = ucal_getTimeZoneIDForWindowsID(winEastern, u_strlen(winEastern), "CA", tzID, UPRV_LENGTHOF(tzID), &status);
2518 if (U_FAILURE(status)) {
2519 log_data_err("FAIL: TZ ID for Eastern Standard Time - CA, status %s\n", u_errorName(status));
2520 } else if (len != u_strlen(tzTronto) || u_strncmp(tzID, tzTronto, len) != 0) {
2521 log_err("FAIL: TZ ID for Eastern Standard Time - CA\n");
2522 }
2523 }
2524 {
2525 status = U_ZERO_ERROR;
2526 len = ucal_getTimeZoneIDForWindowsID(sBogus, -1, NULL, tzID, UPRV_LENGTHOF(tzID), &status);
2527 if (U_FAILURE(status)) {
2528 log_data_err("FAIL: TZ ID for Bogus, status %s\n", u_errorName(status));
2529 } else if (len != 0) {
2530 log_err("FAIL: TZ ID for Bogus\n");
2531 }
2532 }
2533 #ifndef U_DEBUG
2534 // This test is only for release mode because it will cause an assertion failure in debug builds.
2535 // We don't check the API result for errors as the only purpose of this test is to ensure that
2536 // input variant characters don't cause abort() to be called and/or that ICU doesn't crash.
2537 {
2538 status = U_ZERO_ERROR;
2539 len = ucal_getTimeZoneIDForWindowsID(sBogusWithVariantCharacters, -1, NULL, tzID, UPRV_LENGTHOF(tzID), &status);
2540 }
2541 #endif
2542 }
2543
2544 // The following currently assumes that Reiwa is the last known/valid era.
2545 // Filed ICU-20551 to generalize this when we have more time...
TestJpnCalAddSetNextEra()2546 void TestJpnCalAddSetNextEra() {
2547 UErrorCode status = U_ZERO_ERROR;
2548 UCalendar *jCal = ucal_open(NULL, 0, "ja_JP@calendar=japanese", UCAL_DEFAULT, &status);
2549 if ( U_FAILURE(status) ) {
2550 log_data_err("FAIL: ucal_open for ja_JP@calendar=japanese, status %s\n", u_errorName(status));
2551 } else {
2552 ucal_clear(jCal); // This sets to 1970, in Showa
2553 int32_t sEra = ucal_get(jCal, UCAL_ERA, &status); // Don't assume era number for Showa
2554 if ( U_FAILURE(status) ) {
2555 log_data_err("FAIL: ucal_get ERA for Showa, status %s\n", u_errorName(status));
2556 } else {
2557 int32_t iEra, eYear;
2558 int32_t startYears[4] = { 1926, 1989, 2019, 0 }; // start years for Showa, Heisei, Reiwa; 0 marks invalid era
2559 for (iEra = 1; iEra < 3; iEra++) {
2560 status = U_ZERO_ERROR;
2561 ucal_clear(jCal);
2562 ucal_set(jCal, UCAL_ERA, sEra+iEra);
2563 eYear = ucal_get(jCal, UCAL_EXTENDED_YEAR, &status);
2564 if ( U_FAILURE(status) ) {
2565 log_err("FAIL: set %d, ucal_get EXTENDED_YEAR, status %s\n", iEra, u_errorName(status));
2566 } else if (eYear != startYears[iEra]) {
2567 log_err("ERROR: set %d, expected start year %d but get %d\n", iEra, startYears[iEra], eYear);
2568 } else {
2569 ucal_add(jCal, UCAL_ERA, 1, &status);
2570 if ( U_FAILURE(status) ) {
2571 log_err("FAIL: set %d, ucal_add ERA 1, status %s\n", iEra, u_errorName(status));
2572 } else {
2573 eYear = ucal_get(jCal, UCAL_EXTENDED_YEAR, &status);
2574 if ( U_FAILURE(status) ) {
2575 log_err("FAIL: set %d then add ERA 1, ucal_get EXTENDED_YEAR, status %s\n", iEra, u_errorName(status));
2576 } else {
2577 // If this is the last valid era, we expect adding an era to pin to the current era
2578 int32_t nextEraStart = (startYears[iEra+1] == 0)? startYears[iEra]: startYears[iEra+1];
2579 if (eYear != nextEraStart) {
2580 log_err("ERROR: set %d then add ERA 1, expected start year %d but get %d\n", iEra, nextEraStart, eYear);
2581 }
2582 }
2583 }
2584 }
2585 }
2586 }
2587 ucal_close(jCal);
2588 }
2589 }
2590
TestUcalOpenBufferRead()2591 void TestUcalOpenBufferRead() {
2592 // ICU-21004: The issue shows under valgrind or as an Address Sanitizer failure.
2593 UErrorCode status = U_ZERO_ERROR;
2594 // string length: 157 + 1 + 100 = 258
2595 const char *localeID = "x-privatebutreallylongtagfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobar-foobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoorbarfoobarfoo";
2596 UCalendar *cal = ucal_open(NULL, 0, localeID, UCAL_GREGORIAN, &status);
2597 ucal_close(cal);
2598 }
2599
2600
2601 /*
2602 * Testing ucal_getTimeZoneOffsetFromLocal
2603 */
2604 void
TestGetTimeZoneOffsetFromLocal()2605 TestGetTimeZoneOffsetFromLocal() {
2606 static const UChar utc[] = u"Etc/GMT";
2607
2608 const int32_t HOUR = 60*60*1000;
2609 const int32_t MINUTE = 60*1000;
2610
2611 const int32_t DATES[][6] = {
2612 {2006, UCAL_APRIL, 2, 1, 30, 1*HOUR+30*MINUTE},
2613 {2006, UCAL_APRIL, 2, 2, 00, 2*HOUR},
2614 {2006, UCAL_APRIL, 2, 2, 30, 2*HOUR+30*MINUTE},
2615 {2006, UCAL_APRIL, 2, 3, 00, 3*HOUR},
2616 {2006, UCAL_APRIL, 2, 3, 30, 3*HOUR+30*MINUTE},
2617 {2006, UCAL_OCTOBER, 29, 0, 30, 0*HOUR+30*MINUTE},
2618 {2006, UCAL_OCTOBER, 29, 1, 00, 1*HOUR},
2619 {2006, UCAL_OCTOBER, 29, 1, 30, 1*HOUR+30*MINUTE},
2620 {2006, UCAL_OCTOBER, 29, 2, 00, 2*HOUR},
2621 {2006, UCAL_OCTOBER, 29, 2, 30, 2*HOUR+30*MINUTE},
2622 };
2623
2624 // Expected offsets by
2625 // void U_ucal_getTimeZoneOffsetFromLocal(
2626 // const UCalendar* cal,
2627 // UTimeZoneLocalOption nonExistingTimeOpt,
2628 // UTimeZoneLocalOption duplicatedTimeOpt,
2629 // int32_t* rawOffset, int32_t* dstOffset, UErrorCode* status);
2630 // with nonExistingTimeOpt=UCAL_TZ_LOCAL_STANDARD and
2631 // duplicatedTimeOpt=UCAL_TZ_LOCAL_STANDARD
2632 const int32_t OFFSETS2[][2] = {
2633 // April 2, 2006
2634 {-8*HOUR, 0},
2635 {-8*HOUR, 0},
2636 {-8*HOUR, 0},
2637 {-8*HOUR, 1*HOUR},
2638 {-8*HOUR, 1*HOUR},
2639
2640 // Oct 29, 2006
2641 {-8*HOUR, 1*HOUR},
2642 {-8*HOUR, 0},
2643 {-8*HOUR, 0},
2644 {-8*HOUR, 0},
2645 {-8*HOUR, 0},
2646 };
2647
2648 // Expected offsets by
2649 // void U_ucal_getTimeZoneOffsetFromLocal(
2650 // const UCalendar* cal,
2651 // UTimeZoneLocalOption nonExistingTimeOpt,
2652 // UTimeZoneLocalOption duplicatedTimeOpt,
2653 // int32_t* rawOffset, int32_t* dstOffset, UErrorCode* status);
2654 // with nonExistingTimeOpt=UCAL_TZ_LOCAL_DAYLIGHT and
2655 // duplicatedTimeOpt=UCAL_TZ_LOCAL_DAYLIGHT
2656 const int32_t OFFSETS3[][2] = {
2657 // April 2, 2006
2658 {-8*HOUR, 0},
2659 {-8*HOUR, 1*HOUR},
2660 {-8*HOUR, 1*HOUR},
2661 {-8*HOUR, 1*HOUR},
2662 {-8*HOUR, 1*HOUR},
2663
2664 // October 29, 2006
2665 {-8*HOUR, 1*HOUR},
2666 {-8*HOUR, 1*HOUR},
2667 {-8*HOUR, 1*HOUR},
2668 {-8*HOUR, 0},
2669 {-8*HOUR, 0},
2670 };
2671
2672 UErrorCode status = U_ZERO_ERROR;
2673
2674 int32_t rawOffset, dstOffset;
2675 UCalendar *cal = ucal_open(utc, -1, "en", UCAL_GREGORIAN, &status);
2676 if (U_FAILURE(status)) {
2677 log_data_err("ucal_open: %s", u_errorName(status));
2678 return;
2679 }
2680
2681 // Calculate millis
2682 UDate MILLIS[UPRV_LENGTHOF(DATES)];
2683 for (int32_t i = 0; i < UPRV_LENGTHOF(DATES); i++) {
2684 ucal_setDateTime(cal, DATES[i][0], DATES[i][1], DATES[i][2],
2685 DATES[i][3], DATES[i][4], 0, &status);
2686 MILLIS[i] = ucal_getMillis(cal, &status);
2687 if (U_FAILURE(status)) {
2688 log_data_err("ucal_getMillis failed");
2689 return;
2690 }
2691 }
2692 ucal_setTimeZone(cal, AMERICA_LOS_ANGELES, -1, &status);
2693
2694 // Test void ucal_getTimeZoneOffsetFromLocal(
2695 // const UCalendar* cal,
2696 // UTimeZoneLocalOption nonExistingTimeOpt,
2697 // UTimeZoneLocalOption duplicatedTimeOpt,
2698 // int32_t* rawOffset, int32_t* dstOffset, UErrorCode* status);
2699 // with nonExistingTimeOpt=UCAL_TZ_LOCAL_STANDARD and
2700 // duplicatedTimeOpt=UCAL_TZ_LOCAL_STANDARD
2701 for (int m = 0; m < UPRV_LENGTHOF(DATES); m++) {
2702 status = U_ZERO_ERROR;
2703 ucal_setMillis(cal, MILLIS[m], &status);
2704 if (U_FAILURE(status)) {
2705 log_data_err("ucal_setMillis: %s\n", u_errorName(status));
2706 }
2707
2708 ucal_getTimeZoneOffsetFromLocal(cal, UCAL_TZ_LOCAL_STANDARD_FORMER, UCAL_TZ_LOCAL_STANDARD_LATTER,
2709 &rawOffset, &dstOffset, &status);
2710 if (U_FAILURE(status)) {
2711 log_err("ERROR: ucal_getTimeZoneOffsetFromLocal((%d-%d-%d %d:%d:0),"
2712 "UCAL_TZ_LOCAL_STANDARD_FORMER, UCAL_TZ_LOCAL_STANDARD_LATTER: %s\n",
2713 DATES[m][0], DATES[m][1], DATES[m][2], DATES[m][3], DATES[m][4],
2714 u_errorName(status));
2715 } else if (rawOffset != OFFSETS2[m][0] || dstOffset != OFFSETS2[m][1]) {
2716 log_err("Bad offset returned at (%d-%d-%d %d:%d:0) "
2717 "(wall/UCAL_TZ_LOCAL_STANDARD_FORMER/UCAL_TZ_LOCAL_STANDARD_LATTER) \n- Got: %d / %d "
2718 " Expected %d / %d\n",
2719 DATES[m][0], DATES[m][1], DATES[m][2], DATES[m][3], DATES[m][4],
2720 rawOffset, dstOffset, OFFSETS2[m][0], OFFSETS2[m][1]);
2721 }
2722 }
2723
2724 // Test void ucal_getTimeZoneOffsetFromLocal(
2725 // const UCalendar* cal,
2726 // UTimeZoneLocalOption nonExistingTimeOpt,
2727 // UTimeZoneLocalOption duplicatedTimeOpt,
2728 // int32_t* rawOffset, int32_t* dstOffset, UErrorCode* status);
2729 // with nonExistingTimeOpt=UCAL_TZ_LOCAL_DAYLIGHT and
2730 // duplicatedTimeOpt=UCAL_TZ_LOCAL_DAYLIGHT
2731 for (int m = 0; m < UPRV_LENGTHOF(DATES); m++) {
2732 status = U_ZERO_ERROR;
2733 ucal_setMillis(cal, MILLIS[m], &status);
2734 if (U_FAILURE(status)) {
2735 log_data_err("ucal_setMillis: %s\n", u_errorName(status));
2736 }
2737
2738 ucal_getTimeZoneOffsetFromLocal(cal, UCAL_TZ_LOCAL_DAYLIGHT_LATTER, UCAL_TZ_LOCAL_DAYLIGHT_FORMER,
2739 &rawOffset, &dstOffset, &status);
2740 if (U_FAILURE(status)) {
2741 log_err("ERROR: ucal_getTimeZoneOffsetFromLocal((%d-%d-%d %d:%d:0),"
2742 "UCAL_TZ_LOCAL_DAYLIGHT_LATTER, UCAL_TZ_LOCAL_DAYLIGHT_FORMER: %s\n",
2743 DATES[m][0], DATES[m][1], DATES[m][2], DATES[m][3], DATES[m][4],
2744 u_errorName(status));
2745 } else if (rawOffset != OFFSETS3[m][0] || dstOffset != OFFSETS3[m][1]) {
2746 log_err("Bad offset returned at (%d-%d-%d %d:%d:0) "
2747 "(wall/UCAL_TZ_LOCAL_DAYLIGHT_LATTER/UCAL_TZ_LOCAL_DAYLIGHT_FORMER) \n- Got: %d / %d "
2748 " Expected %d / %d\n",
2749 DATES[m][0], DATES[m][1], DATES[m][2], DATES[m][3], DATES[m][4],
2750 rawOffset, dstOffset, OFFSETS3[m][0], OFFSETS3[m][1]);
2751 }
2752 }
2753
2754 // Test void ucal_getTimeZoneOffsetFromLocal(
2755 // const UCalendar* cal,
2756 // UTimeZoneLocalOption nonExistingTimeOpt,
2757 // UTimeZoneLocalOption duplicatedTimeOpt,
2758 // int32_t* rawOffset, int32_t* dstOffset, UErrorCode* status);
2759 // with nonExistingTimeOpt=UCAL_TZ_LOCAL_FORMER and
2760 // duplicatedTimeOpt=UCAL_TZ_LOCAL_LATTER
2761 for (int m = 0; m < UPRV_LENGTHOF(DATES); m++) {
2762 status = U_ZERO_ERROR;
2763 ucal_setMillis(cal, MILLIS[m], &status);
2764 if (U_FAILURE(status)) {
2765 log_data_err("ucal_setMillis: %s\n", u_errorName(status));
2766 }
2767
2768 ucal_getTimeZoneOffsetFromLocal(cal, UCAL_TZ_LOCAL_FORMER, UCAL_TZ_LOCAL_LATTER,
2769 &rawOffset, &dstOffset, &status);
2770 if (U_FAILURE(status)) {
2771 log_err("ERROR: ucal_getTimeZoneOffsetFromLocal((%d-%d-%d %d:%d:0),"
2772 "UCAL_TZ_LOCAL_FORMER, UCAL_TZ_LOCAL_LATTER: %s\n",
2773 DATES[m][0], DATES[m][1], DATES[m][2], DATES[m][3], DATES[m][4],
2774 u_errorName(status));
2775 } else if (rawOffset != OFFSETS2[m][0] || dstOffset != OFFSETS2[m][1]) {
2776 log_err("Bad offset returned at (%d-%d-%d %d:%d:0) "
2777 "(wall/UCAL_TZ_LOCAL_FORMER/UCAL_TZ_LOCAL_LATTER) \n- Got: %d / %d "
2778 " Expected %d / %d\n",
2779 DATES[m][0], DATES[m][1], DATES[m][2], DATES[m][3], DATES[m][4],
2780 rawOffset, dstOffset, OFFSETS2[m][0], OFFSETS2[m][1]);
2781 }
2782 }
2783
2784 // Test void ucal_getTimeZoneOffsetFromLocal(
2785 // const UCalendar* cal,
2786 // UTimeZoneLocalOption nonExistingTimeOpt,
2787 // UTimeZoneLocalOption duplicatedTimeOpt,
2788 // int32_t* rawOffset, int32_t* dstOffset, UErrorCode* status);
2789 // with nonExistingTimeOpt=UCAL_TZ_LOCAL_LATTER and
2790 // duplicatedTimeOpt=UCAL_TZ_LOCAL_FORMER
2791 for (int m = 0; m < UPRV_LENGTHOF(DATES); m++) {
2792 status = U_ZERO_ERROR;
2793 ucal_setMillis(cal, MILLIS[m], &status);
2794 if (U_FAILURE(status)) {
2795 log_data_err("ucal_setMillis: %s\n", u_errorName(status));
2796 }
2797
2798 ucal_getTimeZoneOffsetFromLocal(cal, UCAL_TZ_LOCAL_LATTER, UCAL_TZ_LOCAL_FORMER,
2799 &rawOffset, &dstOffset, &status);
2800 if (U_FAILURE(status)) {
2801 log_err("ERROR: ucal_getTimeZoneOffsetFromLocal((%d-%d-%d %d:%d:0),"
2802 "UCAL_TZ_LOCAL_LATTER, UCAL_TZ_LOCAL_FORMER: %s\n",
2803 DATES[m][0], DATES[m][1], DATES[m][2], DATES[m][3], DATES[m][4],
2804 u_errorName(status));
2805 } else if (rawOffset != OFFSETS3[m][0] || dstOffset != OFFSETS3[m][1]) {
2806 log_err("Bad offset returned at (%d-%d-%d %d:%d:0) "
2807 "(wall/UCAL_TZ_LOCAL_LATTER/UCAL_TZ_LOCAL_FORMER) \n- Got: %d / %d "
2808 " Expected %d / %d\n",
2809 DATES[m][0], DATES[m][1], DATES[m][2], DATES[m][3], DATES[m][4],
2810 rawOffset, dstOffset, OFFSETS3[m][0], OFFSETS3[m][1]);
2811 }
2812 }
2813 ucal_close(cal);
2814 }
2815
2816 void
TestFWWithISO8601()2817 TestFWWithISO8601() {
2818 /* UCAL_SUNDAY is 1, UCAL_MONDAY is 2, ..., UCAL_SATURDAY is 7 */
2819 const char* LOCALES[] = {
2820 "",
2821 "en-u-ca-iso8601-fw-sun",
2822 "en-u-ca-iso8601-fw-mon",
2823 "en-u-ca-iso8601-fw-tue",
2824 "en-u-ca-iso8601-fw-wed",
2825 "en-u-ca-iso8601-fw-thu",
2826 "en-u-ca-iso8601-fw-fri",
2827 "en-u-ca-iso8601-fw-sat",
2828 };
2829 for (int32_t i = UCAL_SUNDAY; i <= UCAL_SATURDAY; i++) {
2830 const char* locale = LOCALES[i];
2831 UErrorCode status = U_ZERO_ERROR;
2832 UCalendar* cal = ucal_open(0, 0, locale, UCAL_TRADITIONAL, &status);
2833 if(U_FAILURE(status)){
2834 log_data_err("FAIL: error in ucal_open caldef : %s\n - (Are you missing data?)", u_errorName(status));
2835 }
2836 int32_t actual = ucal_getAttribute(cal, UCAL_FIRST_DAY_OF_WEEK);
2837 if (i != actual) {
2838 log_err("ERROR: ucal_getAttribute(\"%s\", UCAL_FIRST_DAY_OF_WEEK) should be %d but get %d\n",
2839 locale, i, actual);
2840 }
2841 ucal_close(cal);
2842 }
2843 }
2844
2845 void
TestGetIanaTimeZoneID()2846 TestGetIanaTimeZoneID() {
2847 const UChar* UNKNOWN = u"Etc/Unknown";
2848 typedef struct {
2849 const UChar* id;
2850 const UChar* expected;
2851 } IanaTimeZoneIDTestData;
2852
2853 const IanaTimeZoneIDTestData TESTDATA[] = {
2854 {u"", UNKNOWN},
2855 {0, UNKNOWN},
2856 {UNKNOWN, UNKNOWN},
2857 {u"America/New_York", u"America/New_York"},
2858 {u"Asia/Calcutta", u"Asia/Kolkata"},
2859 {u"Europe/Kiev", u"Europe/Kyiv"},
2860 {u"Europe/Zaporozhye", u"Europe/Kyiv"},
2861 {u"Etc/GMT-1", u"Etc/GMT-1"},
2862 {u"Etc/GMT+20", UNKNOWN},
2863 {u"PST8PDT", u"PST8PDT"},
2864 {u"GMT-08:00", UNKNOWN},
2865 {0, 0}
2866 };
2867
2868 for (int32_t i = 0; TESTDATA[i].expected != 0; i++) {
2869 UErrorCode sts = U_ZERO_ERROR;
2870 UChar ianaID[128];
2871 int32_t ianaLen = 0;
2872
2873 ianaLen = ucal_getIanaTimeZoneID(TESTDATA[i].id, -1, ianaID, sizeof(ianaID), &sts);
2874
2875 if (u_strcmp(TESTDATA[i].expected, UNKNOWN) == 0) {
2876 if (sts != U_ILLEGAL_ARGUMENT_ERROR) {
2877 log_err("Expected U_ILLEGAL_ERROR: TESTDATA[%d]", i);
2878 }
2879 } else {
2880 if (u_strlen(TESTDATA[i].expected) != ianaLen || u_strncmp(TESTDATA[i].expected, ianaID, ianaLen) != 0) {
2881 log_err("Error: TESTDATA[%d]", i);
2882 }
2883 // Calling ucal_getIanaTimeZoneID with an IANA ID should return the same
2884 UChar ianaID2[128];
2885 int32_t ianaLen2 = 0;
2886 ianaLen2 = ucal_getIanaTimeZoneID(ianaID, ianaLen, ianaID2, sizeof(ianaID2), &sts);
2887 if (U_FAILURE(sts) || ianaLen != ianaLen2 || u_strncmp(ianaID, ianaID2, ianaLen) != 0) {
2888 log_err("Error: IANA ID for IANA ID %s", ianaID);
2889 }
2890 }
2891 }
2892 }
2893
2894 #endif /* #if !UCONFIG_NO_FORMATTING */
2895