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