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