1 /************************************************************************
3 * Copyright (c) 1997-2012, International Business Machines Corporation
4 * and others. All Rights Reserved.
5 ************************************************************************/
7 #include "unicode/utypes.h"
9 #if !UCONFIG_NO_FORMATTING
12 #include "unicode/dtfmtsym.h"
13 #include "unicode/gregocal.h"
15 #include "unicode/smpdtfmt.h"
16 #include "unicode/simpletz.h"
18 #include "unicode/udat.h"
19 #include "unicode/ustring.h"
21 #include "unicode/localpointer.h"
23 #define mkcstr(U) u_austrcpy(calloc(8, u_strlen(U) + 1), U)
25 #define TEST_CHECK_STATUS {if (U_FAILURE(status)) {errln("%s:%d: Test failure. status=%s", \
26 __FILE__, __LINE__, u_errorName(status)); return;}}
28 #define TEST_ASSERT(expr) {if ((expr)==FALSE) {errln("%s:%d: Test failure \n", __FILE__, __LINE__);};}
30 // *****************************************************************************
32 // *****************************************************************************
34 UnicodeString
CalendarTest::calToStr(const Calendar
& cal
)
37 UErrorCode status
= U_ZERO_ERROR
;
40 for(i
= 0;i
<UCAL_FIELD_COUNT
;i
++) {
41 out
+= (UnicodeString("") + fieldName((UCalendarDateFields
)i
) + "=" + cal
.get((UCalendarDateFields
)i
, status
) + UnicodeString(" "));
43 out
+= "[" + UnicodeString(cal
.getType()) + "]";
45 if(cal
.inDaylightTime(status
)) {
46 out
+= UnicodeString(" (in DST), zone=");
49 out
+= UnicodeString(", zone=");
53 out
+= cal
.getTimeZone().getDisplayName(str2
);
54 d
= cal
.getTime(status
);
55 out
+= UnicodeString(" :","") + d
;
60 void CalendarTest::runIndexedTest( int32_t index
, UBool exec
, const char* &name
, char* /*par*/ )
62 if (exec
) logln("TestSuite TestCalendar");
67 logln("TestDOW943---"); logln("");
72 name
= "TestClonesUnique908";
74 logln("TestClonesUnique908---"); logln("");
75 TestClonesUnique908();
79 name
= "TestGregorianChange768";
81 logln("TestGregorianChange768---"); logln("");
82 TestGregorianChange768();
86 name
= "TestDisambiguation765";
88 logln("TestDisambiguation765---"); logln("");
89 TestDisambiguation765();
93 name
= "TestGMTvsLocal4064654";
95 logln("TestGMTvsLocal4064654---"); logln("");
96 TestGMTvsLocal4064654();
100 name
= "TestAddSetOrder621";
102 logln("TestAddSetOrder621---"); logln("");
103 TestAddSetOrder621();
109 logln("TestAdd520---"); logln("");
114 name
= "TestFieldSet4781";
116 logln("TestFieldSet4781---"); logln("");
121 name
= "TestSerialize337";
123 logln("TestSerialize337---"); logln("");
124 // TestSerialize337();
128 name
= "TestSecondsZero121";
130 logln("TestSecondsZero121---"); logln("");
131 TestSecondsZero121();
135 name
= "TestAddSetGet0610";
137 logln("TestAddSetGet0610---"); logln("");
142 name
= "TestFields060";
144 logln("TestFields060---"); logln("");
149 name
= "TestEpochStartFields";
151 logln("TestEpochStartFields---"); logln("");
152 TestEpochStartFields();
156 name
= "TestDOWProgression";
158 logln("TestDOWProgression---"); logln("");
159 TestDOWProgression();
163 name
= "TestGenericAPI";
165 logln("TestGenericAPI---"); logln("");
170 name
= "TestAddRollExtensive";
172 logln("TestAddRollExtensive---"); logln("");
173 TestAddRollExtensive();
177 name
= "TestDOW_LOCALandYEAR_WOY";
179 logln("TestDOW_LOCALandYEAR_WOY---"); logln("");
180 TestDOW_LOCALandYEAR_WOY();
186 logln("TestWOY---"); logln("");
193 logln("TestRog---"); logln("");
200 logln("TestYWOY---"); logln("");
207 logln("TestJD---"); logln("");
214 logln("TestDebug---"); logln("");
221 logln("Test6703---"); logln("");
228 logln("Test3785---"); logln("");
235 logln("Test1624---"); logln("");
240 name
= "TestTimeStamp";
242 logln("TestTimeStamp---"); logln("");
247 name
= "TestISO8601";
249 logln("TestISO8601---"); logln("");
254 name
= "TestAmbiguousWallTimeAPIs";
256 logln("TestAmbiguousWallTimeAPIs---"); logln("");
257 TestAmbiguousWallTimeAPIs();
261 name
= "TestRepeatedWallTime";
263 logln("TestRepeatedWallTime---"); logln("");
264 TestRepeatedWallTime();
268 name
= "TestSkippedWallTime";
270 logln("TestSkippedWallTime---"); logln("");
271 TestSkippedWallTime();
275 name
= "TestCloneLocale";
277 logln("TestCloneLocale---"); logln("");
281 default: name
= ""; break;
285 // ---------------------------------------------------------------------------------
287 UnicodeString
CalendarTest::fieldName(UCalendarDateFields f
) {
289 #define FIELD_NAME_STR(x) case x: return (#x+5)
290 FIELD_NAME_STR( UCAL_ERA
);
291 FIELD_NAME_STR( UCAL_YEAR
);
292 FIELD_NAME_STR( UCAL_MONTH
);
293 FIELD_NAME_STR( UCAL_WEEK_OF_YEAR
);
294 FIELD_NAME_STR( UCAL_WEEK_OF_MONTH
);
295 FIELD_NAME_STR( UCAL_DATE
);
296 FIELD_NAME_STR( UCAL_DAY_OF_YEAR
);
297 FIELD_NAME_STR( UCAL_DAY_OF_WEEK
);
298 FIELD_NAME_STR( UCAL_DAY_OF_WEEK_IN_MONTH
);
299 FIELD_NAME_STR( UCAL_AM_PM
);
300 FIELD_NAME_STR( UCAL_HOUR
);
301 FIELD_NAME_STR( UCAL_HOUR_OF_DAY
);
302 FIELD_NAME_STR( UCAL_MINUTE
);
303 FIELD_NAME_STR( UCAL_SECOND
);
304 FIELD_NAME_STR( UCAL_MILLISECOND
);
305 FIELD_NAME_STR( UCAL_ZONE_OFFSET
);
306 FIELD_NAME_STR( UCAL_DST_OFFSET
);
307 FIELD_NAME_STR( UCAL_YEAR_WOY
);
308 FIELD_NAME_STR( UCAL_DOW_LOCAL
);
309 FIELD_NAME_STR( UCAL_EXTENDED_YEAR
);
310 FIELD_NAME_STR( UCAL_JULIAN_DAY
);
311 FIELD_NAME_STR( UCAL_MILLISECONDS_IN_DAY
);
312 #undef FIELD_NAME_STR
314 return UnicodeString("") + ((int32_t)f
);
319 * Test various API methods for API completeness.
322 CalendarTest::TestGenericAPI()
324 UErrorCode status
= U_ZERO_ERROR
;
327 UBool eq
= FALSE
,b4
= FALSE
,af
= FALSE
;
329 UDate when
= date(90, UCAL_APRIL
, 15);
331 UnicodeString
tzid("TestZone");
332 int32_t tzoffset
= 123400;
334 SimpleTimeZone
*zone
= new SimpleTimeZone(tzoffset
, tzid
);
335 Calendar
*cal
= Calendar::createInstance(zone
->clone(), status
);
336 if (failure(status
, "Calendar::createInstance", TRUE
)) return;
338 if (*zone
!= cal
->getTimeZone()) errln("FAIL: Calendar::getTimeZone failed");
340 Calendar
*cal2
= Calendar::createInstance(cal
->getTimeZone(), status
);
341 if (failure(status
, "Calendar::createInstance")) return;
342 cal
->setTime(when
, status
);
343 cal2
->setTime(when
, status
);
344 if (failure(status
, "Calendar::setTime")) return;
346 if (!(*cal
== *cal2
)) errln("FAIL: Calendar::operator== failed");
347 if ((*cal
!= *cal2
)) errln("FAIL: Calendar::operator!= failed");
348 if (!cal
->equals(*cal2
, status
) ||
349 cal
->before(*cal2
, status
) ||
350 cal
->after(*cal2
, status
) ||
351 U_FAILURE(status
)) errln("FAIL: equals/before/after failed");
353 logln(UnicodeString("cal=") +cal
->getTime(status
) + UnicodeString(calToStr(*cal
)));
354 logln(UnicodeString("cal2=") +cal2
->getTime(status
) + UnicodeString(calToStr(*cal2
)));
355 logln("cal2->setTime(when+1000)");
356 cal2
->setTime(when
+ 1000, status
);
357 logln(UnicodeString("cal2=") +cal2
->getTime(status
) + UnicodeString(calToStr(*cal2
)));
359 if (failure(status
, "Calendar::setTime")) return;
360 if (cal
->equals(*cal2
, status
) ||
361 cal2
->before(*cal
, status
) ||
362 cal
->after(*cal2
, status
) ||
363 U_FAILURE(status
)) errln("FAIL: equals/before/after failed after setTime(+1000)");
365 logln("cal->roll(UCAL_SECOND)");
366 cal
->roll(UCAL_SECOND
, (UBool
) TRUE
, status
);
367 logln(UnicodeString("cal=") +cal
->getTime(status
) + UnicodeString(calToStr(*cal
)));
368 cal
->roll(UCAL_SECOND
, (int32_t)0, status
);
369 logln(UnicodeString("cal=") +cal
->getTime(status
) + UnicodeString(calToStr(*cal
)));
370 if (failure(status
, "Calendar::roll")) return;
372 if (!(eq
=cal
->equals(*cal2
, status
)) ||
373 (b4
=cal
->before(*cal2
, status
)) ||
374 (af
=cal
->after(*cal2
, status
)) ||
376 errln("FAIL: equals[%c]/before[%c]/after[%c] failed after roll 1 second [should be T/F/F]",
380 logln(UnicodeString("cal=") +cal
->getTime(status
) + UnicodeString(calToStr(*cal
)));
381 logln(UnicodeString("cal2=") +cal2
->getTime(status
) + UnicodeString(calToStr(*cal2
)));
384 // Roll back to January
385 cal
->roll(UCAL_MONTH
, (int32_t)(1 + UCAL_DECEMBER
- cal
->get(UCAL_MONTH
, status
)), status
);
386 if (failure(status
, "Calendar::roll")) return;
387 if (cal
->equals(*cal2
, status
) ||
388 cal2
->before(*cal
, status
) ||
389 cal
->after(*cal2
, status
) ||
390 U_FAILURE(status
)) errln("FAIL: equals/before/after failed after rollback to January");
392 TimeZone
*z
= cal
->orphanTimeZone();
393 if (z
->getID(str
) != tzid
||
394 z
->getRawOffset() != tzoffset
)
395 errln("FAIL: orphanTimeZone failed");
400 UBool lenient
= ( i
> 0 );
401 cal
->setLenient(lenient
);
402 if (lenient
!= cal
->isLenient()) errln("FAIL: setLenient/isLenient failed");
403 // Later: Check for lenient behavior
406 for (i
=UCAL_SUNDAY
; i
<=UCAL_SATURDAY
; ++i
)
408 cal
->setFirstDayOfWeek((UCalendarDaysOfWeek
)i
);
409 if (cal
->getFirstDayOfWeek() != i
) errln("FAIL: set/getFirstDayOfWeek failed");
410 UErrorCode aStatus
= U_ZERO_ERROR
;
411 if (cal
->getFirstDayOfWeek(aStatus
) != i
|| U_FAILURE(aStatus
)) errln("FAIL: getFirstDayOfWeek(status) failed");
416 cal
->setMinimalDaysInFirstWeek((uint8_t)i
);
417 if (cal
->getMinimalDaysInFirstWeek() != i
) errln("FAIL: set/getFirstDayOfWeek failed");
420 for (i
=0; i
<UCAL_FIELD_COUNT
; ++i
)
422 if (cal
->getMinimum((UCalendarDateFields
)i
) > cal
->getGreatestMinimum((UCalendarDateFields
)i
))
423 errln("FAIL: getMinimum larger than getGreatestMinimum for field " + i
);
424 if (cal
->getLeastMaximum((UCalendarDateFields
)i
) > cal
->getMaximum((UCalendarDateFields
)i
))
425 errln("FAIL: getLeastMaximum larger than getMaximum for field " + i
);
426 if (cal
->getMinimum((UCalendarDateFields
)i
) >= cal
->getMaximum((UCalendarDateFields
)i
))
427 errln("FAIL: getMinimum not less than getMaximum for field " + i
);
430 cal
->adoptTimeZone(TimeZone::createDefault());
432 cal
->set(1984, 5, 24);
433 if (cal
->getTime(status
) != date(84, 5, 24) || U_FAILURE(status
))
434 errln("FAIL: Calendar::set(3 args) failed");
437 cal
->set(1985, 3, 2, 11, 49);
438 if (cal
->getTime(status
) != date(85, 3, 2, 11, 49) || U_FAILURE(status
))
439 errln("FAIL: Calendar::set(5 args) failed");
442 cal
->set(1995, 9, 12, 1, 39, 55);
443 if (cal
->getTime(status
) != date(95, 9, 12, 1, 39, 55) || U_FAILURE(status
))
444 errln("FAIL: Calendar::set(6 args) failed");
446 cal
->getTime(status
);
447 if (failure(status
, "Calendar::getTime")) return;
448 for (i
=0; i
<UCAL_FIELD_COUNT
; ++i
)
451 case UCAL_YEAR
: case UCAL_MONTH
: case UCAL_DATE
:
452 case UCAL_HOUR_OF_DAY
: case UCAL_MINUTE
: case UCAL_SECOND
:
453 case UCAL_EXTENDED_YEAR
:
454 if (!cal
->isSet((UCalendarDateFields
)i
)) errln("FAIL: Calendar::isSet F, should be T " + fieldName((UCalendarDateFields
)i
));
457 if (cal
->isSet((UCalendarDateFields
)i
)) errln("FAIL: Calendar::isSet = T, should be F " + fieldName((UCalendarDateFields
)i
));
459 cal
->clear((UCalendarDateFields
)i
);
460 if (cal
->isSet((UCalendarDateFields
)i
)) errln("FAIL: Calendar::clear/isSet failed " + fieldName((UCalendarDateFields
)i
));
463 if(cal
->getActualMinimum(Calendar::SECOND
, status
) != 0){
464 errln("Calendar is suppose to return 0 for getActualMinimum");
467 Calendar
*cal3
= Calendar::createInstance(status
);
468 cal3
->roll(Calendar::SECOND
, (int32_t)0, status
);
469 if (failure(status
, "Calendar::roll(EDateFields, int32_t, UErrorCode)")) return;
476 const Locale
* loc
= Calendar::getAvailableLocales(count
);
477 if (count
< 1 || loc
== 0)
479 dataerrln("FAIL: getAvailableLocales failed");
483 for (i
=0; i
<count
; ++i
)
485 cal
= Calendar::createInstance(loc
[i
], status
);
486 if (failure(status
, "Calendar::createInstance")) return;
491 cal
= Calendar::createInstance(TimeZone::createDefault(), Locale::getEnglish(), status
);
492 if (failure(status
, "Calendar::createInstance")) return;
495 cal
= Calendar::createInstance(*zone
, Locale::getEnglish(), status
);
496 if (failure(status
, "Calendar::createInstance")) return;
499 GregorianCalendar
*gc
= new GregorianCalendar(*zone
, status
);
500 if (failure(status
, "new GregorianCalendar")) return;
503 gc
= new GregorianCalendar(Locale::getEnglish(), status
);
504 if (failure(status
, "new GregorianCalendar")) return;
507 gc
= new GregorianCalendar(Locale::getEnglish(), status
);
510 gc
= new GregorianCalendar(*zone
, Locale::getEnglish(), status
);
511 if (failure(status
, "new GregorianCalendar")) return;
514 gc
= new GregorianCalendar(zone
, status
);
515 if (failure(status
, "new GregorianCalendar")) return;
518 gc
= new GregorianCalendar(1998, 10, 14, 21, 43, status
);
519 if (gc
->getTime(status
) != (d
=date(98, 10, 14, 21, 43) )|| U_FAILURE(status
))
520 errln("FAIL: new GregorianCalendar(ymdhm) failed with " + UnicodeString(u_errorName(status
)) + ", cal=" + gc
->getTime(status
) + UnicodeString(calToStr(*gc
)) + ", d=" + d
);
522 logln(UnicodeString("GOOD: cal=") +gc
->getTime(status
) + UnicodeString(calToStr(*gc
)) + ", d=" + d
);
525 gc
= new GregorianCalendar(1998, 10, 14, 21, 43, 55, status
);
526 if (gc
->getTime(status
) != (d
=date(98, 10, 14, 21, 43, 55)) || U_FAILURE(status
))
527 errln("FAIL: new GregorianCalendar(ymdhms) failed with " + UnicodeString(u_errorName(status
)));
529 GregorianCalendar
gc2(Locale::getEnglish(), status
);
530 if (failure(status
, "new GregorianCalendar")) return;
532 if (gc2
!= *gc
|| !(gc2
== *gc
)) errln("FAIL: GregorianCalendar assignment/operator==/operator!= failed");
536 /* Code coverage for Calendar class. */
537 cal
= Calendar::createInstance(status
);
538 if (failure(status
, "Calendar::createInstance")) {
541 ((Calendar
*)cal
)->roll(UCAL_HOUR
, (int32_t)100, status
);
542 ((Calendar
*)cal
)->clear(UCAL_HOUR
);
543 #if !UCONFIG_NO_SERVICE
544 URegistryKey key
= cal
->registerFactory(NULL
, status
);
545 cal
->unregister(key
, status
);
550 status
= U_ZERO_ERROR
;
551 cal
= Calendar::createInstance(Locale("he_IL@calendar=hebrew"), status
);
552 if (failure(status
, "Calendar::createInstance")) {
555 cal
->roll(Calendar::MONTH
, (int32_t)100, status
);
558 StringEnumeration
*en
= Calendar::getKeywordValuesForLocale(NULL
, Locale::getDefault(),FALSE
, status
);
559 if (en
== NULL
|| U_FAILURE(status
)) {
560 dataerrln("FAIL: getKeywordValuesForLocale for Calendar. : %s", u_errorName(status
));
566 // -------------------------------------
569 * This test confirms the correct behavior of add when incrementing
570 * through subsequent days.
573 CalendarTest::TestRog()
575 UErrorCode status
= U_ZERO_ERROR
;
576 GregorianCalendar
* gc
= new GregorianCalendar(status
);
577 if (failure(status
, "new GregorianCalendar", TRUE
)) return;
578 int32_t year
= 1997, month
= UCAL_APRIL
, date
= 1;
579 gc
->set(year
, month
, date
);
580 gc
->set(UCAL_HOUR_OF_DAY
, 23);
581 gc
->set(UCAL_MINUTE
, 0);
582 gc
->set(UCAL_SECOND
, 0);
583 gc
->set(UCAL_MILLISECOND
, 0);
584 for (int32_t i
= 0; i
< 9; i
++, gc
->add(UCAL_DATE
, 1, status
)) {
585 if (U_FAILURE(status
)) { errln("Calendar::add failed"); return; }
586 if (gc
->get(UCAL_YEAR
, status
) != year
||
587 gc
->get(UCAL_MONTH
, status
) != month
||
588 gc
->get(UCAL_DATE
, status
) != (date
+ i
)) errln("FAIL: Date wrong");
589 if (U_FAILURE(status
)) { errln("Calendar::get failed"); return; }
594 // -------------------------------------
597 * Test the handling of the day of the week, checking for correctness and
598 * for correct minimum and maximum values.
601 CalendarTest::TestDOW943()
607 void CalendarTest::dowTest(UBool lenient
)
609 UErrorCode status
= U_ZERO_ERROR
;
610 GregorianCalendar
* cal
= new GregorianCalendar(status
);
611 if (failure(status
, "new GregorianCalendar", TRUE
)) return;
612 logln("cal - Aug 12, 1997\n");
613 cal
->set(1997, UCAL_AUGUST
, 12);
614 cal
->getTime(status
);
615 if (U_FAILURE(status
)) { errln("Calendar::getTime failed"); return; }
616 logln((lenient
?UnicodeString("LENIENT0: "):UnicodeString("nonlenient0: ")) + UnicodeString(calToStr(*cal
)));
617 cal
->setLenient(lenient
);
618 logln("cal - Dec 1, 1996\n");
619 cal
->set(1996, UCAL_DECEMBER
, 1);
620 logln((lenient
?UnicodeString("LENIENT: "):UnicodeString("nonlenient: ")) + UnicodeString(calToStr(*cal
)));
621 int32_t dow
= cal
->get(UCAL_DAY_OF_WEEK
, status
);
622 if (U_FAILURE(status
)) { errln("Calendar::get failed [%s]", u_errorName(status
)); return; }
623 int32_t min
= cal
->getMinimum(UCAL_DAY_OF_WEEK
);
624 int32_t max
= cal
->getMaximum(UCAL_DAY_OF_WEEK
);
626 dow
> max
) errln(UnicodeString("FAIL: Day of week ") + (int32_t)dow
+ " out of range");
627 if (dow
!= UCAL_SUNDAY
) errln("FAIL: Day of week should be SUNDAY[%d] not %d", UCAL_SUNDAY
, dow
);
628 if (min
!= UCAL_SUNDAY
||
629 max
!= UCAL_SATURDAY
) errln("FAIL: Min/max bad");
633 // -------------------------------------
636 * Confirm that cloned Calendar objects do not inadvertently share substructures.
639 CalendarTest::TestClonesUnique908()
641 UErrorCode status
= U_ZERO_ERROR
;
642 Calendar
*c
= Calendar::createInstance(status
);
643 if (failure(status
, "Calendar::createInstance", TRUE
)) return;
644 Calendar
*d
= (Calendar
*) c
->clone();
645 c
->set(UCAL_MILLISECOND
, 123);
646 d
->set(UCAL_MILLISECOND
, 456);
647 if (c
->get(UCAL_MILLISECOND
, status
) != 123 ||
648 d
->get(UCAL_MILLISECOND
, status
) != 456) {
649 errln("FAIL: Clones share fields");
651 if (U_FAILURE(status
)) { errln("Calendar::get failed"); return; }
656 // -------------------------------------
659 * Confirm that the Gregorian cutoff value works as advertised.
662 CalendarTest::TestGregorianChange768()
665 UErrorCode status
= U_ZERO_ERROR
;
667 GregorianCalendar
* c
= new GregorianCalendar(status
);
668 if (failure(status
, "new GregorianCalendar", TRUE
)) return;
669 logln(UnicodeString("With cutoff ") + dateToString(c
->getGregorianChange(), str
));
670 b
= c
->isLeapYear(1800);
671 logln(UnicodeString(" isLeapYear(1800) = ") + (b
? "true" : "false"));
672 logln(UnicodeString(" (should be FALSE)"));
673 if (b
) errln("FAIL");
674 c
->setGregorianChange(date(0, 0, 1), status
);
675 if (U_FAILURE(status
)) { errln("GregorianCalendar::setGregorianChange failed"); return; }
676 logln(UnicodeString("With cutoff ") + dateToString(c
->getGregorianChange(), str
));
677 b
= c
->isLeapYear(1800);
678 logln(UnicodeString(" isLeapYear(1800) = ") + (b
? "true" : "false"));
679 logln(UnicodeString(" (should be TRUE)"));
680 if (!b
) errln("FAIL");
684 // -------------------------------------
687 * Confirm the functioning of the field disambiguation algorithm.
690 CalendarTest::TestDisambiguation765()
692 UErrorCode status
= U_ZERO_ERROR
;
693 Calendar
*c
= Calendar::createInstance("en_US", status
);
694 if (failure(status
, "Calendar::createInstance", TRUE
)) return;
695 c
->setLenient(FALSE
);
697 c
->set(UCAL_YEAR
, 1997);
698 c
->set(UCAL_MONTH
, UCAL_JUNE
);
699 c
->set(UCAL_DATE
, 3);
700 verify765("1997 third day of June = ", c
, 1997, UCAL_JUNE
, 3);
702 c
->set(UCAL_YEAR
, 1997);
703 c
->set(UCAL_DAY_OF_WEEK
, UCAL_TUESDAY
);
704 c
->set(UCAL_MONTH
, UCAL_JUNE
);
705 c
->set(UCAL_DAY_OF_WEEK_IN_MONTH
, 1);
706 verify765("1997 first Tuesday in June = ", c
, 1997, UCAL_JUNE
, 3);
708 c
->set(UCAL_YEAR
, 1997);
709 c
->set(UCAL_DAY_OF_WEEK
, UCAL_TUESDAY
);
710 c
->set(UCAL_MONTH
, UCAL_JUNE
);
711 c
->set(UCAL_DAY_OF_WEEK_IN_MONTH
, - 1);
712 verify765("1997 last Tuesday in June = ", c
, 1997, UCAL_JUNE
, 24);
714 status
= U_ZERO_ERROR
;
716 c
->set(UCAL_YEAR
, 1997);
717 c
->set(UCAL_DAY_OF_WEEK
, UCAL_TUESDAY
);
718 c
->set(UCAL_MONTH
, UCAL_JUNE
);
719 c
->set(UCAL_DAY_OF_WEEK_IN_MONTH
, 0);
721 verify765("1997 zero-th Tuesday in June = ", status
);
724 c
->set(UCAL_YEAR
, 1997);
725 c
->set(UCAL_DAY_OF_WEEK
, UCAL_TUESDAY
);
726 c
->set(UCAL_MONTH
, UCAL_JUNE
);
727 c
->set(UCAL_WEEK_OF_MONTH
, 1);
728 verify765("1997 Tuesday in week 1 of June = ", c
, 1997, UCAL_JUNE
, 3);
730 c
->set(UCAL_YEAR
, 1997);
731 c
->set(UCAL_DAY_OF_WEEK
, UCAL_TUESDAY
);
732 c
->set(UCAL_MONTH
, UCAL_JUNE
);
733 c
->set(UCAL_WEEK_OF_MONTH
, 5);
734 verify765("1997 Tuesday in week 5 of June = ", c
, 1997, UCAL_JULY
, 1);
736 status
= U_ZERO_ERROR
;
738 c
->set(UCAL_YEAR
, 1997);
739 c
->set(UCAL_DAY_OF_WEEK
, UCAL_TUESDAY
);
740 c
->set(UCAL_MONTH
, UCAL_JUNE
);
741 c
->set(UCAL_WEEK_OF_MONTH
, 0);
742 c
->setMinimalDaysInFirstWeek(1);
744 verify765("1997 Tuesday in week 0 of June = ", status
);
746 /* Note: The following test used to expect YEAR 1997, WOY 1 to
747 * resolve to a date in Dec 1996; that is, to behave as if
748 * YEAR_WOY were 1997. With the addition of a new explicit
749 * YEAR_WOY field, YEAR_WOY must itself be set if that is what is
750 * desired. Using YEAR in combination with WOY is ambiguous, and
751 * results in the first WOY/DOW day of the year satisfying the
752 * given fields (there may be up to two such days). In this case,
753 * it propertly resolves to Tue Dec 30 1997, which has a WOY value
754 * of 1 (for YEAR_WOY 1998) and a DOW of Tuesday, and falls in the
755 * _calendar_ year 1997, as specified. - aliu */
757 c
->set(UCAL_YEAR_WOY
, 1997); // aliu
758 c
->set(UCAL_DAY_OF_WEEK
, UCAL_TUESDAY
);
759 c
->set(UCAL_WEEK_OF_YEAR
, 1);
760 verify765("1997 Tuesday in week 1 of yearWOY = ", c
, 1996, UCAL_DECEMBER
, 31);
761 c
->clear(); // - add test for YEAR
762 c
->setMinimalDaysInFirstWeek(1);
763 c
->set(UCAL_YEAR
, 1997);
764 c
->set(UCAL_DAY_OF_WEEK
, UCAL_TUESDAY
);
765 c
->set(UCAL_WEEK_OF_YEAR
, 1);
766 verify765("1997 Tuesday in week 1 of year = ", c
, 1997, UCAL_DECEMBER
, 30);
768 c
->set(UCAL_YEAR
, 1997);
769 c
->set(UCAL_DAY_OF_WEEK
, UCAL_TUESDAY
);
770 c
->set(UCAL_WEEK_OF_YEAR
, 10);
771 verify765("1997 Tuesday in week 10 of year = ", c
, 1997, UCAL_MARCH
, 4);
774 // {sfb} week 0 is no longer a valid week of year
776 c->set(Calendar::YEAR, 1997);
777 c->set(Calendar::DAY_OF_WEEK, Calendar::TUESDAY);
778 //c->set(Calendar::WEEK_OF_YEAR, 0);
779 c->set(Calendar::WEEK_OF_YEAR, 1);
780 verify765("1997 Tuesday in week 0 of year = ", c, 1996, Calendar::DECEMBER, 24);*/
783 //catch(IllegalArgumentException ex) {
784 // errln("FAIL: Exception seen:");
785 // ex.printStackTrace(log);
790 // -------------------------------------
793 CalendarTest::verify765(const UnicodeString
& msg
, Calendar
* c
, int32_t year
, int32_t month
, int32_t day
)
796 UErrorCode status
= U_ZERO_ERROR
;
797 int32_t y
= c
->get(UCAL_YEAR
, status
);
798 int32_t m
= c
->get(UCAL_MONTH
, status
);
799 int32_t d
= c
->get(UCAL_DATE
, status
);
803 if (U_FAILURE(status
)) { errln("FAIL: Calendar::get failed"); return; }
804 logln("PASS: " + msg
+ dateToString(c
->getTime(status
), str
));
805 if (U_FAILURE(status
)) { errln("Calendar::getTime failed"); return; }
808 errln("FAIL: " + msg
+ dateToString(c
->getTime(status
), str
) + "; expected " + (int32_t)year
+ "/" + (int32_t)(month
+ 1) + "/" + (int32_t)day
+
809 "; got " + (int32_t)y
+ "/" + (int32_t)(m
+ 1) + "/" + (int32_t)d
+ " for Locale: " + c
->getLocaleID(ULOC_ACTUAL_LOCALE
,status
));
810 if (U_FAILURE(status
)) { errln("Calendar::getTime failed"); return; }
814 // -------------------------------------
817 CalendarTest::verify765(const UnicodeString
& msg
/*, IllegalArgumentException e*/, UErrorCode status
)
819 if (status
!= U_ILLEGAL_ARGUMENT_ERROR
) errln("FAIL: No IllegalArgumentException for " + msg
);
820 else logln("PASS: " + msg
+ "IllegalArgument as expected");
823 // -------------------------------------
826 * Confirm that the offset between local time and GMT behaves as expected.
829 CalendarTest::TestGMTvsLocal4064654()
831 test4064654(1997, 1, 1, 12, 0, 0);
832 test4064654(1997, 4, 16, 18, 30, 0);
835 // -------------------------------------
838 CalendarTest::test4064654(int32_t yr
, int32_t mo
, int32_t dt
, int32_t hr
, int32_t mn
, int32_t sc
)
841 UErrorCode status
= U_ZERO_ERROR
;
843 Calendar
*gmtcal
= Calendar::createInstance(status
);
844 if (failure(status
, "Calendar::createInstance", TRUE
)) return;
845 gmtcal
->adoptTimeZone(TimeZone::createTimeZone("Africa/Casablanca"));
846 gmtcal
->set(yr
, mo
- 1, dt
, hr
, mn
, sc
);
847 gmtcal
->set(UCAL_MILLISECOND
, 0);
848 date
= gmtcal
->getTime(status
);
849 if (U_FAILURE(status
)) { errln("Calendar::getTime failed"); return; }
850 logln("date = " + dateToString(date
, str
));
851 Calendar
*cal
= Calendar::createInstance(status
);
852 if (U_FAILURE(status
)) { errln("Calendar::createInstance failed"); return; }
853 cal
->setTime(date
, status
);
854 if (U_FAILURE(status
)) { errln("Calendar::setTime failed"); return; }
855 int32_t offset
= cal
->getTimeZone().getOffset((uint8_t)cal
->get(UCAL_ERA
, status
),
856 cal
->get(UCAL_YEAR
, status
),
857 cal
->get(UCAL_MONTH
, status
),
858 cal
->get(UCAL_DATE
, status
),
859 (uint8_t)cal
->get(UCAL_DAY_OF_WEEK
, status
),
860 cal
->get(UCAL_MILLISECOND
, status
), status
);
861 if (U_FAILURE(status
)) { errln("Calendar::get failed"); return; }
862 logln("offset for " + dateToString(date
, str
) + "= " + (offset
/ 1000 / 60 / 60.0) + "hr");
863 int32_t utc
= ((cal
->get(UCAL_HOUR_OF_DAY
, status
) * 60 +
864 cal
->get(UCAL_MINUTE
, status
)) * 60 +
865 cal
->get(UCAL_SECOND
, status
)) * 1000 +
866 cal
->get(UCAL_MILLISECOND
, status
) - offset
;
867 if (U_FAILURE(status
)) { errln("Calendar::get failed"); return; }
868 int32_t expected
= ((hr
* 60 + mn
) * 60 + sc
) * 1000;
869 if (utc
!= expected
) errln(UnicodeString("FAIL: Discrepancy of ") + (utc
- expected
) +
870 " millis = " + ((utc
- expected
) / 1000 / 60 / 60.0) + " hr");
875 // -------------------------------------
878 * The operations of adding and setting should not exhibit pathological
879 * dependence on the order of operations. This test checks for this.
882 CalendarTest::TestAddSetOrder621()
884 UDate d
= date(97, 4, 14, 13, 23, 45);
885 UErrorCode status
= U_ZERO_ERROR
;
886 Calendar
*cal
= Calendar::createInstance(status
);
887 if (failure(status
, "Calendar::createInstance", TRUE
)) return;
889 cal
->setTime(d
, status
);
890 if (U_FAILURE(status
)) {
891 errln("Calendar::setTime failed");
895 cal
->add(UCAL_DATE
, - 5, status
);
896 if (U_FAILURE(status
)) {
897 errln("Calendar::add failed");
901 cal
->set(UCAL_HOUR_OF_DAY
, 0);
902 cal
->set(UCAL_MINUTE
, 0);
903 cal
->set(UCAL_SECOND
, 0);
905 dateToString(cal
->getTime(status
), s
);
906 if (U_FAILURE(status
)) {
907 errln("Calendar::getTime failed");
913 cal
= Calendar::createInstance(status
);
914 if (U_FAILURE(status
)) {
915 errln("Calendar::createInstance failed");
919 cal
->setTime(d
, status
);
920 if (U_FAILURE(status
)) {
921 errln("Calendar::setTime failed");
925 cal
->set(UCAL_HOUR_OF_DAY
, 0);
926 cal
->set(UCAL_MINUTE
, 0);
927 cal
->set(UCAL_SECOND
, 0);
928 cal
->add(UCAL_DATE
, - 5, status
);
929 if (U_FAILURE(status
)) {
930 errln("Calendar::add failed");
935 dateToString(cal
->getTime(status
), s2
);
936 if (U_FAILURE(status
)) {
937 errln("Calendar::getTime failed");
942 logln("Pass: " + s
+ " == " + s2
);
944 errln("FAIL: " + s
+ " != " + s2
);
948 // -------------------------------------
951 * Confirm that adding to various fields works.
954 CalendarTest::TestAdd520()
956 int32_t y
= 1997, m
= UCAL_FEBRUARY
, d
= 1;
957 UErrorCode status
= U_ZERO_ERROR
;
958 GregorianCalendar
*temp
= new GregorianCalendar(y
, m
, d
, status
);
959 if (failure(status
, "new GregorianCalendar", TRUE
)) return;
960 check520(temp
, y
, m
, d
);
961 temp
->add(UCAL_YEAR
, 1, status
);
962 if (U_FAILURE(status
)) { errln("Calendar::add failed"); return; }
964 check520(temp
, y
, m
, d
);
965 temp
->add(UCAL_MONTH
, 1, status
);
966 if (U_FAILURE(status
)) { errln("Calendar::add failed"); return; }
968 check520(temp
, y
, m
, d
);
969 temp
->add(UCAL_DATE
, 1, status
);
970 if (U_FAILURE(status
)) { errln("Calendar::add failed"); return; }
972 check520(temp
, y
, m
, d
);
973 temp
->add(UCAL_DATE
, 2, status
);
974 if (U_FAILURE(status
)) { errln("Calendar::add failed"); return; }
976 check520(temp
, y
, m
, d
);
977 temp
->add(UCAL_DATE
, 28, status
);
978 if (U_FAILURE(status
)) { errln("Calendar::add failed"); return; }
980 check520(temp
, y
, m
, d
);
984 // -------------------------------------
987 * Execute adding and rolling in GregorianCalendar extensively,
990 CalendarTest::TestAddRollExtensive()
992 int32_t maxlimit
= 40;
993 int32_t y
= 1997, m
= UCAL_FEBRUARY
, d
= 1, hr
= 1, min
= 1, sec
= 0, ms
= 0;
994 UErrorCode status
= U_ZERO_ERROR
;
995 GregorianCalendar
*temp
= new GregorianCalendar(y
, m
, d
, status
);
996 if (failure(status
, "new GregorianCalendar", TRUE
)) return;
998 temp
->set(UCAL_HOUR
, hr
);
999 temp
->set(UCAL_MINUTE
, min
);
1000 temp
->set(UCAL_SECOND
, sec
);
1001 temp
->set(UCAL_MILLISECOND
, ms
);
1002 temp
->setMinimalDaysInFirstWeek(1);
1004 UCalendarDateFields e
;
1006 logln("Testing GregorianCalendar add...");
1008 while (e
< UCAL_FIELD_COUNT
) {
1010 int32_t limit
= maxlimit
;
1011 status
= U_ZERO_ERROR
;
1012 for (i
= 0; i
< limit
; i
++) {
1013 temp
->add(e
, 1, status
);
1014 if (U_FAILURE(status
)) { limit
= i
; status
= U_ZERO_ERROR
; }
1016 for (i
= 0; i
< limit
; i
++) {
1017 temp
->add(e
, -1, status
);
1018 if (U_FAILURE(status
)) { errln("GregorianCalendar::add -1 failed"); return; }
1020 check520(temp
, y
, m
, d
, hr
, min
, sec
, ms
, e
);
1022 e
= (UCalendarDateFields
) ((int32_t) e
+ 1);
1025 logln("Testing GregorianCalendar roll...");
1027 while (e
< UCAL_FIELD_COUNT
) {
1029 int32_t limit
= maxlimit
;
1030 status
= U_ZERO_ERROR
;
1031 for (i
= 0; i
< limit
; i
++) {
1032 logln(calToStr(*temp
) + UnicodeString(" " ) + fieldName(e
) + UnicodeString("++") );
1033 temp
->roll(e
, 1, status
);
1034 if (U_FAILURE(status
)) {
1035 logln("caltest.cpp:%d e=%d, i=%d - roll(+) err %s\n", __LINE__
, (int) e
, (int) i
, u_errorName(status
));
1036 logln(calToStr(*temp
));
1037 limit
= i
; status
= U_ZERO_ERROR
;
1040 for (i
= 0; i
< limit
; i
++) {
1041 logln("caltest.cpp:%d e=%d, i=%d\n", __LINE__
, (int) e
, (int) i
);
1042 logln(calToStr(*temp
) + UnicodeString(" " ) + fieldName(e
) + UnicodeString("--") );
1043 temp
->roll(e
, -1, status
);
1044 if (U_FAILURE(status
)) { errln(UnicodeString("GregorianCalendar::roll ") + CalendarTest::fieldName(e
) + " count=" + UnicodeString('@'+i
) + " by -1 failed with " + u_errorName(status
) ); return; }
1046 check520(temp
, y
, m
, d
, hr
, min
, sec
, ms
, e
);
1048 e
= (UCalendarDateFields
) ((int32_t) e
+ 1);
1054 // -------------------------------------
1056 CalendarTest::check520(Calendar
* c
,
1057 int32_t y
, int32_t m
, int32_t d
,
1058 int32_t hr
, int32_t min
, int32_t sec
,
1059 int32_t ms
, UCalendarDateFields field
)
1062 UErrorCode status
= U_ZERO_ERROR
;
1063 if (c
->get(UCAL_YEAR
, status
) != y
||
1064 c
->get(UCAL_MONTH
, status
) != m
||
1065 c
->get(UCAL_DATE
, status
) != d
||
1066 c
->get(UCAL_HOUR
, status
) != hr
||
1067 c
->get(UCAL_MINUTE
, status
) != min
||
1068 c
->get(UCAL_SECOND
, status
) != sec
||
1069 c
->get(UCAL_MILLISECOND
, status
) != ms
) {
1070 errln(UnicodeString("U_FAILURE for field ") + (int32_t)field
+
1071 ": Expected y/m/d h:m:s:ms of " +
1072 y
+ "/" + (m
+ 1) + "/" + d
+ " " +
1073 hr
+ ":" + min
+ ":" + sec
+ ":" + ms
+
1074 "; got " + c
->get(UCAL_YEAR
, status
) +
1075 "/" + (c
->get(UCAL_MONTH
, status
) + 1) +
1076 "/" + c
->get(UCAL_DATE
, status
) +
1077 " " + c
->get(UCAL_HOUR
, status
) + ":" +
1078 c
->get(UCAL_MINUTE
, status
) + ":" +
1079 c
->get(UCAL_SECOND
, status
) + ":" +
1080 c
->get(UCAL_MILLISECOND
, status
)
1083 if (U_FAILURE(status
)) { errln("Calendar::get failed"); return; }
1086 logln(UnicodeString("Confirmed: ") + y
+ "/" +
1087 (m
+ 1) + "/" + d
+ " " +
1088 hr
+ ":" + min
+ ":" + sec
+ ":" + ms
);
1091 // -------------------------------------
1093 CalendarTest::check520(Calendar
* c
,
1094 int32_t y
, int32_t m
, int32_t d
)
1097 UErrorCode status
= U_ZERO_ERROR
;
1098 if (c
->get(UCAL_YEAR
, status
) != y
||
1099 c
->get(UCAL_MONTH
, status
) != m
||
1100 c
->get(UCAL_DATE
, status
) != d
) {
1101 errln(UnicodeString("FAILURE: Expected y/m/d of ") +
1102 y
+ "/" + (m
+ 1) + "/" + d
+ " " +
1103 "; got " + c
->get(UCAL_YEAR
, status
) +
1104 "/" + (c
->get(UCAL_MONTH
, status
) + 1) +
1105 "/" + c
->get(UCAL_DATE
, status
)
1108 if (U_FAILURE(status
)) { errln("Calendar::get failed"); return; }
1111 logln(UnicodeString("Confirmed: ") + y
+ "/" +
1115 // -------------------------------------
1118 * Test that setting of fields works. In particular, make sure that all instances
1119 * of GregorianCalendar don't share a static instance of the fields array.
1122 CalendarTest::TestFieldSet4781()
1125 UErrorCode status
= U_ZERO_ERROR
;
1126 GregorianCalendar
*g
= new GregorianCalendar(status
);
1127 if (failure(status
, "new GregorianCalendar", TRUE
)) return;
1128 GregorianCalendar
*g2
= new GregorianCalendar(status
);
1129 if (U_FAILURE(status
)) { errln("Couldn't create GregorianCalendar"); return; }
1130 g2
->set(UCAL_HOUR
, 12, status
);
1131 g2
->set(UCAL_MINUTE
, 0, status
);
1132 g2
->set(UCAL_SECOND
, 0, status
);
1133 if (U_FAILURE(status
)) { errln("Calendar::set failed"); return; }
1134 if (*g
== *g2
) logln("Same");
1135 else logln("Different");
1137 //catch(IllegalArgumentException e) {
1138 //errln("Unexpected exception seen: " + e);
1144 // -------------------------------------
1146 /* We don't support serialization on C++
1148 CalendarTest::TestSerialize337()
1150 Calendar cal = Calendar::getInstance();
1153 FileOutputStream f = new FileOutputStream(FILENAME);
1154 ObjectOutput s = new ObjectOutputStream(f);
1155 s.writeObject(PREFIX);
1157 s.writeObject(POSTFIX);
1159 FileInputStream in = new FileInputStream(FILENAME);
1160 ObjectInputStream t = new ObjectInputStream(in);
1161 UnicodeString& pre = (UnicodeString&) t.readObject();
1162 Calendar c = (Calendar) t.readObject();
1163 UnicodeString& post = (UnicodeString&) t.readObject();
1165 ok = pre.equals(PREFIX) &&
1166 post.equals(POSTFIX) &&
1168 File fl = new File(FILENAME);
1171 catch(IOException e) {
1172 errln("FAIL: Exception received:");
1173 e.printStackTrace(log);
1175 catch(ClassNotFoundException e) {
1176 errln("FAIL: Exception received:");
1177 e.printStackTrace(log);
1179 if (!ok) errln("Serialization of Calendar object failed.");
1182 UnicodeString& CalendarTest::PREFIX = "abc";
1184 UnicodeString& CalendarTest::POSTFIX = "def";
1186 UnicodeString& CalendarTest::FILENAME = "tmp337.bin";
1189 // -------------------------------------
1192 * Verify that the seconds of a Calendar can be zeroed out through the
1193 * expected sequence of operations.
1196 CalendarTest::TestSecondsZero121()
1198 UErrorCode status
= U_ZERO_ERROR
;
1199 Calendar
*cal
= new GregorianCalendar(status
);
1200 if (failure(status
, "new GregorianCalendar", TRUE
)) return;
1201 cal
->setTime(Calendar::getNow(), status
);
1202 if (U_FAILURE(status
)) { errln("Calendar::setTime failed"); return; }
1203 cal
->set(UCAL_SECOND
, 0);
1204 if (U_FAILURE(status
)) { errln("Calendar::set failed"); return; }
1205 UDate d
= cal
->getTime(status
);
1206 if (U_FAILURE(status
)) { errln("Calendar::getTime failed"); return; }
1209 if (s
.indexOf("DATE_FORMAT_FAILURE") >= 0) {
1210 dataerrln("Got: \"DATE_FORMAT_FAILURE\".");
1211 } else if (s
.indexOf(":00 ") < 0) {
1212 errln("Expected to see :00 in " + s
);
1217 // -------------------------------------
1220 * Verify that a specific sequence of adding and setting works as expected;
1221 * it should not vary depending on when and whether the get method is
1225 CalendarTest::TestAddSetGet0610()
1227 UnicodeString
EXPECTED_0610("1993/0/5", "");
1228 UErrorCode status
= U_ZERO_ERROR
;
1230 Calendar
*calendar
= new GregorianCalendar(status
);
1231 if (failure(status
, "new GregorianCalendar", TRUE
)) return;
1232 calendar
->set(1993, UCAL_JANUARY
, 4);
1233 logln("1A) " + value(calendar
));
1234 calendar
->add(UCAL_DATE
, 1, status
);
1235 if (U_FAILURE(status
)) { errln("Calendar::add failed"); return; }
1236 UnicodeString v
= value(calendar
);
1238 logln("--) 1993/0/5");
1239 if (!(v
== EXPECTED_0610
)) errln("Expected " + EXPECTED_0610
+ "; saw " + v
);
1243 Calendar
*calendar
= new GregorianCalendar(1993, UCAL_JANUARY
, 4, status
);
1244 if (U_FAILURE(status
)) { errln("Couldn't create GregorianCalendar"); return; }
1245 logln("2A) " + value(calendar
));
1246 calendar
->add(UCAL_DATE
, 1, status
);
1247 if (U_FAILURE(status
)) { errln("Calendar::add failed"); return; }
1248 UnicodeString v
= value(calendar
);
1250 logln("--) 1993/0/5");
1251 if (!(v
== EXPECTED_0610
)) errln("Expected " + EXPECTED_0610
+ "; saw " + v
);
1255 Calendar
*calendar
= new GregorianCalendar(1993, UCAL_JANUARY
, 4, status
);
1256 if (U_FAILURE(status
)) { errln("Couldn't create GregorianCalendar"); return; }
1257 logln("3A) " + value(calendar
));
1258 calendar
->getTime(status
);
1259 if (U_FAILURE(status
)) { errln("Calendar::getTime failed"); return; }
1260 calendar
->add(UCAL_DATE
, 1, status
);
1261 if (U_FAILURE(status
)) { errln("Calendar::add failed"); return; }
1262 UnicodeString v
= value(calendar
);
1264 logln("--) 1993/0/5");
1265 if (!(v
== EXPECTED_0610
)) errln("Expected " + EXPECTED_0610
+ "; saw " + v
);
1270 // -------------------------------------
1273 CalendarTest::value(Calendar
* calendar
)
1275 UErrorCode status
= U_ZERO_ERROR
;
1276 return UnicodeString("") + (int32_t)calendar
->get(UCAL_YEAR
, status
) +
1277 "/" + (int32_t)calendar
->get(UCAL_MONTH
, status
) +
1278 "/" + (int32_t)calendar
->get(UCAL_DATE
, status
) +
1279 (U_FAILURE(status
) ? " FAIL: Calendar::get failed" : "");
1283 // -------------------------------------
1286 * Verify that various fields on a known date are set correctly.
1289 CalendarTest::TestFields060()
1291 UErrorCode status
= U_ZERO_ERROR
;
1292 int32_t year
= 1997;
1293 int32_t month
= UCAL_OCTOBER
;
1295 GregorianCalendar
*calendar
= 0;
1296 calendar
= new GregorianCalendar(year
, month
, dDate
, status
);
1297 if (failure(status
, "new GregorianCalendar", TRUE
)) return;
1298 for (int32_t i
= 0; i
< EXPECTED_FIELDS_length
;) {
1299 UCalendarDateFields field
= (UCalendarDateFields
)EXPECTED_FIELDS
[i
++];
1300 int32_t expected
= EXPECTED_FIELDS
[i
++];
1301 if (calendar
->get(field
, status
) != expected
) {
1302 errln(UnicodeString("Expected field ") + (int32_t)field
+ " to have value " + (int32_t)expected
+
1303 "; received " + (int32_t)calendar
->get(field
, status
) + " instead");
1304 if (U_FAILURE(status
)) { errln("Calendar::get failed"); return; }
1310 int32_t CalendarTest::EXPECTED_FIELDS
[] = {
1312 UCAL_MONTH
, UCAL_OCTOBER
,
1314 UCAL_DAY_OF_WEEK
, UCAL_WEDNESDAY
,
1315 UCAL_DAY_OF_WEEK_IN_MONTH
, 4,
1316 UCAL_DAY_OF_YEAR
, 295
1319 const int32_t CalendarTest::EXPECTED_FIELDS_length
= (int32_t)(sizeof(CalendarTest::EXPECTED_FIELDS
) /
1320 sizeof(CalendarTest::EXPECTED_FIELDS
[0]));
1322 // -------------------------------------
1325 * Verify that various fields on a known date are set correctly. In this
1326 * case, the start of the epoch (January 1 1970).
1329 CalendarTest::TestEpochStartFields()
1331 UErrorCode status
= U_ZERO_ERROR
;
1332 TimeZone
*z
= TimeZone::createDefault();
1333 Calendar
*c
= Calendar::createInstance(status
);
1334 if (failure(status
, "Calendar::createInstance", TRUE
)) return;
1335 UDate d
= - z
->getRawOffset();
1336 GregorianCalendar
*gc
= new GregorianCalendar(status
);
1337 if (U_FAILURE(status
)) { errln("Couldn't create GregorianCalendar"); return; }
1338 gc
->setTimeZone(*z
);
1339 gc
->setTime(d
, status
);
1340 if (U_FAILURE(status
)) { errln("Calendar::setTime failed"); return; }
1341 UBool idt
= gc
->inDaylightTime(status
);
1342 if (U_FAILURE(status
)) { errln("GregorianCalendar::inDaylightTime failed"); return; }
1345 logln("Warning: Skipping test because " + dateToString(d
, str
) + " is in DST.");
1348 c
->setTime(d
, status
);
1349 if (U_FAILURE(status
)) { errln("Calendar::setTime failed"); return; }
1350 for (int32_t i
= 0; i
< UCAL_ZONE_OFFSET
;++i
) {
1351 if (c
->get((UCalendarDateFields
)i
, status
) != EPOCH_FIELDS
[i
])
1352 dataerrln(UnicodeString("Expected field ") + i
+ " to have value " + EPOCH_FIELDS
[i
] +
1353 "; saw " + c
->get((UCalendarDateFields
)i
, status
) + " instead");
1354 if (U_FAILURE(status
)) { errln("Calendar::get failed"); return; }
1356 if (c
->get(UCAL_ZONE_OFFSET
, status
) != z
->getRawOffset())
1358 errln(UnicodeString("Expected field ZONE_OFFSET to have value ") + z
->getRawOffset() +
1359 "; saw " + c
->get(UCAL_ZONE_OFFSET
, status
) + " instead");
1360 if (U_FAILURE(status
)) { errln("Calendar::get failed"); return; }
1362 if (c
->get(UCAL_DST_OFFSET
, status
) != 0)
1364 errln(UnicodeString("Expected field DST_OFFSET to have value 0") +
1365 "; saw " + c
->get(UCAL_DST_OFFSET
, status
) + " instead");
1366 if (U_FAILURE(status
)) { errln("Calendar::get failed"); return; }
1374 int32_t CalendarTest::EPOCH_FIELDS
[] = {
1375 1, 1970, 0, 1, 1, 1, 1, 5, 1, 0, 0, 0, 0, 0, 0, - 28800000, 0
1378 // -------------------------------------
1381 * Test that the days of the week progress properly when add is called repeatedly
1382 * for increments of 24 days.
1385 CalendarTest::TestDOWProgression()
1387 UErrorCode status
= U_ZERO_ERROR
;
1388 Calendar
*cal
= new GregorianCalendar(1972, UCAL_OCTOBER
, 26, status
);
1389 if (failure(status
, "new GregorianCalendar", TRUE
)) return;
1390 marchByDelta(cal
, 24);
1394 // -------------------------------------
1397 CalendarTest::TestDOW_LOCALandYEAR_WOY()
1399 /* Note: I've commented out the loop_addroll tests for YEAR and
1400 * YEAR_WOY below because these two fields should NOT behave
1401 * identically when adding. YEAR should keep the month/dom
1402 * invariant. YEAR_WOY should keep the woy/dow invariant. I've
1403 * added a new test that checks for this in place of the old call
1404 * to loop_addroll. - aliu */
1405 UErrorCode status
= U_ZERO_ERROR
;
1407 Calendar
*cal
=Calendar::createInstance(Locale::getGermany(), status
);
1408 if (failure(status
, "Calendar::createInstance", TRUE
)) return;
1409 SimpleDateFormat
*sdf
=new SimpleDateFormat(UnicodeString("YYYY'-W'ww-ee"), Locale::getGermany(), status
);
1410 if (U_FAILURE(status
)) { dataerrln("Couldn't create SimpleDateFormat - %s", u_errorName(status
)); return; }
1412 // ICU no longer use localized date-time pattern characters by default.
1413 // So we set pattern chars using 'J' instead of 'Y'.
1414 DateFormatSymbols
*dfs
= new DateFormatSymbols(Locale::getGermany(), status
);
1415 dfs
->setLocalPatternChars(UnicodeString("GyMdkHmsSEDFwWahKzJeugAZvcLQq"));
1416 sdf
->adoptDateFormatSymbols(dfs
);
1417 sdf
->applyLocalizedPattern(UnicodeString("JJJJ'-W'ww-ee"), status
);
1418 if (U_FAILURE(status
)) { errln("Couldn't apply localized pattern"); return; }
1421 cal
->set(1997, UCAL_DECEMBER
, 25);
1422 doYEAR_WOYLoop(cal
, sdf
, times
, status
);
1423 //loop_addroll(cal, /*sdf,*/ times, UCAL_YEAR_WOY, UCAL_YEAR, status);
1424 yearAddTest(*cal
, status
); // aliu
1425 loop_addroll(cal
, /*sdf,*/ times
, UCAL_DOW_LOCAL
, UCAL_DAY_OF_WEEK
, status
);
1426 if (U_FAILURE(status
)) { errln("Error in parse/calculate test for 1997"); return; }
1429 cal
->set(1998, UCAL_DECEMBER
, 25);
1430 doYEAR_WOYLoop(cal
, sdf
, times
, status
);
1431 //loop_addroll(cal, /*sdf,*/ times, UCAL_YEAR_WOY, UCAL_YEAR, status);
1432 yearAddTest(*cal
, status
); // aliu
1433 loop_addroll(cal
, /*sdf,*/ times
, UCAL_DOW_LOCAL
, UCAL_DAY_OF_WEEK
, status
);
1434 if (U_FAILURE(status
)) { errln("Error in parse/calculate test for 1998"); return; }
1437 cal
->set(1582, UCAL_OCTOBER
, 1);
1438 doYEAR_WOYLoop(cal
, sdf
, times
, status
);
1439 //loop_addroll(cal, /*sdf,*/ times, Calendar::YEAR_WOY, Calendar::YEAR, status);
1440 yearAddTest(*cal
, status
); // aliu
1441 loop_addroll(cal
, /*sdf,*/ times
, UCAL_DOW_LOCAL
, UCAL_DAY_OF_WEEK
, status
);
1442 if (U_FAILURE(status
)) { errln("Error in parse/calculate test for 1582"); return; }
1450 * Confirm that adding a YEAR and adding a YEAR_WOY work properly for
1451 * the given Calendar at its current setting.
1453 void CalendarTest::yearAddTest(Calendar
& cal
, UErrorCode
& status
) {
1455 * When adding the YEAR, the month and day should remain constant.
1456 * When adding the YEAR_WOY, the WOY and DOW should remain constant. - aliu
1458 * Wed Jan 14 1998 / 1998-W03-03 Add(YEAR_WOY, 1) -> Wed Jan 20 1999 / 1999-W03-03
1459 * Add(YEAR, 1) -> Thu Jan 14 1999 / 1999-W02-04
1460 * Thu Jan 14 1999 / 1999-W02-04 Add(YEAR_WOY, 1) -> Thu Jan 13 2000 / 2000-W02-04
1461 * Add(YEAR, 1) -> Fri Jan 14 2000 / 2000-W02-05
1462 * Sun Oct 31 1582 / 1582-W42-07 Add(YEAR_WOY, 1) -> Sun Oct 23 1583 / 1583-W42-07
1463 * Add(YEAR, 1) -> Mon Oct 31 1583 / 1583-W44-01
1465 int32_t y
= cal
.get(UCAL_YEAR
, status
);
1466 int32_t mon
= cal
.get(UCAL_MONTH
, status
);
1467 int32_t day
= cal
.get(UCAL_DATE
, status
);
1468 int32_t ywy
= cal
.get(UCAL_YEAR_WOY
, status
);
1469 int32_t woy
= cal
.get(UCAL_WEEK_OF_YEAR
, status
);
1470 int32_t dow
= cal
.get(UCAL_DOW_LOCAL
, status
);
1471 UDate t
= cal
.getTime(status
);
1473 if(U_FAILURE(status
)){
1474 errln(UnicodeString("Failed to create Calendar for locale. Error: ") + UnicodeString(u_errorName(status
)));
1477 UnicodeString str
, str2
;
1478 SimpleDateFormat
fmt(UnicodeString("EEE MMM dd yyyy / YYYY'-W'ww-ee"), status
);
1479 fmt
.setCalendar(cal
);
1481 fmt
.format(t
, str
.remove());
1482 str
+= ".add(YEAR, 1) =>";
1483 cal
.add(UCAL_YEAR
, 1, status
);
1484 int32_t y2
= cal
.get(UCAL_YEAR
, status
);
1485 int32_t mon2
= cal
.get(UCAL_MONTH
, status
);
1486 int32_t day2
= cal
.get(UCAL_DATE
, status
);
1487 fmt
.format(cal
.getTime(status
), str
);
1488 if (y2
!= (y
+1) || mon2
!= mon
|| day2
!= day
) {
1489 str
+= (UnicodeString
)", expected year " +
1490 (y
+1) + ", month " + (mon
+1) + ", day " + day
;
1491 errln((UnicodeString
)"FAIL: " + str
);
1492 logln( UnicodeString(" -> ") + CalendarTest::calToStr(cal
) );
1497 fmt
.format(t
, str
.remove());
1498 str
+= ".add(YEAR_WOY, 1)=>";
1499 cal
.setTime(t
, status
);
1500 logln( UnicodeString(" <- ") + CalendarTest::calToStr(cal
) );
1501 cal
.add(UCAL_YEAR_WOY
, 1, status
);
1502 int32_t ywy2
= cal
.get(UCAL_YEAR_WOY
, status
);
1503 int32_t woy2
= cal
.get(UCAL_WEEK_OF_YEAR
, status
);
1504 int32_t dow2
= cal
.get(UCAL_DOW_LOCAL
, status
);
1505 fmt
.format(cal
.getTime(status
), str
);
1506 if (ywy2
!= (ywy
+1) || woy2
!= woy
|| dow2
!= dow
) {
1507 str
+= (UnicodeString
)", expected yearWOY " +
1508 (ywy
+1) + ", woy " + woy
+ ", dowLocal " + dow
;
1509 errln((UnicodeString
)"FAIL: " + str
);
1510 logln( UnicodeString(" -> ") + CalendarTest::calToStr(cal
) );
1516 // -------------------------------------
1518 void CalendarTest::loop_addroll(Calendar
*cal
, /*SimpleDateFormat *sdf,*/ int times
, UCalendarDateFields field
, UCalendarDateFields field2
, UErrorCode
& errorCode
) {
1520 SimpleDateFormat
fmt(UnicodeString("EEE MMM dd yyyy / YYYY'-W'ww-ee"), errorCode
);
1521 fmt
.setCalendar(*cal
);
1524 for(i
= 0; i
<times
; i
++) {
1525 calclone
= cal
->clone();
1526 UDate start
= cal
->getTime(errorCode
);
1527 cal
->add(field
,1,errorCode
);
1528 if (U_FAILURE(errorCode
)) { errln("Error in add"); delete calclone
; return; }
1529 calclone
->add(field2
,1,errorCode
);
1530 if (U_FAILURE(errorCode
)) { errln("Error in add"); delete calclone
; return; }
1531 if(cal
->getTime(errorCode
) != calclone
->getTime(errorCode
)) {
1532 UnicodeString
str("FAIL: Results of add differ. "), str2
;
1533 str
+= fmt
.format(start
, str2
) + " ";
1534 str
+= UnicodeString("Add(") + fieldName(field
) + ", 1) -> " +
1535 fmt
.format(cal
->getTime(errorCode
), str2
.remove()) + "; ";
1536 str
+= UnicodeString("Add(") + fieldName(field2
) + ", 1) -> " +
1537 fmt
.format(calclone
->getTime(errorCode
), str2
.remove());
1545 for(i
= 0; i
<times
; i
++) {
1546 calclone
= cal
->clone();
1547 cal
->roll(field
,(int32_t)1,errorCode
);
1548 if (U_FAILURE(errorCode
)) { errln("Error in roll"); delete calclone
; return; }
1549 calclone
->roll(field2
,(int32_t)1,errorCode
);
1550 if (U_FAILURE(errorCode
)) { errln("Error in roll"); delete calclone
; return; }
1551 if(cal
->getTime(errorCode
) != calclone
->getTime(errorCode
)) {
1553 errln("Results of roll differ!");
1560 // -------------------------------------
1563 CalendarTest::doYEAR_WOYLoop(Calendar
*cal
, SimpleDateFormat
*sdf
,
1564 int32_t times
, UErrorCode
& errorCode
) {
1567 UDate tst
, original
;
1568 Calendar
*tstres
= new GregorianCalendar(Locale::getGermany(), errorCode
);
1569 for(int i
=0; i
<times
; ++i
) {
1570 sdf
->format(Formattable(cal
->getTime(errorCode
),Formattable::kIsDate
), us
, errorCode
);
1571 //logln("expected: "+us);
1572 if (U_FAILURE(errorCode
)) { errln("Format error"); return; }
1573 tst
=sdf
->parse(us
,errorCode
);
1574 if (U_FAILURE(errorCode
)) { errln("Parse error"); return; }
1576 tstres
->setTime(tst
, errorCode
);
1577 //logln((UnicodeString)"Parsed week of year is "+tstres->get(UCAL_WEEK_OF_YEAR, errorCode));
1578 if (U_FAILURE(errorCode
)) { errln("Set time error"); return; }
1579 original
= cal
->getTime(errorCode
);
1581 sdf
->format(Formattable(tst
,Formattable::kIsDate
), us
, errorCode
);
1582 //logln("got: "+us);
1583 if (U_FAILURE(errorCode
)) { errln("Get time error"); return; }
1586 sdf
->format(Formattable(original
, Formattable::kIsDate
), us
, errorCode
);
1587 errln("FAIL: Parsed time doesn't match with regular");
1588 logln("expected "+us
+ " " + calToStr(*cal
));
1590 sdf
->format(Formattable(tst
, Formattable::kIsDate
), us
, errorCode
);
1591 logln("got "+us
+ " " + calToStr(*tstres
));
1594 tstres
->set(UCAL_YEAR_WOY
, cal
->get(UCAL_YEAR_WOY
, errorCode
));
1595 tstres
->set(UCAL_WEEK_OF_YEAR
, cal
->get(UCAL_WEEK_OF_YEAR
, errorCode
));
1596 tstres
->set(UCAL_DOW_LOCAL
, cal
->get(UCAL_DOW_LOCAL
, errorCode
));
1597 if(cal
->get(UCAL_YEAR
, errorCode
) != tstres
->get(UCAL_YEAR
, errorCode
)) {
1598 errln("FAIL: Different Year!");
1599 logln((UnicodeString
)"Expected "+cal
->get(UCAL_YEAR
, errorCode
));
1600 logln((UnicodeString
)"Got "+tstres
->get(UCAL_YEAR
, errorCode
));
1603 if(cal
->get(UCAL_DAY_OF_YEAR
, errorCode
) != tstres
->get(UCAL_DAY_OF_YEAR
, errorCode
)) {
1604 errln("FAIL: Different Day Of Year!");
1605 logln((UnicodeString
)"Expected "+cal
->get(UCAL_DAY_OF_YEAR
, errorCode
));
1606 logln((UnicodeString
)"Got "+tstres
->get(UCAL_DAY_OF_YEAR
, errorCode
));
1609 //logln(calToStr(*cal));
1610 cal
->add(UCAL_DATE
, 1, errorCode
);
1611 if (U_FAILURE(errorCode
)) { errln("Add error"); return; }
1616 // -------------------------------------
1619 CalendarTest::marchByDelta(Calendar
* cal
, int32_t delta
)
1621 UErrorCode status
= U_ZERO_ERROR
;
1622 Calendar
*cur
= (Calendar
*) cal
->clone();
1623 int32_t initialDOW
= cur
->get(UCAL_DAY_OF_WEEK
, status
);
1624 if (U_FAILURE(status
)) { errln("Calendar::get failed"); return; }
1625 int32_t DOW
, newDOW
= initialDOW
;
1629 logln(UnicodeString("DOW = ") + DOW
+ " " + dateToString(cur
->getTime(status
), str
));
1630 if (U_FAILURE(status
)) { errln("Calendar::getTime failed"); return; }
1631 cur
->add(UCAL_DAY_OF_WEEK
, delta
, status
);
1632 if (U_FAILURE(status
)) { errln("Calendar::add failed"); return; }
1633 newDOW
= cur
->get(UCAL_DAY_OF_WEEK
, status
);
1634 if (U_FAILURE(status
)) { errln("Calendar::get failed"); return; }
1635 int32_t expectedDOW
= 1 + (DOW
+ delta
- 1) % 7;
1636 if (newDOW
!= expectedDOW
) {
1637 errln(UnicodeString("Day of week should be ") + expectedDOW
+ " instead of " + newDOW
+
1638 " on " + dateToString(cur
->getTime(status
), str
));
1639 if (U_FAILURE(status
)) { errln("Calendar::getTime failed"); return; }
1643 while (newDOW
!= initialDOW
);
1647 #define CHECK(status, msg) \
1648 if (U_FAILURE(status)) { \
1649 errcheckln(status, msg); \
1653 void CalendarTest::TestWOY(void) {
1655 FDW = Mon, MDFW = 4:
1656 Sun Dec 26 1999, WOY 51
1657 Mon Dec 27 1999, WOY 52
1658 Tue Dec 28 1999, WOY 52
1659 Wed Dec 29 1999, WOY 52
1660 Thu Dec 30 1999, WOY 52
1661 Fri Dec 31 1999, WOY 52
1662 Sat Jan 01 2000, WOY 52 ***
1663 Sun Jan 02 2000, WOY 52 ***
1664 Mon Jan 03 2000, WOY 1
1665 Tue Jan 04 2000, WOY 1
1666 Wed Jan 05 2000, WOY 1
1667 Thu Jan 06 2000, WOY 1
1668 Fri Jan 07 2000, WOY 1
1669 Sat Jan 08 2000, WOY 1
1670 Sun Jan 09 2000, WOY 1
1671 Mon Jan 10 2000, WOY 2
1673 FDW = Mon, MDFW = 2:
1674 Sun Dec 26 1999, WOY 52
1675 Mon Dec 27 1999, WOY 1 ***
1676 Tue Dec 28 1999, WOY 1 ***
1677 Wed Dec 29 1999, WOY 1 ***
1678 Thu Dec 30 1999, WOY 1 ***
1679 Fri Dec 31 1999, WOY 1 ***
1680 Sat Jan 01 2000, WOY 1
1681 Sun Jan 02 2000, WOY 1
1682 Mon Jan 03 2000, WOY 2
1683 Tue Jan 04 2000, WOY 2
1684 Wed Jan 05 2000, WOY 2
1685 Thu Jan 06 2000, WOY 2
1686 Fri Jan 07 2000, WOY 2
1687 Sat Jan 08 2000, WOY 2
1688 Sun Jan 09 2000, WOY 2
1689 Mon Jan 10 2000, WOY 3
1693 UErrorCode status
= U_ZERO_ERROR
;
1696 GregorianCalendar
cal(status
);
1697 SimpleDateFormat
fmt(UnicodeString("EEE MMM dd yyyy', WOY' w"), status
);
1698 if (failure(status
, "Cannot construct calendar/format", TRUE
)) return;
1700 UCalendarDaysOfWeek fdw
= (UCalendarDaysOfWeek
) 0;
1702 //for (int8_t pass=2; pass<=2; ++pass) {
1703 for (int8_t pass
=1; pass
<=2; ++pass
) {
1707 cal
.setFirstDayOfWeek(fdw
);
1708 cal
.setMinimalDaysInFirstWeek(4);
1709 fmt
.adoptCalendar(cal
.clone());
1713 cal
.setFirstDayOfWeek(fdw
);
1714 cal
.setMinimalDaysInFirstWeek(2);
1715 fmt
.adoptCalendar(cal
.clone());
1719 //for (i=2; i<=6; ++i) {
1720 for (i
=0; i
<16; ++i
) {
1722 int32_t t_y
, t_woy
, t_dow
;
1724 cal
.set(1999, UCAL_DECEMBER
, 26 + i
);
1725 fmt
.format(t
= cal
.getTime(status
), str
.remove());
1726 CHECK(status
, "Fail: getTime failed");
1727 logln(UnicodeString("* ") + str
);
1728 int32_t dow
= cal
.get(UCAL_DAY_OF_WEEK
, status
);
1729 int32_t woy
= cal
.get(UCAL_WEEK_OF_YEAR
, status
);
1730 int32_t year
= cal
.get(UCAL_YEAR
, status
);
1731 int32_t mon
= cal
.get(UCAL_MONTH
, status
);
1732 logln(calToStr(cal
));
1733 CHECK(status
, "Fail: get failed");
1734 int32_t dowLocal
= dow
- fdw
;
1735 if (dowLocal
< 0) dowLocal
+= 7;
1737 int32_t yearWoy
= year
;
1738 if (mon
== UCAL_JANUARY
) {
1739 if (woy
>= 52) --yearWoy
;
1741 if (woy
== 1) ++yearWoy
;
1744 // Basic fields->time check y/woy/dow
1745 // Since Y/WOY is ambiguous, we do a check of the fields,
1746 // not of the specific time.
1748 cal
.set(UCAL_YEAR
, year
);
1749 cal
.set(UCAL_WEEK_OF_YEAR
, woy
);
1750 cal
.set(UCAL_DAY_OF_WEEK
, dow
);
1751 t_y
= cal
.get(UCAL_YEAR
, status
);
1752 t_woy
= cal
.get(UCAL_WEEK_OF_YEAR
, status
);
1753 t_dow
= cal
.get(UCAL_DAY_OF_WEEK
, status
);
1754 CHECK(status
, "Fail: get failed");
1755 if (t_y
!= year
|| t_woy
!= woy
|| t_dow
!= dow
) {
1756 str
= "Fail: y/woy/dow fields->time => ";
1757 fmt
.format(cal
.getTime(status
), str
);
1759 logln(calToStr(cal
));
1760 logln("[get!=set] Y%d!=%d || woy%d!=%d || dow%d!=%d\n",
1761 t_y
, year
, t_woy
, woy
, t_dow
, dow
);
1763 logln("y/woy/dow fields->time OK");
1766 // Basic fields->time check y/woy/dow_local
1767 // Since Y/WOY is ambiguous, we do a check of the fields,
1768 // not of the specific time.
1770 cal
.set(UCAL_YEAR
, year
);
1771 cal
.set(UCAL_WEEK_OF_YEAR
, woy
);
1772 cal
.set(UCAL_DOW_LOCAL
, dowLocal
);
1773 t_y
= cal
.get(UCAL_YEAR
, status
);
1774 t_woy
= cal
.get(UCAL_WEEK_OF_YEAR
, status
);
1775 t_dow
= cal
.get(UCAL_DOW_LOCAL
, status
);
1776 CHECK(status
, "Fail: get failed");
1777 if (t_y
!= year
|| t_woy
!= woy
|| t_dow
!= dowLocal
) {
1778 str
= "Fail: y/woy/dow_local fields->time => ";
1779 fmt
.format(cal
.getTime(status
), str
);
1783 // Basic fields->time check y_woy/woy/dow
1785 cal
.set(UCAL_YEAR_WOY
, yearWoy
);
1786 cal
.set(UCAL_WEEK_OF_YEAR
, woy
);
1787 cal
.set(UCAL_DAY_OF_WEEK
, dow
);
1788 t2
= cal
.getTime(status
);
1789 CHECK(status
, "Fail: getTime failed");
1791 str
= "Fail: y_woy/woy/dow fields->time => ";
1792 fmt
.format(t2
, str
);
1794 logln(calToStr(cal
));
1795 logln("%.f != %.f\n", t
, t2
);
1797 logln("y_woy/woy/dow OK");
1800 // Basic fields->time check y_woy/woy/dow_local
1802 cal
.set(UCAL_YEAR_WOY
, yearWoy
);
1803 cal
.set(UCAL_WEEK_OF_YEAR
, woy
);
1804 cal
.set(UCAL_DOW_LOCAL
, dowLocal
);
1805 t2
= cal
.getTime(status
);
1806 CHECK(status
, "Fail: getTime failed");
1808 str
= "Fail: y_woy/woy/dow_local fields->time => ";
1809 fmt
.format(t2
, str
);
1813 logln("Testing DOW_LOCAL.. dow%d\n", dow
);
1814 // Make sure DOW_LOCAL disambiguates over DOW
1815 int32_t wrongDow
= dow
- 3;
1816 if (wrongDow
< 1) wrongDow
+= 7;
1817 cal
.setTime(t
, status
);
1818 cal
.set(UCAL_DAY_OF_WEEK
, wrongDow
);
1819 cal
.set(UCAL_DOW_LOCAL
, dowLocal
);
1820 t2
= cal
.getTime(status
);
1821 CHECK(status
, "Fail: set/getTime failed");
1823 str
= "Fail: DOW_LOCAL fields->time => ";
1824 fmt
.format(t2
, str
);
1826 logln(calToStr(cal
));
1827 logln("%.f : DOW%d, DOW_LOCAL%d -> %.f\n",
1828 t
, wrongDow
, dowLocal
, t2
);
1831 // Make sure DOW disambiguates over DOW_LOCAL
1832 int32_t wrongDowLocal
= dowLocal
- 3;
1833 if (wrongDowLocal
< 1) wrongDowLocal
+= 7;
1834 cal
.setTime(t
, status
);
1835 cal
.set(UCAL_DOW_LOCAL
, wrongDowLocal
);
1836 cal
.set(UCAL_DAY_OF_WEEK
, dow
);
1837 t2
= cal
.getTime(status
);
1838 CHECK(status
, "Fail: set/getTime failed");
1840 str
= "Fail: DOW fields->time => ";
1841 fmt
.format(t2
, str
);
1845 // Make sure YEAR_WOY disambiguates over YEAR
1846 cal
.setTime(t
, status
);
1847 cal
.set(UCAL_YEAR
, year
- 2);
1848 cal
.set(UCAL_YEAR_WOY
, yearWoy
);
1849 t2
= cal
.getTime(status
);
1850 CHECK(status
, "Fail: set/getTime failed");
1852 str
= "Fail: YEAR_WOY fields->time => ";
1853 fmt
.format(t2
, str
);
1857 // Make sure YEAR disambiguates over YEAR_WOY
1858 cal
.setTime(t
, status
);
1859 cal
.set(UCAL_YEAR_WOY
, yearWoy
- 2);
1860 cal
.set(UCAL_YEAR
, year
);
1861 t2
= cal
.getTime(status
);
1862 CHECK(status
, "Fail: set/getTime failed");
1864 str
= "Fail: YEAR fields->time => ";
1865 fmt
.format(t2
, str
);
1872 FDW = Mon, MDFW = 4:
1873 Sun Dec 26 1999, WOY 51
1874 Mon Dec 27 1999, WOY 52
1875 Tue Dec 28 1999, WOY 52
1876 Wed Dec 29 1999, WOY 52
1877 Thu Dec 30 1999, WOY 52
1878 Fri Dec 31 1999, WOY 52
1879 Sat Jan 01 2000, WOY 52
1880 Sun Jan 02 2000, WOY 52
1883 // Roll the DOW_LOCAL within week 52
1884 for (i
=27; i
<=33; ++i
) {
1886 for (amount
=-7; amount
<=7; ++amount
) {
1888 cal
.set(1999, UCAL_DECEMBER
, i
);
1890 fmt
.format(cal
.getTime(status
), str
);
1891 CHECK(status
, "Fail: getTime failed");
1892 str
+= UnicodeString(", ") + amount
+ ") = ";
1894 cal
.roll(UCAL_DOW_LOCAL
, amount
, status
);
1895 CHECK(status
, "Fail: roll failed");
1897 t
= cal
.getTime(status
);
1898 int32_t newDom
= i
+ amount
;
1899 while (newDom
< 27) newDom
+= 7;
1900 while (newDom
> 33) newDom
-= 7;
1901 cal
.set(1999, UCAL_DECEMBER
, newDom
);
1902 t2
= cal
.getTime(status
);
1903 CHECK(status
, "Fail: getTime failed");
1907 str
.append(", exp ");
1908 fmt
.format(t2
, str
);
1917 void CalendarTest::TestYWOY()
1920 UErrorCode status
= U_ZERO_ERROR
;
1922 GregorianCalendar
cal(status
);
1923 if (failure(status
, "construct GregorianCalendar", TRUE
)) return;
1925 cal
.setFirstDayOfWeek(UCAL_SUNDAY
);
1926 cal
.setMinimalDaysInFirstWeek(1);
1928 logln("Setting: ywoy=2004, woy=1, dow=MONDAY");
1930 cal
.set(UCAL_YEAR_WOY
,2004);
1931 cal
.set(UCAL_WEEK_OF_YEAR
,1);
1932 cal
.set(UCAL_DAY_OF_WEEK
, UCAL_MONDAY
);
1934 logln(calToStr(cal
));
1935 if(cal
.get(UCAL_YEAR
, status
) != 2003) {
1936 errln("year not 2003");
1939 logln("+ setting DOW to THURSDAY");
1941 cal
.set(UCAL_YEAR_WOY
,2004);
1942 cal
.set(UCAL_WEEK_OF_YEAR
,1);
1943 cal
.set(UCAL_DAY_OF_WEEK
, UCAL_THURSDAY
);
1945 logln(calToStr(cal
));
1946 if(cal
.get(UCAL_YEAR
, status
) != 2004) {
1947 errln("year not 2004");
1950 logln("+ setting DOW_LOCAL to 1");
1952 cal
.set(UCAL_YEAR_WOY
,2004);
1953 cal
.set(UCAL_WEEK_OF_YEAR
,1);
1954 cal
.set(UCAL_DAY_OF_WEEK
, UCAL_THURSDAY
);
1955 cal
.set(UCAL_DOW_LOCAL
, 1);
1957 logln(calToStr(cal
));
1958 if(cal
.get(UCAL_YEAR
, status
) != 2003) {
1959 errln("year not 2003");
1962 cal
.setFirstDayOfWeek(UCAL_MONDAY
);
1963 cal
.setMinimalDaysInFirstWeek(4);
1964 UDate t
= 946713600000.;
1965 cal
.setTime(t
, status
);
1966 cal
.set(UCAL_DAY_OF_WEEK
, 4);
1967 cal
.set(UCAL_DOW_LOCAL
, 6);
1968 if(cal
.getTime(status
) != t
) {
1969 logln(calToStr(cal
));
1970 errln("FAIL: DOW_LOCAL did not take precedence");
1975 void CalendarTest::TestJD()
1978 static const int32_t kEpochStartAsJulianDay
= 2440588;
1979 UErrorCode status
= U_ZERO_ERROR
;
1980 GregorianCalendar
cal(status
);
1981 if (failure(status
, "construct GregorianCalendar", TRUE
)) return;
1982 cal
.setTimeZone(*TimeZone::getGMT());
1984 jd
= cal
.get(UCAL_JULIAN_DAY
, status
);
1985 if(jd
!= kEpochStartAsJulianDay
) {
1986 errln("Wanted JD of %d at time=0, [epoch 1970] but got %d\n", kEpochStartAsJulianDay
, jd
);
1988 logln("Wanted JD of %d at time=0, [epoch 1970], got %d\n", kEpochStartAsJulianDay
, jd
);
1991 cal
.setTime(Calendar::getNow(), status
);
1993 cal
.set(UCAL_JULIAN_DAY
, kEpochStartAsJulianDay
);
1994 UDate epochTime
= cal
.getTime(status
);
1995 if(epochTime
!= 0) {
1996 errln("Wanted time of 0 at jd=%d, got %.1lf\n", kEpochStartAsJulianDay
, epochTime
);
1998 logln("Wanted time of 0 at jd=%d, got %.1lf\n", kEpochStartAsJulianDay
, epochTime
);
2003 // make sure the ctestfw utilities are in sync with the Calendar
2004 void CalendarTest::TestDebug()
2006 for(int32_t t
=0;t
<=UDBG_ENUM_COUNT
;t
++) {
2007 int32_t count
= udbg_enumCount((UDebugEnumType
)t
);
2009 logln("enumCount(%d) returned -1", count
);
2012 for(int32_t i
=0;i
<=count
;i
++) {
2013 if(t
<=UDBG_HIGHEST_CONTIGUOUS_ENUM
&& i
<count
) {
2014 if( i
!=udbg_enumArrayValue((UDebugEnumType
)t
, i
)) {
2015 errln("FAIL: udbg_enumArrayValue(%d,%d) returned %d, expected %d", t
, i
, udbg_enumArrayValue((UDebugEnumType
)t
,i
), i
);
2018 logln("Testing count+1:");
2020 const char *name
= udbg_enumName((UDebugEnumType
)t
,i
);
2022 if(i
==count
|| t
>UDBG_HIGHEST_CONTIGUOUS_ENUM
) {
2023 logln(" null name - expected.\n");
2025 errln("FAIL: udbg_enumName(%d,%d) returned NULL", t
, i
);
2029 logln("udbg_enumArrayValue(%d,%d) = %s, returned %d", t
, i
,
2030 name
, udbg_enumArrayValue((UDebugEnumType
)t
,i
));
2031 logln("udbg_enumString = " + udbg_enumString((UDebugEnumType
)t
,i
));
2033 if(udbg_enumExpectedCount((UDebugEnumType
)t
) != count
&& t
<=UDBG_HIGHEST_CONTIGUOUS_ENUM
) {
2034 errln("FAIL: udbg_enumExpectedCount(%d): %d, != UCAL_FIELD_COUNT=%d ", t
, udbg_enumExpectedCount((UDebugEnumType
)t
), count
);
2036 logln("udbg_ucal_fieldCount: %d, UCAL_FIELD_COUNT=udbg_enumCount %d ", udbg_enumExpectedCount((UDebugEnumType
)t
), count
);
2044 // List of interesting locales
2045 const char *CalendarTest::testLocaleID(int32_t i
)
2048 case 0: return "he_IL@calendar=hebrew";
2049 case 1: return "en_US@calendar=hebrew";
2050 case 2: return "fr_FR@calendar=hebrew";
2051 case 3: return "fi_FI@calendar=hebrew";
2052 case 4: return "nl_NL@calendar=hebrew";
2053 case 5: return "hu_HU@calendar=hebrew";
2054 case 6: return "nl_BE@currency=MTL;calendar=islamic";
2055 case 7: return "th_TH_TRADITIONAL@calendar=gregorian";
2056 case 8: return "ar_JO@calendar=islamic-civil";
2057 case 9: return "fi_FI@calendar=islamic";
2058 case 10: return "fr_CH@calendar=islamic-civil";
2059 case 11: return "he_IL@calendar=islamic-civil";
2060 case 12: return "hu_HU@calendar=buddhist";
2061 case 13: return "hu_HU@calendar=islamic";
2062 case 14: return "en_US@calendar=japanese";
2063 default: return NULL
;
2067 int32_t CalendarTest::testLocaleCount()
2069 static int32_t gLocaleCount
= -1;
2070 if(gLocaleCount
< 0) {
2072 for(i
=0;testLocaleID(i
) != NULL
;i
++) {
2077 return gLocaleCount
;
2080 static UDate
doMinDateOfCalendar(Calendar
* adopt
, UBool
&isGregorian
, UErrorCode
& status
) {
2081 if(U_FAILURE(status
)) return 0.0;
2084 adopt
->set(UCAL_EXTENDED_YEAR
, adopt
->getActualMinimum(UCAL_EXTENDED_YEAR
, status
));
2085 UDate ret
= adopt
->getTime(status
);
2086 isGregorian
= dynamic_cast<GregorianCalendar
*>(adopt
) != NULL
;
2091 UDate
CalendarTest::minDateOfCalendar(const Locale
& locale
, UBool
&isGregorian
, UErrorCode
& status
) {
2092 if(U_FAILURE(status
)) return 0.0;
2093 return doMinDateOfCalendar(Calendar::createInstance(locale
, status
), isGregorian
, status
);
2096 UDate
CalendarTest::minDateOfCalendar(const Calendar
& cal
, UBool
&isGregorian
, UErrorCode
& status
) {
2097 if(U_FAILURE(status
)) return 0.0;
2098 return doMinDateOfCalendar(cal
.clone(), isGregorian
, status
);
2101 void CalendarTest::Test6703()
2103 UErrorCode status
= U_ZERO_ERROR
;
2106 Locale
loc1("en@calendar=fubar");
2107 cal
= Calendar::createInstance(loc1
, status
);
2108 if (failure(status
, "Calendar::createInstance", TRUE
)) return;
2111 status
= U_ZERO_ERROR
;
2113 cal
= Calendar::createInstance(loc2
, status
);
2114 if (failure(status
, "Calendar::createInstance")) return;
2117 status
= U_ZERO_ERROR
;
2118 Locale
loc3("en@calendar=roc");
2119 cal
= Calendar::createInstance(loc3
, status
);
2120 if (failure(status
, "Calendar::createInstance")) return;
2126 void CalendarTest::Test3785()
2128 UErrorCode status
= U_ZERO_ERROR
;
2129 UnicodeString uzone
= UNICODE_STRING_SIMPLE("Europe/Paris");
2130 UnicodeString exp1
= UNICODE_STRING_SIMPLE("Mon 30 Jumada II 1433 AH, 01:47:03");
2131 UnicodeString exp2
= UNICODE_STRING_SIMPLE("Mon 1 Rajab 1433 AH, 01:47:04");
2133 LocalUDateFormatPointer
df(udat_open(UDAT_NONE
, UDAT_NONE
, "en@calendar=islamic", uzone
.getTerminatedBuffer(),
2134 uzone
.length(), NULL
, 0, &status
));
2135 if (df
.isNull() || U_FAILURE(status
)) return;
2138 u_uastrcpy(upattern
, "EEE d MMMM y G, HH:mm:ss");
2139 udat_applyPattern(df
.getAlias(), FALSE
, upattern
, u_strlen(upattern
));
2141 UChar ubuffer
[1024];
2142 UDate ud0
= 1337557623000.0;
2144 status
= U_ZERO_ERROR
;
2145 udat_format(df
.getAlias(), ud0
, ubuffer
, 1024, NULL
, &status
);
2146 if (U_FAILURE(status
)) {
2147 errln("Error formatting date 1\n");
2150 //printf("formatted: '%s'\n", mkcstr(ubuffer));
2152 UnicodeString
act1(ubuffer
);
2153 if ( act1
!= exp1
) {
2154 errln("Unexpected result from date 1 format\n");
2156 ud0
+= 1000.0; // add one second
2158 status
= U_ZERO_ERROR
;
2159 udat_format(df
.getAlias(), ud0
, ubuffer
, 1024, NULL
, &status
);
2160 if (U_FAILURE(status
)) {
2161 errln("Error formatting date 2\n");
2164 //printf("formatted: '%s'\n", mkcstr(ubuffer));
2165 UnicodeString
act2(ubuffer
);
2166 if ( act2
!= exp2
) {
2167 errln("Unexpected result from date 2 format\n");
2173 void CalendarTest::Test1624() {
2174 UErrorCode status
= U_ZERO_ERROR
;
2175 Locale
loc("he_IL@calendar=hebrew");
2176 HebrewCalendar
hc(loc
,status
);
2178 for (int32_t year
= 5600; year
< 5800; year
++ ) {
2180 for (int32_t month
= HebrewCalendar::TISHRI
; month
<= HebrewCalendar::ELUL
; month
++) {
2181 // skip the adar 1 month if year is not a leap year
2182 if (HebrewCalendar::isLeapYear(year
) == FALSE
&& month
== HebrewCalendar::ADAR_1
) {
2186 hc
.set(year
,month
,day
);
2187 int32_t dayHC
= hc
.get(UCAL_DATE
,status
);
2188 int32_t monthHC
= hc
.get(UCAL_MONTH
,status
);
2189 int32_t yearHC
= hc
.get(UCAL_YEAR
,status
);
2191 if (failure(status
, "HebrewCalendar.get()", TRUE
)) continue;
2194 errln(" ==> day %d incorrect, should be: %d\n",dayHC
,day
);
2197 if (monthHC
!= month
) {
2198 errln(" ==> month %d incorrect, should be: %d\n",monthHC
,month
);
2201 if (yearHC
!= year
) {
2202 errln(" ==> day %d incorrect, should be: %d\n",yearHC
,year
);
2210 void CalendarTest::TestTimeStamp() {
2211 UErrorCode status
= U_ZERO_ERROR
;
2212 UDate start
= 0.0, time
;
2215 // Create a new Gregorian Calendar.
2216 cal
= Calendar::createInstance("en_US@calender=gregorian", status
);
2217 if (U_FAILURE(status
)) {
2218 dataerrln("Error creating Gregorian calendar.");
2222 for (int i
= 0; i
< 20000; i
++) {
2223 // Set the Gregorian Calendar to a specific date for testing.
2224 cal
->set(2009, UCAL_JULY
, 3, 0, 49, 46);
2226 time
= cal
->getTime(status
);
2227 if (U_FAILURE(status
)) {
2228 errln("Error calling getTime()");
2235 if (start
!= time
) {
2236 errln("start and time not equal.");
2245 void CalendarTest::TestISO8601() {
2246 const char* TEST_LOCALES
[] = {
2247 "en_US@calendar=iso8601",
2248 "en_US@calendar=Iso8601",
2249 "th_TH@calendar=iso8601",
2250 "ar_EG@calendar=iso8601",
2254 int32_t TEST_DATA
[][3] = {
2265 for (int i
= 0; TEST_LOCALES
[i
] != NULL
; i
++) {
2266 UErrorCode status
= U_ZERO_ERROR
;
2267 Calendar
*cal
= Calendar::createInstance(TEST_LOCALES
[i
], status
);
2268 if (U_FAILURE(status
)) {
2269 errln("Error: Failed to create a calendar for locale: %s", TEST_LOCALES
[i
]);
2272 if (uprv_strcmp(cal
->getType(), "gregorian") != 0) {
2273 errln("Error: Gregorian calendar is not used for locale: %s", TEST_LOCALES
[i
]);
2276 for (int j
= 0; TEST_DATA
[j
][0] != 0; j
++) {
2277 cal
->set(TEST_DATA
[j
][0], UCAL_JANUARY
, 1);
2278 int32_t weekNum
= cal
->get(UCAL_WEEK_OF_YEAR
, status
);
2279 int32_t weekYear
= cal
->get(UCAL_YEAR_WOY
, status
);
2280 if (U_FAILURE(status
)) {
2281 errln("Error: Failed to get week of year");
2284 if (weekNum
!= TEST_DATA
[j
][1] || weekYear
!= TEST_DATA
[j
][2]) {
2285 errln("Error: Incorrect week of year on January 1st, %d for locale %s: Returned [weekNum=%d, weekYear=%d], Expected [weekNum=%d, weekYear=%d]",
2286 TEST_DATA
[j
][0], TEST_LOCALES
[i
], weekNum
, weekYear
, TEST_DATA
[j
][1], TEST_DATA
[j
][2]);
2295 CalendarTest::TestAmbiguousWallTimeAPIs(void) {
2296 UErrorCode status
= U_ZERO_ERROR
;
2297 Calendar
* cal
= Calendar::createInstance(status
);
2298 if (U_FAILURE(status
)) {
2299 errln("Fail: Error creating a calendar instance.");
2303 if (cal
->getRepeatedWallTimeOption() != UCAL_WALLTIME_LAST
) {
2304 errln("Fail: Default repeted time option is not UCAL_WALLTIME_LAST");
2306 if (cal
->getSkippedWallTimeOption() != UCAL_WALLTIME_LAST
) {
2307 errln("Fail: Default skipped time option is not UCAL_WALLTIME_LAST");
2310 Calendar
* cal2
= cal
->clone();
2312 if (*cal
!= *cal2
) {
2313 errln("Fail: Cloned calendar != the original");
2315 if (!cal
->equals(*cal2
, status
)) {
2316 errln("Fail: The time of cloned calendar is not equal to the original");
2317 } else if (U_FAILURE(status
)) {
2318 errln("Fail: Error equals");
2320 status
= U_ZERO_ERROR
;
2322 cal2
->setRepeatedWallTimeOption(UCAL_WALLTIME_FIRST
);
2323 cal2
->setSkippedWallTimeOption(UCAL_WALLTIME_FIRST
);
2325 if (*cal
== *cal2
) {
2326 errln("Fail: Cloned and modified calendar == the original");
2328 if (!cal
->equals(*cal2
, status
)) {
2329 errln("Fail: The time of cloned calendar is not equal to the original after changing wall time options");
2330 } else if (U_FAILURE(status
)) {
2331 errln("Fail: Error equals after changing wall time options");
2333 status
= U_ZERO_ERROR
;
2335 if (cal2
->getRepeatedWallTimeOption() != UCAL_WALLTIME_FIRST
) {
2336 errln("Fail: Repeted time option is not UCAL_WALLTIME_FIRST");
2338 if (cal2
->getSkippedWallTimeOption() != UCAL_WALLTIME_FIRST
) {
2339 errln("Fail: Skipped time option is not UCAL_WALLTIME_FIRST");
2342 cal2
->setRepeatedWallTimeOption(UCAL_WALLTIME_NEXT_VALID
);
2343 if (cal2
->getRepeatedWallTimeOption() != UCAL_WALLTIME_FIRST
) {
2344 errln("Fail: Repeated wall time option was updated other than UCAL_WALLTIME_FIRST");
2353 CalFields(int32_t year
, int32_t month
, int32_t day
, int32_t hour
, int32_t min
, int32_t sec
);
2354 CalFields(const Calendar
& cal
, UErrorCode
& status
);
2355 void setTo(Calendar
& cal
) const;
2356 char* toString(char* buf
, int32_t len
) const;
2357 UBool
operator==(const CalFields
& rhs
) const;
2358 UBool
operator!=(const CalFields
& rhs
) const;
2369 CalFields::CalFields(int32_t year
, int32_t month
, int32_t day
, int32_t hour
, int32_t min
, int32_t sec
)
2370 : year(year
), month(month
), day(day
), hour(hour
), min(min
), sec(sec
) {
2373 CalFields::CalFields(const Calendar
& cal
, UErrorCode
& status
) {
2374 year
= cal
.get(UCAL_YEAR
, status
);
2375 month
= cal
.get(UCAL_MONTH
, status
) + 1;
2376 day
= cal
.get(UCAL_DAY_OF_MONTH
, status
);
2377 hour
= cal
.get(UCAL_HOUR_OF_DAY
, status
);
2378 min
= cal
.get(UCAL_MINUTE
, status
);
2379 sec
= cal
.get(UCAL_SECOND
, status
);
2383 CalFields::setTo(Calendar
& cal
) const {
2385 cal
.set(year
, month
- 1, day
, hour
, min
, sec
);
2389 CalFields::toString(char* buf
, int32_t len
) const {
2391 sprintf(local
, "%04d-%02d-%02d %02d:%02d:%02d", year
, month
, day
, hour
, min
, sec
);
2392 uprv_strncpy(buf
, local
, len
- 1);
2398 CalFields::operator==(const CalFields
& rhs
) const {
2399 return year
== rhs
.year
2400 && month
== rhs
.month
2408 CalFields::operator!=(const CalFields
& rhs
) const {
2409 return !(*this == rhs
);
2415 const CalFields expLastGMT
;
2416 const CalFields expFirstGMT
;
2417 } RepeatedWallTimeTestData
;
2419 static const RepeatedWallTimeTestData RPDATA
[] =
2421 // Time zone Input wall time WALLTIME_LAST in GMT WALLTIME_FIRST in GMT
2422 {"America/New_York", CalFields(2011,11,6,0,59,59), CalFields(2011,11,6,4,59,59), CalFields(2011,11,6,4,59,59)},
2423 {"America/New_York", CalFields(2011,11,6,1,0,0), CalFields(2011,11,6,6,0,0), CalFields(2011,11,6,5,0,0)},
2424 {"America/New_York", CalFields(2011,11,6,1,0,1), CalFields(2011,11,6,6,0,1), CalFields(2011,11,6,5,0,1)},
2425 {"America/New_York", CalFields(2011,11,6,1,30,0), CalFields(2011,11,6,6,30,0), CalFields(2011,11,6,5,30,0)},
2426 {"America/New_York", CalFields(2011,11,6,1,59,59), CalFields(2011,11,6,6,59,59), CalFields(2011,11,6,5,59,59)},
2427 {"America/New_York", CalFields(2011,11,6,2,0,0), CalFields(2011,11,6,7,0,0), CalFields(2011,11,6,7,0,0)},
2428 {"America/New_York", CalFields(2011,11,6,2,0,1), CalFields(2011,11,6,7,0,1), CalFields(2011,11,6,7,0,1)},
2430 {"Australia/Lord_Howe", CalFields(2011,4,3,1,29,59), CalFields(2011,4,2,14,29,59), CalFields(2011,4,2,14,29,59)},
2431 {"Australia/Lord_Howe", CalFields(2011,4,3,1,30,0), CalFields(2011,4,2,15,0,0), CalFields(2011,4,2,14,30,0)},
2432 {"Australia/Lord_Howe", CalFields(2011,4,3,1,45,0), CalFields(2011,4,2,15,15,0), CalFields(2011,4,2,14,45,0)},
2433 {"Australia/Lord_Howe", CalFields(2011,4,3,1,59,59), CalFields(2011,4,2,15,29,59), CalFields(2011,4,2,14,59,59)},
2434 {"Australia/Lord_Howe", CalFields(2011,4,3,2,0,0), CalFields(2011,4,2,15,30,0), CalFields(2011,4,2,15,30,0)},
2435 {"Australia/Lord_Howe", CalFields(2011,4,3,2,0,1), CalFields(2011,4,2,15,30,1), CalFields(2011,4,2,15,30,1)},
2437 {NULL
, CalFields(0,0,0,0,0,0), CalFields(0,0,0,0,0,0), CalFields(0,0,0,0,0,0)}
2440 void CalendarTest::TestRepeatedWallTime(void) {
2441 UErrorCode status
= U_ZERO_ERROR
;
2442 GregorianCalendar
calGMT((const TimeZone
&)*TimeZone::getGMT(), status
);
2443 GregorianCalendar
calDefault(status
);
2444 GregorianCalendar
calLast(status
);
2445 GregorianCalendar
calFirst(status
);
2447 if (U_FAILURE(status
)) {
2448 errln("Fail: Failed to create a calendar object.");
2452 calLast
.setRepeatedWallTimeOption(UCAL_WALLTIME_LAST
);
2453 calFirst
.setRepeatedWallTimeOption(UCAL_WALLTIME_FIRST
);
2455 for (int32_t i
= 0; RPDATA
[i
].tzid
!= NULL
; i
++) {
2457 TimeZone
*tz
= TimeZone::createTimeZone(RPDATA
[i
].tzid
);
2459 // UCAL_WALLTIME_LAST
2460 status
= U_ZERO_ERROR
;
2461 calLast
.setTimeZone(*tz
);
2462 RPDATA
[i
].in
.setTo(calLast
);
2463 calGMT
.setTime(calLast
.getTime(status
), status
);
2464 CalFields
outLastGMT(calGMT
, status
);
2465 if (U_FAILURE(status
)) {
2466 errln(UnicodeString("Fail: Failed to get/set time calLast/calGMT (UCAL_WALLTIME_LAST) - ")
2467 + RPDATA
[i
].in
.toString(buf
, sizeof(buf
)) + "[" + RPDATA
[i
].tzid
+ "]");
2469 if (outLastGMT
!= RPDATA
[i
].expLastGMT
) {
2470 dataerrln(UnicodeString("Fail: UCAL_WALLTIME_LAST ") + RPDATA
[i
].in
.toString(buf
, sizeof(buf
)) + "[" + RPDATA
[i
].tzid
+ "] is parsed as "
2471 + outLastGMT
.toString(buf
, sizeof(buf
)) + "[GMT]. Expected: " + RPDATA
[i
].expLastGMT
.toString(buf
, sizeof(buf
)) + "[GMT]");
2476 status
= U_ZERO_ERROR
;
2477 calDefault
.setTimeZone(*tz
);
2478 RPDATA
[i
].in
.setTo(calDefault
);
2479 calGMT
.setTime(calDefault
.getTime(status
), status
);
2480 CalFields
outDefGMT(calGMT
, status
);
2481 if (U_FAILURE(status
)) {
2482 errln(UnicodeString("Fail: Failed to get/set time calLast/calGMT (default) - ")
2483 + RPDATA
[i
].in
.toString(buf
, sizeof(buf
)) + "[" + RPDATA
[i
].tzid
+ "]");
2485 if (outDefGMT
!= RPDATA
[i
].expLastGMT
) {
2486 dataerrln(UnicodeString("Fail: (default) ") + RPDATA
[i
].in
.toString(buf
, sizeof(buf
)) + "[" + RPDATA
[i
].tzid
+ "] is parsed as "
2487 + outDefGMT
.toString(buf
, sizeof(buf
)) + "[GMT]. Expected: " + RPDATA
[i
].expLastGMT
.toString(buf
, sizeof(buf
)) + "[GMT]");
2491 // UCAL_WALLTIME_FIRST
2492 status
= U_ZERO_ERROR
;
2493 calFirst
.setTimeZone(*tz
);
2494 RPDATA
[i
].in
.setTo(calFirst
);
2495 calGMT
.setTime(calFirst
.getTime(status
), status
);
2496 CalFields
outFirstGMT(calGMT
, status
);
2497 if (U_FAILURE(status
)) {
2498 errln(UnicodeString("Fail: Failed to get/set time calLast/calGMT (UCAL_WALLTIME_FIRST) - ")
2499 + RPDATA
[i
].in
.toString(buf
, sizeof(buf
)) + "[" + RPDATA
[i
].tzid
+ "]");
2501 if (outFirstGMT
!= RPDATA
[i
].expFirstGMT
) {
2502 dataerrln(UnicodeString("Fail: UCAL_WALLTIME_FIRST ") + RPDATA
[i
].in
.toString(buf
, sizeof(buf
)) + "[" + RPDATA
[i
].tzid
+ "] is parsed as "
2503 + outFirstGMT
.toString(buf
, sizeof(buf
)) + "[GMT]. Expected: " + RPDATA
[i
].expFirstGMT
.toString(buf
, sizeof(buf
)) + "[GMT]");
2514 const CalFields expLastGMT
;
2515 const CalFields expFirstGMT
;
2516 const CalFields expNextAvailGMT
;
2517 } SkippedWallTimeTestData
;
2519 static SkippedWallTimeTestData SKDATA
[] =
2521 // Time zone Input wall time valid? WALLTIME_LAST in GMT WALLTIME_FIRST in GMT WALLTIME_NEXT_VALID in GMT
2522 {"America/New_York", CalFields(2011,3,13,1,59,59), TRUE
, CalFields(2011,3,13,6,59,59), CalFields(2011,3,13,6,59,59), CalFields(2011,3,13,6,59,59)},
2523 {"America/New_York", CalFields(2011,3,13,2,0,0), FALSE
, CalFields(2011,3,13,7,0,0), CalFields(2011,3,13,6,0,0), CalFields(2011,3,13,7,0,0)},
2524 {"America/New_York", CalFields(2011,3,13,2,1,0), FALSE
, CalFields(2011,3,13,7,1,0), CalFields(2011,3,13,6,1,0), CalFields(2011,3,13,7,0,0)},
2525 {"America/New_York", CalFields(2011,3,13,2,30,0), FALSE
, CalFields(2011,3,13,7,30,0), CalFields(2011,3,13,6,30,0), CalFields(2011,3,13,7,0,0)},
2526 {"America/New_York", CalFields(2011,3,13,2,59,59), FALSE
, CalFields(2011,3,13,7,59,59), CalFields(2011,3,13,6,59,59), CalFields(2011,3,13,7,0,0)},
2527 {"America/New_York", CalFields(2011,3,13,3,0,0), TRUE
, CalFields(2011,3,13,7,0,0), CalFields(2011,3,13,7,0,0), CalFields(2011,3,13,7,0,0)},
2529 {"Pacific/Apia", CalFields(2011,12,29,23,59,59), TRUE
, CalFields(2011,12,30,9,59,59), CalFields(2011,12,30,9,59,59), CalFields(2011,12,30,9,59,59)},
2530 {"Pacific/Apia", CalFields(2011,12,30,0,0,0), FALSE
, CalFields(2011,12,30,10,0,0), CalFields(2011,12,29,10,0,0), CalFields(2011,12,30,10,0,0)},
2531 {"Pacific/Apia", CalFields(2011,12,30,12,0,0), FALSE
, CalFields(2011,12,30,22,0,0), CalFields(2011,12,29,22,0,0), CalFields(2011,12,30,10,0,0)},
2532 {"Pacific/Apia", CalFields(2011,12,30,23,59,59), FALSE
, CalFields(2011,12,31,9,59,59), CalFields(2011,12,30,9,59,59), CalFields(2011,12,30,10,0,0)},
2533 {"Pacific/Apia", CalFields(2011,12,31,0,0,0), TRUE
, CalFields(2011,12,30,10,0,0), CalFields(2011,12,30,10,0,0), CalFields(2011,12,30,10,0,0)},
2535 {NULL
, CalFields(0,0,0,0,0,0), TRUE
, CalFields(0,0,0,0,0,0), CalFields(0,0,0,0,0,0), CalFields(0,0,0,0,0,0)}
2539 void CalendarTest::TestSkippedWallTime(void) {
2540 UErrorCode status
= U_ZERO_ERROR
;
2541 GregorianCalendar
calGMT((const TimeZone
&)*TimeZone::getGMT(), status
);
2542 GregorianCalendar
calDefault(status
);
2543 GregorianCalendar
calLast(status
);
2544 GregorianCalendar
calFirst(status
);
2545 GregorianCalendar
calNextAvail(status
);
2547 if (U_FAILURE(status
)) {
2548 errln("Fail: Failed to create a calendar object.");
2552 calLast
.setSkippedWallTimeOption(UCAL_WALLTIME_LAST
);
2553 calFirst
.setSkippedWallTimeOption(UCAL_WALLTIME_FIRST
);
2554 calNextAvail
.setSkippedWallTimeOption(UCAL_WALLTIME_NEXT_VALID
);
2556 for (int32_t i
= 0; SKDATA
[i
].tzid
!= NULL
; i
++) {
2559 TimeZone
*tz
= TimeZone::createTimeZone(SKDATA
[i
].tzid
);
2561 for (int32_t j
= 0; j
< 2; j
++) {
2562 UBool bLenient
= (j
== 0);
2564 // UCAL_WALLTIME_LAST
2565 status
= U_ZERO_ERROR
;
2566 calLast
.setLenient(bLenient
);
2567 calLast
.setTimeZone(*tz
);
2568 SKDATA
[i
].in
.setTo(calLast
);
2569 d
= calLast
.getTime(status
);
2570 if (bLenient
|| SKDATA
[i
].isValid
) {
2571 calGMT
.setTime(d
, status
);
2572 CalFields
outLastGMT(calGMT
, status
);
2573 if (U_FAILURE(status
)) {
2574 errln(UnicodeString("Fail: Failed to get/set time calLast/calGMT (UCAL_WALLTIME_LAST) - ")
2575 + SKDATA
[i
].in
.toString(buf
, sizeof(buf
)) + "[" + SKDATA
[i
].tzid
+ "]");
2577 if (outLastGMT
!= SKDATA
[i
].expLastGMT
) {
2578 dataerrln(UnicodeString("Fail: UCAL_WALLTIME_LAST ") + SKDATA
[i
].in
.toString(buf
, sizeof(buf
)) + "[" + SKDATA
[i
].tzid
+ "] is parsed as "
2579 + outLastGMT
.toString(buf
, sizeof(buf
)) + "[GMT]. Expected: " + SKDATA
[i
].expLastGMT
.toString(buf
, sizeof(buf
)) + "[GMT]");
2582 } else if (U_SUCCESS(status
)) {
2583 // strict, invalid wall time - must report an error
2584 dataerrln(UnicodeString("Fail: An error expected (UCAL_WALLTIME_LAST)") +
2585 + SKDATA
[i
].in
.toString(buf
, sizeof(buf
)) + "[" + SKDATA
[i
].tzid
+ "]");
2589 status
= U_ZERO_ERROR
;
2590 calDefault
.setLenient(bLenient
);
2591 calDefault
.setTimeZone(*tz
);
2592 SKDATA
[i
].in
.setTo(calDefault
);
2593 d
= calDefault
.getTime(status
);
2594 if (bLenient
|| SKDATA
[i
].isValid
) {
2595 calGMT
.setTime(d
, status
);
2596 CalFields
outDefGMT(calGMT
, status
);
2597 if (U_FAILURE(status
)) {
2598 errln(UnicodeString("Fail: Failed to get/set time calDefault/calGMT (default) - ")
2599 + SKDATA
[i
].in
.toString(buf
, sizeof(buf
)) + "[" + SKDATA
[i
].tzid
+ "]");
2601 if (outDefGMT
!= SKDATA
[i
].expLastGMT
) {
2602 dataerrln(UnicodeString("Fail: (default) ") + SKDATA
[i
].in
.toString(buf
, sizeof(buf
)) + "[" + SKDATA
[i
].tzid
+ "] is parsed as "
2603 + outDefGMT
.toString(buf
, sizeof(buf
)) + "[GMT]. Expected: " + SKDATA
[i
].expLastGMT
.toString(buf
, sizeof(buf
)) + "[GMT]");
2606 } else if (U_SUCCESS(status
)) {
2607 // strict, invalid wall time - must report an error
2608 dataerrln(UnicodeString("Fail: An error expected (default)") +
2609 + SKDATA
[i
].in
.toString(buf
, sizeof(buf
)) + "[" + SKDATA
[i
].tzid
+ "]");
2612 // UCAL_WALLTIME_FIRST
2613 status
= U_ZERO_ERROR
;
2614 calFirst
.setLenient(bLenient
);
2615 calFirst
.setTimeZone(*tz
);
2616 SKDATA
[i
].in
.setTo(calFirst
);
2617 d
= calFirst
.getTime(status
);
2618 if (bLenient
|| SKDATA
[i
].isValid
) {
2619 calGMT
.setTime(d
, status
);
2620 CalFields
outFirstGMT(calGMT
, status
);
2621 if (U_FAILURE(status
)) {
2622 errln(UnicodeString("Fail: Failed to get/set time calFirst/calGMT (UCAL_WALLTIME_FIRST) - ")
2623 + SKDATA
[i
].in
.toString(buf
, sizeof(buf
)) + "[" + SKDATA
[i
].tzid
+ "]");
2625 if (outFirstGMT
!= SKDATA
[i
].expFirstGMT
) {
2626 dataerrln(UnicodeString("Fail: UCAL_WALLTIME_FIRST ") + SKDATA
[i
].in
.toString(buf
, sizeof(buf
)) + "[" + SKDATA
[i
].tzid
+ "] is parsed as "
2627 + outFirstGMT
.toString(buf
, sizeof(buf
)) + "[GMT]. Expected: " + SKDATA
[i
].expFirstGMT
.toString(buf
, sizeof(buf
)) + "[GMT]");
2630 } else if (U_SUCCESS(status
)) {
2631 // strict, invalid wall time - must report an error
2632 dataerrln(UnicodeString("Fail: An error expected (UCAL_WALLTIME_FIRST)") +
2633 + SKDATA
[i
].in
.toString(buf
, sizeof(buf
)) + "[" + SKDATA
[i
].tzid
+ "]");
2636 // UCAL_WALLTIME_NEXT_VALID
2637 status
= U_ZERO_ERROR
;
2638 calNextAvail
.setLenient(bLenient
);
2639 calNextAvail
.setTimeZone(*tz
);
2640 SKDATA
[i
].in
.setTo(calNextAvail
);
2641 d
= calNextAvail
.getTime(status
);
2642 if (bLenient
|| SKDATA
[i
].isValid
) {
2643 calGMT
.setTime(d
, status
);
2644 CalFields
outNextAvailGMT(calGMT
, status
);
2645 if (U_FAILURE(status
)) {
2646 errln(UnicodeString("Fail: Failed to get/set time calNextAvail/calGMT (UCAL_WALLTIME_NEXT_VALID) - ")
2647 + SKDATA
[i
].in
.toString(buf
, sizeof(buf
)) + "[" + SKDATA
[i
].tzid
+ "]");
2649 if (outNextAvailGMT
!= SKDATA
[i
].expNextAvailGMT
) {
2650 dataerrln(UnicodeString("Fail: UCAL_WALLTIME_NEXT_VALID ") + SKDATA
[i
].in
.toString(buf
, sizeof(buf
)) + "[" + SKDATA
[i
].tzid
+ "] is parsed as "
2651 + outNextAvailGMT
.toString(buf
, sizeof(buf
)) + "[GMT]. Expected: " + SKDATA
[i
].expNextAvailGMT
.toString(buf
, sizeof(buf
)) + "[GMT]");
2654 } else if (U_SUCCESS(status
)) {
2655 // strict, invalid wall time - must report an error
2656 dataerrln(UnicodeString("Fail: An error expected (UCAL_WALLTIME_NEXT_VALID)") +
2657 + SKDATA
[i
].in
.toString(buf
, sizeof(buf
)) + "[" + SKDATA
[i
].tzid
+ "]");
2665 void CalendarTest::TestCloneLocale(void) {
2666 UErrorCode status
= U_ZERO_ERROR
;
2667 LocalPointer
<Calendar
> cal(Calendar::createInstance(TimeZone::getGMT()->clone(),
2668 Locale::createFromName("en"), status
));
2670 Locale l0
= cal
->getLocale(ULOC_VALID_LOCALE
, status
);
2672 LocalPointer
<Calendar
> cal2(cal
->clone());
2673 Locale l
= cal2
->getLocale(ULOC_VALID_LOCALE
, status
);
2675 errln("Error: cloned locale %s != original locale %s, status %s\n", l0
.getName(), l
.getName(), u_errorName(status
));
2680 #endif /* #if !UCONFIG_NO_FORMATTING */