1 /********************************************************************
2 * Copyright (c) 1997-2008, International Business Machines
3 * Corporation and others. All Rights Reserved.
4 ********************************************************************
8 * Modification History:
10 * Madhu Katragadda Creation
11 ********************************************************************
14 /* C API AND FUNCTIONALITY TEST FOR CALENDAR (ucol.h)*/
16 #include "unicode/utypes.h"
18 #if !UCONFIG_NO_FORMATTING
23 #include "unicode/uloc.h"
24 #include "unicode/ucal.h"
25 #include "unicode/udat.h"
26 #include "unicode/ustring.h"
32 void TestGregorianChange(void);
34 void addCalTest(TestNode
** root
);
36 void addCalTest(TestNode
** root
)
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");
50 static const UChar fgGMTID
[] = { 0x0047, 0x004d, 0x0054, 0x0000 };
53 static const UChar PST
[] = {0x50, 0x53, 0x54, 0x00}; /* "PST" */
55 static const UChar EUROPE_PARIS
[] = {0x45, 0x75, 0x72, 0x6F, 0x70, 0x65, 0x2F, 0x50, 0x61, 0x72, 0x69, 0x73, 0x00}; /* "Europe/Paris" */
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 */
62 UCalendarType calType
;
63 const char * expectedResult
;
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 */
74 static void TestCalendar()
76 UCalendar
*caldef
= 0, *caldef2
= 0, *calfr
= 0, *calit
= 0, *calfrclone
= 0;
77 UEnumeration
* uenum
= NULL
;
78 int32_t count
, count2
, i
,j
;
81 UErrorCode status
= U_ZERO_ERROR
;
83 UDateFormat
*datdef
= 0;
85 int32_t resultlength
, resultlengthneeded
;
87 UChar zone1
[32], zone2
[32];
88 const char *tzver
= 0;
89 UChar canonicalID
[64];
90 UBool isSystemID
= FALSE
;
91 const UCalGetTypeTest
* ucalGetTypeTestPtr
;
93 #ifdef U_USE_UCAL_OBSOLETE_2_8
94 /*Testing countAvailableTimeZones*/
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
);
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
));
109 log_verbose("%s\n", u_austrcpy(tempMsgBuf
, ucal_getAvailableTZIDs(offset
, i
, &status
)));
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
));
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
));
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
);
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
));
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
));
147 log_verbose("%s\n", id
);
150 /* Next one should be NULL */
151 id
= uenum_next(uenum
, &len
, &status
);
153 log_err("FAIL: uenum_next for %s returned %s, expected NULL\n",
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
));
167 log_err("FAIL: ucal_getDSTSavings(GMT) => %d, expect 0\n", i
);
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);
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
));
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
));
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
));
194 if (u_strcmp(zone2
, EUROPE_PARIS
) != 0) {
195 log_err("FAIL: ucal_getDefaultTimeZone() did not return Europe/Paris\n");
199 status
= U_ZERO_ERROR
;
200 ucal_setDefaultTimeZone(zone1
, &status
);
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");
211 log_verbose("PASS: ucal_getTZDataVersion returned %s\n", tzver
);
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
));
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
);
226 log_err("FAIL: ucal_getCanonicalTimeZoneID(%s) set %d to isSystemID\n",
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
));
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
));
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
));
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
));
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
));
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 */
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
);
268 log_data_err("FAIL: Error in countAvailable()\n");
271 for(i
=0;i
<count
;i
++) {
272 log_verbose("%s\n", ucal_getAvailable(i
));
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");
283 log_verbose("PASS: equivalentTo test passed\n");
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");
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
));
297 log_verbose("PASS: The current date and time fetched is %s\n", u_austrcpy(tempMsgBuf
, myDateFormat(datdef
, now
)) );
300 /*Testing the TimeZoneDisplayName */
301 log_verbose("\nTesting the fetching of time zone display name\n");
302 /*for the US locale */
304 resultlengthneeded
=ucal_getTimeZoneDisplayName(caldef
, UCAL_DST
, "en_US", NULL
, resultlength
, &status
);
306 if(status
==U_BUFFER_OVERFLOW_ERROR
)
309 resultlength
=resultlengthneeded
+1;
310 result
=(UChar
*)malloc(sizeof(UChar
) * resultlength
);
311 ucal_getTimeZoneDisplayName(caldef
, UCAL_DST
, "en_US", result
, resultlength
, &status
);
313 if(U_FAILURE(status
)) {
314 log_err("FAIL: Error in getting the timezone display name : %s\n", u_errorName(status
));
317 log_verbose("PASS: getting the time zone display name successful : %s, %d needed \n",
318 u_errorName(status
), resultlengthneeded
);
322 #define expectPDT "Pacific Daylight Time"
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
) );
330 log_err("FAIL: got the wrong time zone(DST) display name %s, wanted %s\n", austrdup(result
) , expectPDT
);
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
));
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
));
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
));
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");
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");
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");
376 /*get bogus Attribute*/
377 count
=ucal_getAttribute(calit
, (UCalendarAttribute
)99); /* BOGUS_ATTRIBUTE */
379 log_err("FAIL: get/bogus attribute should return -1\n");
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");
391 /*set it back to normal */
392 ucal_setAttribute(calit
, UCAL_MINIMAL_DAYS_IN_FIRST_WEEK
,count
);
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
));
402 if(!ucal_inDaylightTime(caldef
, &status
))
403 log_verbose("PASS: It is not in daylight saving's time\n");
405 log_err("FAIL: It is not in daylight saving's time\n");
407 /*closing the UCalendar*/
412 ucal_close(calfrclone
);
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
);
426 log_err("FAIL: ucal_open %s type %d, then ucal_getType fails\n", ucalGetTypeTestPtr
->locale
, ucalGetTypeTestPtr
->calType
);
430 log_err("FAIL: ucal_open %s type %d fails\n", ucalGetTypeTestPtr
->locale
, ucalGetTypeTestPtr
->calType
);
434 /*closing the UDateFormat used */
440 /*------------------------------------------------------*/
441 /*Testing the getMillis, setMillis, setDate and setDateTime functions extensively*/
443 static void TestGetSetDateAPI()
445 UCalendar
*caldef
= 0, *caldef2
= 0;
450 UDateFormat
*datdef
= 0;
451 UErrorCode status
=U_ZERO_ERROR
;
452 UDate d2
= 837039928046.0;
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
))
467 log_err("error in creating the dateformat : %s\n", u_errorName(status
));
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
));
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
));
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 /*-------------------*/
494 ctest_setTimeZone(NULL
, &status
);
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
));;
502 hour
=ucal_get(caldef2
, UCAL_HOUR_OF_DAY
, &status
);
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
));
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");
516 log_verbose("PASS: setTimeZone works fine\n");
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
));
525 if(d2
==ucal_getMillis(caldef2
, &status
))
526 log_verbose("PASS: setTimeZone roundtrip test passed\n");
528 log_err("FAIL: setTimeZone roundtrip test failed\n");
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
));
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
);
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
));
542 if(ucal_getMillis(caldef2
, &status
))
543 log_verbose("PASS: setTimeZone roundtrip test passed\n");
545 log_err("FAIL: setTimeZone roundtrip test failed\n");
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
));
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
);
555 ctest_resetTimeZone();
557 /*----------------------------* */
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
));
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");
573 log_err("FAIL:Error in setDate()\n");
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
);
590 /*--------------------*/
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
));
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");
604 log_err("FAIL: Error in setDateTime\n");
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);
625 /*close caldef and datdef*/
631 /*----------------------------------------------------------- */
633 * Confirm the functioning of the calendar field related functions.
635 static void TestFieldGetSet()
639 UDateFormat
*datdef
= 0;
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
));
650 datdef
=udat_open(UDAT_SHORT
,UDAT_SHORT
,NULL
,fgGMTID
,-1,NULL
, 0, &status
);
651 if(U_FAILURE(status
))
653 log_err("error in creating the dateformat : %s\n", u_errorName(status
));
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
));
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");
669 log_verbose("PASS: ucal_get() works fine\n");
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);
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);
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);
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
;
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");
702 log_verbose("PASS: U_ILLEGAL_ARGUMENT_ERROR as expected\n");
704 status
= U_ZERO_ERROR
;
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);
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
;
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");
727 log_verbose("PASS: U_ILLEGAL_ARGUMENT_ERROR as expected\n");
729 status
= U_ZERO_ERROR
;
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);
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);
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");
749 log_verbose("PASS: U_ILLEGAL_ARGUMENT_ERROR as expected\n");
752 ucal_setAttribute(cal
, UCAL_LENIENT
, 1);
754 ucal_set(cal
, UCAL_MONTH
, -3);
755 verify1("1999 -3th month should be", cal
, datdef
, 1998, UCAL_OCTOBER
, 1);
758 /*testing isSet and clearField()*/
759 if(!ucal_isSet(cal
, UCAL_WEEK_OF_YEAR
))
760 log_err("FAIL: error in isSet\n");
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");
767 log_verbose("PASS :clearField working fine\n");
769 /*-------------------------------*/
777 /* ------------------------------------- */
779 * Execute adding and rolling in Calendar extensively,
781 static void TestAddRollExtensive()
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;
792 log_verbose("Testing add and roll extensively\n");
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
));
802 ucal_set(cal
, UCAL_YEAR
, y
);
803 ucal_set(cal
, UCAL_MONTH
, m
);
804 ucal_set(cal
, UCAL_DATE
, d
);
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; }
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; }
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; }
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; }
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; }
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");
833 log_err("Fail: No, illegal argument error as expected. Got....: %s\n", u_errorName(status
));
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; }
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; }
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; }
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; }
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; }
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");
867 log_err("Fail: no illegal argument error got..: %s\n", u_errorName(status
));
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
));
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
);
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
));
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
);
901 log_verbose("\nTesting UCalendar add...\n");
902 for(e
= UCAL_YEAR
;e
< UCAL_FIELD_COUNT
; e
=(UCalendarDateFields
)((int32_t)e
+ 1)) {
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
; }
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
));
916 checkDateTime(cal
, y
, m
, d
, hr
, min
, sec
, ms
, e
);
918 log_verbose("\nTesting calendar ucal_roll()...\n");
919 for(e
= UCAL_YEAR
;e
< UCAL_FIELD_COUNT
; e
=(UCalendarDateFields
)((int32_t)e
+ 1)) {
921 status
= U_ZERO_ERROR
;
922 for (i
= 0; i
< limit
; i
++) {
923 ucal_roll(cal
, e
, 1, &status
);
924 if (U_FAILURE(status
)) {
926 status
= U_ZERO_ERROR
;
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
));
936 checkDateTime(cal
, y
, m
, d
, hr
, min
, sec
, ms
, e
);
942 /*------------------------------------------------------ */
943 /*Testing the Limits for various Fields of Calendar*/
944 static void TestGetLimits()
947 int32_t min
, max
, gr_min
, le_max
, ac_min
, ac_max
, val
;
949 UErrorCode status
= U_ZERO_ERROR
;
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
));
960 log_verbose("\nTesting the getLimits function for various fields\n");
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
);
974 log_verbose("getLimits successful\n");
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
);
986 log_verbose("getLimits successful\n");
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
));
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");
1000 log_verbose("getLimits successful\n");
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){
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
);
1018 log_verbose("getLimits successful\n");
1021 /*get BOGUS_LIMIT type*/
1022 val
=ucal_getLimit(cal
, UCAL_SECOND
, (UCalendarLimitType
)99, &status
);
1024 log_err("FAIL: ucal_getLimit() with BOGUS type should return -1\n");
1026 status
=U_ZERO_ERROR
;
1034 /* ------------------------------------- */
1037 * Test that the days of the week progress properly when add is called repeatedly
1038 * for increments of 24 days.
1040 static void TestDOWProgression()
1042 int32_t initialDOW
, DOW
, newDOW
, expectedDOW
;
1044 UDateFormat
*datfor
= 0;
1047 UErrorCode status
= U_ZERO_ERROR
;
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
));
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
));
1064 ucal_setDate(cal
, 1999, UCAL_JANUARY
, 1, &status
);
1066 log_verbose("\nTesting the DOW progression\n");
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
;
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
)));
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; }
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
)) );
1092 while (newDOW
!= initialDOW
);
1098 /* ------------------------------------- */
1101 * Confirm that the offset between local time and GMT behaves as expected.
1103 static void TestGMTvsLocal()
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);
1111 /* ------------------------------------- */
1113 static void testZones(int32_t yr
, int32_t mo
, int32_t dt
, int32_t hr
, int32_t mn
, int32_t sc
)
1115 int32_t offset
,utc
, expected
;
1116 UCalendar
*gmtcal
= 0, *cal
= 0;
1119 UDateFormat
*datfor
= 0;
1120 UErrorCode status
= U_ZERO_ERROR
;
1122 char tempMsgBuf
[256];
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
));
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
));
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
));
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
));
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
));
1153 log_verbose("date = %s\n", u_austrcpy(tempMsgBuf
, myDateFormat(datfor
, date1
)) );
1156 ucal_setMillis(cal
, date1
, &status
);
1157 if (U_FAILURE(status
)) {
1158 log_err("ucal_setMillis() failed: %s\n", u_errorName(status
));
1162 offset
= ucal_get(cal
, UCAL_ZONE_OFFSET
, &status
);
1163 offset
+= ucal_get(cal
, UCAL_DST_OFFSET
, &status
);
1165 if (U_FAILURE(status
)) {
1166 log_err("ucal_get() failed: %s\n", u_errorName(status
));
1169 temp
=(double)((double)offset
/ 1000.0 / 60.0 / 60.0);
1170 /*printf("offset for %s %f hr\n", austrdup(myDateFormat(datfor, date1)), temp);*/
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
));
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
);
1187 log_verbose("PASS: the offset between local and GMT is correct\n");
1193 /* ------------------------------------- */
1198 /* INTERNAL FUNCTIONS USED */
1199 /*------------------------------------------------------------------------------------------- */
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
)
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
) );
1226 if (U_FAILURE(status
)){
1227 log_err("ucal_get failed: %s\n", u_errorName(status
));
1233 log_verbose("Confirmed: %d/%d/%d %d:%d:%d:%d\n", y
, m
+ 1, d
, hr
, min
, sec
, ms
);
1237 /* ------------------------------------- */
1238 static void checkDate(UCalendar
* c
, int32_t y
, int32_t m
, int32_t d
)
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
) {
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
) );
1250 if (U_FAILURE(status
)) {
1251 log_err("ucal_get failed: %s\n", u_errorName(status
));
1256 log_verbose("Confirmed: %d/%d/%d\n", y
, m
+ 1, d
);
1261 /* ------------------------------------- */
1263 /* ------------------------------------- */
1265 static void verify1(const char* msg
, UCalendar
* c
, UDateFormat
* dat
, int32_t year
, int32_t month
, int32_t day
)
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
));
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
));
1282 /*log_verbose(austrdup(myDateFormat(dat, d1)) );*/
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
) );
1291 log_err("Got %s Expected %d/%d/%d \n", austrdup(myDateFormat(dat
, d1
)), year
, month
+ 1, day
);
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
)
1303 UErrorCode status
= U_ZERO_ERROR
;
1304 char tempMsgBuf
[256];
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
));
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
));
1323 log_verbose("%s\n" , u_austrcpy(tempMsgBuf
, myDateFormat(dat
, d1
)) );
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
));
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");
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 */
1346 UErrorCode errorCode
= U_ZERO_ERROR
;
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
));
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
));
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
);
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
));
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
));
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
));
1387 #endif /* #if !UCONFIG_NO_FORMATTING */