1 /********************************************************************
3 * Copyright (c) 1997-2006, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
6 /******************************************************************************
10 * Modification History:
12 * Madhu Katragadda Creation
13 *******************************************************************************/
15 /* C API AND FUNCTIONALITY TEST FOR CALENDAR (ucol.h)*/
17 #include "unicode/utypes.h"
19 #if !UCONFIG_NO_FORMATTING
23 #include "unicode/uloc.h"
24 #include "unicode/ucal.h"
25 #include "unicode/udat.h"
26 #include "unicode/ustring.h"
31 void TestGregorianChange(void);
33 void addCalTest(TestNode
** root
);
35 void addCalTest(TestNode
** root
)
38 addTest(root
, &TestCalendar
, "tsformat/ccaltst/TestCalendar");
39 addTest(root
, &TestGetSetDateAPI
, "tsformat/ccaltst/TestGetSetDateAPI");
40 addTest(root
, &TestFieldGetSet
, "tsformat/ccaltst/TestFieldGetSet");
41 addTest(root
, &TestAddRollExtensive
, "tsformat/ccaltst/TestAddRollExtensive");
42 addTest(root
, &TestGetLimits
, "tsformat/ccaltst/TestGetLimits");
43 addTest(root
, &TestDOWProgression
, "tsformat/ccaltst/TestDOWProgression");
44 addTest(root
, &TestGMTvsLocal
, "tsformat/ccaltst/TestGMTvsLocal");
45 addTest(root
, &TestGregorianChange
, "tsformat/ccaltst/TestGregorianChange");
49 static const UChar fgGMTID
[] = { 0x0047, 0x004d, 0x0054, 0x0000 };
52 static const UChar PST
[] = {0x50, 0x53, 0x54, 0x00}; /* "PST" */
54 static const UChar EUROPE_PARIS
[] = {0x45, 0x75, 0x72, 0x6F, 0x70, 0x65, 0x2F, 0x50, 0x61, 0x72, 0x69, 0x73, 0x00}; /* "Europe/Paris" */
56 static void TestCalendar()
58 UCalendar
*caldef
= 0, *caldef2
= 0, *calfr
= 0, *calit
= 0;
59 UEnumeration
* uenum
= NULL
;
60 int32_t count
, count2
, i
,j
;
63 UErrorCode status
= U_ZERO_ERROR
;
65 UDateFormat
*datdef
= 0;
67 int32_t resultlength
, resultlengthneeded
;
69 UChar zone1
[32], zone2
[32];
71 #ifdef U_USE_UCAL_OBSOLETE_2_8
72 /*Testing countAvailableTimeZones*/
74 log_verbose("\nTesting ucal_countAvailableTZIDs\n");
75 count
=ucal_countAvailableTZIDs(offset
);
76 log_verbose("The number of timezone id's present with offset 0 are %d:\n", count
);
77 if(count
< 5) /* Don't hard code an exact == test here! */
78 log_err("FAIL: error in the ucal_countAvailableTZIDs - got %d expected at least 5 total\n", count
);
80 /*Testing getAvailableTZIDs*/
81 log_verbose("\nTesting ucal_getAvailableTZIDs");
83 ucal_getAvailableTZIDs(offset
, i
, &status
);
84 if(U_FAILURE(status
)){
85 log_err("FAIL: ucal_getAvailableTZIDs returned %s\n", u_errorName(status
));
87 log_verbose("%s\n", u_austrcpy(tempMsgBuf
, ucal_getAvailableTZIDs(offset
, i
, &status
)));
89 /*get Illegal TZID where index >= count*/
90 ucal_getAvailableTZIDs(offset
, i
, &status
);
91 if(status
!= U_INDEX_OUTOFBOUNDS_ERROR
){
92 log_err("FAIL:for TZID index >= count Expected INDEX_OUTOFBOUNDS_ERROR Got %s\n", u_errorName(status
));
97 /*Test ucal_openTimeZones & ucal_openCountryTimeZones*/
99 const char* api
= (j
==0) ? "ucal_openTimeZones()" :
100 "ucal_openCountryTimeZones(US)";
101 uenum
= (j
==0) ? ucal_openTimeZones(&status
) :
102 ucal_openCountryTimeZones("US", &status
);
103 if (U_FAILURE(status
)) {
104 log_err("FAIL: %s failed with %s", api
,
105 u_errorName(status
));
109 count
= uenum_count(uenum
, &status
);
110 log_verbose("%s returned %d timezone id's:\n", api
, count
);
111 if (count
< 5) { /* Don't hard code an exact == test here! */
112 log_err("FAIL: in %s, got %d, expected at least 5\n", api
, count
);
114 uenum_reset(uenum
, &status
);
115 if (U_FAILURE(status
)){
116 log_err("FAIL: uenum_reset for %s returned %s\n",
117 api
, u_errorName(status
));
119 for (i
=0; i
<count
; i
++) {
120 id
= uenum_next(uenum
, &len
, &status
);
121 if (U_FAILURE(status
)){
122 log_err("FAIL: uenum_next for %s returned %s\n",
123 api
, u_errorName(status
));
125 log_verbose("%s\n", id
);
128 /* Next one should be NULL */
129 id
= uenum_next(uenum
, &len
, &status
);
131 log_err("FAIL: uenum_next for %s returned %s, expected NULL\n",
138 /*Test ucal_getDSTSavings*/
139 status
= U_ZERO_ERROR
;
140 i
= ucal_getDSTSavings(fgGMTID
, &status
);
141 if (U_FAILURE(status
)) {
142 log_err("FAIL: ucal_getDSTSavings(GMT) => %s\n",
143 u_errorName(status
));
145 log_err("FAIL: ucal_getDSTSavings(GMT) => %d, expect 0\n", i
);
147 i
= ucal_getDSTSavings(PST
, &status
);
148 if (U_FAILURE(status
)) {
149 log_err("FAIL: ucal_getDSTSavings(PST) => %s\n",
150 u_errorName(status
));
151 } else if (i
!= 1*60*60*1000) {
152 log_err("FAIL: ucal_getDSTSavings(PST) => %d, expect %d\n", i
, 1*60*60*1000);
155 /*Test ucal_set/getDefaultTimeZone*/
156 status
= U_ZERO_ERROR
;
157 i
= ucal_getDefaultTimeZone(zone1
, sizeof(zone1
)/sizeof(zone1
[0]), &status
);
158 if (U_FAILURE(status
)) {
159 log_err("FAIL: ucal_getDefaultTimeZone() => %s\n",
160 u_errorName(status
));
162 ucal_setDefaultTimeZone(EUROPE_PARIS
, &status
);
163 if (U_FAILURE(status
)) {
164 log_err("FAIL: ucal_setDefaultTimeZone(Europe/Paris) => %s\n",
165 u_errorName(status
));
167 i
= ucal_getDefaultTimeZone(zone2
, sizeof(zone2
)/sizeof(zone2
[0]), &status
);
168 if (U_FAILURE(status
)) {
169 log_err("FAIL: ucal_getDefaultTimeZone() => %s\n",
170 u_errorName(status
));
172 if (u_strcmp(zone2
, EUROPE_PARIS
) != 0) {
173 log_err("FAIL: ucal_getDefaultTimeZone() did not return Europe/Paris\n");
177 status
= U_ZERO_ERROR
;
178 ucal_setDefaultTimeZone(zone1
, &status
);
181 /*Testing the ucal_open() function*/
182 log_verbose("\nTesting the ucal_open()\n");
183 tzID
=(UChar
*)malloc(sizeof(UChar
) * 4);
184 u_uastrcpy(tzID
, "PST");
185 caldef
=ucal_open(tzID
, u_strlen(tzID
), "en_US", UCAL_TRADITIONAL
, &status
);
186 if(U_FAILURE(status
)){
187 log_err("FAIL: error in ucal_open caldef : %s\n", u_errorName(status
));
190 caldef2
=ucal_open(tzID
, u_strlen(tzID
), "en_US", UCAL_TRADITIONAL
, &status
);
191 if(U_FAILURE(status
)){
192 log_err("FAIL: error in ucal_open caldef : %s\n", u_errorName(status
));
194 u_strcpy(tzID
, fgGMTID
);
195 calfr
=ucal_open(tzID
, u_strlen(tzID
), "fr_FR", UCAL_TRADITIONAL
, &status
);
196 if(U_FAILURE(status
)){
197 log_err("FAIL: error in ucal_open calfr : %s\n", u_errorName(status
));
199 calit
=ucal_open(tzID
, u_strlen(tzID
), "it_IT", UCAL_TRADITIONAL
, &status
);
200 if(U_FAILURE(status
)) {
201 log_err("FAIL: error in ucal_open calit : %s\n", u_errorName(status
));
205 /*Testing udat_getAvailable() and udat_countAvailable()*/
206 log_verbose("\nTesting getAvailableLocales and countAvailable()\n");
207 count
=ucal_countAvailable();
208 /* use something sensible w/o hardcoding the count */
210 log_verbose("PASS: ucal_countAvailable() works fine\n");
211 log_verbose("The no: of locales for which calendars are avilable are %d\n", count
);
213 log_data_err("FAIL: Error in countAvailable()\n");
216 for(i
=0;i
<count
;i
++) {
217 log_verbose("%s\n", ucal_getAvailable(i
));
221 /*Testing the equality between calendar's*/
222 log_verbose("\nTesting ucal_equivalentTo()\n");
223 if(caldef
&& caldef2
&& calfr
&& calit
) {
224 if(ucal_equivalentTo(caldef
, caldef2
) == FALSE
|| ucal_equivalentTo(caldef
, calfr
)== TRUE
||
225 ucal_equivalentTo(caldef
, calit
)== TRUE
) {
226 log_err("FAIL: Error. equivalentTo test failed\n");
228 log_verbose("PASS: equivalentTo test passed\n");
233 /*Testing the current time and date using ucal_getnow()*/
234 log_verbose("\nTesting the ucal_getNow function to check if it is fetching tbe current time\n");
236 /* open the date format and format the date to check the output */
237 datdef
=udat_open(UDAT_FULL
,UDAT_FULL
,NULL
, NULL
, 0,NULL
,0,&status
);
238 if(U_FAILURE(status
)){
239 log_err("FAIL: error in creating the dateformat : %s\n", u_errorName(status
));
242 log_verbose("PASS: The current date and time fetched is %s\n", u_austrcpy(tempMsgBuf
, myDateFormat(datdef
, now
)) );
245 /*Testing the TimeZoneDisplayName */
246 log_verbose("\nTesting the fetching of time zone display name\n");
247 /*for the US locale */
249 resultlengthneeded
=ucal_getTimeZoneDisplayName(caldef
, UCAL_DST
, "en_US", NULL
, resultlength
, &status
);
251 if(status
==U_BUFFER_OVERFLOW_ERROR
)
254 resultlength
=resultlengthneeded
+1;
255 result
=(UChar
*)malloc(sizeof(UChar
) * resultlength
);
256 ucal_getTimeZoneDisplayName(caldef
, UCAL_DST
, "en_US", result
, resultlength
, &status
);
258 if(U_FAILURE(status
)) {
259 log_err("FAIL: Error in getting the timezone display name : %s\n", u_errorName(status
));
262 log_verbose("PASS: getting the time zone display name successful : %s, %d needed \n",
263 u_errorName(status
), resultlengthneeded
);
267 #define expectPDT "Pacific Daylight Time"
269 tzdname
=(UChar
*)malloc(sizeof(UChar
) * (sizeof(expectPDT
)+1));
270 u_uastrcpy(tzdname
, expectPDT
);
271 if(u_strcmp(tzdname
, result
)==0){
272 log_verbose("PASS: got the correct time zone display name %s\n", u_austrcpy(tempMsgBuf
, result
) );
275 log_err("FAIL: got the wrong time zone(DST) display name %s, wanted %s\n", austrdup(result
) , expectPDT
);
278 ucal_getTimeZoneDisplayName(caldef
, UCAL_SHORT_DST
, "en_US", result
, resultlength
, &status
);
279 u_uastrcpy(tzdname
, "PDT");
280 if(u_strcmp(tzdname
, result
) != 0){
281 log_err("FAIL: got the wrong time zone(SHORT_DST) display name %s, wanted %s\n", austrdup(result
), austrdup(tzdname
));
284 ucal_getTimeZoneDisplayName(caldef
, UCAL_STANDARD
, "en_US", result
, resultlength
, &status
);
285 u_uastrcpy(tzdname
, "Pacific Standard Time");
286 if(u_strcmp(tzdname
, result
) != 0){
287 log_err("FAIL: got the wrong time zone(STANDARD) display name %s, wanted %s\n", austrdup(result
), austrdup(tzdname
));
290 ucal_getTimeZoneDisplayName(caldef
, UCAL_SHORT_STANDARD
, "en_US", result
, resultlength
, &status
);
291 u_uastrcpy(tzdname
, "PST");
292 if(u_strcmp(tzdname
, result
) != 0){
293 log_err("FAIL: got the wrong time zone(SHORT_STANDARD) display name %s, wanted %s\n", austrdup(result
), austrdup(tzdname
));
297 /*testing the setAttributes and getAttributes of a UCalendar*/
298 log_verbose("\nTesting the getAttributes and set Attributes\n");
299 count
=ucal_getAttribute(calit
, UCAL_LENIENT
);
300 count2
=ucal_getAttribute(calfr
, UCAL_LENIENT
);
301 ucal_setAttribute(calit
, UCAL_LENIENT
, 0);
302 ucal_setAttribute(caldef
, UCAL_LENIENT
, count2
);
303 if( ucal_getAttribute(calit
, UCAL_LENIENT
) !=0 ||
304 ucal_getAttribute(calfr
, UCAL_LENIENT
)!=ucal_getAttribute(caldef
, UCAL_LENIENT
) )
305 log_err("FAIL: there is an error in getAttributes or setAttributes\n");
307 log_verbose("PASS: attribute set and got successfully\n");
308 /*set it back to orginal value */
309 log_verbose("Setting it back to normal\n");
310 ucal_setAttribute(calit
, UCAL_LENIENT
, count
);
311 if(ucal_getAttribute(calit
, UCAL_LENIENT
)!=count
)
312 log_err("FAIL: Error in setting the attribute back to normal\n");
314 /*setting the first day of the week to other values */
315 count
=ucal_getAttribute(calit
, UCAL_FIRST_DAY_OF_WEEK
);
316 for (i
=1; i
<=7; ++i
) {
317 ucal_setAttribute(calit
, UCAL_FIRST_DAY_OF_WEEK
,i
);
318 if (ucal_getAttribute(calit
, UCAL_FIRST_DAY_OF_WEEK
) != i
)
319 log_err("FAIL: set/getFirstDayOfWeek failed\n");
321 /*get bogus Attribute*/
322 count
=ucal_getAttribute(calit
, (UCalendarAttribute
)99); /* BOGUS_ATTRIBUTE */
324 log_err("FAIL: get/bogus attribute should return -1\n");
327 /*set it back to normal */
328 ucal_setAttribute(calit
, UCAL_FIRST_DAY_OF_WEEK
,count
);
329 /*setting minimal days of the week to other values */
330 count
=ucal_getAttribute(calit
, UCAL_MINIMAL_DAYS_IN_FIRST_WEEK
);
331 for (i
=1; i
<=7; ++i
) {
332 ucal_setAttribute(calit
, UCAL_MINIMAL_DAYS_IN_FIRST_WEEK
,i
);
333 if (ucal_getAttribute(calit
, UCAL_MINIMAL_DAYS_IN_FIRST_WEEK
) != i
)
334 log_err("FAIL: set/getMinimalDaysInFirstWeek failed\n");
336 /*set it back to normal */
337 ucal_setAttribute(calit
, UCAL_MINIMAL_DAYS_IN_FIRST_WEEK
,count
);
340 /*testing if the UCalendar's timezone is currently in day light saving's time*/
341 log_verbose("\nTesting if the UCalendar is currently in daylight saving's time\n");
342 ucal_setDateTime(caldef
, 1999, UCAL_MARCH
, 3, 10, 45, 20, &status
);
343 ucal_inDaylightTime(caldef
, &status
);
344 if(U_FAILURE(status
)) {
345 log_err("Error in ucal_inDaylightTime: %s\n", u_errorName(status
));
347 if(!ucal_inDaylightTime(caldef
, &status
))
348 log_verbose("PASS: It is not in daylight saving's time\n");
350 log_err("FAIL: It is not in daylight saving's time\n");
354 /*closing the UCalendar*/
359 /*closing the UDateFormat used */
367 /*------------------------------------------------------*/
368 /*Testing the getMillis, setMillis, setDate and setDateTime functions extensively*/
370 static void TestGetSetDateAPI()
372 UCalendar
*caldef
= 0, *caldef2
= 0;
377 UDateFormat
*datdef
= 0;
378 UErrorCode status
=U_ZERO_ERROR
;
379 UDate d2
= 837039928046.0;
382 log_verbose("\nOpening the calendars()\n");
383 tzID
=(UChar
*)malloc(sizeof(UChar
) * 4);
384 u_strcpy(tzID
, fgGMTID
);
385 /*open the calendars used */
386 caldef
=ucal_open(tzID
, u_strlen(tzID
), "en_US", UCAL_TRADITIONAL
, &status
);
387 caldef2
=ucal_open(tzID
, u_strlen(tzID
), "en_US", UCAL_TRADITIONAL
, &status
);
388 /*open the dateformat */
389 /* this is supposed to open default date format, but later on it treats it like it is "en_US"
390 - very bad if you try to run the tests on machine where default locale is NOT "en_US" */
391 /*datdef=udat_open(UDAT_DEFAULT,UDAT_DEFAULT ,NULL,fgGMTID,-1, &status);*/
392 datdef
=udat_open(UDAT_DEFAULT
,UDAT_DEFAULT
,"en_US",fgGMTID
,-1,NULL
,0, &status
);
393 if(U_FAILURE(status
))
395 log_err("error in creating the dateformat : %s\n", u_errorName(status
));
400 /*Testing getMillis and setMillis */
401 log_verbose("\nTesting the date and time fetched in millis for a calendar using getMillis\n");
402 d1
=ucal_getMillis(caldef
, &status
);
403 if(U_FAILURE(status
)){
404 log_err("Error in getMillis : %s\n", u_errorName(status
));
407 /*testing setMillis */
408 log_verbose("\nTesting the set date and time function using setMillis\n");
409 ucal_setMillis(caldef
, d2
, &status
);
410 if(U_FAILURE(status
)){
411 log_err("Error in setMillis : %s\n", u_errorName(status
));
414 /*testing if the calendar date is set properly or not */
415 d1
=ucal_getMillis(caldef
, &status
);
416 if(u_strcmp(myDateFormat(datdef
, d1
), myDateFormat(datdef
, d2
))!=0)
417 log_err("error in setMillis or getMillis\n");
418 /*-------------------*/
422 ctest_setTimeZone(NULL
, &status
);
424 /*testing ucal_setTimeZone() function*/
425 log_verbose("\nTesting if the function ucal_setTimeZone() works fine\n");
426 ucal_setMillis(caldef2
, d2
, &status
);
427 if(U_FAILURE(status
)){
428 log_err("Error in getMillis : %s\n", u_errorName(status
));;
430 hour
=ucal_get(caldef2
, UCAL_HOUR_OF_DAY
, &status
);
432 u_uastrcpy(tzID
, "PST");
433 ucal_setTimeZone(caldef2
,tzID
, 3, &status
);
434 if(U_FAILURE(status
)){
435 log_err("Error in setting the time zone using ucal_setTimeZone(): %s\n", u_errorName(status
));
438 log_verbose("ucal_setTimeZone worked fine\n");
439 if(hour
== ucal_get(caldef2
, UCAL_HOUR_OF_DAY
, &status
))
440 log_err("FAIL: Error setting the time zone doesn't change the represented time\n");
441 else if((hour
-8 + 1) != ucal_get(caldef2
, UCAL_HOUR_OF_DAY
, &status
)) /*because it is not in daylight savings time */
442 log_err("FAIL: Error setTimeZone doesn't change the represented time correctly with 8 hour offset\n");
444 log_verbose("PASS: setTimeZone works fine\n");
446 /*testing setTimeZone roundtrip */
447 log_verbose("\nTesting setTimeZone() roundtrip\n");
448 u_strcpy(tzID
, fgGMTID
);
449 ucal_setTimeZone(caldef2
, tzID
, 3, &status
);
450 if(U_FAILURE(status
)){
451 log_err("Error in setting the time zone using ucal_setTimeZone(): %s\n", u_errorName(status
));
453 if(d2
==ucal_getMillis(caldef2
, &status
))
454 log_verbose("PASS: setTimeZone roundtrip test passed\n");
456 log_err("FAIL: setTimeZone roundtrip test failed\n");
458 zoneOffset
= ucal_get(caldef2
, UCAL_ZONE_OFFSET
, &status
);
459 if(U_FAILURE(status
)){
460 log_err("Error in getting the time zone using ucal_get() after using ucal_setTimeZone(): %s\n", u_errorName(status
));
462 else if (zoneOffset
!= 0) {
463 log_err("Error in getting the time zone using ucal_get() after using ucal_setTimeZone() offset=%d\n", zoneOffset
);
466 ucal_setTimeZone(caldef2
, NULL
, -1, &status
);
467 if(U_FAILURE(status
)){
468 log_err("Error in setting the time zone using ucal_setTimeZone(): %s\n", u_errorName(status
));
470 if(ucal_getMillis(caldef2
, &status
))
471 log_verbose("PASS: setTimeZone roundtrip test passed\n");
473 log_err("FAIL: setTimeZone roundtrip test failed\n");
475 zoneOffset
= ucal_get(caldef2
, UCAL_ZONE_OFFSET
, &status
);
476 if(U_FAILURE(status
)){
477 log_err("Error in getting the time zone using ucal_get() after using ucal_setTimeZone(): %s\n", u_errorName(status
));
479 else if (zoneOffset
!= -28800000) {
480 log_err("Error in getting the time zone using ucal_get() after using ucal_setTimeZone() offset=%d\n", zoneOffset
);
483 ctest_resetTimeZone();
485 /*----------------------------* */
489 /*Testing if setDate works fine */
490 log_verbose("\nTesting the ucal_setDate() function \n");
491 u_uastrcpy(temp
, "Dec 17, 1971 11:05:28 PM");
492 ucal_setDate(caldef
,1971, UCAL_DECEMBER
, 17, &status
);
493 if(U_FAILURE(status
)){
494 log_err("error in setting the calendar date : %s\n", u_errorName(status
));
496 /*checking if the calendar date is set properly or not */
497 d1
=ucal_getMillis(caldef
, &status
);
498 if(u_strcmp(myDateFormat(datdef
, d1
), temp
)==0)
499 log_verbose("PASS:setDate works fine\n");
501 log_err("FAIL:Error in setDate()\n");
504 /* Testing setDate Extensively with various input values */
505 log_verbose("\nTesting ucal_setDate() extensively\n");
506 ucal_setDate(caldef
, 1999, UCAL_JANUARY
, 10, &status
);
507 verify1("1999 10th day of January is :", caldef
, datdef
, 1999, UCAL_JANUARY
, 10);
508 ucal_setDate(caldef
, 1999, UCAL_DECEMBER
, 3, &status
);
509 verify1("1999 3rd day of December is :", caldef
, datdef
, 1999, UCAL_DECEMBER
, 3);
510 ucal_setDate(caldef
, 2000, UCAL_MAY
, 3, &status
);
511 verify1("2000 3rd day of May is :", caldef
, datdef
, 2000, UCAL_MAY
, 3);
512 ucal_setDate(caldef
, 1999, UCAL_AUGUST
, 32, &status
);
513 verify1("1999 32th day of August is :", caldef
, datdef
, 1999, UCAL_SEPTEMBER
, 1);
514 ucal_setDate(caldef
, 1999, UCAL_MARCH
, 0, &status
);
515 verify1("1999 0th day of March is :", caldef
, datdef
, 1999, UCAL_FEBRUARY
, 28);
516 ucal_setDate(caldef
, 0, UCAL_MARCH
, 12, &status
);
518 /*--------------------*/
520 /*Testing if setDateTime works fine */
521 log_verbose("\nTesting the ucal_setDateTime() function \n");
522 u_uastrcpy(temp
, "May 3, 1972 4:30:42 PM");
523 ucal_setDateTime(caldef
,1972, UCAL_MAY
, 3, 16, 30, 42, &status
);
524 if(U_FAILURE(status
)){
525 log_err("error in setting the calendar date : %s\n", u_errorName(status
));
527 /*checking if the calendar date is set properly or not */
528 d1
=ucal_getMillis(caldef
, &status
);
529 if(u_strcmp(myDateFormat(datdef
, d1
), temp
)==0)
530 log_verbose("PASS: setDateTime works fine\n");
532 log_err("FAIL: Error in setDateTime\n");
536 /*Testing setDateTime extensively with various input values*/
537 log_verbose("\nTesting ucal_setDateTime() function extensively\n");
538 ucal_setDateTime(caldef
, 1999, UCAL_OCTOBER
, 10, 6, 45, 30, &status
);
539 verify2("1999 10th day of October at 6:45:30 is :", caldef
, datdef
, 1999, UCAL_OCTOBER
, 10, 6, 45, 30, 0 );
540 ucal_setDateTime(caldef
, 1999, UCAL_MARCH
, 3, 15, 10, 55, &status
);
541 verify2("1999 3rd day of March at 15:10:55 is :", caldef
, datdef
, 1999, UCAL_MARCH
, 3, 3, 10, 55, 1);
542 ucal_setDateTime(caldef
, 1999, UCAL_MAY
, 3, 25, 30, 45, &status
);
543 verify2("1999 3rd day of May at 25:30:45 is :", caldef
, datdef
, 1999, UCAL_MAY
, 4, 1, 30, 45, 0);
544 ucal_setDateTime(caldef
, 1999, UCAL_AUGUST
, 32, 22, 65, 40, &status
);
545 verify2("1999 32th day of August at 22:65:40 is :", caldef
, datdef
, 1999, UCAL_SEPTEMBER
, 1, 11, 5, 40,1);
546 ucal_setDateTime(caldef
, 1999, UCAL_MARCH
, 12, 0, 0, 0,&status
);
547 verify2("1999 12th day of March at 0:0:0 is :", caldef
, datdef
, 1999, UCAL_MARCH
, 12, 0, 0, 0, 0);
548 ucal_setDateTime(caldef
, 1999, UCAL_MARCH
, 12, -10, -10,0, &status
);
549 verify2("1999 12th day of March is at -10:-10:0 :", caldef
, datdef
, 1999, UCAL_MARCH
, 11, 1, 50, 0, 1);
553 /*close caldef and datdef*/
561 /*----------------------------------------------------------- */
563 * Confirm the functioning of the calendar field related functions.
565 static void TestFieldGetSet()
569 UDateFormat
*datdef
= 0;
571 UErrorCode status
=U_ZERO_ERROR
;
572 log_verbose("\nFetching pointer to UCalendar using the ucal_open()\n");
573 tzID
=(UChar
*)malloc(sizeof(UChar
) * 4);
574 u_strcpy(tzID
, fgGMTID
);
575 /*open the calendar used */
576 cal
=ucal_open(tzID
, u_strlen(tzID
), "en_US", UCAL_TRADITIONAL
, &status
);
577 if (U_FAILURE(status
)) {
578 log_err("ucal_open failed: %s\n", u_errorName(status
));
581 datdef
=udat_open(UDAT_SHORT
,UDAT_SHORT
,NULL
,fgGMTID
,-1,NULL
, 0, &status
);
582 if(U_FAILURE(status
))
584 log_err("error in creating the dateformat : %s\n", u_errorName(status
));
587 /*Testing ucal_get()*/
588 log_verbose("\nTesting the ucal_get() function of Calendar\n");
589 ucal_setDateTime(cal
, 1999, UCAL_MARCH
, 12, 5, 25, 30, &status
);
590 if(U_FAILURE(status
)){
591 log_err("error in the setDateTime() : %s\n", u_errorName(status
));
593 if(ucal_get(cal
, UCAL_YEAR
, &status
)!=1999 || ucal_get(cal
, UCAL_MONTH
, &status
)!=2 ||
594 ucal_get(cal
, UCAL_DATE
, &status
)!=12 || ucal_get(cal
, UCAL_HOUR
, &status
)!=5)
595 log_err("error in ucal_get()\n");
596 else if(ucal_get(cal
, UCAL_DAY_OF_WEEK_IN_MONTH
, &status
)!=2 || ucal_get(cal
, UCAL_DAY_OF_WEEK
, &status
)!=6
597 || ucal_get(cal
, UCAL_WEEK_OF_MONTH
, &status
)!=2 || ucal_get(cal
, UCAL_WEEK_OF_YEAR
, &status
)!= 11)
598 log_err("FAIL: error in ucal_get()\n");
600 log_verbose("PASS: ucal_get() works fine\n");
602 /*Testing the ucal_set() , ucal_clear() functions of calendar*/
603 log_verbose("\nTesting the set, and clear field functions of calendar\n");
604 ucal_setAttribute(cal
, UCAL_LENIENT
, 0);
606 ucal_set(cal
, UCAL_YEAR
, 1997);
607 ucal_set(cal
, UCAL_MONTH
, UCAL_JUNE
);
608 ucal_set(cal
, UCAL_DATE
, 3);
609 verify1("1997 third day of June = ", cal
, datdef
, 1997, UCAL_JUNE
, 3);
611 ucal_set(cal
, UCAL_YEAR
, 1997);
612 ucal_set(cal
, UCAL_DAY_OF_WEEK
, UCAL_TUESDAY
);
613 ucal_set(cal
, UCAL_MONTH
, UCAL_JUNE
);
614 ucal_set(cal
, UCAL_DAY_OF_WEEK_IN_MONTH
, 1);
615 verify1("1997 first Tuesday in June = ", cal
, datdef
, 1997, UCAL_JUNE
, 3);
617 ucal_set(cal
, UCAL_YEAR
, 1997);
618 ucal_set(cal
, UCAL_DAY_OF_WEEK
, UCAL_TUESDAY
);
619 ucal_set(cal
, UCAL_MONTH
, UCAL_JUNE
);
620 ucal_set(cal
, UCAL_DAY_OF_WEEK_IN_MONTH
, - 1);
621 verify1("1997 last Tuesday in June = ", cal
, datdef
,1997, UCAL_JUNE
, 24);
622 /*give undesirable input */
623 status
= U_ZERO_ERROR
;
625 ucal_set(cal
, UCAL_YEAR
, 1997);
626 ucal_set(cal
, UCAL_DAY_OF_WEEK
, UCAL_TUESDAY
);
627 ucal_set(cal
, UCAL_MONTH
, UCAL_JUNE
);
628 ucal_set(cal
, UCAL_DAY_OF_WEEK_IN_MONTH
, 0);
629 d1
=ucal_getMillis(cal
,&status
);
630 if (status
!= U_ILLEGAL_ARGUMENT_ERROR
){
631 log_err("FAIL: No IllegalArgumentError for :");
632 log_err("1997 zero-th Tuesday in June \n");
635 log_verbose("PASS: IllegalArgumentError as expected\n");
638 ucal_set(cal
, UCAL_YEAR
, 1997);
639 ucal_set(cal
, UCAL_DAY_OF_WEEK
, UCAL_TUESDAY
);
640 ucal_set(cal
, UCAL_MONTH
, UCAL_JUNE
);
641 ucal_set(cal
, UCAL_WEEK_OF_MONTH
, 1);
642 verify1("1997 Tuesday in week 1 of June = ", cal
,datdef
, 1997, UCAL_JUNE
, 3);
644 ucal_set(cal
, UCAL_YEAR
, 1997);
645 ucal_set(cal
, UCAL_DAY_OF_WEEK
, UCAL_TUESDAY
);
646 ucal_set(cal
, UCAL_MONTH
, UCAL_JUNE
);
647 ucal_set(cal
, UCAL_WEEK_OF_MONTH
, 5);
648 verify1("1997 Tuesday in week 5 of June = ", cal
,datdef
, 1997, UCAL_JULY
, 1);
649 status
= U_ZERO_ERROR
;
651 ucal_set(cal
, UCAL_YEAR
, 1997);
652 ucal_set(cal
, UCAL_DAY_OF_WEEK
, UCAL_TUESDAY
);
653 ucal_set(cal
, UCAL_MONTH
, UCAL_JUNE
);
654 ucal_set(cal
, UCAL_WEEK_OF_MONTH
, 0);
655 verify1("1997 Tuesday in week 0 of June = ", cal
, datdef
, 1997, UCAL_MAY
, 27);
657 ucal_set(cal
, UCAL_YEAR_WOY
, 1997);
658 ucal_set(cal
, UCAL_DAY_OF_WEEK
, UCAL_TUESDAY
);
659 ucal_set(cal
, UCAL_WEEK_OF_YEAR
, 1);
660 verify1("1997 Tuesday in week 1 of year = ", cal
, datdef
,1996, UCAL_DECEMBER
, 31);
662 ucal_set(cal
, UCAL_YEAR
, 1997);
663 ucal_set(cal
, UCAL_DAY_OF_WEEK
, UCAL_TUESDAY
);
664 ucal_set(cal
, UCAL_WEEK_OF_YEAR
, 10);
665 verify1("1997 Tuesday in week 10 of year = ", cal
,datdef
, 1997, UCAL_MARCH
, 4);
667 ucal_set(cal
, UCAL_YEAR
, 1999);
668 ucal_set(cal
, UCAL_DAY_OF_YEAR
, 1);
669 verify1("1999 1st day of the year =", cal
, datdef
, 1999, UCAL_JANUARY
, 1);
670 ucal_set(cal
, UCAL_MONTH
, -3);
671 d1
=ucal_getMillis(cal
,&status
);
672 if (status
!= U_ILLEGAL_ARGUMENT_ERROR
){
673 log_err("FAIL: No IllegalArgumentError for :\"1999 -3th month \" ");
676 log_verbose("PASS: IllegalArgumentError as expected\n");
678 ucal_setAttribute(cal
, UCAL_LENIENT
, 1);
680 ucal_set(cal
, UCAL_MONTH
, -3);
681 verify1("1999 -3th month should be", cal
, datdef
, 1998, UCAL_OCTOBER
, 1);
684 /*testing isSet and clearField()*/
685 if(!ucal_isSet(cal
, UCAL_WEEK_OF_YEAR
))
686 log_err("FAIL: error in isSet\n");
688 log_verbose("PASS: isSet working fine\n");
689 ucal_clearField(cal
, UCAL_WEEK_OF_YEAR
);
690 if(ucal_isSet(cal
, UCAL_WEEK_OF_YEAR
))
691 log_err("FAIL: there is an error in clearField or isSet\n");
693 log_verbose("PASS :clearField working fine\n");
695 /*-------------------------------*/
705 /* ------------------------------------- */
707 * Execute adding and rolling in Calendar extensively,
709 static void TestAddRollExtensive()
714 UCalendarDateFields e
;
715 int32_t y
,m
,d
,hr
,min
,sec
,ms
;
716 int32_t maxlimit
= 40;
717 UErrorCode status
= U_ZERO_ERROR
;
718 y
= 1997; m
= UCAL_FEBRUARY
; d
= 1; hr
= 1; min
= 1; sec
= 0; ms
= 0;
720 log_verbose("Testing add and roll extensively\n");
722 tzID
=(UChar
*)malloc(sizeof(UChar
) * 4);
723 u_uastrcpy(tzID
, "PST");
724 /*open the calendar used */
725 cal
=ucal_open(tzID
, u_strlen(tzID
), "en_US", UCAL_GREGORIAN
, &status
);;
726 if (U_FAILURE(status
)) {
727 log_err("ucal_open() failed : %s\n", u_errorName(status
));
731 ucal_set(cal
, UCAL_YEAR
, y
);
732 ucal_set(cal
, UCAL_MONTH
, m
);
733 ucal_set(cal
, UCAL_DATE
, d
);
735 /* Confirm that adding to various fields works.*/
736 log_verbose("\nTesting to confirm that adding to various fields works with ucal_add()\n");
737 checkDate(cal
, y
, m
, d
);
738 ucal_add(cal
,UCAL_YEAR
, 1, &status
);
739 if (U_FAILURE(status
)) { log_err("ucal_add failed: %s\n", u_errorName(status
)); return; }
741 checkDate(cal
, y
, m
, d
);
742 ucal_add(cal
,UCAL_MONTH
, 12, &status
);
743 if (U_FAILURE(status
)) { log_err("ucal_add failed: %s\n", u_errorName(status
) ); return; }
745 checkDate(cal
, y
, m
, d
);
746 ucal_add(cal
,UCAL_DATE
, 1, &status
);
747 if (U_FAILURE(status
)) { log_err("ucal_add failed: %s\n", u_errorName(status
) ); return; }
749 checkDate(cal
, y
, m
, d
);
750 ucal_add(cal
,UCAL_DATE
, 2, &status
);
751 if (U_FAILURE(status
)) { log_err("ucal_add failed: %s\n", u_errorName(status
) ); return; }
753 checkDate(cal
, y
, m
, d
);
754 ucal_add(cal
,UCAL_DATE
, 28, &status
);
755 if (U_FAILURE(status
)) { log_err("ucal_add failed: %s\n", u_errorName(status
) ); return; }
757 checkDate(cal
, y
, m
, d
);
758 ucal_add(cal
, (UCalendarDateFields
)-1, 10, &status
);
759 if(status
==U_ILLEGAL_ARGUMENT_ERROR
)
760 log_verbose("Pass: Illegal argument error as expected\n");
762 log_err("Fail: No, illegal argument error as expected. Got....: %s\n", u_errorName(status
));
767 /*confirm that applying roll to various fields works fine*/
768 log_verbose("\nTesting to confirm that ucal_roll() works\n");
769 ucal_roll(cal
, UCAL_DATE
, -1, &status
);
770 if (U_FAILURE(status
)) { log_err("ucal_roll failed: %s\n", u_errorName(status
) ); return; }
772 checkDate(cal
, y
, m
, d
);
773 ucal_roll(cal
, UCAL_MONTH
, -2, &status
);
774 if (U_FAILURE(status
)) { log_err("ucal_roll failed: %s\n", u_errorName(status
) ); return; }
776 checkDate(cal
, y
, m
, d
);
777 ucal_roll(cal
, UCAL_DATE
, 1, &status
);
778 if (U_FAILURE(status
)) { log_err("ucal_roll failed: %s\n", u_errorName(status
) ); return; }
780 checkDate(cal
, y
, m
, d
);
781 ucal_roll(cal
, UCAL_MONTH
, -12, &status
);
782 if (U_FAILURE(status
)) { log_err("ucal_roll failed: %s\n", u_errorName(status
) ); return; }
783 checkDate(cal
, y
, m
, d
);
784 ucal_roll(cal
, UCAL_YEAR
, -1, &status
);
785 if (U_FAILURE(status
)) { log_err("ucal_roll failed: %s\n", u_errorName(status
) ); return; }
787 checkDate(cal
, y
, m
, d
);
788 ucal_roll(cal
, UCAL_DATE
, 29, &status
);
789 if (U_FAILURE(status
)) { log_err("ucal_roll failed: %s\n", u_errorName(status
) ); return; }
791 checkDate(cal
, y
, m
, d
);
792 ucal_roll(cal
, (UCalendarDateFields
)-1, 10, &status
);
793 if(status
==U_ILLEGAL_ARGUMENT_ERROR
)
794 log_verbose("Pass: illegal arguement error as expected\n");
796 log_err("Fail: no illegal argument error got..: %s\n", u_errorName(status
));
801 ucal_setDateTime(cal
, 1999, UCAL_FEBRUARY
, 28, 10, 30, 45, &status
);
802 if(U_FAILURE(status
)){
803 log_err("error is setting the datetime: %s\n", u_errorName(status
));
805 ucal_add(cal
, UCAL_MONTH
, 1, &status
);
806 checkDate(cal
, 1999, UCAL_MARCH
, 28);
807 ucal_add(cal
, UCAL_MILLISECOND
, 1000, &status
);
808 checkDateTime(cal
, 1999, UCAL_MARCH
, 28, 10, 30, 46, 0, UCAL_MILLISECOND
);
813 /* Testing add and roll extensively */
814 log_verbose("\nTesting the ucal_add() and ucal_roll() functions extensively\n");
815 y
= 1997; m
= UCAL_FEBRUARY
; d
= 1; hr
= 1; min
= 1; sec
= 0; ms
= 0;
816 cal
=ucal_open(tzID
, u_strlen(tzID
), "en_US", UCAL_TRADITIONAL
, &status
);
817 if (U_FAILURE(status
)) {
818 log_err("ucal_open failed: %s\n", u_errorName(status
));
821 ucal_set(cal
, UCAL_YEAR
, y
);
822 ucal_set(cal
, UCAL_MONTH
, m
);
823 ucal_set(cal
, UCAL_DATE
, d
);
824 ucal_set(cal
, UCAL_HOUR
, hr
);
825 ucal_set(cal
, UCAL_MINUTE
, min
);
826 ucal_set(cal
, UCAL_SECOND
,sec
);
827 ucal_set(cal
, UCAL_MILLISECOND
, ms
);
830 log_verbose("\nTesting UCalendar add...\n");
831 for(e
= UCAL_YEAR
;e
< UCAL_FIELD_COUNT
; e
=(UCalendarDateFields
)((int32_t)e
+ 1)) {
833 status
= U_ZERO_ERROR
;
834 for (i
= 0; i
< limit
; i
++) {
835 ucal_add(cal
, e
, 1, &status
);
836 if (U_FAILURE(status
)) { limit
= i
; status
= U_ZERO_ERROR
; }
838 for (i
= 0; i
< limit
; i
++) {
839 ucal_add(cal
, e
, -1, &status
);
840 if (U_FAILURE(status
)) {
841 log_err("ucal_add -1 failed: %s\n", u_errorName(status
));
845 checkDateTime(cal
, y
, m
, d
, hr
, min
, sec
, ms
, e
);
847 log_verbose("\nTesting calendar ucal_roll()...\n");
848 for(e
= UCAL_YEAR
;e
< UCAL_FIELD_COUNT
; e
=(UCalendarDateFields
)((int32_t)e
+ 1)) {
850 status
= U_ZERO_ERROR
;
851 for (i
= 0; i
< limit
; i
++) {
852 ucal_roll(cal
, e
, 1, &status
);
853 if (U_FAILURE(status
)) {
855 status
= U_ZERO_ERROR
;
858 for (i
= 0; i
< limit
; i
++) {
859 ucal_roll(cal
, e
, -1, &status
);
860 if (U_FAILURE(status
)) {
861 log_err("ucal_roll -1 failed: %s\n", u_errorName(status
));
865 checkDateTime(cal
, y
, m
, d
, hr
, min
, sec
, ms
, e
);
872 /*------------------------------------------------------ */
873 /*Testing the Limits for various Fields of Calendar*/
874 static void TestGetLimits()
877 int32_t min
, max
, gr_min
, le_max
, ac_min
, ac_max
, val
;
879 UErrorCode status
= U_ZERO_ERROR
;
882 tzID
=(UChar
*)malloc(sizeof(UChar
) * 4);
883 u_uastrcpy(tzID
, "PST");
884 /*open the calendar used */
885 cal
=ucal_open(tzID
, u_strlen(tzID
), "en_US", UCAL_GREGORIAN
, &status
);;
886 if (U_FAILURE(status
)) {
887 log_err("ucal_open() for gregorian calendar failed in TestGetLimits: %s\n", u_errorName(status
));
891 log_verbose("\nTesting the getLimits function for various fields\n");
895 ucal_setDate(cal
, 1999, UCAL_MARCH
, 5, &status
); /* Set the date to be March 5, 1999 */
896 val
= ucal_get(cal
, UCAL_DAY_OF_WEEK
, &status
);
897 min
= ucal_getLimit(cal
, UCAL_DAY_OF_WEEK
, UCAL_MINIMUM
, &status
);
898 max
= ucal_getLimit(cal
, UCAL_DAY_OF_WEEK
, UCAL_MAXIMUM
, &status
);
899 if ( (min
!= UCAL_SUNDAY
|| max
!= UCAL_SATURDAY
) && (min
> val
&& val
> max
) && (val
!= UCAL_FRIDAY
)){
900 log_err("FAIL: Min/max bad\n");
901 log_err("FAIL: Day of week %d out of range\n", val
);
902 log_err("FAIL: FAIL: Day of week should be SUNDAY Got %d\n", val
);
905 log_verbose("getLimits successful\n");
907 val
= ucal_get(cal
, UCAL_DAY_OF_WEEK_IN_MONTH
, &status
);
908 min
= ucal_getLimit(cal
, UCAL_DAY_OF_WEEK_IN_MONTH
, UCAL_MINIMUM
, &status
);
909 max
= ucal_getLimit(cal
, UCAL_DAY_OF_WEEK_IN_MONTH
, UCAL_MAXIMUM
, &status
);
910 if ( (min
!= 0 || max
!= 5 ) && (min
> val
&& val
> max
) && (val
!= 1)){
911 log_err("FAIL: Min/max bad\n");
912 log_err("FAIL: Day of week in month %d out of range\n", val
);
913 log_err("FAIL: FAIL: Day of week in month should be SUNDAY Got %d\n", val
);
917 log_verbose("getLimits successful\n");
919 min
=ucal_getLimit(cal
, UCAL_MONTH
, UCAL_MINIMUM
, &status
);
920 max
=ucal_getLimit(cal
, UCAL_MONTH
, UCAL_MAXIMUM
, &status
);
921 gr_min
=ucal_getLimit(cal
, UCAL_MONTH
, UCAL_GREATEST_MINIMUM
, &status
);
922 le_max
=ucal_getLimit(cal
, UCAL_MONTH
, UCAL_LEAST_MAXIMUM
, &status
);
923 ac_min
=ucal_getLimit(cal
, UCAL_MONTH
, UCAL_ACTUAL_MINIMUM
, &status
);
924 ac_max
=ucal_getLimit(cal
, UCAL_MONTH
, UCAL_ACTUAL_MAXIMUM
, &status
);
925 if(U_FAILURE(status
)){
926 log_err("Error in getLimits: %s\n", u_errorName(status
));
928 if(min
!=0 || max
!=11 || gr_min
!=0 || le_max
!=11 || ac_min
!=0 || ac_max
!=11)
929 log_err("There is and error in getLimits in fetching the values\n");
931 log_verbose("getLimits successful\n");
933 ucal_setDateTime(cal
, 1999, UCAL_MARCH
, 5, 4, 10, 35, &status
);
934 val
=ucal_get(cal
, UCAL_HOUR_OF_DAY
, &status
);
935 min
=ucal_getLimit(cal
, UCAL_HOUR_OF_DAY
, UCAL_MINIMUM
, &status
);
936 max
=ucal_getLimit(cal
, UCAL_HOUR_OF_DAY
, UCAL_MAXIMUM
, &status
);
937 gr_min
=ucal_getLimit(cal
, UCAL_MINUTE
, UCAL_GREATEST_MINIMUM
, &status
);
938 le_max
=ucal_getLimit(cal
, UCAL_MINUTE
, UCAL_LEAST_MAXIMUM
, &status
);
939 ac_min
=ucal_getLimit(cal
, UCAL_MINUTE
, UCAL_ACTUAL_MINIMUM
, &status
);
940 ac_max
=ucal_getLimit(cal
, UCAL_SECOND
, UCAL_ACTUAL_MAXIMUM
, &status
);
941 if( (min
!=0 || max
!= 11 || gr_min
!=0 || le_max
!=60 || ac_min
!=0 || ac_max
!=60) &&
942 (min
>val
&& val
>max
) && val
!=4){
944 log_err("FAIL: Min/max bad\n");
945 log_err("FAIL: Hour of Day %d out of range\n", val
);
946 log_err("FAIL: HOUR_OF_DAY should be 4 Got %d\n", val
);
949 log_verbose("getLimits successful\n");
952 /*get BOGUS_LIMIT type*/
953 val
=ucal_getLimit(cal
, UCAL_SECOND
, (UCalendarLimitType
)99, &status
);
955 log_err("FAIL: ucal_getLimit() with BOGUS type should return -1\n");
966 /* ------------------------------------- */
969 * Test that the days of the week progress properly when add is called repeatedly
970 * for increments of 24 days.
972 static void TestDOWProgression()
974 int32_t initialDOW
, DOW
, newDOW
, expectedDOW
;
976 UDateFormat
*datfor
= 0;
979 UErrorCode status
= U_ZERO_ERROR
;
981 char tempMsgBuf
[256];
982 tzID
=(UChar
*)malloc(sizeof(UChar
) * 4);
983 u_strcpy(tzID
, fgGMTID
);
984 /*open the calendar used */
985 cal
=ucal_open(tzID
, u_strlen(tzID
), "en_US", UCAL_TRADITIONAL
, &status
);;
986 if (U_FAILURE(status
)) {
987 log_err("ucal_open failed: %s\n", u_errorName(status
));
991 datfor
=udat_open(UDAT_MEDIUM
,UDAT_MEDIUM
,NULL
, fgGMTID
,-1,NULL
, 0, &status
);
992 if(U_FAILURE(status
)){
993 log_err("error in creating the dateformat : %s\n", u_errorName(status
));
997 ucal_setDate(cal
, 1999, UCAL_JANUARY
, 1, &status
);
999 log_verbose("\nTesting the DOW progression\n");
1001 initialDOW
= ucal_get(cal
, UCAL_DAY_OF_WEEK
, &status
);
1002 if (U_FAILURE(status
)) { log_err("ucal_get() failed: %s\n", u_errorName(status
) ); return; }
1003 newDOW
= initialDOW
;
1006 log_verbose("DOW = %d...\n", DOW
);
1007 date1
=ucal_getMillis(cal
, &status
);
1008 if(U_FAILURE(status
)){ log_err("ucal_getMiilis() failed: %s\n", u_errorName(status
)); return;}
1009 log_verbose("%s\n", u_austrcpy(tempMsgBuf
, myDateFormat(datfor
, date1
)));
1011 ucal_add(cal
,UCAL_DAY_OF_WEEK
, delta
, &status
);
1012 if (U_FAILURE(status
)) { log_err("ucal_add() failed: %s\n", u_errorName(status
)); return; }
1014 newDOW
= ucal_get(cal
, UCAL_DAY_OF_WEEK
, &status
);
1015 if (U_FAILURE(status
)) { log_err("ucal_get() failed: %s\n", u_errorName(status
)); return; }
1016 expectedDOW
= 1 + (DOW
+ delta
- 1) % 7;
1017 date1
=ucal_getMillis(cal
, &status
);
1018 if(U_FAILURE(status
)){ log_err("ucal_getMiilis() failed: %s\n", u_errorName(status
)); return;}
1019 if (newDOW
!= expectedDOW
) {
1020 log_err("Day of week should be %d instead of %d on %s", expectedDOW
, newDOW
,
1021 u_austrcpy(tempMsgBuf
, myDateFormat(datfor
, date1
)) );
1025 while (newDOW
!= initialDOW
);
1033 /* ------------------------------------- */
1036 * Confirm that the offset between local time and GMT behaves as expected.
1038 static void TestGMTvsLocal()
1040 log_verbose("\nTesting the offset between the GMT and local time\n");
1041 testZones(1999, 1, 1, 12, 0, 0);
1042 testZones(1999, 4, 16, 18, 30, 0);
1043 testZones(1998, 12, 17, 19, 0, 0);
1046 /* ------------------------------------- */
1048 static void testZones(int32_t yr
, int32_t mo
, int32_t dt
, int32_t hr
, int32_t mn
, int32_t sc
)
1050 int32_t offset
,utc
, expected
;
1051 UCalendar
*gmtcal
= 0, *cal
= 0;
1054 UDateFormat
*datfor
= 0;
1055 UErrorCode status
= U_ZERO_ERROR
;
1057 char tempMsgBuf
[256];
1059 tzID
=(UChar
*)malloc(sizeof(UChar
) * 4);
1060 u_strcpy(tzID
, fgGMTID
);
1061 gmtcal
=ucal_open(tzID
, 3, "en_US", UCAL_TRADITIONAL
, &status
);;
1062 if (U_FAILURE(status
)) {
1063 log_err("ucal_open failed: %s\n", u_errorName(status
));
1066 u_uastrcpy(tzID
, "PST");
1067 cal
= ucal_open(tzID
, 3, "en_US", UCAL_TRADITIONAL
, &status
);
1068 if (U_FAILURE(status
)) {
1069 log_err("ucal_open failed: %s\n", u_errorName(status
));
1073 datfor
=udat_open(UDAT_MEDIUM
,UDAT_MEDIUM
,NULL
, fgGMTID
,-1,NULL
, 0, &status
);
1074 if(U_FAILURE(status
)){
1075 log_err("error in creating the dateformat : %s\n", u_errorName(status
));
1078 ucal_setDateTime(gmtcal
, yr
, mo
- 1, dt
, hr
, mn
, sc
, &status
);
1079 if (U_FAILURE(status
)) {
1080 log_err("ucal_setDateTime failed: %s\n", u_errorName(status
));
1083 ucal_set(gmtcal
, UCAL_MILLISECOND
, 0);
1084 date1
= ucal_getMillis(gmtcal
, &status
);
1085 if (U_FAILURE(status
)) {
1086 log_err("ucal_getMillis failed: %s\n", u_errorName(status
));
1089 log_verbose("date = %s\n", u_austrcpy(tempMsgBuf
, myDateFormat(datfor
, date1
)) );
1092 ucal_setMillis(cal
, date1
, &status
);
1093 if (U_FAILURE(status
)) {
1094 log_err("ucal_setMillis() failed: %s\n", u_errorName(status
));
1098 offset
= ucal_get(cal
, UCAL_ZONE_OFFSET
, &status
);
1099 offset
+= ucal_get(cal
, UCAL_DST_OFFSET
, &status
);
1101 if (U_FAILURE(status
)) {
1102 log_err("ucal_get() failed: %s\n", u_errorName(status
));
1105 temp
=(double)((double)offset
/ 1000.0 / 60.0 / 60.0);
1106 /*printf("offset for %s %f hr\n", austrdup(myDateFormat(datfor, date1)), temp);*/
1108 utc
= ((ucal_get(cal
, UCAL_HOUR_OF_DAY
, &status
) * 60 +
1109 ucal_get(cal
, UCAL_MINUTE
, &status
)) * 60 +
1110 ucal_get(cal
, UCAL_SECOND
, &status
)) * 1000 +
1111 ucal_get(cal
, UCAL_MILLISECOND
, &status
) - offset
;
1112 if (U_FAILURE(status
)) {
1113 log_err("ucal_get() failed: %s\n", u_errorName(status
));
1117 expected
= ((hr
* 60 + mn
) * 60 + sc
) * 1000;
1118 if (utc
!= expected
) {
1119 temp
=(double)(utc
- expected
)/ 1000 / 60 / 60.0;
1120 log_err("FAIL: Discrepancy of %d millis = %fhr\n", utc
-expected
, temp
);
1123 log_verbose("PASS: the offset between local and GMT is correct\n");
1130 /* ------------------------------------- */
1135 /* INTERNAL FUNCTIONS USED */
1136 /*------------------------------------------------------------------------------------------- */
1138 /* ------------------------------------- */
1139 static void checkDateTime(UCalendar
* c
,
1140 int32_t y
, int32_t m
, int32_t d
,
1141 int32_t hr
, int32_t min
, int32_t sec
,
1142 int32_t ms
, UCalendarDateFields field
)
1145 UErrorCode status
= U_ZERO_ERROR
;
1146 if (ucal_get(c
, UCAL_YEAR
, &status
) != y
||
1147 ucal_get(c
, UCAL_MONTH
, &status
) != m
||
1148 ucal_get(c
, UCAL_DATE
, &status
) != d
||
1149 ucal_get(c
, UCAL_HOUR
, &status
) != hr
||
1150 ucal_get(c
, UCAL_MINUTE
, &status
) != min
||
1151 ucal_get(c
, UCAL_SECOND
, &status
) != sec
||
1152 ucal_get(c
, UCAL_MILLISECOND
, &status
) != ms
) {
1153 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",
1154 (int32_t)field
, y
, m
+ 1, d
, hr
, min
, sec
, ms
,
1155 ucal_get(c
, UCAL_YEAR
, &status
),
1156 ucal_get(c
, UCAL_MONTH
, &status
) + 1,
1157 ucal_get(c
, UCAL_DATE
, &status
),
1158 ucal_get(c
, UCAL_HOUR
, &status
),
1159 ucal_get(c
, UCAL_MINUTE
, &status
) + 1,
1160 ucal_get(c
, UCAL_SECOND
, &status
),
1161 ucal_get(c
, UCAL_MILLISECOND
, &status
) );
1163 if (U_FAILURE(status
)){
1164 log_err("ucal_get failed: %s\n", u_errorName(status
));
1170 log_verbose("Confirmed: %d/%d/%d %d:%d:%d:%d\n", y
, m
+ 1, d
, hr
, min
, sec
, ms
);
1174 /* ------------------------------------- */
1175 static void checkDate(UCalendar
* c
, int32_t y
, int32_t m
, int32_t d
)
1177 UErrorCode status
= U_ZERO_ERROR
;
1178 if (ucal_get(c
,UCAL_YEAR
, &status
) != y
||
1179 ucal_get(c
, UCAL_MONTH
, &status
) != m
||
1180 ucal_get(c
, UCAL_DATE
, &status
) != d
) {
1182 log_err("FAILURE: Expected y/m/d of %d/%d/%d got %d/%d/%d\n", y
, m
+ 1, d
,
1183 ucal_get(c
, UCAL_YEAR
, &status
),
1184 ucal_get(c
, UCAL_MONTH
, &status
) + 1,
1185 ucal_get(c
, UCAL_DATE
, &status
) );
1187 if (U_FAILURE(status
)) {
1188 log_err("ucal_get failed: %s\n", u_errorName(status
));
1193 log_verbose("Confirmed: %d/%d/%d\n", y
, m
+ 1, d
);
1198 /* ------------------------------------- */
1200 /* ------------------------------------- */
1202 static void verify1(const char* msg
, UCalendar
* c
, UDateFormat
* dat
, int32_t year
, int32_t month
, int32_t day
)
1205 UErrorCode status
= U_ZERO_ERROR
;
1206 if (ucal_get(c
, UCAL_YEAR
, &status
) == year
&&
1207 ucal_get(c
, UCAL_MONTH
, &status
) == month
&&
1208 ucal_get(c
, UCAL_DATE
, &status
) == day
) {
1209 if (U_FAILURE(status
)) {
1210 log_err("FAIL: Calendar::get failed: %s\n", u_errorName(status
));
1213 log_verbose("PASS: %s\n", msg
);
1214 d1
=ucal_getMillis(c
, &status
);
1215 if (U_FAILURE(status
)) {
1216 log_err("ucal_getMillis failed: %s\n", u_errorName(status
));
1219 /*log_verbose(austrdup(myDateFormat(dat, d1)) );*/
1222 log_err("FAIL: %s\n", msg
);
1223 d1
=ucal_getMillis(c
, &status
);
1224 if (U_FAILURE(status
)) {
1225 log_err("ucal_getMillis failed: %s\n", u_errorName(status
) );
1228 log_err("Got %s Expected %d/%d/%d \n", austrdup(myDateFormat(dat
, d1
)), year
, month
+ 1, day
);
1235 /* ------------------------------------ */
1236 static void verify2(const char* msg
, UCalendar
* c
, UDateFormat
* dat
, int32_t year
, int32_t month
, int32_t day
,
1237 int32_t hour
, int32_t min
, int32_t sec
, int32_t am_pm
)
1240 UErrorCode status
= U_ZERO_ERROR
;
1241 char tempMsgBuf
[256];
1243 if (ucal_get(c
, UCAL_YEAR
, &status
) == year
&&
1244 ucal_get(c
, UCAL_MONTH
, &status
) == month
&&
1245 ucal_get(c
, UCAL_DATE
, &status
) == day
&&
1246 ucal_get(c
, UCAL_HOUR
, &status
) == hour
&&
1247 ucal_get(c
, UCAL_MINUTE
, &status
) == min
&&
1248 ucal_get(c
, UCAL_SECOND
, &status
) == sec
&&
1249 ucal_get(c
, UCAL_AM_PM
, &status
) == am_pm
){
1250 if (U_FAILURE(status
)) {
1251 log_err("FAIL: Calendar::get failed: %s\n", u_errorName(status
));
1254 log_verbose("PASS: %s\n", msg
);
1255 d1
=ucal_getMillis(c
, &status
);
1256 if (U_FAILURE(status
)) {
1257 log_err("ucal_getMillis failed: %s\n", u_errorName(status
));
1260 log_verbose("%s\n" , u_austrcpy(tempMsgBuf
, myDateFormat(dat
, d1
)) );
1263 log_err("FAIL: %s\n", msg
);
1264 d1
=ucal_getMillis(c
, &status
);
1265 if (U_FAILURE(status
)) {
1266 log_err("ucal_getMillis failed: %s\n", u_errorName(status
));
1269 log_err("Got %s Expected %d/%d/%d/ %d:%d:%d %s\n", austrdup(myDateFormat(dat
, d1
)),
1270 year
, month
+ 1, day
, hour
, min
, sec
, (am_pm
==0) ? "AM": "PM");
1278 void TestGregorianChange() {
1279 static const UChar utc
[] = { 0x45, 0x74, 0x63, 0x2f, 0x47, 0x4d, 0x54, 0 }; /* "Etc/GMT" */
1280 const int32_t dayMillis
= 86400 * INT64_C(1000); /* 1 day = 86400 seconds */
1283 UErrorCode errorCode
= U_ZERO_ERROR
;
1285 /* Test ucal_setGregorianChange() on a Gregorian calendar. */
1286 errorCode
= U_ZERO_ERROR
;
1287 cal
= ucal_open(utc
, -1, "", UCAL_GREGORIAN
, &errorCode
);
1288 if(U_FAILURE(errorCode
)) {
1289 log_err("ucal_open(UTC) failed: %s\n", u_errorName(errorCode
));
1292 ucal_setGregorianChange(cal
, -365 * (dayMillis
* (UDate
)1), &errorCode
);
1293 if(U_FAILURE(errorCode
)) {
1294 log_err("ucal_setGregorianChange(1969) failed: %s\n", u_errorName(errorCode
));
1296 date
= ucal_getGregorianChange(cal
, &errorCode
);
1297 if(U_FAILURE(errorCode
) || date
!= -365 * (dayMillis
* (UDate
)1)) {
1298 log_err("ucal_getGregorianChange() failed: %s, date = %f\n", u_errorName(errorCode
), date
);
1303 /* Test ucal_setGregorianChange() on a non-Gregorian calendar where it should fail. */
1304 errorCode
= U_ZERO_ERROR
;
1305 cal
= ucal_open(utc
, -1, "th@calendar=buddhist", UCAL_TRADITIONAL
, &errorCode
);
1306 if(U_FAILURE(errorCode
)) {
1307 log_err("ucal_open(UTC, non-Gregorian) failed: %s\n", u_errorName(errorCode
));
1310 ucal_setGregorianChange(cal
, -730 * (dayMillis
* (UDate
)1), &errorCode
);
1311 if(errorCode
!= U_UNSUPPORTED_ERROR
) {
1312 log_err("ucal_setGregorianChange(non-Gregorian calendar) did not yield U_UNSUPPORTED_ERROR but %s\n",
1313 u_errorName(errorCode
));
1315 errorCode
= U_ZERO_ERROR
;
1316 date
= ucal_getGregorianChange(cal
, &errorCode
);
1317 if(errorCode
!= U_UNSUPPORTED_ERROR
) {
1318 log_err("ucal_getGregorianChange(non-Gregorian calendar) did not yield U_UNSUPPORTED_ERROR but %s\n",
1319 u_errorName(errorCode
));
1324 #endif /* #if !UCONFIG_NO_FORMATTING */