]> git.saurik.com Git - apple/icu.git/blob - icuSources/test/cintltst/ccaltst.c
ICU-400.39.tar.gz
[apple/icu.git] / icuSources / test / cintltst / ccaltst.c
1 /********************************************************************
2 * Copyright (c) 1997-2008, 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
32 void TestGregorianChange(void);
33
34 void addCalTest(TestNode** root);
35
36 void addCalTest(TestNode** root)
37 {
38
39 addTest(root, &TestCalendar, "tsformat/ccaltst/TestCalendar");
40 addTest(root, &TestGetSetDateAPI, "tsformat/ccaltst/TestGetSetDateAPI");
41 addTest(root, &TestFieldGetSet, "tsformat/ccaltst/TestFieldGetSet");
42 addTest(root, &TestAddRollExtensive, "tsformat/ccaltst/TestAddRollExtensive");
43 addTest(root, &TestGetLimits, "tsformat/ccaltst/TestGetLimits");
44 addTest(root, &TestDOWProgression, "tsformat/ccaltst/TestDOWProgression");
45 addTest(root, &TestGMTvsLocal, "tsformat/ccaltst/TestGMTvsLocal");
46 addTest(root, &TestGregorianChange, "tsformat/ccaltst/TestGregorianChange");
47 }
48
49 /* "GMT" */
50 static const UChar fgGMTID [] = { 0x0047, 0x004d, 0x0054, 0x0000 };
51
52 /* "PST" */
53 static const UChar PST[] = {0x50, 0x53, 0x54, 0x00}; /* "PST" */
54
55 static const UChar EUROPE_PARIS[] = {0x45, 0x75, 0x72, 0x6F, 0x70, 0x65, 0x2F, 0x50, 0x61, 0x72, 0x69, 0x73, 0x00}; /* "Europe/Paris" */
56
57 static const UChar AMERICA_LOS_ANGELES[] = {0x41, 0x6D, 0x65, 0x72, 0x69, 0x63, 0x61, 0x2F,
58 0x4C, 0x6F, 0x73, 0x5F, 0x41, 0x6E, 0x67, 0x65, 0x6C, 0x65, 0x73, 0x00}; /* America/Los_Angeles */
59
60 typedef struct {
61 const char * locale;
62 UCalendarType calType;
63 const char * expectedResult;
64 } UCalGetTypeTest;
65
66 static const UCalGetTypeTest ucalGetTypeTests[] = {
67 { "en_US", UCAL_GREGORIAN, "gregorian" },
68 { "ja_JP@calendar=japanese", UCAL_DEFAULT, "japanese" },
69 { "th_TH", UCAL_GREGORIAN, "gregorian" },
70 { "th_TH", UCAL_DEFAULT, "buddhist" },
71 { NULL, 0, NULL } /* terminator */
72 };
73
74 static void TestCalendar()
75 {
76 UCalendar *caldef = 0, *caldef2 = 0, *calfr = 0, *calit = 0, *calfrclone = 0;
77 UEnumeration* uenum = NULL;
78 int32_t count, count2, i,j;
79 UChar tzID[4];
80 UChar *tzdname = 0;
81 UErrorCode status = U_ZERO_ERROR;
82 UDate now;
83 UDateFormat *datdef = 0;
84 UChar *result = 0;
85 int32_t resultlength, resultlengthneeded;
86 char tempMsgBuf[256];
87 UChar zone1[32], zone2[32];
88 const char *tzver = 0;
89 UChar canonicalID[64];
90 UBool isSystemID = FALSE;
91 const UCalGetTypeTest * ucalGetTypeTestPtr;
92
93 #ifdef U_USE_UCAL_OBSOLETE_2_8
94 /*Testing countAvailableTimeZones*/
95 int32_t offset=0;
96 log_verbose("\nTesting ucal_countAvailableTZIDs\n");
97 count=ucal_countAvailableTZIDs(offset);
98 log_verbose("The number of timezone id's present with offset 0 are %d:\n", count);
99 if(count < 5) /* Don't hard code an exact == test here! */
100 log_err("FAIL: error in the ucal_countAvailableTZIDs - got %d expected at least 5 total\n", count);
101
102 /*Testing getAvailableTZIDs*/
103 log_verbose("\nTesting ucal_getAvailableTZIDs");
104 for(i=0;i<count;i++){
105 ucal_getAvailableTZIDs(offset, i, &status);
106 if(U_FAILURE(status)){
107 log_err("FAIL: ucal_getAvailableTZIDs returned %s\n", u_errorName(status));
108 }
109 log_verbose("%s\n", u_austrcpy(tempMsgBuf, ucal_getAvailableTZIDs(offset, i, &status)));
110 }
111 /*get Illegal TZID where index >= count*/
112 ucal_getAvailableTZIDs(offset, i, &status);
113 if(status != U_INDEX_OUTOFBOUNDS_ERROR){
114 log_err("FAIL:for TZID index >= count Expected INDEX_OUTOFBOUNDS_ERROR Got %s\n", u_errorName(status));
115 }
116 status=U_ZERO_ERROR;
117 #endif
118
119 /*Test ucal_openTimeZones & ucal_openCountryTimeZones*/
120 for (j=0; j<2; ++j) {
121 const char* api = (j==0) ? "ucal_openTimeZones()" :
122 "ucal_openCountryTimeZones(US)";
123 uenum = (j==0) ? ucal_openTimeZones(&status) :
124 ucal_openCountryTimeZones("US", &status);
125 if (U_FAILURE(status)) {
126 log_err("FAIL: %s failed with %s", api,
127 u_errorName(status));
128 } else {
129 const char* id;
130 int32_t len;
131 count = uenum_count(uenum, &status);
132 log_verbose("%s returned %d timezone id's:\n", api, count);
133 if (count < 5) { /* Don't hard code an exact == test here! */
134 log_err("FAIL: in %s, got %d, expected at least 5\n", api, count);
135 }
136 uenum_reset(uenum, &status);
137 if (U_FAILURE(status)){
138 log_err("FAIL: uenum_reset for %s returned %s\n",
139 api, u_errorName(status));
140 }
141 for (i=0; i<count; i++) {
142 id = uenum_next(uenum, &len, &status);
143 if (U_FAILURE(status)){
144 log_err("FAIL: uenum_next for %s returned %s\n",
145 api, u_errorName(status));
146 } else {
147 log_verbose("%s\n", id);
148 }
149 }
150 /* Next one should be NULL */
151 id = uenum_next(uenum, &len, &status);
152 if (id != NULL) {
153 log_err("FAIL: uenum_next for %s returned %s, expected NULL\n",
154 api, id);
155 }
156 }
157 uenum_close(uenum);
158 }
159
160 /*Test ucal_getDSTSavings*/
161 status = U_ZERO_ERROR;
162 i = ucal_getDSTSavings(fgGMTID, &status);
163 if (U_FAILURE(status)) {
164 log_err("FAIL: ucal_getDSTSavings(GMT) => %s\n",
165 u_errorName(status));
166 } else if (i != 0) {
167 log_err("FAIL: ucal_getDSTSavings(GMT) => %d, expect 0\n", i);
168 }
169 i = ucal_getDSTSavings(PST, &status);
170 if (U_FAILURE(status)) {
171 log_err("FAIL: ucal_getDSTSavings(PST) => %s\n",
172 u_errorName(status));
173 } else if (i != 1*60*60*1000) {
174 log_err("FAIL: ucal_getDSTSavings(PST) => %d, expect %d\n", i, 1*60*60*1000);
175 }
176
177 /*Test ucal_set/getDefaultTimeZone*/
178 status = U_ZERO_ERROR;
179 i = ucal_getDefaultTimeZone(zone1, sizeof(zone1)/sizeof(zone1[0]), &status);
180 if (U_FAILURE(status)) {
181 log_err("FAIL: ucal_getDefaultTimeZone() => %s\n",
182 u_errorName(status));
183 } else {
184 ucal_setDefaultTimeZone(EUROPE_PARIS, &status);
185 if (U_FAILURE(status)) {
186 log_err("FAIL: ucal_setDefaultTimeZone(Europe/Paris) => %s\n",
187 u_errorName(status));
188 } else {
189 i = ucal_getDefaultTimeZone(zone2, sizeof(zone2)/sizeof(zone2[0]), &status);
190 if (U_FAILURE(status)) {
191 log_err("FAIL: ucal_getDefaultTimeZone() => %s\n",
192 u_errorName(status));
193 } else {
194 if (u_strcmp(zone2, EUROPE_PARIS) != 0) {
195 log_err("FAIL: ucal_getDefaultTimeZone() did not return Europe/Paris\n");
196 }
197 }
198 }
199 status = U_ZERO_ERROR;
200 ucal_setDefaultTimeZone(zone1, &status);
201 }
202
203 /*Test ucal_getTZDataVersion*/
204 status = U_ZERO_ERROR;
205 tzver = ucal_getTZDataVersion(&status);
206 if (U_FAILURE(status)) {
207 log_err("FAIL: ucal_getTZDataVersion() => %s\n", u_errorName(status));
208 } else if (uprv_strlen(tzver) != 5 /*4 digits + 1 letter*/) {
209 log_err("FAIL: Bad version string was returned by ucal_getTZDataVersion\n");
210 } else {
211 log_verbose("PASS: ucal_getTZDataVersion returned %s\n", tzver);
212 }
213
214 /*Testing ucal_getCanonicalTimeZoneID*/
215 status = U_ZERO_ERROR;
216 resultlength = ucal_getCanonicalTimeZoneID(PST, -1,
217 canonicalID, sizeof(canonicalID)/sizeof(UChar), &isSystemID, &status);
218 if (U_FAILURE(status)) {
219 log_err("FAIL: error in ucal_getCanonicalTimeZoneID : %s\n", u_errorName(status));
220 } else {
221 if (u_strcmp(AMERICA_LOS_ANGELES, canonicalID) != 0) {
222 log_err("FAIL: ucal_getCanonicalTimeZoneID(%s) returned %s : expected - %s\n",
223 PST, canonicalID, AMERICA_LOS_ANGELES);
224 }
225 if (!isSystemID) {
226 log_err("FAIL: ucal_getCanonicalTimeZoneID(%s) set %d to isSystemID\n",
227 PST, isSystemID);
228 }
229 }
230
231 /*Testing the ucal_open() function*/
232 status = U_ZERO_ERROR;
233 log_verbose("\nTesting the ucal_open()\n");
234 u_uastrcpy(tzID, "PST");
235 caldef=ucal_open(tzID, u_strlen(tzID), "en_US", UCAL_TRADITIONAL, &status);
236 if(U_FAILURE(status)){
237 log_err("FAIL: error in ucal_open caldef : %s\n", u_errorName(status));
238 }
239
240 caldef2=ucal_open(tzID, u_strlen(tzID), "en_US", UCAL_TRADITIONAL, &status);
241 if(U_FAILURE(status)){
242 log_err("FAIL: error in ucal_open caldef : %s\n", u_errorName(status));
243 }
244 u_strcpy(tzID, fgGMTID);
245 calfr=ucal_open(tzID, u_strlen(tzID), "fr_FR", UCAL_TRADITIONAL, &status);
246 if(U_FAILURE(status)){
247 log_err("FAIL: error in ucal_open calfr : %s\n", u_errorName(status));
248 }
249 calit=ucal_open(tzID, u_strlen(tzID), "it_IT", UCAL_TRADITIONAL, &status);
250 if(U_FAILURE(status)) {
251 log_err("FAIL: error in ucal_open calit : %s\n", u_errorName(status));
252 }
253
254 /*Testing the clone() function*/
255 calfrclone = ucal_clone(calfr, &status);
256 if(U_FAILURE(status)){
257 log_err("FAIL: error in ucal_clone calfr : %s\n", u_errorName(status));
258 }
259
260 /*Testing udat_getAvailable() and udat_countAvailable()*/
261 log_verbose("\nTesting getAvailableLocales and countAvailable()\n");
262 count=ucal_countAvailable();
263 /* use something sensible w/o hardcoding the count */
264 if(count > 0) {
265 log_verbose("PASS: ucal_countAvailable() works fine\n");
266 log_verbose("The no: of locales for which calendars are avilable are %d\n", count);
267 } else {
268 log_data_err("FAIL: Error in countAvailable()\n");
269 }
270
271 for(i=0;i<count;i++) {
272 log_verbose("%s\n", ucal_getAvailable(i));
273 }
274
275
276 /*Testing the equality between calendar's*/
277 log_verbose("\nTesting ucal_equivalentTo()\n");
278 if(caldef && caldef2 && calfr && calit) {
279 if(ucal_equivalentTo(caldef, caldef2) == FALSE || ucal_equivalentTo(caldef, calfr)== TRUE ||
280 ucal_equivalentTo(caldef, calit)== TRUE || ucal_equivalentTo(calfr, calfrclone) == FALSE) {
281 log_err("FAIL: Error. equivalentTo test failed\n");
282 } else {
283 log_verbose("PASS: equivalentTo test passed\n");
284 }
285 }
286
287
288 /*Testing the current time and date using ucal_getnow()*/
289 log_verbose("\nTesting the ucal_getNow function to check if it is fetching tbe current time\n");
290 now=ucal_getNow();
291 /* open the date format and format the date to check the output */
292 datdef=udat_open(UDAT_FULL,UDAT_FULL ,NULL, NULL, 0,NULL,0,&status);
293 if(U_FAILURE(status)){
294 log_err("FAIL: error in creating the dateformat : %s\n", u_errorName(status));
295 return;
296 }
297 log_verbose("PASS: The current date and time fetched is %s\n", u_austrcpy(tempMsgBuf, myDateFormat(datdef, now)) );
298
299
300 /*Testing the TimeZoneDisplayName */
301 log_verbose("\nTesting the fetching of time zone display name\n");
302 /*for the US locale */
303 resultlength=0;
304 resultlengthneeded=ucal_getTimeZoneDisplayName(caldef, UCAL_DST, "en_US", NULL, resultlength, &status);
305
306 if(status==U_BUFFER_OVERFLOW_ERROR)
307 {
308 status=U_ZERO_ERROR;
309 resultlength=resultlengthneeded+1;
310 result=(UChar*)malloc(sizeof(UChar) * resultlength);
311 ucal_getTimeZoneDisplayName(caldef, UCAL_DST, "en_US", result, resultlength, &status);
312 }
313 if(U_FAILURE(status)) {
314 log_err("FAIL: Error in getting the timezone display name : %s\n", u_errorName(status));
315 }
316 else{
317 log_verbose("PASS: getting the time zone display name successful : %s, %d needed \n",
318 u_errorName(status), resultlengthneeded);
319 }
320
321
322 #define expectPDT "Pacific Daylight Time"
323
324 tzdname=(UChar*)malloc(sizeof(UChar) * (sizeof(expectPDT)+1));
325 u_uastrcpy(tzdname, expectPDT);
326 if(u_strcmp(tzdname, result)==0){
327 log_verbose("PASS: got the correct time zone display name %s\n", u_austrcpy(tempMsgBuf, result) );
328 }
329 else{
330 log_err("FAIL: got the wrong time zone(DST) display name %s, wanted %s\n", austrdup(result) , expectPDT);
331 }
332
333 ucal_getTimeZoneDisplayName(caldef, UCAL_SHORT_DST, "en_US", result, resultlength, &status);
334 u_uastrcpy(tzdname, "PDT");
335 if(u_strcmp(tzdname, result) != 0){
336 log_err("FAIL: got the wrong time zone(SHORT_DST) display name %s, wanted %s\n", austrdup(result), austrdup(tzdname));
337 }
338
339 ucal_getTimeZoneDisplayName(caldef, UCAL_STANDARD, "en_US", result, resultlength, &status);
340 u_uastrcpy(tzdname, "Pacific Standard Time");
341 if(u_strcmp(tzdname, result) != 0){
342 log_err("FAIL: got the wrong time zone(STANDARD) display name %s, wanted %s\n", austrdup(result), austrdup(tzdname));
343 }
344
345 ucal_getTimeZoneDisplayName(caldef, UCAL_SHORT_STANDARD, "en_US", result, resultlength, &status);
346 u_uastrcpy(tzdname, "PST");
347 if(u_strcmp(tzdname, result) != 0){
348 log_err("FAIL: got the wrong time zone(SHORT_STANDARD) display name %s, wanted %s\n", austrdup(result), austrdup(tzdname));
349 }
350
351
352 /*testing the setAttributes and getAttributes of a UCalendar*/
353 log_verbose("\nTesting the getAttributes and set Attributes\n");
354 count=ucal_getAttribute(calit, UCAL_LENIENT);
355 count2=ucal_getAttribute(calfr, UCAL_LENIENT);
356 ucal_setAttribute(calit, UCAL_LENIENT, 0);
357 ucal_setAttribute(caldef, UCAL_LENIENT, count2);
358 if( ucal_getAttribute(calit, UCAL_LENIENT) !=0 ||
359 ucal_getAttribute(calfr, UCAL_LENIENT)!=ucal_getAttribute(caldef, UCAL_LENIENT) )
360 log_err("FAIL: there is an error in getAttributes or setAttributes\n");
361 else
362 log_verbose("PASS: attribute set and got successfully\n");
363 /*set it back to orginal value */
364 log_verbose("Setting it back to normal\n");
365 ucal_setAttribute(calit, UCAL_LENIENT, count);
366 if(ucal_getAttribute(calit, UCAL_LENIENT)!=count)
367 log_err("FAIL: Error in setting the attribute back to normal\n");
368
369 /*setting the first day of the week to other values */
370 count=ucal_getAttribute(calit, UCAL_FIRST_DAY_OF_WEEK);
371 for (i=1; i<=7; ++i) {
372 ucal_setAttribute(calit, UCAL_FIRST_DAY_OF_WEEK,i);
373 if (ucal_getAttribute(calit, UCAL_FIRST_DAY_OF_WEEK) != i)
374 log_err("FAIL: set/getFirstDayOfWeek failed\n");
375 }
376 /*get bogus Attribute*/
377 count=ucal_getAttribute(calit, (UCalendarAttribute)99); /* BOGUS_ATTRIBUTE */
378 if(count != -1){
379 log_err("FAIL: get/bogus attribute should return -1\n");
380 }
381
382 /*set it back to normal */
383 ucal_setAttribute(calit, UCAL_FIRST_DAY_OF_WEEK,count);
384 /*setting minimal days of the week to other values */
385 count=ucal_getAttribute(calit, UCAL_MINIMAL_DAYS_IN_FIRST_WEEK);
386 for (i=1; i<=7; ++i) {
387 ucal_setAttribute(calit, UCAL_MINIMAL_DAYS_IN_FIRST_WEEK,i);
388 if (ucal_getAttribute(calit, UCAL_MINIMAL_DAYS_IN_FIRST_WEEK) != i)
389 log_err("FAIL: set/getMinimalDaysInFirstWeek failed\n");
390 }
391 /*set it back to normal */
392 ucal_setAttribute(calit, UCAL_MINIMAL_DAYS_IN_FIRST_WEEK,count);
393
394
395 /*testing if the UCalendar's timezone is currently in day light saving's time*/
396 log_verbose("\nTesting if the UCalendar is currently in daylight saving's time\n");
397 ucal_setDateTime(caldef, 1999, UCAL_MARCH, 3, 10, 45, 20, &status);
398 ucal_inDaylightTime(caldef, &status );
399 if(U_FAILURE(status)) {
400 log_err("Error in ucal_inDaylightTime: %s\n", u_errorName(status));
401 }
402 if(!ucal_inDaylightTime(caldef, &status))
403 log_verbose("PASS: It is not in daylight saving's time\n");
404 else
405 log_err("FAIL: It is not in daylight saving's time\n");
406
407 /*closing the UCalendar*/
408 ucal_close(caldef);
409 ucal_close(caldef2);
410 ucal_close(calfr);
411 ucal_close(calit);
412 ucal_close(calfrclone);
413
414 /*testing ucal_getType, and ucal_open with UCAL_GREGORIAN*/
415 for (ucalGetTypeTestPtr = ucalGetTypeTests; ucalGetTypeTestPtr->locale != NULL; ++ucalGetTypeTestPtr) {
416 status = U_ZERO_ERROR;
417 caldef = ucal_open(NULL, 0, ucalGetTypeTestPtr->locale, ucalGetTypeTestPtr->calType, &status);
418 if ( U_SUCCESS(status) ) {
419 const char * calType = ucal_getType(caldef, &status);
420 if ( U_SUCCESS(status) && calType != NULL ) {
421 if ( strcmp( calType, ucalGetTypeTestPtr->expectedResult ) != 0 ) {
422 log_err("FAIL: ucal_open %s type %d does not return %s calendar\n", ucalGetTypeTestPtr->locale,
423 ucalGetTypeTestPtr->calType, ucalGetTypeTestPtr->expectedResult);
424 }
425 } else {
426 log_err("FAIL: ucal_open %s type %d, then ucal_getType fails\n", ucalGetTypeTestPtr->locale, ucalGetTypeTestPtr->calType);
427 }
428 ucal_close(caldef);
429 } else {
430 log_err("FAIL: ucal_open %s type %d fails\n", ucalGetTypeTestPtr->locale, ucalGetTypeTestPtr->calType);
431 }
432 }
433
434 /*closing the UDateFormat used */
435 udat_close(datdef);
436 free(result);
437 free(tzdname);
438 }
439
440 /*------------------------------------------------------*/
441 /*Testing the getMillis, setMillis, setDate and setDateTime functions extensively*/
442
443 static void TestGetSetDateAPI()
444 {
445 UCalendar *caldef = 0, *caldef2 = 0;
446 UChar tzID[4];
447 UDate d1;
448 int32_t hour;
449 int32_t zoneOffset;
450 UDateFormat *datdef = 0;
451 UErrorCode status=U_ZERO_ERROR;
452 UDate d2= 837039928046.0;
453 UChar temp[30];
454
455 log_verbose("\nOpening the calendars()\n");
456 u_strcpy(tzID, fgGMTID);
457 /*open the calendars used */
458 caldef=ucal_open(tzID, u_strlen(tzID), "en_US", UCAL_TRADITIONAL, &status);
459 caldef2=ucal_open(tzID, u_strlen(tzID), "en_US", UCAL_TRADITIONAL, &status);
460 /*open the dateformat */
461 /* this is supposed to open default date format, but later on it treats it like it is "en_US"
462 - very bad if you try to run the tests on machine where default locale is NOT "en_US" */
463 /*datdef=udat_open(UDAT_DEFAULT,UDAT_DEFAULT ,NULL,fgGMTID,-1, &status);*/
464 datdef=udat_open(UDAT_DEFAULT,UDAT_DEFAULT ,"en_US",fgGMTID,-1,NULL,0, &status);
465 if(U_FAILURE(status))
466 {
467 log_err("error in creating the dateformat : %s\n", u_errorName(status));
468 return;
469 }
470
471
472 /*Testing getMillis and setMillis */
473 log_verbose("\nTesting the date and time fetched in millis for a calendar using getMillis\n");
474 d1=ucal_getMillis(caldef, &status);
475 if(U_FAILURE(status)){
476 log_err("Error in getMillis : %s\n", u_errorName(status));
477 }
478
479 /*testing setMillis */
480 log_verbose("\nTesting the set date and time function using setMillis\n");
481 ucal_setMillis(caldef, d2, &status);
482 if(U_FAILURE(status)){
483 log_err("Error in setMillis : %s\n", u_errorName(status));
484 }
485
486 /*testing if the calendar date is set properly or not */
487 d1=ucal_getMillis(caldef, &status);
488 if(u_strcmp(myDateFormat(datdef, d1), myDateFormat(datdef, d2))!=0)
489 log_err("error in setMillis or getMillis\n");
490 /*-------------------*/
491
492
493
494 ctest_setTimeZone(NULL, &status);
495
496 /*testing ucal_setTimeZone() function*/
497 log_verbose("\nTesting if the function ucal_setTimeZone() works fine\n");
498 ucal_setMillis(caldef2, d2, &status);
499 if(U_FAILURE(status)){
500 log_err("Error in getMillis : %s\n", u_errorName(status));;
501 }
502 hour=ucal_get(caldef2, UCAL_HOUR_OF_DAY, &status);
503
504 u_uastrcpy(tzID, "PST");
505 ucal_setTimeZone(caldef2,tzID, 3, &status);
506 if(U_FAILURE(status)){
507 log_err("Error in setting the time zone using ucal_setTimeZone(): %s\n", u_errorName(status));
508 }
509 else
510 log_verbose("ucal_setTimeZone worked fine\n");
511 if(hour == ucal_get(caldef2, UCAL_HOUR_OF_DAY, &status))
512 log_err("FAIL: Error setting the time zone doesn't change the represented time\n");
513 else if((hour-8 + 1) != ucal_get(caldef2, UCAL_HOUR_OF_DAY, &status)) /*because it is not in daylight savings time */
514 log_err("FAIL: Error setTimeZone doesn't change the represented time correctly with 8 hour offset\n");
515 else
516 log_verbose("PASS: setTimeZone works fine\n");
517
518 /*testing setTimeZone roundtrip */
519 log_verbose("\nTesting setTimeZone() roundtrip\n");
520 u_strcpy(tzID, fgGMTID);
521 ucal_setTimeZone(caldef2, tzID, 3, &status);
522 if(U_FAILURE(status)){
523 log_err("Error in setting the time zone using ucal_setTimeZone(): %s\n", u_errorName(status));
524 }
525 if(d2==ucal_getMillis(caldef2, &status))
526 log_verbose("PASS: setTimeZone roundtrip test passed\n");
527 else
528 log_err("FAIL: setTimeZone roundtrip test failed\n");
529
530 zoneOffset = ucal_get(caldef2, UCAL_ZONE_OFFSET, &status);
531 if(U_FAILURE(status)){
532 log_err("Error in getting the time zone using ucal_get() after using ucal_setTimeZone(): %s\n", u_errorName(status));
533 }
534 else if (zoneOffset != 0) {
535 log_err("Error in getting the time zone using ucal_get() after using ucal_setTimeZone() offset=%d\n", zoneOffset);
536 }
537
538 ucal_setTimeZone(caldef2, NULL, -1, &status);
539 if(U_FAILURE(status)){
540 log_err("Error in setting the time zone using ucal_setTimeZone(): %s\n", u_errorName(status));
541 }
542 if(ucal_getMillis(caldef2, &status))
543 log_verbose("PASS: setTimeZone roundtrip test passed\n");
544 else
545 log_err("FAIL: setTimeZone roundtrip test failed\n");
546
547 zoneOffset = ucal_get(caldef2, UCAL_ZONE_OFFSET, &status);
548 if(U_FAILURE(status)){
549 log_err("Error in getting the time zone using ucal_get() after using ucal_setTimeZone(): %s\n", u_errorName(status));
550 }
551 else if (zoneOffset != -28800000) {
552 log_err("Error in getting the time zone using ucal_get() after using ucal_setTimeZone() offset=%d\n", zoneOffset);
553 }
554
555 ctest_resetTimeZone();
556
557 /*----------------------------* */
558
559
560
561 /*Testing if setDate works fine */
562 log_verbose("\nTesting the ucal_setDate() function \n");
563 u_uastrcpy(temp, "Dec 17, 1971 11:05:28 PM");
564 ucal_setDate(caldef,1971, UCAL_DECEMBER, 17, &status);
565 if(U_FAILURE(status)){
566 log_err("error in setting the calendar date : %s\n", u_errorName(status));
567 }
568 /*checking if the calendar date is set properly or not */
569 d1=ucal_getMillis(caldef, &status);
570 if(u_strcmp(myDateFormat(datdef, d1), temp)==0)
571 log_verbose("PASS:setDate works fine\n");
572 else
573 log_err("FAIL:Error in setDate()\n");
574
575
576 /* Testing setDate Extensively with various input values */
577 log_verbose("\nTesting ucal_setDate() extensively\n");
578 ucal_setDate(caldef, 1999, UCAL_JANUARY, 10, &status);
579 verify1("1999 10th day of January is :", caldef, datdef, 1999, UCAL_JANUARY, 10);
580 ucal_setDate(caldef, 1999, UCAL_DECEMBER, 3, &status);
581 verify1("1999 3rd day of December is :", caldef, datdef, 1999, UCAL_DECEMBER, 3);
582 ucal_setDate(caldef, 2000, UCAL_MAY, 3, &status);
583 verify1("2000 3rd day of May is :", caldef, datdef, 2000, UCAL_MAY, 3);
584 ucal_setDate(caldef, 1999, UCAL_AUGUST, 32, &status);
585 verify1("1999 32th day of August is :", caldef, datdef, 1999, UCAL_SEPTEMBER, 1);
586 ucal_setDate(caldef, 1999, UCAL_MARCH, 0, &status);
587 verify1("1999 0th day of March is :", caldef, datdef, 1999, UCAL_FEBRUARY, 28);
588 ucal_setDate(caldef, 0, UCAL_MARCH, 12, &status);
589
590 /*--------------------*/
591
592 /*Testing if setDateTime works fine */
593 log_verbose("\nTesting the ucal_setDateTime() function \n");
594 u_uastrcpy(temp, "May 3, 1972 4:30:42 PM");
595 ucal_setDateTime(caldef,1972, UCAL_MAY, 3, 16, 30, 42, &status);
596 if(U_FAILURE(status)){
597 log_err("error in setting the calendar date : %s\n", u_errorName(status));
598 }
599 /*checking if the calendar date is set properly or not */
600 d1=ucal_getMillis(caldef, &status);
601 if(u_strcmp(myDateFormat(datdef, d1), temp)==0)
602 log_verbose("PASS: setDateTime works fine\n");
603 else
604 log_err("FAIL: Error in setDateTime\n");
605
606
607
608 /*Testing setDateTime extensively with various input values*/
609 log_verbose("\nTesting ucal_setDateTime() function extensively\n");
610 ucal_setDateTime(caldef, 1999, UCAL_OCTOBER, 10, 6, 45, 30, &status);
611 verify2("1999 10th day of October at 6:45:30 is :", caldef, datdef, 1999, UCAL_OCTOBER, 10, 6, 45, 30, 0 );
612 ucal_setDateTime(caldef, 1999, UCAL_MARCH, 3, 15, 10, 55, &status);
613 verify2("1999 3rd day of March at 15:10:55 is :", caldef, datdef, 1999, UCAL_MARCH, 3, 3, 10, 55, 1);
614 ucal_setDateTime(caldef, 1999, UCAL_MAY, 3, 25, 30, 45, &status);
615 verify2("1999 3rd day of May at 25:30:45 is :", caldef, datdef, 1999, UCAL_MAY, 4, 1, 30, 45, 0);
616 ucal_setDateTime(caldef, 1999, UCAL_AUGUST, 32, 22, 65, 40, &status);
617 verify2("1999 32th day of August at 22:65:40 is :", caldef, datdef, 1999, UCAL_SEPTEMBER, 1, 11, 5, 40,1);
618 ucal_setDateTime(caldef, 1999, UCAL_MARCH, 12, 0, 0, 0,&status);
619 verify2("1999 12th day of March at 0:0:0 is :", caldef, datdef, 1999, UCAL_MARCH, 12, 0, 0, 0, 0);
620 ucal_setDateTime(caldef, 1999, UCAL_MARCH, 12, -10, -10,0, &status);
621 verify2("1999 12th day of March is at -10:-10:0 :", caldef, datdef, 1999, UCAL_MARCH, 11, 1, 50, 0, 1);
622
623
624
625 /*close caldef and datdef*/
626 ucal_close(caldef);
627 ucal_close(caldef2);
628 udat_close(datdef);
629 }
630
631 /*----------------------------------------------------------- */
632 /**
633 * Confirm the functioning of the calendar field related functions.
634 */
635 static void TestFieldGetSet()
636 {
637 UCalendar *cal = 0;
638 UChar tzID[4];
639 UDateFormat *datdef = 0;
640 UDate d1;
641 UErrorCode status=U_ZERO_ERROR;
642 log_verbose("\nFetching pointer to UCalendar using the ucal_open()\n");
643 u_strcpy(tzID, fgGMTID);
644 /*open the calendar used */
645 cal=ucal_open(tzID, u_strlen(tzID), "en_US", UCAL_TRADITIONAL, &status);
646 if (U_FAILURE(status)) {
647 log_err("ucal_open failed: %s\n", u_errorName(status));
648 return;
649 }
650 datdef=udat_open(UDAT_SHORT,UDAT_SHORT ,NULL,fgGMTID,-1,NULL, 0, &status);
651 if(U_FAILURE(status))
652 {
653 log_err("error in creating the dateformat : %s\n", u_errorName(status));
654 }
655
656 /*Testing ucal_get()*/
657 log_verbose("\nTesting the ucal_get() function of Calendar\n");
658 ucal_setDateTime(cal, 1999, UCAL_MARCH, 12, 5, 25, 30, &status);
659 if(U_FAILURE(status)){
660 log_err("error in the setDateTime() : %s\n", u_errorName(status));
661 }
662 if(ucal_get(cal, UCAL_YEAR, &status)!=1999 || ucal_get(cal, UCAL_MONTH, &status)!=2 ||
663 ucal_get(cal, UCAL_DATE, &status)!=12 || ucal_get(cal, UCAL_HOUR, &status)!=5)
664 log_err("error in ucal_get()\n");
665 else if(ucal_get(cal, UCAL_DAY_OF_WEEK_IN_MONTH, &status)!=2 || ucal_get(cal, UCAL_DAY_OF_WEEK, &status)!=6
666 || ucal_get(cal, UCAL_WEEK_OF_MONTH, &status)!=2 || ucal_get(cal, UCAL_WEEK_OF_YEAR, &status)!= 11)
667 log_err("FAIL: error in ucal_get()\n");
668 else
669 log_verbose("PASS: ucal_get() works fine\n");
670
671 /*Testing the ucal_set() , ucal_clear() functions of calendar*/
672 log_verbose("\nTesting the set, and clear field functions of calendar\n");
673 ucal_setAttribute(cal, UCAL_LENIENT, 0);
674 ucal_clear(cal);
675 ucal_set(cal, UCAL_YEAR, 1997);
676 ucal_set(cal, UCAL_MONTH, UCAL_JUNE);
677 ucal_set(cal, UCAL_DATE, 3);
678 verify1("1997 third day of June = ", cal, datdef, 1997, UCAL_JUNE, 3);
679 ucal_clear(cal);
680 ucal_set(cal, UCAL_YEAR, 1997);
681 ucal_set(cal, UCAL_DAY_OF_WEEK, UCAL_TUESDAY);
682 ucal_set(cal, UCAL_MONTH, UCAL_JUNE);
683 ucal_set(cal, UCAL_DAY_OF_WEEK_IN_MONTH, 1);
684 verify1("1997 first Tuesday in June = ", cal, datdef, 1997, UCAL_JUNE, 3);
685 ucal_clear(cal);
686 ucal_set(cal, UCAL_YEAR, 1997);
687 ucal_set(cal, UCAL_DAY_OF_WEEK, UCAL_TUESDAY);
688 ucal_set(cal, UCAL_MONTH, UCAL_JUNE);
689 ucal_set(cal, UCAL_DAY_OF_WEEK_IN_MONTH, - 1);
690 verify1("1997 last Tuesday in June = ", cal, datdef,1997, UCAL_JUNE, 24);
691 /*give undesirable input */
692 status = U_ZERO_ERROR;
693 ucal_clear(cal);
694 ucal_set(cal, UCAL_YEAR, 1997);
695 ucal_set(cal, UCAL_DAY_OF_WEEK, UCAL_TUESDAY);
696 ucal_set(cal, UCAL_MONTH, UCAL_JUNE);
697 ucal_set(cal, UCAL_DAY_OF_WEEK_IN_MONTH, 0);
698 d1 = ucal_getMillis(cal, &status);
699 if (status != U_ILLEGAL_ARGUMENT_ERROR) {
700 log_err("FAIL: U_ILLEGAL_ARGUMENT_ERROR was not returned for : 1997 zero-th Tuesday in June\n");
701 } else {
702 log_verbose("PASS: U_ILLEGAL_ARGUMENT_ERROR as expected\n");
703 }
704 status = U_ZERO_ERROR;
705 ucal_clear(cal);
706 ucal_set(cal, UCAL_YEAR, 1997);
707 ucal_set(cal, UCAL_DAY_OF_WEEK, UCAL_TUESDAY);
708 ucal_set(cal, UCAL_MONTH, UCAL_JUNE);
709 ucal_set(cal, UCAL_WEEK_OF_MONTH, 1);
710 verify1("1997 Tuesday in week 1 of June = ", cal,datdef, 1997, UCAL_JUNE, 3);
711 ucal_clear(cal);
712 ucal_set(cal, UCAL_YEAR, 1997);
713 ucal_set(cal, UCAL_DAY_OF_WEEK, UCAL_TUESDAY);
714 ucal_set(cal, UCAL_MONTH, UCAL_JUNE);
715 ucal_set(cal, UCAL_WEEK_OF_MONTH, 5);
716 verify1("1997 Tuesday in week 5 of June = ", cal,datdef, 1997, UCAL_JULY, 1);
717 status = U_ZERO_ERROR;
718 ucal_clear(cal);
719 ucal_set(cal, UCAL_YEAR, 1997);
720 ucal_set(cal, UCAL_DAY_OF_WEEK, UCAL_TUESDAY);
721 ucal_set(cal, UCAL_MONTH, UCAL_JUNE);
722 ucal_set(cal, UCAL_WEEK_OF_MONTH, 0);
723 d1 = ucal_getMillis(cal,&status);
724 if (status != U_ILLEGAL_ARGUMENT_ERROR){
725 log_err("FAIL: U_ILLEGAL_ARGUMENT_ERROR was not returned for : 1997 Tuesday zero-th week in June\n");
726 } else {
727 log_verbose("PASS: U_ILLEGAL_ARGUMENT_ERROR as expected\n");
728 }
729 status = U_ZERO_ERROR;
730 ucal_clear(cal);
731 ucal_set(cal, UCAL_YEAR_WOY, 1997);
732 ucal_set(cal, UCAL_DAY_OF_WEEK, UCAL_TUESDAY);
733 ucal_set(cal, UCAL_WEEK_OF_YEAR, 1);
734 verify1("1997 Tuesday in week 1 of year = ", cal, datdef,1996, UCAL_DECEMBER, 31);
735 ucal_clear(cal);
736 ucal_set(cal, UCAL_YEAR, 1997);
737 ucal_set(cal, UCAL_DAY_OF_WEEK, UCAL_TUESDAY);
738 ucal_set(cal, UCAL_WEEK_OF_YEAR, 10);
739 verify1("1997 Tuesday in week 10 of year = ", cal,datdef, 1997, UCAL_MARCH, 4);
740 ucal_clear(cal);
741 ucal_set(cal, UCAL_YEAR, 1999);
742 ucal_set(cal, UCAL_DAY_OF_YEAR, 1);
743 verify1("1999 1st day of the year =", cal, datdef, 1999, UCAL_JANUARY, 1);
744 ucal_set(cal, UCAL_MONTH, -3);
745 d1 = ucal_getMillis(cal,&status);
746 if (status != U_ILLEGAL_ARGUMENT_ERROR){
747 log_err("FAIL: U_ILLEGAL_ARGUMENT_ERROR was not returned for : 1999 -3th month\n");
748 } else {
749 log_verbose("PASS: U_ILLEGAL_ARGUMENT_ERROR as expected\n");
750 }
751
752 ucal_setAttribute(cal, UCAL_LENIENT, 1);
753
754 ucal_set(cal, UCAL_MONTH, -3);
755 verify1("1999 -3th month should be", cal, datdef, 1998, UCAL_OCTOBER, 1);
756
757
758 /*testing isSet and clearField()*/
759 if(!ucal_isSet(cal, UCAL_WEEK_OF_YEAR))
760 log_err("FAIL: error in isSet\n");
761 else
762 log_verbose("PASS: isSet working fine\n");
763 ucal_clearField(cal, UCAL_WEEK_OF_YEAR);
764 if(ucal_isSet(cal, UCAL_WEEK_OF_YEAR))
765 log_err("FAIL: there is an error in clearField or isSet\n");
766 else
767 log_verbose("PASS :clearField working fine\n");
768
769 /*-------------------------------*/
770
771 ucal_close(cal);
772 udat_close(datdef);
773 }
774
775
776
777 /* ------------------------------------- */
778 /**
779 * Execute adding and rolling in Calendar extensively,
780 */
781 static void TestAddRollExtensive()
782 {
783 UCalendar *cal = 0;
784 int32_t i,limit;
785 UChar tzID[4];
786 UCalendarDateFields e;
787 int32_t y,m,d,hr,min,sec,ms;
788 int32_t maxlimit = 40;
789 UErrorCode status = U_ZERO_ERROR;
790 y = 1997; m = UCAL_FEBRUARY; d = 1; hr = 1; min = 1; sec = 0; ms = 0;
791
792 log_verbose("Testing add and roll extensively\n");
793
794 u_uastrcpy(tzID, "PST");
795 /*open the calendar used */
796 cal=ucal_open(tzID, u_strlen(tzID), "en_US", UCAL_GREGORIAN, &status);;
797 if (U_FAILURE(status)) {
798 log_err("ucal_open() failed : %s\n", u_errorName(status));
799 return;
800 }
801
802 ucal_set(cal, UCAL_YEAR, y);
803 ucal_set(cal, UCAL_MONTH, m);
804 ucal_set(cal, UCAL_DATE, d);
805
806 /* Confirm that adding to various fields works.*/
807 log_verbose("\nTesting to confirm that adding to various fields works with ucal_add()\n");
808 checkDate(cal, y, m, d);
809 ucal_add(cal,UCAL_YEAR, 1, &status);
810 if (U_FAILURE(status)) { log_err("ucal_add failed: %s\n", u_errorName(status)); return; }
811 y++;
812 checkDate(cal, y, m, d);
813 ucal_add(cal,UCAL_MONTH, 12, &status);
814 if (U_FAILURE(status)) { log_err("ucal_add failed: %s\n", u_errorName(status) ); return; }
815 y+=1;
816 checkDate(cal, y, m, d);
817 ucal_add(cal,UCAL_DATE, 1, &status);
818 if (U_FAILURE(status)) { log_err("ucal_add failed: %s\n", u_errorName(status) ); return; }
819 d++;
820 checkDate(cal, y, m, d);
821 ucal_add(cal,UCAL_DATE, 2, &status);
822 if (U_FAILURE(status)) { log_err("ucal_add failed: %s\n", u_errorName(status) ); return; }
823 d += 2;
824 checkDate(cal, y, m, d);
825 ucal_add(cal,UCAL_DATE, 28, &status);
826 if (U_FAILURE(status)) { log_err("ucal_add failed: %s\n", u_errorName(status) ); return; }
827 ++m;
828 checkDate(cal, y, m, d);
829 ucal_add(cal, (UCalendarDateFields)-1, 10, &status);
830 if(status==U_ILLEGAL_ARGUMENT_ERROR)
831 log_verbose("Pass: Illegal argument error as expected\n");
832 else{
833 log_err("Fail: No, illegal argument error as expected. Got....: %s\n", u_errorName(status));
834 }
835 status=U_ZERO_ERROR;
836
837
838 /*confirm that applying roll to various fields works fine*/
839 log_verbose("\nTesting to confirm that ucal_roll() works\n");
840 ucal_roll(cal, UCAL_DATE, -1, &status);
841 if (U_FAILURE(status)) { log_err("ucal_roll failed: %s\n", u_errorName(status) ); return; }
842 d -=1;
843 checkDate(cal, y, m, d);
844 ucal_roll(cal, UCAL_MONTH, -2, &status);
845 if (U_FAILURE(status)) { log_err("ucal_roll failed: %s\n", u_errorName(status) ); return; }
846 m -=2;
847 checkDate(cal, y, m, d);
848 ucal_roll(cal, UCAL_DATE, 1, &status);
849 if (U_FAILURE(status)) { log_err("ucal_roll failed: %s\n", u_errorName(status) ); return; }
850 d +=1;
851 checkDate(cal, y, m, d);
852 ucal_roll(cal, UCAL_MONTH, -12, &status);
853 if (U_FAILURE(status)) { log_err("ucal_roll failed: %s\n", u_errorName(status) ); return; }
854 checkDate(cal, y, m, d);
855 ucal_roll(cal, UCAL_YEAR, -1, &status);
856 if (U_FAILURE(status)) { log_err("ucal_roll failed: %s\n", u_errorName(status) ); return; }
857 y -=1;
858 checkDate(cal, y, m, d);
859 ucal_roll(cal, UCAL_DATE, 29, &status);
860 if (U_FAILURE(status)) { log_err("ucal_roll failed: %s\n", u_errorName(status) ); return; }
861 d = 2;
862 checkDate(cal, y, m, d);
863 ucal_roll(cal, (UCalendarDateFields)-1, 10, &status);
864 if(status==U_ILLEGAL_ARGUMENT_ERROR)
865 log_verbose("Pass: illegal arguement error as expected\n");
866 else{
867 log_err("Fail: no illegal argument error got..: %s\n", u_errorName(status));
868 return;
869 }
870 status=U_ZERO_ERROR;
871 ucal_clear(cal);
872 ucal_setDateTime(cal, 1999, UCAL_FEBRUARY, 28, 10, 30, 45, &status);
873 if(U_FAILURE(status)){
874 log_err("error is setting the datetime: %s\n", u_errorName(status));
875 }
876 ucal_add(cal, UCAL_MONTH, 1, &status);
877 checkDate(cal, 1999, UCAL_MARCH, 28);
878 ucal_add(cal, UCAL_MILLISECOND, 1000, &status);
879 checkDateTime(cal, 1999, UCAL_MARCH, 28, 10, 30, 46, 0, UCAL_MILLISECOND);
880
881 ucal_close(cal);
882 /*--------------- */
883 status=U_ZERO_ERROR;
884 /* Testing add and roll extensively */
885 log_verbose("\nTesting the ucal_add() and ucal_roll() functions extensively\n");
886 y = 1997; m = UCAL_FEBRUARY; d = 1; hr = 1; min = 1; sec = 0; ms = 0;
887 cal=ucal_open(tzID, u_strlen(tzID), "en_US", UCAL_TRADITIONAL, &status);
888 if (U_FAILURE(status)) {
889 log_err("ucal_open failed: %s\n", u_errorName(status));
890 return;
891 }
892 ucal_set(cal, UCAL_YEAR, y);
893 ucal_set(cal, UCAL_MONTH, m);
894 ucal_set(cal, UCAL_DATE, d);
895 ucal_set(cal, UCAL_HOUR, hr);
896 ucal_set(cal, UCAL_MINUTE, min);
897 ucal_set(cal, UCAL_SECOND,sec);
898 ucal_set(cal, UCAL_MILLISECOND, ms);
899 status=U_ZERO_ERROR;
900
901 log_verbose("\nTesting UCalendar add...\n");
902 for(e = UCAL_YEAR;e < UCAL_FIELD_COUNT; e=(UCalendarDateFields)((int32_t)e + 1)) {
903 limit = maxlimit;
904 status = U_ZERO_ERROR;
905 for (i = 0; i < limit; i++) {
906 ucal_add(cal, e, 1, &status);
907 if (U_FAILURE(status)) { limit = i; status = U_ZERO_ERROR; }
908 }
909 for (i = 0; i < limit; i++) {
910 ucal_add(cal, e, -1, &status);
911 if (U_FAILURE(status)) {
912 log_err("ucal_add -1 failed: %s\n", u_errorName(status));
913 return;
914 }
915 }
916 checkDateTime(cal, y, m, d, hr, min, sec, ms, e);
917 }
918 log_verbose("\nTesting calendar ucal_roll()...\n");
919 for(e = UCAL_YEAR;e < UCAL_FIELD_COUNT; e=(UCalendarDateFields)((int32_t)e + 1)) {
920 limit = maxlimit;
921 status = U_ZERO_ERROR;
922 for (i = 0; i < limit; i++) {
923 ucal_roll(cal, e, 1, &status);
924 if (U_FAILURE(status)) {
925 limit = i;
926 status = U_ZERO_ERROR;
927 }
928 }
929 for (i = 0; i < limit; i++) {
930 ucal_roll(cal, e, -1, &status);
931 if (U_FAILURE(status)) {
932 log_err("ucal_roll -1 failed: %s\n", u_errorName(status));
933 return;
934 }
935 }
936 checkDateTime(cal, y, m, d, hr, min, sec, ms, e);
937 }
938
939 ucal_close(cal);
940 }
941
942 /*------------------------------------------------------ */
943 /*Testing the Limits for various Fields of Calendar*/
944 static void TestGetLimits()
945 {
946 UCalendar *cal = 0;
947 int32_t min, max, gr_min, le_max, ac_min, ac_max, val;
948 UChar tzID[4];
949 UErrorCode status = U_ZERO_ERROR;
950
951
952 u_uastrcpy(tzID, "PST");
953 /*open the calendar used */
954 cal=ucal_open(tzID, u_strlen(tzID), "en_US", UCAL_GREGORIAN, &status);;
955 if (U_FAILURE(status)) {
956 log_err("ucal_open() for gregorian calendar failed in TestGetLimits: %s\n", u_errorName(status));
957 return;
958 }
959
960 log_verbose("\nTesting the getLimits function for various fields\n");
961
962
963
964 ucal_setDate(cal, 1999, UCAL_MARCH, 5, &status); /* Set the date to be March 5, 1999 */
965 val = ucal_get(cal, UCAL_DAY_OF_WEEK, &status);
966 min = ucal_getLimit(cal, UCAL_DAY_OF_WEEK, UCAL_MINIMUM, &status);
967 max = ucal_getLimit(cal, UCAL_DAY_OF_WEEK, UCAL_MAXIMUM, &status);
968 if ( (min != UCAL_SUNDAY || max != UCAL_SATURDAY ) && (min > val && val > max) && (val != UCAL_FRIDAY)){
969 log_err("FAIL: Min/max bad\n");
970 log_err("FAIL: Day of week %d out of range\n", val);
971 log_err("FAIL: FAIL: Day of week should be SUNDAY Got %d\n", val);
972 }
973 else
974 log_verbose("getLimits successful\n");
975
976 val = ucal_get(cal, UCAL_DAY_OF_WEEK_IN_MONTH, &status);
977 min = ucal_getLimit(cal, UCAL_DAY_OF_WEEK_IN_MONTH, UCAL_MINIMUM, &status);
978 max = ucal_getLimit(cal, UCAL_DAY_OF_WEEK_IN_MONTH, UCAL_MAXIMUM, &status);
979 if ( (min != 0 || max != 5 ) && (min > val && val > max) && (val != 1)){
980 log_err("FAIL: Min/max bad\n");
981 log_err("FAIL: Day of week in month %d out of range\n", val);
982 log_err("FAIL: FAIL: Day of week in month should be SUNDAY Got %d\n", val);
983
984 }
985 else
986 log_verbose("getLimits successful\n");
987
988 min=ucal_getLimit(cal, UCAL_MONTH, UCAL_MINIMUM, &status);
989 max=ucal_getLimit(cal, UCAL_MONTH, UCAL_MAXIMUM, &status);
990 gr_min=ucal_getLimit(cal, UCAL_MONTH, UCAL_GREATEST_MINIMUM, &status);
991 le_max=ucal_getLimit(cal, UCAL_MONTH, UCAL_LEAST_MAXIMUM, &status);
992 ac_min=ucal_getLimit(cal, UCAL_MONTH, UCAL_ACTUAL_MINIMUM, &status);
993 ac_max=ucal_getLimit(cal, UCAL_MONTH, UCAL_ACTUAL_MAXIMUM, &status);
994 if(U_FAILURE(status)){
995 log_err("Error in getLimits: %s\n", u_errorName(status));
996 }
997 if(min!=0 || max!=11 || gr_min!=0 || le_max!=11 || ac_min!=0 || ac_max!=11)
998 log_err("There is and error in getLimits in fetching the values\n");
999 else
1000 log_verbose("getLimits successful\n");
1001
1002 ucal_setDateTime(cal, 1999, UCAL_MARCH, 5, 4, 10, 35, &status);
1003 val=ucal_get(cal, UCAL_HOUR_OF_DAY, &status);
1004 min=ucal_getLimit(cal, UCAL_HOUR_OF_DAY, UCAL_MINIMUM, &status);
1005 max=ucal_getLimit(cal, UCAL_HOUR_OF_DAY, UCAL_MAXIMUM, &status);
1006 gr_min=ucal_getLimit(cal, UCAL_MINUTE, UCAL_GREATEST_MINIMUM, &status);
1007 le_max=ucal_getLimit(cal, UCAL_MINUTE, UCAL_LEAST_MAXIMUM, &status);
1008 ac_min=ucal_getLimit(cal, UCAL_MINUTE, UCAL_ACTUAL_MINIMUM, &status);
1009 ac_max=ucal_getLimit(cal, UCAL_SECOND, UCAL_ACTUAL_MAXIMUM, &status);
1010 if( (min!=0 || max!= 11 || gr_min!=0 || le_max!=60 || ac_min!=0 || ac_max!=60) &&
1011 (min>val && val>max) && val!=4){
1012
1013 log_err("FAIL: Min/max bad\n");
1014 log_err("FAIL: Hour of Day %d out of range\n", val);
1015 log_err("FAIL: HOUR_OF_DAY should be 4 Got %d\n", val);
1016 }
1017 else
1018 log_verbose("getLimits successful\n");
1019
1020
1021 /*get BOGUS_LIMIT type*/
1022 val=ucal_getLimit(cal, UCAL_SECOND, (UCalendarLimitType)99, &status);
1023 if(val != -1){
1024 log_err("FAIL: ucal_getLimit() with BOGUS type should return -1\n");
1025 }
1026 status=U_ZERO_ERROR;
1027
1028
1029 ucal_close(cal);
1030 }
1031
1032
1033
1034 /* ------------------------------------- */
1035
1036 /**
1037 * Test that the days of the week progress properly when add is called repeatedly
1038 * for increments of 24 days.
1039 */
1040 static void TestDOWProgression()
1041 {
1042 int32_t initialDOW, DOW, newDOW, expectedDOW;
1043 UCalendar *cal = 0;
1044 UDateFormat *datfor = 0;
1045 UDate date1;
1046 int32_t delta=24;
1047 UErrorCode status = U_ZERO_ERROR;
1048 UChar tzID[4];
1049 char tempMsgBuf[256];
1050 u_strcpy(tzID, fgGMTID);
1051 /*open the calendar used */
1052 cal=ucal_open(tzID, u_strlen(tzID), "en_US", UCAL_TRADITIONAL, &status);;
1053 if (U_FAILURE(status)) {
1054 log_err("ucal_open failed: %s\n", u_errorName(status));
1055 return;
1056 }
1057
1058 datfor=udat_open(UDAT_MEDIUM,UDAT_MEDIUM ,NULL, fgGMTID,-1,NULL, 0, &status);
1059 if(U_FAILURE(status)){
1060 log_err("error in creating the dateformat : %s\n", u_errorName(status));
1061 }
1062
1063
1064 ucal_setDate(cal, 1999, UCAL_JANUARY, 1, &status);
1065
1066 log_verbose("\nTesting the DOW progression\n");
1067
1068 initialDOW = ucal_get(cal, UCAL_DAY_OF_WEEK, &status);
1069 if (U_FAILURE(status)) { log_err("ucal_get() failed: %s\n", u_errorName(status) ); return; }
1070 newDOW = initialDOW;
1071 do {
1072 DOW = newDOW;
1073 log_verbose("DOW = %d...\n", DOW);
1074 date1=ucal_getMillis(cal, &status);
1075 if(U_FAILURE(status)){ log_err("ucal_getMiilis() failed: %s\n", u_errorName(status)); return;}
1076 log_verbose("%s\n", u_austrcpy(tempMsgBuf, myDateFormat(datfor, date1)));
1077
1078 ucal_add(cal,UCAL_DAY_OF_WEEK, delta, &status);
1079 if (U_FAILURE(status)) { log_err("ucal_add() failed: %s\n", u_errorName(status)); return; }
1080
1081 newDOW = ucal_get(cal, UCAL_DAY_OF_WEEK, &status);
1082 if (U_FAILURE(status)) { log_err("ucal_get() failed: %s\n", u_errorName(status)); return; }
1083 expectedDOW = 1 + (DOW + delta - 1) % 7;
1084 date1=ucal_getMillis(cal, &status);
1085 if(U_FAILURE(status)){ log_err("ucal_getMiilis() failed: %s\n", u_errorName(status)); return;}
1086 if (newDOW != expectedDOW) {
1087 log_err("Day of week should be %d instead of %d on %s", expectedDOW, newDOW,
1088 u_austrcpy(tempMsgBuf, myDateFormat(datfor, date1)) );
1089 return;
1090 }
1091 }
1092 while (newDOW != initialDOW);
1093
1094 ucal_close(cal);
1095 udat_close(datfor);
1096 }
1097
1098 /* ------------------------------------- */
1099
1100 /**
1101 * Confirm that the offset between local time and GMT behaves as expected.
1102 */
1103 static void TestGMTvsLocal()
1104 {
1105 log_verbose("\nTesting the offset between the GMT and local time\n");
1106 testZones(1999, 1, 1, 12, 0, 0);
1107 testZones(1999, 4, 16, 18, 30, 0);
1108 testZones(1998, 12, 17, 19, 0, 0);
1109 }
1110
1111 /* ------------------------------------- */
1112
1113 static void testZones(int32_t yr, int32_t mo, int32_t dt, int32_t hr, int32_t mn, int32_t sc)
1114 {
1115 int32_t offset,utc, expected;
1116 UCalendar *gmtcal = 0, *cal = 0;
1117 UDate date1;
1118 double temp;
1119 UDateFormat *datfor = 0;
1120 UErrorCode status = U_ZERO_ERROR;
1121 UChar tzID[4];
1122 char tempMsgBuf[256];
1123
1124 u_strcpy(tzID, fgGMTID);
1125 gmtcal=ucal_open(tzID, 3, "en_US", UCAL_TRADITIONAL, &status);;
1126 if (U_FAILURE(status)) {
1127 log_err("ucal_open failed: %s\n", u_errorName(status));
1128 return;
1129 }
1130 u_uastrcpy(tzID, "PST");
1131 cal = ucal_open(tzID, 3, "en_US", UCAL_TRADITIONAL, &status);
1132 if (U_FAILURE(status)) {
1133 log_err("ucal_open failed: %s\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_err("error in creating the dateformat : %s\n", u_errorName(status));
1140 }
1141
1142 ucal_setDateTime(gmtcal, yr, mo - 1, dt, hr, mn, sc, &status);
1143 if (U_FAILURE(status)) {
1144 log_err("ucal_setDateTime failed: %s\n", u_errorName(status));
1145 return;
1146 }
1147 ucal_set(gmtcal, UCAL_MILLISECOND, 0);
1148 date1 = ucal_getMillis(gmtcal, &status);
1149 if (U_FAILURE(status)) {
1150 log_err("ucal_getMillis failed: %s\n", u_errorName(status));
1151 return;
1152 }
1153 log_verbose("date = %s\n", u_austrcpy(tempMsgBuf, myDateFormat(datfor, date1)) );
1154
1155
1156 ucal_setMillis(cal, date1, &status);
1157 if (U_FAILURE(status)) {
1158 log_err("ucal_setMillis() failed: %s\n", u_errorName(status));
1159 return;
1160 }
1161
1162 offset = ucal_get(cal, UCAL_ZONE_OFFSET, &status);
1163 offset += ucal_get(cal, UCAL_DST_OFFSET, &status);
1164
1165 if (U_FAILURE(status)) {
1166 log_err("ucal_get() failed: %s\n", u_errorName(status));
1167 return;
1168 }
1169 temp=(double)((double)offset / 1000.0 / 60.0 / 60.0);
1170 /*printf("offset for %s %f hr\n", austrdup(myDateFormat(datfor, date1)), temp);*/
1171
1172 utc = ((ucal_get(cal, UCAL_HOUR_OF_DAY, &status) * 60 +
1173 ucal_get(cal, UCAL_MINUTE, &status)) * 60 +
1174 ucal_get(cal, UCAL_SECOND, &status)) * 1000 +
1175 ucal_get(cal, UCAL_MILLISECOND, &status) - offset;
1176 if (U_FAILURE(status)) {
1177 log_err("ucal_get() failed: %s\n", u_errorName(status));
1178 return;
1179 }
1180
1181 expected = ((hr * 60 + mn) * 60 + sc) * 1000;
1182 if (utc != expected) {
1183 temp=(double)(utc - expected)/ 1000 / 60 / 60.0;
1184 log_err("FAIL: Discrepancy of %d millis = %fhr\n", utc-expected, temp );
1185 }
1186 else
1187 log_verbose("PASS: the offset between local and GMT is correct\n");
1188 ucal_close(gmtcal);
1189 ucal_close(cal);
1190 udat_close(datfor);
1191 }
1192
1193 /* ------------------------------------- */
1194
1195
1196
1197
1198 /* INTERNAL FUNCTIONS USED */
1199 /*------------------------------------------------------------------------------------------- */
1200
1201 /* ------------------------------------- */
1202 static void checkDateTime(UCalendar* c,
1203 int32_t y, int32_t m, int32_t d,
1204 int32_t hr, int32_t min, int32_t sec,
1205 int32_t ms, UCalendarDateFields field)
1206
1207 {
1208 UErrorCode status = U_ZERO_ERROR;
1209 if (ucal_get(c, UCAL_YEAR, &status) != y ||
1210 ucal_get(c, UCAL_MONTH, &status) != m ||
1211 ucal_get(c, UCAL_DATE, &status) != d ||
1212 ucal_get(c, UCAL_HOUR, &status) != hr ||
1213 ucal_get(c, UCAL_MINUTE, &status) != min ||
1214 ucal_get(c, UCAL_SECOND, &status) != sec ||
1215 ucal_get(c, UCAL_MILLISECOND, &status) != ms) {
1216 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",
1217 (int32_t)field, y, m + 1, d, hr, min, sec, ms,
1218 ucal_get(c, UCAL_YEAR, &status),
1219 ucal_get(c, UCAL_MONTH, &status) + 1,
1220 ucal_get(c, UCAL_DATE, &status),
1221 ucal_get(c, UCAL_HOUR, &status),
1222 ucal_get(c, UCAL_MINUTE, &status) + 1,
1223 ucal_get(c, UCAL_SECOND, &status),
1224 ucal_get(c, UCAL_MILLISECOND, &status) );
1225
1226 if (U_FAILURE(status)){
1227 log_err("ucal_get failed: %s\n", u_errorName(status));
1228 return;
1229 }
1230
1231 }
1232 else
1233 log_verbose("Confirmed: %d/%d/%d %d:%d:%d:%d\n", y, m + 1, d, hr, min, sec, ms);
1234
1235 }
1236
1237 /* ------------------------------------- */
1238 static void checkDate(UCalendar* c, int32_t y, int32_t m, int32_t d)
1239 {
1240 UErrorCode status = U_ZERO_ERROR;
1241 if (ucal_get(c,UCAL_YEAR, &status) != y ||
1242 ucal_get(c, UCAL_MONTH, &status) != m ||
1243 ucal_get(c, UCAL_DATE, &status) != d) {
1244
1245 log_err("FAILURE: Expected y/m/d of %d/%d/%d got %d/%d/%d\n", y, m + 1, d,
1246 ucal_get(c, UCAL_YEAR, &status),
1247 ucal_get(c, UCAL_MONTH, &status) + 1,
1248 ucal_get(c, UCAL_DATE, &status) );
1249
1250 if (U_FAILURE(status)) {
1251 log_err("ucal_get failed: %s\n", u_errorName(status));
1252 return;
1253 }
1254 }
1255 else
1256 log_verbose("Confirmed: %d/%d/%d\n", y, m + 1, d);
1257
1258
1259 }
1260
1261 /* ------------------------------------- */
1262
1263 /* ------------------------------------- */
1264
1265 static void verify1(const char* msg, UCalendar* c, UDateFormat* dat, int32_t year, int32_t month, int32_t day)
1266 {
1267 UDate d1;
1268 UErrorCode status = U_ZERO_ERROR;
1269 if (ucal_get(c, UCAL_YEAR, &status) == year &&
1270 ucal_get(c, UCAL_MONTH, &status) == month &&
1271 ucal_get(c, UCAL_DATE, &status) == day) {
1272 if (U_FAILURE(status)) {
1273 log_err("FAIL: Calendar::get failed: %s\n", u_errorName(status));
1274 return;
1275 }
1276 log_verbose("PASS: %s\n", msg);
1277 d1=ucal_getMillis(c, &status);
1278 if (U_FAILURE(status)) {
1279 log_err("ucal_getMillis failed: %s\n", u_errorName(status));
1280 return;
1281 }
1282 /*log_verbose(austrdup(myDateFormat(dat, d1)) );*/
1283 }
1284 else {
1285 log_err("FAIL: %s\n", msg);
1286 d1=ucal_getMillis(c, &status);
1287 if (U_FAILURE(status)) {
1288 log_err("ucal_getMillis failed: %s\n", u_errorName(status) );
1289 return;
1290 }
1291 log_err("Got %s Expected %d/%d/%d \n", austrdup(myDateFormat(dat, d1)), year, month + 1, day );
1292 return;
1293 }
1294
1295
1296 }
1297
1298 /* ------------------------------------ */
1299 static void verify2(const char* msg, UCalendar* c, UDateFormat* dat, int32_t year, int32_t month, int32_t day,
1300 int32_t hour, int32_t min, int32_t sec, int32_t am_pm)
1301 {
1302 UDate d1;
1303 UErrorCode status = U_ZERO_ERROR;
1304 char tempMsgBuf[256];
1305
1306 if (ucal_get(c, UCAL_YEAR, &status) == year &&
1307 ucal_get(c, UCAL_MONTH, &status) == month &&
1308 ucal_get(c, UCAL_DATE, &status) == day &&
1309 ucal_get(c, UCAL_HOUR, &status) == hour &&
1310 ucal_get(c, UCAL_MINUTE, &status) == min &&
1311 ucal_get(c, UCAL_SECOND, &status) == sec &&
1312 ucal_get(c, UCAL_AM_PM, &status) == am_pm ){
1313 if (U_FAILURE(status)) {
1314 log_err("FAIL: Calendar::get failed: %s\n", u_errorName(status));
1315 return;
1316 }
1317 log_verbose("PASS: %s\n", msg);
1318 d1=ucal_getMillis(c, &status);
1319 if (U_FAILURE(status)) {
1320 log_err("ucal_getMillis failed: %s\n", u_errorName(status));
1321 return;
1322 }
1323 log_verbose("%s\n" , u_austrcpy(tempMsgBuf, myDateFormat(dat, d1)) );
1324 }
1325 else {
1326 log_err("FAIL: %s\n", msg);
1327 d1=ucal_getMillis(c, &status);
1328 if (U_FAILURE(status)) {
1329 log_err("ucal_getMillis failed: %s\n", u_errorName(status));
1330 return;
1331 }
1332 log_err("Got %s Expected %d/%d/%d/ %d:%d:%d %s\n", austrdup(myDateFormat(dat, d1)),
1333 year, month + 1, day, hour, min, sec, (am_pm==0) ? "AM": "PM");
1334
1335 return;
1336 }
1337
1338
1339 }
1340
1341 void TestGregorianChange() {
1342 static const UChar utc[] = { 0x45, 0x74, 0x63, 0x2f, 0x47, 0x4d, 0x54, 0 }; /* "Etc/GMT" */
1343 const int32_t dayMillis = 86400 * INT64_C(1000); /* 1 day = 86400 seconds */
1344 UCalendar *cal;
1345 UDate date;
1346 UErrorCode errorCode = U_ZERO_ERROR;
1347
1348 /* Test ucal_setGregorianChange() on a Gregorian calendar. */
1349 errorCode = U_ZERO_ERROR;
1350 cal = ucal_open(utc, -1, "", UCAL_GREGORIAN, &errorCode);
1351 if(U_FAILURE(errorCode)) {
1352 log_err("ucal_open(UTC) failed: %s\n", u_errorName(errorCode));
1353 return;
1354 }
1355 ucal_setGregorianChange(cal, -365 * (dayMillis * (UDate)1), &errorCode);
1356 if(U_FAILURE(errorCode)) {
1357 log_err("ucal_setGregorianChange(1969) failed: %s\n", u_errorName(errorCode));
1358 } else {
1359 date = ucal_getGregorianChange(cal, &errorCode);
1360 if(U_FAILURE(errorCode) || date != -365 * (dayMillis * (UDate)1)) {
1361 log_err("ucal_getGregorianChange() failed: %s, date = %f\n", u_errorName(errorCode), date);
1362 }
1363 }
1364 ucal_close(cal);
1365
1366 /* Test ucal_setGregorianChange() on a non-Gregorian calendar where it should fail. */
1367 errorCode = U_ZERO_ERROR;
1368 cal = ucal_open(utc, -1, "th@calendar=buddhist", UCAL_TRADITIONAL, &errorCode);
1369 if(U_FAILURE(errorCode)) {
1370 log_err("ucal_open(UTC, non-Gregorian) failed: %s\n", u_errorName(errorCode));
1371 return;
1372 }
1373 ucal_setGregorianChange(cal, -730 * (dayMillis * (UDate)1), &errorCode);
1374 if(errorCode != U_UNSUPPORTED_ERROR) {
1375 log_err("ucal_setGregorianChange(non-Gregorian calendar) did not yield U_UNSUPPORTED_ERROR but %s\n",
1376 u_errorName(errorCode));
1377 }
1378 errorCode = U_ZERO_ERROR;
1379 date = ucal_getGregorianChange(cal, &errorCode);
1380 if(errorCode != U_UNSUPPORTED_ERROR) {
1381 log_err("ucal_getGregorianChange(non-Gregorian calendar) did not yield U_UNSUPPORTED_ERROR but %s\n",
1382 u_errorName(errorCode));
1383 }
1384 ucal_close(cal);
1385 }
1386
1387 #endif /* #if !UCONFIG_NO_FORMATTING */