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