1 /********************************************************************
3 * Copyright (c) 1997-2003, 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 addCalTest(TestNode
** root
);
33 void addCalTest(TestNode
** root
)
36 addTest(root
, &TestCalendar
, "tsformat/ccaltst/TestCalendar");
37 addTest(root
, &TestGetSetDateAPI
, "tsformat/ccaltst/TestGetSetDateAPI");
38 addTest(root
, &TestFieldGetSet
, "tsformat/ccaltst/TestFieldGetSet");
39 addTest(root
, &TestAddRollExtensive
, "tsformat/ccaltst/TestAddRollExtensive");
40 addTest(root
, &TestGetLimits
, "tsformat/ccaltst/TestGetLimits");
41 addTest(root
, &TestDOWProgression
, "tsformat/ccaltst/TestDOWProgression");
42 addTest(root
, &TestGMTvsLocal
, "tsformat/ccaltst/TestGMTvsLocal");
47 static const UChar fgGMTID
[] = { 0x0047, 0x004d, 0x0054, 0x0000 };
50 static const UChar PST
[] = {0x50, 0x53, 0x54, 0x00}; /* "PST" */
52 static const UChar EUROPE_PARIS
[] = {0x45, 0x75, 0x72, 0x6F, 0x70, 0x65, 0x2F, 0x50, 0x61, 0x72, 0x69, 0x73, 0x00}; /* "Europe/Paris" */
54 static void TestCalendar()
56 UCalendar
*caldef
= 0, *caldef2
= 0, *calfr
= 0, *calit
= 0;
57 UEnumeration
* uenum
= NULL
;
58 int32_t count
, count2
, i
,j
;
61 UErrorCode status
= U_ZERO_ERROR
;
63 UDateFormat
*datdef
= 0;
65 int32_t resultlength
, resultlengthneeded
;
67 UChar zone1
[32], zone2
[32];
69 #ifdef U_USE_UCAL_OBSOLETE_2_8
70 /*Testing countAvailableTimeZones*/
72 log_verbose("\nTesting ucal_countAvailableTZIDs\n");
73 count
=ucal_countAvailableTZIDs(offset
);
74 log_verbose("The number of timezone id's present with offset 0 are %d:\n", count
);
75 if(count
< 5) /* Don't hard code an exact == test here! */
76 log_err("FAIL: error in the ucal_countAvailableTZIDs - got %d expected at least 5 total\n", count
);
78 /*Testing getAvailableTZIDs*/
79 log_verbose("\nTesting ucal_getAvailableTZIDs");
81 ucal_getAvailableTZIDs(offset
, i
, &status
);
82 if(U_FAILURE(status
)){
83 log_err("FAIL: ucal_getAvailableTZIDs returned %s\n", u_errorName(status
));
85 log_verbose("%s\n", u_austrcpy(tempMsgBuf
, ucal_getAvailableTZIDs(offset
, i
, &status
)));
87 /*get Illegal TZID where index >= count*/
88 ucal_getAvailableTZIDs(offset
, i
, &status
);
89 if(status
!= U_INDEX_OUTOFBOUNDS_ERROR
){
90 log_err("FAIL:for TZID index >= count Expected INDEX_OUTOFBOUNDS_ERROR Got %s\n", u_errorName(status
));
95 /*Test ucal_openTimeZones & ucal_openCountryTimeZones*/
97 const char* api
= (j
==0) ? "ucal_openTimeZones()" :
98 "ucal_openCountryTimeZones(US)";
99 uenum
= (j
==0) ? ucal_openTimeZones(&status
) :
100 ucal_openCountryTimeZones("US", &status
);
101 if (U_FAILURE(status
)) {
102 log_err("FAIL: %s failed with %s", api
,
103 u_errorName(status
));
107 count
= uenum_count(uenum
, &status
);
108 log_verbose("%s returned %d timezone id's:\n", api
, count
);
109 if (count
< 5) { /* Don't hard code an exact == test here! */
110 log_err("FAIL: in %s, got %d, expected at least 5\n", api
, count
);
112 uenum_reset(uenum
, &status
);
113 if (U_FAILURE(status
)){
114 log_err("FAIL: uenum_reset for %s returned %s\n",
115 api
, u_errorName(status
));
117 for (i
=0; i
<count
; i
++) {
118 id
= uenum_next(uenum
, &len
, &status
);
119 if (U_FAILURE(status
)){
120 log_err("FAIL: uenum_next for %s returned %s\n",
121 api
, u_errorName(status
));
123 log_verbose("%s\n", id
);
126 /* Next one should be NULL */
127 id
= uenum_next(uenum
, &len
, &status
);
129 log_err("FAIL: uenum_next for %s returned %s, expected NULL\n",
136 /*Test ucal_getDSTSavings*/
137 status
= U_ZERO_ERROR
;
138 i
= ucal_getDSTSavings(fgGMTID
, &status
);
139 if (U_FAILURE(status
)) {
140 log_err("FAIL: ucal_getDSTSavings(GMT) => %s\n",
141 u_errorName(status
));
143 log_err("FAIL: ucal_getDSTSavings(GMT) => %d, expect 0\n", i
);
145 i
= ucal_getDSTSavings(PST
, &status
);
146 if (U_FAILURE(status
)) {
147 log_err("FAIL: ucal_getDSTSavings(PST) => %s\n",
148 u_errorName(status
));
149 } else if (i
!= 1*60*60*1000) {
150 log_err("FAIL: ucal_getDSTSavings(PST) => %d, expect %d\n", i
, 1*60*60*1000);
153 /*Test ucal_set/getDefaultTimeZone*/
154 status
= U_ZERO_ERROR
;
155 i
= ucal_getDefaultTimeZone(zone1
, sizeof(zone1
)/sizeof(zone1
[0]), &status
);
156 if (U_FAILURE(status
)) {
157 log_err("FAIL: ucal_getDefaultTimeZone() => %s\n",
158 u_errorName(status
));
160 ucal_setDefaultTimeZone(EUROPE_PARIS
, &status
);
161 if (U_FAILURE(status
)) {
162 log_err("FAIL: ucal_setDefaultTimeZone(Europe/Paris) => %s\n",
163 u_errorName(status
));
165 i
= ucal_getDefaultTimeZone(zone2
, sizeof(zone2
)/sizeof(zone2
[0]), &status
);
166 if (U_FAILURE(status
)) {
167 log_err("FAIL: ucal_getDefaultTimeZone() => %s\n",
168 u_errorName(status
));
170 if (u_strcmp(zone2
, EUROPE_PARIS
) != 0) {
171 log_err("FAIL: ucal_getDefaultTimeZone() did not return Europe/Paris\n");
175 status
= U_ZERO_ERROR
;
176 ucal_setDefaultTimeZone(zone1
, &status
);
179 /*Testing the ucal_open() function*/
180 log_verbose("\nTesting the ucal_open()\n");
181 tzID
=(UChar
*)malloc(sizeof(UChar
) * 4);
182 u_uastrcpy(tzID
, "PST");
183 caldef
=ucal_open(tzID
, u_strlen(tzID
), "en_US", UCAL_TRADITIONAL
, &status
);
184 if(U_FAILURE(status
)){
185 log_err("FAIL: error in ucal_open caldef : %s\n", u_errorName(status
));
188 caldef2
=ucal_open(tzID
, u_strlen(tzID
), "en_US", UCAL_TRADITIONAL
, &status
);
189 if(U_FAILURE(status
)){
190 log_err("FAIL: error in ucal_open caldef : %s\n", u_errorName(status
));
192 u_strcpy(tzID
, fgGMTID
);
193 calfr
=ucal_open(tzID
, u_strlen(tzID
), "fr_FR", UCAL_TRADITIONAL
, &status
);
194 if(U_FAILURE(status
)){
195 log_err("FAIL: error in ucal_open calfr : %s\n", u_errorName(status
));
197 calit
=ucal_open(tzID
, u_strlen(tzID
), "it_IT", UCAL_TRADITIONAL
, &status
);
198 if(U_FAILURE(status
)) {
199 log_err("FAIL: error in ucal_open calit : %s\n", u_errorName(status
));
203 /*Testing udat_getAvailable() and udat_countAvailable()*/
204 log_verbose("\nTesting getAvailableLocales and countAvailable()\n");
205 count
=ucal_countAvailable();
206 /* use something sensible w/o hardcoding the count */
208 log_verbose("PASS: ucal_countAvailable() works fine\n");
209 log_verbose("The no: of locales for which calendars are avilable are %d\n", count
);
211 log_data_err("FAIL: Error in countAvailable()\n");
214 for(i
=0;i
<count
;i
++) {
215 log_verbose("%s\n", ucal_getAvailable(i
));
219 /*Testing the equality between calendar's*/
220 log_verbose("\nTesting ucal_equivalentTo()\n");
221 if(caldef
&& caldef2
&& calfr
&& calit
) {
222 if(ucal_equivalentTo(caldef
, caldef2
) == FALSE
|| ucal_equivalentTo(caldef
, calfr
)== TRUE
||
223 ucal_equivalentTo(caldef
, calit
)== TRUE
) {
224 log_err("FAIL: Error. equivalentTo test failed\n");
226 log_verbose("PASS: equivalentTo test passed\n");
231 /*Testing the current time and date using ucal_getnow()*/
232 log_verbose("\nTesting the ucal_getNow function to check if it is fetching tbe current time\n");
234 /* open the date format and format the date to check the output */
235 datdef
=udat_open(UDAT_FULL
,UDAT_FULL
,NULL
, NULL
, 0,NULL
,0,&status
);
236 if(U_FAILURE(status
)){
237 log_err("FAIL: error in creating the dateformat : %s\n", u_errorName(status
));
240 log_verbose("PASS: The current date and time fetched is %s\n", u_austrcpy(tempMsgBuf
, myDateFormat(datdef
, now
)) );
243 /*Testing the TimeZoneDisplayName */
244 log_verbose("\nTesting the fetching of time zone display name\n");
245 /*for the US locale */
247 resultlengthneeded
=ucal_getTimeZoneDisplayName(caldef
, UCAL_DST
, "en_US", NULL
, resultlength
, &status
);
249 if(status
==U_BUFFER_OVERFLOW_ERROR
)
252 resultlength
=resultlengthneeded
+1;
253 result
=(UChar
*)malloc(sizeof(UChar
) * resultlength
);
254 ucal_getTimeZoneDisplayName(caldef
, UCAL_DST
, "en_US", result
, resultlength
, &status
);
256 if(U_FAILURE(status
)) {
257 log_err("FAIL: Error in getting the timezone display name : %s\n", u_errorName(status
));
260 log_verbose("PASS: getting the time zone display name successful : %s, %d needed \n",
261 u_errorName(status
), resultlengthneeded
);
265 #define expectPDT "Pacific Daylight Time"
267 tzdname
=(UChar
*)malloc(sizeof(UChar
) * (sizeof(expectPDT
)+1));
268 u_uastrcpy(tzdname
, expectPDT
);
269 if(u_strcmp(tzdname
, result
)==0){
270 log_verbose("PASS: got the correct time zone display name %s\n", u_austrcpy(tempMsgBuf
, result
) );
273 log_err("FAIL: got the wrong time zone(DST) display name %s, wanted %s\n", austrdup(result
) , expectPDT
);
276 ucal_getTimeZoneDisplayName(caldef
, UCAL_SHORT_DST
, "en_US", result
, resultlength
, &status
);
277 u_uastrcpy(tzdname
, "PDT");
278 if(u_strcmp(tzdname
, result
) != 0){
279 log_err("FAIL: got the wrong time zone(SHORT_DST) display name %s, wanted %s\n", austrdup(result
), austrdup(tzdname
));
282 ucal_getTimeZoneDisplayName(caldef
, UCAL_STANDARD
, "en_US", result
, resultlength
, &status
);
283 u_uastrcpy(tzdname
, "Pacific Standard Time");
284 if(u_strcmp(tzdname
, result
) != 0){
285 log_err("FAIL: got the wrong time zone(STANDARD) display name %s, wanted %s\n", austrdup(result
), austrdup(tzdname
));
288 ucal_getTimeZoneDisplayName(caldef
, UCAL_SHORT_STANDARD
, "en_US", result
, resultlength
, &status
);
289 u_uastrcpy(tzdname
, "PST");
290 if(u_strcmp(tzdname
, result
) != 0){
291 log_err("FAIL: got the wrong time zone(SHORT_STANDARD) display name %s, wanted %s\n", austrdup(result
), austrdup(tzdname
));
295 /*testing the setAttributes and getAttributes of a UCalendar*/
296 log_verbose("\nTesting the getAttributes and set Attributes\n");
297 count
=ucal_getAttribute(calit
, UCAL_LENIENT
);
298 count2
=ucal_getAttribute(calfr
, UCAL_LENIENT
);
299 ucal_setAttribute(calit
, UCAL_LENIENT
, 0);
300 ucal_setAttribute(caldef
, UCAL_LENIENT
, count2
);
301 if( ucal_getAttribute(calit
, UCAL_LENIENT
) !=0 ||
302 ucal_getAttribute(calfr
, UCAL_LENIENT
)!=ucal_getAttribute(caldef
, UCAL_LENIENT
) )
303 log_err("FAIL: there is an error in getAttributes or setAttributes\n");
305 log_verbose("PASS: attribute set and got successfully\n");
306 /*set it back to orginal value */
307 log_verbose("Setting it back to normal\n");
308 ucal_setAttribute(calit
, UCAL_LENIENT
, count
);
309 if(ucal_getAttribute(calit
, UCAL_LENIENT
)!=count
)
310 log_err("FAIL: Error in setting the attribute back to normal\n");
312 /*setting the first day of the week to other values */
313 count
=ucal_getAttribute(calit
, UCAL_FIRST_DAY_OF_WEEK
);
314 for (i
=1; i
<=7; ++i
) {
315 ucal_setAttribute(calit
, UCAL_FIRST_DAY_OF_WEEK
,i
);
316 if (ucal_getAttribute(calit
, UCAL_FIRST_DAY_OF_WEEK
) != i
)
317 log_err("FAIL: set/getFirstDayOfWeek failed\n");
319 /*get bogus Attribute*/
320 count
=ucal_getAttribute(calit
, (UCalendarAttribute
)99); /* BOGUS_ATTRIBUTE */
322 log_err("FAIL: get/bogus attribute should return -1\n");
325 /*set it back to normal */
326 ucal_setAttribute(calit
, UCAL_FIRST_DAY_OF_WEEK
,count
);
327 /*setting minimal days of the week to other values */
328 count
=ucal_getAttribute(calit
, UCAL_MINIMAL_DAYS_IN_FIRST_WEEK
);
329 for (i
=1; i
<=7; ++i
) {
330 ucal_setAttribute(calit
, UCAL_MINIMAL_DAYS_IN_FIRST_WEEK
,i
);
331 if (ucal_getAttribute(calit
, UCAL_MINIMAL_DAYS_IN_FIRST_WEEK
) != i
)
332 log_err("FAIL: set/getMinimalDaysInFirstWeek failed\n");
334 /*set it back to normal */
335 ucal_setAttribute(calit
, UCAL_MINIMAL_DAYS_IN_FIRST_WEEK
,count
);
338 /*testing if the UCalendar's timezone is currently in day light saving's time*/
339 log_verbose("\nTesting if the UCalendar is currently in daylight saving's time\n");
340 ucal_setDateTime(caldef
, 1999, UCAL_MARCH
, 3, 10, 45, 20, &status
);
341 ucal_inDaylightTime(caldef
, &status
);
342 if(U_FAILURE(status
)) {
343 log_err("Error in ucal_inDaylightTime: %s\n", u_errorName(status
));
345 if(!ucal_inDaylightTime(caldef
, &status
))
346 log_verbose("PASS: It is not in daylight saving's time\n");
348 log_err("FAIL: It is not in daylight saving's time\n");
352 /*closing the UCalendar*/
357 /*closing the UDateFormat used */
365 /*------------------------------------------------------*/
366 /*Testing the getMillis, setMillis, setDate and setDateTime functions extensively*/
368 static void TestGetSetDateAPI()
370 UCalendar
*caldef
= 0, *caldef2
= 0;
375 UDateFormat
*datdef
= 0;
376 UErrorCode status
=U_ZERO_ERROR
;
377 UDate d2
= 837039928046.0;
380 log_verbose("\nOpening the calendars()\n");
381 tzID
=(UChar
*)malloc(sizeof(UChar
) * 4);
382 u_strcpy(tzID
, fgGMTID
);
383 /*open the calendars used */
384 caldef
=ucal_open(tzID
, u_strlen(tzID
), "en_US", UCAL_TRADITIONAL
, &status
);
385 caldef2
=ucal_open(tzID
, u_strlen(tzID
), "en_US", UCAL_TRADITIONAL
, &status
);
386 /*open the dateformat */
387 /* this is supposed to open default date format, but later on it treats it like it is "en_US"
388 - very bad if you try to run the tests on machine where default locale is NOT "en_US" */
389 /*datdef=udat_open(UDAT_DEFAULT,UDAT_DEFAULT ,NULL,fgGMTID,-1, &status);*/
390 datdef
=udat_open(UDAT_DEFAULT
,UDAT_DEFAULT
,"en_US",fgGMTID
,-1,NULL
,0, &status
);
391 if(U_FAILURE(status
))
393 log_err("error in creating the dateformat : %s\n", u_errorName(status
));
398 /*Testing getMillis and setMillis */
399 log_verbose("\nTesting the date and time fetched in millis for a calendar using getMillis\n");
400 d1
=ucal_getMillis(caldef
, &status
);
401 if(U_FAILURE(status
)){
402 log_err("Error in getMillis : %s\n", u_errorName(status
));
405 /*testing setMillis */
406 log_verbose("\nTesting the set date and time function using setMillis\n");
407 ucal_setMillis(caldef
, d2
, &status
);
408 if(U_FAILURE(status
)){
409 log_err("Error in setMillis : %s\n", u_errorName(status
));
412 /*testing if the calendar date is set properly or not */
413 d1
=ucal_getMillis(caldef
, &status
);
414 if(u_strcmp(myDateFormat(datdef
, d1
), myDateFormat(datdef
, d2
))!=0)
415 log_err("error in setMillis or getMillis\n");
416 /*-------------------*/
420 ctest_setTimeZone(NULL
, &status
);
422 /*testing ucal_setTimeZone() function*/
423 log_verbose("\nTesting if the function ucal_setTimeZone() works fine\n");
424 ucal_setMillis(caldef2
, d2
, &status
);
425 if(U_FAILURE(status
)){
426 log_err("Error in getMillis : %s\n", u_errorName(status
));;
428 hour
=ucal_get(caldef2
, UCAL_HOUR_OF_DAY
, &status
);
430 u_uastrcpy(tzID
, "PST");
431 ucal_setTimeZone(caldef2
,tzID
, 3, &status
);
432 if(U_FAILURE(status
)){
433 log_err("Error in setting the time zone using ucal_setTimeZone(): %s\n", u_errorName(status
));
436 log_verbose("ucal_setTimeZone worked fine\n");
437 if(hour
== ucal_get(caldef2
, UCAL_HOUR_OF_DAY
, &status
))
438 log_err("FAIL: Error setting the time zone doesn't change the represented time\n");
439 else if((hour
-8 + 1) != ucal_get(caldef2
, UCAL_HOUR_OF_DAY
, &status
)) /*because it is not in daylight savings time */
440 log_err("FAIL: Error setTimeZone doesn't change the represented time correctly with 8 hour offset\n");
442 log_verbose("PASS: setTimeZone works fine\n");
444 /*testing setTimeZone roundtrip */
445 log_verbose("\nTesting setTimeZone() roundtrip\n");
446 u_strcpy(tzID
, fgGMTID
);
447 ucal_setTimeZone(caldef2
, tzID
, 3, &status
);
448 if(U_FAILURE(status
)){
449 log_err("Error in setting the time zone using ucal_setTimeZone(): %s\n", u_errorName(status
));
451 if(d2
==ucal_getMillis(caldef2
, &status
))
452 log_verbose("PASS: setTimeZone roundtrip test passed\n");
454 log_err("FAIL: setTimeZone roundtrip test failed\n");
456 zoneOffset
= ucal_get(caldef2
, UCAL_ZONE_OFFSET
, &status
);
457 if(U_FAILURE(status
)){
458 log_err("Error in getting the time zone using ucal_get() after using ucal_setTimeZone(): %s\n", u_errorName(status
));
460 else if (zoneOffset
!= 0) {
461 log_err("Error in getting the time zone using ucal_get() after using ucal_setTimeZone() offset=%d\n", zoneOffset
);
464 ucal_setTimeZone(caldef2
, NULL
, -1, &status
);
465 if(U_FAILURE(status
)){
466 log_err("Error in setting the time zone using ucal_setTimeZone(): %s\n", u_errorName(status
));
468 if(ucal_getMillis(caldef2
, &status
))
469 log_verbose("PASS: setTimeZone roundtrip test passed\n");
471 log_err("FAIL: setTimeZone roundtrip test failed\n");
473 zoneOffset
= ucal_get(caldef2
, UCAL_ZONE_OFFSET
, &status
);
474 if(U_FAILURE(status
)){
475 log_err("Error in getting the time zone using ucal_get() after using ucal_setTimeZone(): %s\n", u_errorName(status
));
477 else if (zoneOffset
!= -28800000) {
478 log_err("Error in getting the time zone using ucal_get() after using ucal_setTimeZone() offset=%d\n", zoneOffset
);
481 ctest_resetTimeZone();
483 /*----------------------------* */
487 /*Testing if setDate works fine */
488 log_verbose("\nTesting the ucal_setDate() function \n");
489 u_uastrcpy(temp
, "Dec 17, 1971 11:05:28 PM");
490 ucal_setDate(caldef
,1971, UCAL_DECEMBER
, 17, &status
);
491 if(U_FAILURE(status
)){
492 log_err("error in setting the calendar date : %s\n", u_errorName(status
));
494 /*checking if the calendar date is set properly or not */
495 d1
=ucal_getMillis(caldef
, &status
);
496 if(u_strcmp(myDateFormat(datdef
, d1
), temp
)==0)
497 log_verbose("PASS:setDate works fine\n");
499 log_err("FAIL:Error in setDate()\n");
502 /* Testing setDate Extensively with various input values */
503 log_verbose("\nTesting ucal_setDate() extensively\n");
504 ucal_setDate(caldef
, 1999, UCAL_JANUARY
, 10, &status
);
505 verify1("1999 10th day of January is :", caldef
, datdef
, 1999, UCAL_JANUARY
, 10);
506 ucal_setDate(caldef
, 1999, UCAL_DECEMBER
, 3, &status
);
507 verify1("1999 3rd day of December is :", caldef
, datdef
, 1999, UCAL_DECEMBER
, 3);
508 ucal_setDate(caldef
, 2000, UCAL_MAY
, 3, &status
);
509 verify1("2000 3rd day of May is :", caldef
, datdef
, 2000, UCAL_MAY
, 3);
510 ucal_setDate(caldef
, 1999, UCAL_AUGUST
, 32, &status
);
511 verify1("1999 32th day of August is :", caldef
, datdef
, 1999, UCAL_SEPTEMBER
, 1);
512 ucal_setDate(caldef
, 1999, UCAL_MARCH
, 0, &status
);
513 verify1("1999 0th day of March is :", caldef
, datdef
, 1999, UCAL_FEBRUARY
, 28);
514 ucal_setDate(caldef
, 0, UCAL_MARCH
, 12, &status
);
516 /*--------------------*/
518 /*Testing if setDateTime works fine */
519 log_verbose("\nTesting the ucal_setDateTime() function \n");
520 u_uastrcpy(temp
, "May 3, 1972 4:30:42 PM");
521 ucal_setDateTime(caldef
,1972, UCAL_MAY
, 3, 16, 30, 42, &status
);
522 if(U_FAILURE(status
)){
523 log_err("error in setting the calendar date : %s\n", u_errorName(status
));
525 /*checking if the calendar date is set properly or not */
526 d1
=ucal_getMillis(caldef
, &status
);
527 if(u_strcmp(myDateFormat(datdef
, d1
), temp
)==0)
528 log_verbose("PASS: setDateTime works fine\n");
530 log_err("FAIL: Error in setDateTime\n");
534 /*Testing setDateTime extensively with various input values*/
535 log_verbose("\nTesting ucal_setDateTime() function extensively\n");
536 ucal_setDateTime(caldef
, 1999, UCAL_OCTOBER
, 10, 6, 45, 30, &status
);
537 verify2("1999 10th day of October at 6:45:30 is :", caldef
, datdef
, 1999, UCAL_OCTOBER
, 10, 6, 45, 30, 0 );
538 ucal_setDateTime(caldef
, 1999, UCAL_MARCH
, 3, 15, 10, 55, &status
);
539 verify2("1999 3rd day of March at 15:10:55 is :", caldef
, datdef
, 1999, UCAL_MARCH
, 3, 3, 10, 55, 1);
540 ucal_setDateTime(caldef
, 1999, UCAL_MAY
, 3, 25, 30, 45, &status
);
541 verify2("1999 3rd day of May at 25:30:45 is :", caldef
, datdef
, 1999, UCAL_MAY
, 4, 1, 30, 45, 0);
542 ucal_setDateTime(caldef
, 1999, UCAL_AUGUST
, 32, 22, 65, 40, &status
);
543 verify2("1999 32th day of August at 22:65:40 is :", caldef
, datdef
, 1999, UCAL_SEPTEMBER
, 1, 11, 5, 40,1);
544 ucal_setDateTime(caldef
, 1999, UCAL_MARCH
, 12, 0, 0, 0,&status
);
545 verify2("1999 12th day of March at 0:0:0 is :", caldef
, datdef
, 1999, UCAL_MARCH
, 12, 0, 0, 0, 0);
546 ucal_setDateTime(caldef
, 1999, UCAL_MARCH
, 12, -10, -10,0, &status
);
547 verify2("1999 12th day of March is at -10:-10:0 :", caldef
, datdef
, 1999, UCAL_MARCH
, 11, 1, 50, 0, 1);
551 /*close caldef and datdef*/
559 /*----------------------------------------------------------- */
561 * Confirm the functioning of the calendar field related functions.
563 static void TestFieldGetSet()
567 UDateFormat
*datdef
= 0;
569 UErrorCode status
=U_ZERO_ERROR
;
570 log_verbose("\nFetching pointer to UCalendar using the ucal_open()\n");
571 tzID
=(UChar
*)malloc(sizeof(UChar
) * 4);
572 u_strcpy(tzID
, fgGMTID
);
573 /*open the calendar used */
574 cal
=ucal_open(tzID
, u_strlen(tzID
), "en_US", UCAL_TRADITIONAL
, &status
);
575 if (U_FAILURE(status
)) {
576 log_err("ucal_open failed: %s\n", u_errorName(status
));
579 datdef
=udat_open(UDAT_SHORT
,UDAT_SHORT
,NULL
,fgGMTID
,-1,NULL
, 0, &status
);
580 if(U_FAILURE(status
))
582 log_err("error in creating the dateformat : %s\n", u_errorName(status
));
585 /*Testing ucal_get()*/
586 log_verbose("\nTesting the ucal_get() function of Calendar\n");
587 ucal_setDateTime(cal
, 1999, UCAL_MARCH
, 12, 5, 25, 30, &status
);
588 if(U_FAILURE(status
)){
589 log_err("error in the setDateTime() : %s\n", u_errorName(status
));
591 if(ucal_get(cal
, UCAL_YEAR
, &status
)!=1999 || ucal_get(cal
, UCAL_MONTH
, &status
)!=2 ||
592 ucal_get(cal
, UCAL_DATE
, &status
)!=12 || ucal_get(cal
, UCAL_HOUR
, &status
)!=5)
593 log_err("error in ucal_get()\n");
594 else if(ucal_get(cal
, UCAL_DAY_OF_WEEK_IN_MONTH
, &status
)!=2 || ucal_get(cal
, UCAL_DAY_OF_WEEK
, &status
)!=6
595 || ucal_get(cal
, UCAL_WEEK_OF_MONTH
, &status
)!=2 || ucal_get(cal
, UCAL_WEEK_OF_YEAR
, &status
)!= 11)
596 log_err("FAIL: error in ucal_get()\n");
598 log_verbose("PASS: ucal_get() works fine\n");
600 /*Testing the ucal_set() , ucal_clear() functions of calendar*/
601 log_verbose("\nTesting the set, and clear field functions of calendar\n");
602 ucal_setAttribute(cal
, UCAL_LENIENT
, 0);
604 ucal_set(cal
, UCAL_YEAR
, 1997);
605 ucal_set(cal
, UCAL_MONTH
, UCAL_JUNE
);
606 ucal_set(cal
, UCAL_DATE
, 3);
607 verify1("1997 third day of June = ", cal
, datdef
, 1997, UCAL_JUNE
, 3);
609 ucal_set(cal
, UCAL_YEAR
, 1997);
610 ucal_set(cal
, UCAL_DAY_OF_WEEK
, UCAL_TUESDAY
);
611 ucal_set(cal
, UCAL_MONTH
, UCAL_JUNE
);
612 ucal_set(cal
, UCAL_DAY_OF_WEEK_IN_MONTH
, 1);
613 verify1("1997 first Tuesday in June = ", cal
, datdef
, 1997, UCAL_JUNE
, 3);
615 ucal_set(cal
, UCAL_YEAR
, 1997);
616 ucal_set(cal
, UCAL_DAY_OF_WEEK
, UCAL_TUESDAY
);
617 ucal_set(cal
, UCAL_MONTH
, UCAL_JUNE
);
618 ucal_set(cal
, UCAL_DAY_OF_WEEK_IN_MONTH
, - 1);
619 verify1("1997 last Tuesday in June = ", cal
, datdef
,1997, UCAL_JUNE
, 24);
620 /*give undesirable input */
621 status
= U_ZERO_ERROR
;
623 ucal_set(cal
, UCAL_YEAR
, 1997);
624 ucal_set(cal
, UCAL_DAY_OF_WEEK
, UCAL_TUESDAY
);
625 ucal_set(cal
, UCAL_MONTH
, UCAL_JUNE
);
626 ucal_set(cal
, UCAL_DAY_OF_WEEK_IN_MONTH
, 0);
627 d1
=ucal_getMillis(cal
,&status
);
628 if (status
!= U_ILLEGAL_ARGUMENT_ERROR
){
629 log_err("FAIL: No IllegalArgumentError for :");
630 log_err("1997 zero-th Tuesday in June \n");
633 log_verbose("PASS: IllegalArgumentError as expected\n");
636 ucal_set(cal
, UCAL_YEAR
, 1997);
637 ucal_set(cal
, UCAL_DAY_OF_WEEK
, UCAL_TUESDAY
);
638 ucal_set(cal
, UCAL_MONTH
, UCAL_JUNE
);
639 ucal_set(cal
, UCAL_WEEK_OF_MONTH
, 1);
640 verify1("1997 Tuesday in week 1 of June = ", cal
,datdef
, 1997, UCAL_JUNE
, 3);
642 ucal_set(cal
, UCAL_YEAR
, 1997);
643 ucal_set(cal
, UCAL_DAY_OF_WEEK
, UCAL_TUESDAY
);
644 ucal_set(cal
, UCAL_MONTH
, UCAL_JUNE
);
645 ucal_set(cal
, UCAL_WEEK_OF_MONTH
, 5);
646 verify1("1997 Tuesday in week 5 of June = ", cal
,datdef
, 1997, UCAL_JULY
, 1);
647 status
= U_ZERO_ERROR
;
649 ucal_set(cal
, UCAL_YEAR
, 1997);
650 ucal_set(cal
, UCAL_DAY_OF_WEEK
, UCAL_TUESDAY
);
651 ucal_set(cal
, UCAL_MONTH
, UCAL_JUNE
);
652 ucal_set(cal
, UCAL_WEEK_OF_MONTH
, 0);
653 verify1("1997 Tuesday in week 0 of June = ", cal
, datdef
, 1997, UCAL_MAY
, 27);
655 ucal_set(cal
, UCAL_YEAR_WOY
, 1997);
656 ucal_set(cal
, UCAL_DAY_OF_WEEK
, UCAL_TUESDAY
);
657 ucal_set(cal
, UCAL_WEEK_OF_YEAR
, 1);
658 verify1("1997 Tuesday in week 1 of year = ", cal
, datdef
,1996, UCAL_DECEMBER
, 31);
660 ucal_set(cal
, UCAL_YEAR
, 1997);
661 ucal_set(cal
, UCAL_DAY_OF_WEEK
, UCAL_TUESDAY
);
662 ucal_set(cal
, UCAL_WEEK_OF_YEAR
, 10);
663 verify1("1997 Tuesday in week 10 of year = ", cal
,datdef
, 1997, UCAL_MARCH
, 4);
665 ucal_set(cal
, UCAL_YEAR
, 1999);
666 ucal_set(cal
, UCAL_DAY_OF_YEAR
, 1);
667 verify1("1999 1st day of the year =", cal
, datdef
, 1999, UCAL_JANUARY
, 1);
668 ucal_set(cal
, UCAL_MONTH
, -3);
669 d1
=ucal_getMillis(cal
,&status
);
670 if (status
!= U_ILLEGAL_ARGUMENT_ERROR
){
671 log_err("FAIL: No IllegalArgumentError for :\"1999 -3th month \" ");
674 log_verbose("PASS: IllegalArgumentError as expected\n");
676 ucal_setAttribute(cal
, UCAL_LENIENT
, 1);
678 ucal_set(cal
, UCAL_MONTH
, -3);
679 verify1("1999 -3th month should be", cal
, datdef
, 1998, UCAL_OCTOBER
, 1);
682 /*testing isSet and clearField()*/
683 if(!ucal_isSet(cal
, UCAL_WEEK_OF_YEAR
))
684 log_err("FAIL: error in isSet\n");
686 log_verbose("PASS: isSet working fine\n");
687 ucal_clearField(cal
, UCAL_WEEK_OF_YEAR
);
688 if(ucal_isSet(cal
, UCAL_WEEK_OF_YEAR
))
689 log_err("FAIL: there is an error in clearField or isSet\n");
691 log_verbose("PASS :clearField working fine\n");
693 /*-------------------------------*/
703 /* ------------------------------------- */
705 * Execute adding and rolling in Calendar extensively,
707 static void TestAddRollExtensive()
712 UCalendarDateFields e
;
713 int32_t y
,m
,d
,hr
,min
,sec
,ms
;
714 int32_t maxlimit
= 40;
715 UErrorCode status
= U_ZERO_ERROR
;
716 y
= 1997; m
= UCAL_FEBRUARY
; d
= 1; hr
= 1; min
= 1; sec
= 0; ms
= 0;
718 log_verbose("Testing add and roll extensively\n");
720 tzID
=(UChar
*)malloc(sizeof(UChar
) * 4);
721 u_uastrcpy(tzID
, "PST");
722 /*open the calendar used */
723 cal
=ucal_open(tzID
, u_strlen(tzID
), "en_US", UCAL_GREGORIAN
, &status
);;
724 if (U_FAILURE(status
)) {
725 log_err("ucal_open() failed : %s\n", u_errorName(status
));
729 ucal_set(cal
, UCAL_YEAR
, y
);
730 ucal_set(cal
, UCAL_MONTH
, m
);
731 ucal_set(cal
, UCAL_DATE
, d
);
733 /* Confirm that adding to various fields works.*/
734 log_verbose("\nTesting to confirm that adding to various fields works with ucal_add()\n");
735 checkDate(cal
, y
, m
, d
);
736 ucal_add(cal
,UCAL_YEAR
, 1, &status
);
737 if (U_FAILURE(status
)) { log_err("ucal_add failed: %s\n", u_errorName(status
)); return; }
739 checkDate(cal
, y
, m
, d
);
740 ucal_add(cal
,UCAL_MONTH
, 12, &status
);
741 if (U_FAILURE(status
)) { log_err("ucal_add failed: %s\n", u_errorName(status
) ); return; }
743 checkDate(cal
, y
, m
, d
);
744 ucal_add(cal
,UCAL_DATE
, 1, &status
);
745 if (U_FAILURE(status
)) { log_err("ucal_add failed: %s\n", u_errorName(status
) ); return; }
747 checkDate(cal
, y
, m
, d
);
748 ucal_add(cal
,UCAL_DATE
, 2, &status
);
749 if (U_FAILURE(status
)) { log_err("ucal_add failed: %s\n", u_errorName(status
) ); return; }
751 checkDate(cal
, y
, m
, d
);
752 ucal_add(cal
,UCAL_DATE
, 28, &status
);
753 if (U_FAILURE(status
)) { log_err("ucal_add failed: %s\n", u_errorName(status
) ); return; }
755 checkDate(cal
, y
, m
, d
);
756 ucal_add(cal
, (UCalendarDateFields
)-1, 10, &status
);
757 if(status
==U_ILLEGAL_ARGUMENT_ERROR
)
758 log_verbose("Pass: Illegal argument error as expected\n");
760 log_err("Fail: No, illegal argument error as expected. Got....: %s\n", u_errorName(status
));
765 /*confirm that applying roll to various fields works fine*/
766 log_verbose("\nTesting to confirm that ucal_roll() works\n");
767 ucal_roll(cal
, UCAL_DATE
, -1, &status
);
768 if (U_FAILURE(status
)) { log_err("ucal_roll failed: %s\n", u_errorName(status
) ); return; }
770 checkDate(cal
, y
, m
, d
);
771 ucal_roll(cal
, UCAL_MONTH
, -2, &status
);
772 if (U_FAILURE(status
)) { log_err("ucal_roll failed: %s\n", u_errorName(status
) ); return; }
774 checkDate(cal
, y
, m
, d
);
775 ucal_roll(cal
, UCAL_DATE
, 1, &status
);
776 if (U_FAILURE(status
)) { log_err("ucal_roll failed: %s\n", u_errorName(status
) ); return; }
778 checkDate(cal
, y
, m
, d
);
779 ucal_roll(cal
, UCAL_MONTH
, -12, &status
);
780 if (U_FAILURE(status
)) { log_err("ucal_roll failed: %s\n", u_errorName(status
) ); return; }
781 checkDate(cal
, y
, m
, d
);
782 ucal_roll(cal
, UCAL_YEAR
, -1, &status
);
783 if (U_FAILURE(status
)) { log_err("ucal_roll failed: %s\n", u_errorName(status
) ); return; }
785 checkDate(cal
, y
, m
, d
);
786 ucal_roll(cal
, UCAL_DATE
, 29, &status
);
787 if (U_FAILURE(status
)) { log_err("ucal_roll failed: %s\n", u_errorName(status
) ); return; }
789 checkDate(cal
, y
, m
, d
);
790 ucal_roll(cal
, (UCalendarDateFields
)-1, 10, &status
);
791 if(status
==U_ILLEGAL_ARGUMENT_ERROR
)
792 log_verbose("Pass: illegal arguement error as expected\n");
794 log_err("Fail: no illegal argument error got..: %s\n", u_errorName(status
));
798 ucal_setDateTime(cal
, 1999, UCAL_FEBRUARY
, 28, 10, 30, 45, &status
);
799 if(U_FAILURE(status
)){
800 log_err("error is setting the datetime: %s\n", u_errorName(status
));
802 ucal_add(cal
, UCAL_MONTH
, 1, &status
);
803 checkDate(cal
, 1999, UCAL_MARCH
, 28);
804 ucal_add(cal
, UCAL_MILLISECOND
, 1000, &status
);
805 checkDateTime(cal
, 1999, UCAL_MARCH
, 28, 10, 30, 46, 0, UCAL_MILLISECOND
);
810 /* Testing add and roll extensively */
811 log_verbose("\nTesting the ucal_add() and ucal_roll() functions extensively\n");
812 y
= 1997; m
= UCAL_FEBRUARY
; d
= 1; hr
= 1; min
= 1; sec
= 0; ms
= 0;
813 cal
=ucal_open(tzID
, u_strlen(tzID
), "en_US", UCAL_TRADITIONAL
, &status
);
814 if (U_FAILURE(status
)) {
815 log_err("ucal_open failed: %s\n", u_errorName(status
));
818 ucal_set(cal
, UCAL_YEAR
, y
);
819 ucal_set(cal
, UCAL_MONTH
, m
);
820 ucal_set(cal
, UCAL_DATE
, d
);
821 ucal_set(cal
, UCAL_HOUR
, hr
);
822 ucal_set(cal
, UCAL_MINUTE
, min
);
823 ucal_set(cal
, UCAL_SECOND
,sec
);
824 ucal_set(cal
, UCAL_MILLISECOND
, ms
);
827 log_verbose("\nTesting UCalendar add...\n");
828 for(e
= UCAL_YEAR
;e
< UCAL_FIELD_COUNT
; e
=(UCalendarDateFields
)((int32_t)e
+ 1)) {
830 status
= U_ZERO_ERROR
;
831 for (i
= 0; i
< limit
; i
++) {
832 ucal_add(cal
, e
, 1, &status
);
833 if (U_FAILURE(status
)) { limit
= i
; status
= U_ZERO_ERROR
; }
835 for (i
= 0; i
< limit
; i
++) {
836 ucal_add(cal
, e
, -1, &status
);
837 if (U_FAILURE(status
)) {
838 log_err("ucal_add -1 failed: %s\n", u_errorName(status
));
842 checkDateTime(cal
, y
, m
, d
, hr
, min
, sec
, ms
, e
);
844 log_verbose("\nTesting calendar ucal_roll()...\n");
845 for(e
= UCAL_YEAR
;e
< UCAL_FIELD_COUNT
; e
=(UCalendarDateFields
)((int32_t)e
+ 1)) {
847 status
= U_ZERO_ERROR
;
848 for (i
= 0; i
< limit
; i
++) {
849 ucal_roll(cal
, e
, 1, &status
);
850 if (U_FAILURE(status
)) {
852 status
= U_ZERO_ERROR
;
855 for (i
= 0; i
< limit
; i
++) {
856 ucal_roll(cal
, e
, -1, &status
);
857 if (U_FAILURE(status
)) {
858 log_err("ucal_roll -1 failed: %s\n", u_errorName(status
));
862 checkDateTime(cal
, y
, m
, d
, hr
, min
, sec
, ms
, e
);
869 /*------------------------------------------------------ */
870 /*Testing the Limits for various Fields of Calendar*/
871 static void TestGetLimits()
874 int32_t min
, max
, gr_min
, le_max
, ac_min
, ac_max
, val
;
876 UErrorCode status
= U_ZERO_ERROR
;
879 tzID
=(UChar
*)malloc(sizeof(UChar
) * 4);
880 u_uastrcpy(tzID
, "PST");
881 /*open the calendar used */
882 cal
=ucal_open(tzID
, u_strlen(tzID
), "en_US", UCAL_GREGORIAN
, &status
);;
883 if (U_FAILURE(status
)) {
884 log_err("ucal_open() for gregorian calendar failed in TestGetLimits: %s\n", u_errorName(status
));
888 log_verbose("\nTesting the getLimits function for various fields\n");
892 ucal_setDate(cal
, 1999, UCAL_MARCH
, 5, &status
); /* Set the date to be March 5, 1999 */
893 val
= ucal_get(cal
, UCAL_DAY_OF_WEEK
, &status
);
894 min
= ucal_getLimit(cal
, UCAL_DAY_OF_WEEK
, UCAL_MINIMUM
, &status
);
895 max
= ucal_getLimit(cal
, UCAL_DAY_OF_WEEK
, UCAL_MAXIMUM
, &status
);
896 if ( (min
!= UCAL_SUNDAY
|| max
!= UCAL_SATURDAY
) && (min
> val
> max
) && (val
!= UCAL_FRIDAY
)){
897 log_err("FAIL: Min/max bad\n");
898 log_err("FAIL: Day of week %d out of range\n", val
);
899 log_err("FAIL: FAIL: Day of week should be SUNDAY Got %d\n", val
);
902 log_verbose("getLimits successful\n");
904 val
= ucal_get(cal
, UCAL_DAY_OF_WEEK_IN_MONTH
, &status
);
905 min
= ucal_getLimit(cal
, UCAL_DAY_OF_WEEK_IN_MONTH
, UCAL_MINIMUM
, &status
);
906 max
= ucal_getLimit(cal
, UCAL_DAY_OF_WEEK_IN_MONTH
, UCAL_MAXIMUM
, &status
);
907 if ( (min
!= 0 || max
!= 5 ) && (min
> val
> max
) && (val
!= 1)){
908 log_err("FAIL: Min/max bad\n");
909 log_err("FAIL: Day of week in month %d out of range\n", val
);
910 log_err("FAIL: FAIL: Day of week in month should be SUNDAY Got %d\n", val
);
914 log_verbose("getLimits successful\n");
916 min
=ucal_getLimit(cal
, UCAL_MONTH
, UCAL_MINIMUM
, &status
);
917 max
=ucal_getLimit(cal
, UCAL_MONTH
, UCAL_MAXIMUM
, &status
);
918 gr_min
=ucal_getLimit(cal
, UCAL_MONTH
, UCAL_GREATEST_MINIMUM
, &status
);
919 le_max
=ucal_getLimit(cal
, UCAL_MONTH
, UCAL_LEAST_MAXIMUM
, &status
);
920 ac_min
=ucal_getLimit(cal
, UCAL_MONTH
, UCAL_ACTUAL_MINIMUM
, &status
);
921 ac_max
=ucal_getLimit(cal
, UCAL_MONTH
, UCAL_ACTUAL_MAXIMUM
, &status
);
922 if(U_FAILURE(status
)){
923 log_err("Error in getLimits: %s\n", u_errorName(status
));
925 if(min
!=0 || max
!=11 || gr_min
!=0 || le_max
!=11 || ac_min
!=0 || ac_max
!=11)
926 log_err("There is and error in getLimits in fetching the values\n");
928 log_verbose("getLimits successful\n");
930 ucal_setDateTime(cal
, 1999, UCAL_MARCH
, 5, 4, 10, 35, &status
);
931 val
=ucal_get(cal
, UCAL_HOUR_OF_DAY
, &status
);
932 min
=ucal_getLimit(cal
, UCAL_HOUR_OF_DAY
, UCAL_MINIMUM
, &status
);
933 max
=ucal_getLimit(cal
, UCAL_HOUR_OF_DAY
, UCAL_MAXIMUM
, &status
);
934 gr_min
=ucal_getLimit(cal
, UCAL_MINUTE
, UCAL_GREATEST_MINIMUM
, &status
);
935 le_max
=ucal_getLimit(cal
, UCAL_MINUTE
, UCAL_LEAST_MAXIMUM
, &status
);
936 ac_min
=ucal_getLimit(cal
, UCAL_MINUTE
, UCAL_ACTUAL_MINIMUM
, &status
);
937 ac_max
=ucal_getLimit(cal
, UCAL_SECOND
, UCAL_ACTUAL_MAXIMUM
, &status
);
938 if( (min
!=0 || max
!= 11 || gr_min
!=0 || le_max
!=60 || ac_min
!=0 || ac_max
!=60) &&
939 (min
>val
>max
) && val
!=4){
941 log_err("FAIL: Min/max bad\n");
942 log_err("FAIL: Hour of Day %d out of range\n", val
);
943 log_err("FAIL: HOUR_OF_DAY should be 4 Got %d\n", val
);
946 log_verbose("getLimits successful\n");
949 /*get BOGUS_LIMIT type*/
950 val
=ucal_getLimit(cal
, UCAL_SECOND
, (UCalendarLimitType
)99, &status
);
952 log_err("FAIL: ucal_getLimit() with BOGUS type should return -1\n");
963 /* ------------------------------------- */
966 * Test that the days of the week progress properly when add is called repeatedly
967 * for increments of 24 days.
969 static void TestDOWProgression()
971 int32_t initialDOW
, DOW
, newDOW
, expectedDOW
;
973 UDateFormat
*datfor
= 0;
976 UErrorCode status
= U_ZERO_ERROR
;
978 char tempMsgBuf
[256];
979 tzID
=(UChar
*)malloc(sizeof(UChar
) * 4);
980 u_strcpy(tzID
, fgGMTID
);
981 /*open the calendar used */
982 cal
=ucal_open(tzID
, u_strlen(tzID
), "en_US", UCAL_TRADITIONAL
, &status
);;
983 if (U_FAILURE(status
)) {
984 log_err("ucal_open failed: %s\n", u_errorName(status
));
988 datfor
=udat_open(UDAT_MEDIUM
,UDAT_MEDIUM
,NULL
, fgGMTID
,-1,NULL
, 0, &status
);
989 if(U_FAILURE(status
)){
990 log_err("error in creating the dateformat : %s\n", u_errorName(status
));
994 ucal_setDate(cal
, 1999, UCAL_JANUARY
, 1, &status
);
996 log_verbose("\nTesting the DOW progression\n");
998 initialDOW
= ucal_get(cal
, UCAL_DAY_OF_WEEK
, &status
);
999 if (U_FAILURE(status
)) { log_err("ucal_get() failed: %s\n", u_errorName(status
) ); return; }
1000 newDOW
= initialDOW
;
1003 log_verbose("DOW = %d...\n", DOW
);
1004 date1
=ucal_getMillis(cal
, &status
);
1005 if(U_FAILURE(status
)){ log_err("ucal_getMiilis() failed: %s\n", u_errorName(status
)); return;}
1006 log_verbose("%s\n", u_austrcpy(tempMsgBuf
, myDateFormat(datfor
, date1
)));
1008 ucal_add(cal
,UCAL_DAY_OF_WEEK
, delta
, &status
);
1009 if (U_FAILURE(status
)) { log_err("ucal_add() failed: %s\n", u_errorName(status
)); return; }
1011 newDOW
= ucal_get(cal
, UCAL_DAY_OF_WEEK
, &status
);
1012 if (U_FAILURE(status
)) { log_err("ucal_get() failed: %s\n", u_errorName(status
)); return; }
1013 expectedDOW
= 1 + (DOW
+ delta
- 1) % 7;
1014 date1
=ucal_getMillis(cal
, &status
);
1015 if(U_FAILURE(status
)){ log_err("ucal_getMiilis() failed: %s\n", u_errorName(status
)); return;}
1016 if (newDOW
!= expectedDOW
) {
1017 log_err("Day of week should be %d instead of %d on %s", expectedDOW
, newDOW
,
1018 u_austrcpy(tempMsgBuf
, myDateFormat(datfor
, date1
)) );
1022 while (newDOW
!= initialDOW
);
1030 /* ------------------------------------- */
1033 * Confirm that the offset between local time and GMT behaves as expected.
1035 static void TestGMTvsLocal()
1037 log_verbose("\nTesting the offset between the GMT and local time\n");
1038 testZones(1999, 1, 1, 12, 0, 0);
1039 testZones(1999, 4, 16, 18, 30, 0);
1040 testZones(1998, 12, 17, 19, 0, 0);
1043 /* ------------------------------------- */
1045 static void testZones(int32_t yr
, int32_t mo
, int32_t dt
, int32_t hr
, int32_t mn
, int32_t sc
)
1047 int32_t offset
,utc
, expected
;
1048 UCalendar
*gmtcal
= 0, *cal
= 0;
1051 UDateFormat
*datfor
= 0;
1052 UErrorCode status
= U_ZERO_ERROR
;
1054 char tempMsgBuf
[256];
1056 tzID
=(UChar
*)malloc(sizeof(UChar
) * 4);
1057 u_strcpy(tzID
, fgGMTID
);
1058 gmtcal
=ucal_open(tzID
, 3, "en_US", UCAL_TRADITIONAL
, &status
);;
1059 if (U_FAILURE(status
)) {
1060 log_err("ucal_open failed: %s\n", u_errorName(status
));
1063 u_uastrcpy(tzID
, "PST");
1064 cal
= ucal_open(tzID
, 3, "en_US", UCAL_TRADITIONAL
, &status
);
1065 if (U_FAILURE(status
)) {
1066 log_err("ucal_open failed: %s\n", u_errorName(status
));
1070 datfor
=udat_open(UDAT_MEDIUM
,UDAT_MEDIUM
,NULL
, fgGMTID
,-1,NULL
, 0, &status
);
1071 if(U_FAILURE(status
)){
1072 log_err("error in creating the dateformat : %s\n", u_errorName(status
));
1075 ucal_setDateTime(gmtcal
, yr
, mo
- 1, dt
, hr
, mn
, sc
, &status
);
1076 if (U_FAILURE(status
)) {
1077 log_err("ucal_setDateTime failed: %s\n", u_errorName(status
));
1080 ucal_set(gmtcal
, UCAL_MILLISECOND
, 0);
1081 date1
= ucal_getMillis(gmtcal
, &status
);
1082 if (U_FAILURE(status
)) {
1083 log_err("ucal_getMillis failed: %s\n", u_errorName(status
));
1086 log_verbose("date = %s\n", u_austrcpy(tempMsgBuf
, myDateFormat(datfor
, date1
)) );
1089 ucal_setMillis(cal
, date1
, &status
);
1090 if (U_FAILURE(status
)) {
1091 log_err("ucal_setMillis() failed: %s\n", u_errorName(status
));
1095 offset
= ucal_get(cal
, UCAL_ZONE_OFFSET
, &status
);
1096 offset
+= ucal_get(cal
, UCAL_DST_OFFSET
, &status
);
1098 if (U_FAILURE(status
)) {
1099 log_err("ucal_get() failed: %s\n", u_errorName(status
));
1102 temp
=(double)((double)offset
/ 1000.0 / 60.0 / 60.0);
1103 /*printf("offset for %s %f hr\n", austrdup(myDateFormat(datfor, date1)), temp);*/
1105 utc
= ((ucal_get(cal
, UCAL_HOUR_OF_DAY
, &status
) * 60 +
1106 ucal_get(cal
, UCAL_MINUTE
, &status
)) * 60 +
1107 ucal_get(cal
, UCAL_SECOND
, &status
)) * 1000 +
1108 ucal_get(cal
, UCAL_MILLISECOND
, &status
) - offset
;
1109 if (U_FAILURE(status
)) {
1110 log_err("ucal_get() failed: %s\n", u_errorName(status
));
1114 expected
= ((hr
* 60 + mn
) * 60 + sc
) * 1000;
1115 if (utc
!= expected
) {
1116 temp
=(double)(utc
- expected
)/ 1000 / 60 / 60.0;
1117 log_err("FAIL: Discrepancy of %d millis = %fhr\n", utc
-expected
, temp
);
1120 log_verbose("PASS: the offset between local and GMT is correct\n");
1127 /* ------------------------------------- */
1132 /* INTERNAL FUNCTIONS USED */
1133 /*------------------------------------------------------------------------------------------- */
1135 /* ------------------------------------- */
1136 static void checkDateTime(UCalendar
* c
,
1137 int32_t y
, int32_t m
, int32_t d
,
1138 int32_t hr
, int32_t min
, int32_t sec
,
1139 int32_t ms
, UCalendarDateFields field
)
1142 UErrorCode status
= U_ZERO_ERROR
;
1143 if (ucal_get(c
, UCAL_YEAR
, &status
) != y
||
1144 ucal_get(c
, UCAL_MONTH
, &status
) != m
||
1145 ucal_get(c
, UCAL_DATE
, &status
) != d
||
1146 ucal_get(c
, UCAL_HOUR
, &status
) != hr
||
1147 ucal_get(c
, UCAL_MINUTE
, &status
) != min
||
1148 ucal_get(c
, UCAL_SECOND
, &status
) != sec
||
1149 ucal_get(c
, UCAL_MILLISECOND
, &status
) != ms
) {
1150 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",
1151 (int32_t)field
, y
, m
+ 1, d
, hr
, min
, sec
, ms
,
1152 ucal_get(c
, UCAL_YEAR
, &status
),
1153 ucal_get(c
, UCAL_MONTH
, &status
) + 1,
1154 ucal_get(c
, UCAL_DATE
, &status
),
1155 ucal_get(c
, UCAL_HOUR
, &status
),
1156 ucal_get(c
, UCAL_MINUTE
, &status
) + 1,
1157 ucal_get(c
, UCAL_SECOND
, &status
),
1158 ucal_get(c
, UCAL_MILLISECOND
, &status
) );
1160 if (U_FAILURE(status
)){
1161 log_err("ucal_get failed: %s\n", u_errorName(status
));
1167 log_verbose("Confirmed: %d/%d/%d %d:%d:%d:%d\n", y
, m
+ 1, d
, hr
, min
, sec
, ms
);
1171 /* ------------------------------------- */
1172 static void checkDate(UCalendar
* c
, int32_t y
, int32_t m
, int32_t d
)
1174 UErrorCode status
= U_ZERO_ERROR
;
1175 if (ucal_get(c
,UCAL_YEAR
, &status
) != y
||
1176 ucal_get(c
, UCAL_MONTH
, &status
) != m
||
1177 ucal_get(c
, UCAL_DATE
, &status
) != d
) {
1179 log_err("FAILURE: Expected y/m/d of %d/%d/%d got %d/%d/%d\n", y
, m
+ 1, d
,
1180 ucal_get(c
, UCAL_YEAR
, &status
),
1181 ucal_get(c
, UCAL_MONTH
, &status
) + 1,
1182 ucal_get(c
, UCAL_DATE
, &status
) );
1184 if (U_FAILURE(status
)) {
1185 log_err("ucal_get failed: %s\n", u_errorName(status
));
1190 log_verbose("Confirmed: %d/%d/%d\n", y
, m
+ 1, d
);
1195 /* ------------------------------------- */
1197 /* ------------------------------------- */
1199 static void verify1(const char* msg
, UCalendar
* c
, UDateFormat
* dat
, int32_t year
, int32_t month
, int32_t day
)
1202 UErrorCode status
= U_ZERO_ERROR
;
1203 if (ucal_get(c
, UCAL_YEAR
, &status
) == year
&&
1204 ucal_get(c
, UCAL_MONTH
, &status
) == month
&&
1205 ucal_get(c
, UCAL_DATE
, &status
) == day
) {
1206 if (U_FAILURE(status
)) {
1207 log_err("FAIL: Calendar::get failed: %s\n", u_errorName(status
));
1210 log_verbose("PASS: %s\n", msg
);
1211 d1
=ucal_getMillis(c
, &status
);
1212 if (U_FAILURE(status
)) {
1213 log_err("ucal_getMillis failed: %s\n", u_errorName(status
));
1216 /*log_verbose(austrdup(myDateFormat(dat, d1)) );*/
1219 log_err("FAIL: %s\n", msg
);
1220 d1
=ucal_getMillis(c
, &status
);
1221 if (U_FAILURE(status
)) {
1222 log_err("ucal_getMillis failed: %s\n", u_errorName(status
) );
1225 log_err("Got %s Expected %d/%d/%d \n", austrdup(myDateFormat(dat
, d1
)), year
, month
+ 1, day
);
1232 /* ------------------------------------ */
1233 static void verify2(const char* msg
, UCalendar
* c
, UDateFormat
* dat
, int32_t year
, int32_t month
, int32_t day
,
1234 int32_t hour
, int32_t min
, int32_t sec
, int32_t am_pm
)
1237 UErrorCode status
= U_ZERO_ERROR
;
1238 char tempMsgBuf
[256];
1240 if (ucal_get(c
, UCAL_YEAR
, &status
) == year
&&
1241 ucal_get(c
, UCAL_MONTH
, &status
) == month
&&
1242 ucal_get(c
, UCAL_DATE
, &status
) == day
&&
1243 ucal_get(c
, UCAL_HOUR
, &status
) == hour
&&
1244 ucal_get(c
, UCAL_MINUTE
, &status
) == min
&&
1245 ucal_get(c
, UCAL_SECOND
, &status
) == sec
&&
1246 ucal_get(c
, UCAL_AM_PM
, &status
) == am_pm
){
1247 if (U_FAILURE(status
)) {
1248 log_err("FAIL: Calendar::get failed: %s\n", u_errorName(status
));
1251 log_verbose("PASS: %s\n", msg
);
1252 d1
=ucal_getMillis(c
, &status
);
1253 if (U_FAILURE(status
)) {
1254 log_err("ucal_getMillis failed: %s\n", u_errorName(status
));
1257 log_verbose("%s\n" , u_austrcpy(tempMsgBuf
, myDateFormat(dat
, d1
)) );
1260 log_err("FAIL: %s\n", msg
);
1261 d1
=ucal_getMillis(c
, &status
);
1262 if (U_FAILURE(status
)) {
1263 log_err("ucal_getMillis failed: %s\n", u_errorName(status
));
1266 log_err("Got %s Expected %d/%d/%d/ %d:%d:%d %s\n", austrdup(myDateFormat(dat
, d1
)),
1267 year
, month
+ 1, day
, hour
, min
, sec
, (am_pm
==0) ? "AM": "PM");
1275 #endif /* #if !UCONFIG_NO_FORMATTING */