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