1 /************************************************************************
3 * Copyright (c) 1997-2004, International Business Machines Corporation
4 * and others. All Rights Reserved.
5 ************************************************************************/
7 #include "unicode/utypes.h"
9 #if !UCONFIG_NO_FORMATTING
12 #include "unicode/gregocal.h"
13 #include "unicode/smpdtfmt.h"
14 #include "unicode/simpletz.h"
16 // *****************************************************************************
18 // *****************************************************************************
20 UnicodeString
CalendarTest::calToStr(const Calendar
& cal
)
23 UErrorCode status
= U_ZERO_ERROR
;
26 for(i
= 0;i
<UCAL_FIELD_COUNT
;i
++) {
27 out
+= (UnicodeString("") + fieldName((UCalendarDateFields
)i
) + "=" + cal
.get((UCalendarDateFields
)i
, status
) + UnicodeString(" "));
29 out
+= "[" + UnicodeString(cal
.getType()) + "]";
31 if(cal
.inDaylightTime(status
)) {
32 out
+= UnicodeString(" (in DST), zone=");
35 out
+= UnicodeString(", zone=");
39 out
+= cal
.getTimeZone().getDisplayName(str2
);
40 d
= cal
.getTime(status
);
41 out
+= UnicodeString(" :","") + d
;
46 void CalendarTest::runIndexedTest( int32_t index
, UBool exec
, const char* &name
, char* /*par*/ )
48 if (exec
) logln("TestSuite TestCalendar");
53 logln("TestDOW943---"); logln("");
58 name
= "TestClonesUnique908";
60 logln("TestClonesUnique908---"); logln("");
61 TestClonesUnique908();
65 name
= "TestGregorianChange768";
67 logln("TestGregorianChange768---"); logln("");
68 TestGregorianChange768();
72 name
= "TestDisambiguation765";
74 logln("TestDisambiguation765---"); logln("");
75 TestDisambiguation765();
79 name
= "TestGMTvsLocal4064654";
81 logln("TestGMTvsLocal4064654---"); logln("");
82 TestGMTvsLocal4064654();
86 name
= "TestAddSetOrder621";
88 logln("TestAddSetOrder621---"); logln("");
95 logln("TestAdd520---"); logln("");
100 name
= "TestFieldSet4781";
102 logln("TestFieldSet4781---"); logln("");
107 name
= "TestSerialize337";
109 logln("TestSerialize337---"); logln("");
110 // TestSerialize337();
114 name
= "TestSecondsZero121";
116 logln("TestSecondsZero121---"); logln("");
117 TestSecondsZero121();
121 name
= "TestAddSetGet0610";
123 logln("TestAddSetGet0610---"); logln("");
128 name
= "TestFields060";
130 logln("TestFields060---"); logln("");
135 name
= "TestEpochStartFields";
137 logln("TestEpochStartFields---"); logln("");
138 TestEpochStartFields();
142 name
= "TestDOWProgression";
144 logln("TestDOWProgression---"); logln("");
145 TestDOWProgression();
149 name
= "TestGenericAPI";
151 logln("TestGenericAPI---"); logln("");
156 name
= "TestAddRollExtensive";
158 logln("TestAddRollExtensive---"); logln("");
159 TestAddRollExtensive();
163 name
= "TestDOW_LOCALandYEAR_WOY";
165 logln("TestDOW_LOCALandYEAR_WOY---"); logln("");
166 TestDOW_LOCALandYEAR_WOY();
172 logln("TestWOY---"); logln("");
179 logln("TestRog---"); logln("");
186 logln("TestYWOY---"); logln("");
193 logln("TestJD---"); logln("");
198 default: name
= ""; break;
202 // ---------------------------------------------------------------------------------
204 UnicodeString
CalendarTest::fieldName(UCalendarDateFields f
) {
206 #define FIELD_NAME_STR(x) case x: return (#x+5)
207 FIELD_NAME_STR( UCAL_ERA
);
208 FIELD_NAME_STR( UCAL_YEAR
);
209 FIELD_NAME_STR( UCAL_MONTH
);
210 FIELD_NAME_STR( UCAL_WEEK_OF_YEAR
);
211 FIELD_NAME_STR( UCAL_WEEK_OF_MONTH
);
212 FIELD_NAME_STR( UCAL_DATE
);
213 FIELD_NAME_STR( UCAL_DAY_OF_YEAR
);
214 FIELD_NAME_STR( UCAL_DAY_OF_WEEK
);
215 FIELD_NAME_STR( UCAL_DAY_OF_WEEK_IN_MONTH
);
216 FIELD_NAME_STR( UCAL_AM_PM
);
217 FIELD_NAME_STR( UCAL_HOUR
);
218 FIELD_NAME_STR( UCAL_HOUR_OF_DAY
);
219 FIELD_NAME_STR( UCAL_MINUTE
);
220 FIELD_NAME_STR( UCAL_SECOND
);
221 FIELD_NAME_STR( UCAL_MILLISECOND
);
222 FIELD_NAME_STR( UCAL_ZONE_OFFSET
);
223 FIELD_NAME_STR( UCAL_DST_OFFSET
);
224 FIELD_NAME_STR( UCAL_YEAR_WOY
);
225 FIELD_NAME_STR( UCAL_DOW_LOCAL
);
226 FIELD_NAME_STR( UCAL_EXTENDED_YEAR
);
227 FIELD_NAME_STR( UCAL_JULIAN_DAY
);
228 FIELD_NAME_STR( UCAL_MILLISECONDS_IN_DAY
);
229 #undef FIELD_NAME_STR
231 return UnicodeString("") + ((int32_t)f
);
236 * Test various API methods for API completeness.
239 CalendarTest::TestGenericAPI()
241 UErrorCode status
= U_ZERO_ERROR
;
244 UBool eq
= FALSE
,b4
= FALSE
,af
= FALSE
;
246 UDate when
= date(90, UCAL_APRIL
, 15);
248 UnicodeString
tzid("TestZone");
249 int32_t tzoffset
= 123400;
251 SimpleTimeZone
*zone
= new SimpleTimeZone(tzoffset
, tzid
);
252 Calendar
*cal
= Calendar::createInstance(zone
->clone(), status
);
253 if (failure(status
, "Calendar::createInstance")) return;
255 if (*zone
!= cal
->getTimeZone()) errln("FAIL: Calendar::getTimeZone failed");
257 Calendar
*cal2
= Calendar::createInstance(cal
->getTimeZone(), status
);
258 if (failure(status
, "Calendar::createInstance")) return;
259 cal
->setTime(when
, status
);
260 cal2
->setTime(when
, status
);
261 if (failure(status
, "Calendar::setTime")) return;
263 if (!(*cal
== *cal2
)) errln("FAIL: Calendar::operator== failed");
264 if ((*cal
!= *cal2
)) errln("FAIL: Calendar::operator!= failed");
265 if (!cal
->equals(*cal2
, status
) ||
266 cal
->before(*cal2
, status
) ||
267 cal
->after(*cal2
, status
) ||
268 U_FAILURE(status
)) errln("FAIL: equals/before/after failed");
270 logln(UnicodeString("cal=") +cal
->getTime(status
) + UnicodeString(calToStr(*cal
)));
271 logln(UnicodeString("cal2=") +cal2
->getTime(status
) + UnicodeString(calToStr(*cal2
)));
272 logln("cal2->setTime(when+1000)");
273 cal2
->setTime(when
+ 1000, status
);
274 logln(UnicodeString("cal2=") +cal2
->getTime(status
) + UnicodeString(calToStr(*cal2
)));
276 if (failure(status
, "Calendar::setTime")) return;
277 if (cal
->equals(*cal2
, status
) ||
278 cal2
->before(*cal
, status
) ||
279 cal
->after(*cal2
, status
) ||
280 U_FAILURE(status
)) errln("FAIL: equals/before/after failed after setTime(+1000)");
282 logln("cal1->roll(UCAL_SECOND)");
283 cal
->roll(UCAL_SECOND
, (UBool
) TRUE
, status
);
284 logln(UnicodeString("cal=") +cal
->getTime(status
) + UnicodeString(calToStr(*cal
)));
286 if (failure(status
, "Calendar::roll")) return;
287 if (!(eq
=cal
->equals(*cal2
, status
)) ||
288 (b4
=cal
->before(*cal2
, status
)) ||
289 (af
=cal
->after(*cal2
, status
)) ||
291 errln("FAIL: equals[%c]/before[%c]/after[%c] failed after roll 1 second [should be T/F/F]",
295 logln(UnicodeString("cal=") +cal
->getTime(status
) + UnicodeString(calToStr(*cal
)));
296 logln(UnicodeString("cal2=") +cal2
->getTime(status
) + UnicodeString(calToStr(*cal2
)));
299 // Roll back to January
300 cal
->roll(UCAL_MONTH
, (int32_t)(1 + UCAL_DECEMBER
- cal
->get(UCAL_MONTH
, status
)), status
);
301 if (failure(status
, "Calendar::roll")) return;
302 if (cal
->equals(*cal2
, status
) ||
303 cal2
->before(*cal
, status
) ||
304 cal
->after(*cal2
, status
) ||
305 U_FAILURE(status
)) errln("FAIL: equals/before/after failed after rollback to January");
307 TimeZone
*z
= cal
->orphanTimeZone();
308 if (z
->getID(str
) != tzid
||
309 z
->getRawOffset() != tzoffset
)
310 errln("FAIL: orphanTimeZone failed");
315 UBool lenient
= ( i
> 0 );
316 cal
->setLenient(lenient
);
317 if (lenient
!= cal
->isLenient()) errln("FAIL: setLenient/isLenient failed");
318 // Later: Check for lenient behavior
321 for (i
=UCAL_SUNDAY
; i
<=UCAL_SATURDAY
; ++i
)
323 cal
->setFirstDayOfWeek((UCalendarDaysOfWeek
)i
);
324 if (cal
->getFirstDayOfWeek() != i
) errln("FAIL: set/getFirstDayOfWeek failed");
325 UErrorCode aStatus
= U_ZERO_ERROR
;
326 if (cal
->getFirstDayOfWeek(aStatus
) != i
|| U_FAILURE(aStatus
)) errln("FAIL: getFirstDayOfWeek(status) failed");
331 cal
->setMinimalDaysInFirstWeek((uint8_t)i
);
332 if (cal
->getMinimalDaysInFirstWeek() != i
) errln("FAIL: set/getFirstDayOfWeek failed");
335 for (i
=0; i
<UCAL_FIELD_COUNT
; ++i
)
337 if (cal
->getMinimum((UCalendarDateFields
)i
) != cal
->getGreatestMinimum((UCalendarDateFields
)i
))
338 errln("FAIL: getMinimum doesn't match getGreatestMinimum for field " + i
);
339 if (cal
->getLeastMaximum((UCalendarDateFields
)i
) > cal
->getMaximum((UCalendarDateFields
)i
))
340 errln("FAIL: getLeastMaximum larger than getMaximum for field " + i
);
341 if (cal
->getMinimum((UCalendarDateFields
)i
) >= cal
->getMaximum((UCalendarDateFields
)i
))
342 errln("FAIL: getMinimum not less than getMaximum for field " + i
);
345 cal
->adoptTimeZone(TimeZone::createDefault());
347 cal
->set(1984, 5, 24);
348 if (cal
->getTime(status
) != date(84, 5, 24) || U_FAILURE(status
))
349 errln("FAIL: Calendar::set(3 args) failed");
352 cal
->set(1985, 3, 2, 11, 49);
353 if (cal
->getTime(status
) != date(85, 3, 2, 11, 49) || U_FAILURE(status
))
354 errln("FAIL: Calendar::set(5 args) failed");
357 cal
->set(1995, 9, 12, 1, 39, 55);
358 if (cal
->getTime(status
) != date(95, 9, 12, 1, 39, 55) || U_FAILURE(status
))
359 errln("FAIL: Calendar::set(6 args) failed");
361 cal
->getTime(status
);
362 if (failure(status
, "Calendar::getTime")) return;
363 for (i
=0; i
<UCAL_FIELD_COUNT
; ++i
)
366 case UCAL_YEAR
: case UCAL_MONTH
: case UCAL_DATE
:
367 case UCAL_HOUR_OF_DAY
: case UCAL_MINUTE
: case UCAL_SECOND
:
368 case UCAL_EXTENDED_YEAR
:
369 if (!cal
->isSet((UCalendarDateFields
)i
)) errln("FAIL: Calendar::isSet F, should be T " + fieldName((UCalendarDateFields
)i
));
372 if (cal
->isSet((UCalendarDateFields
)i
)) errln("FAIL: Calendar::isSet = T, should be F " + fieldName((UCalendarDateFields
)i
));
374 cal
->clear((UCalendarDateFields
)i
);
375 if (cal
->isSet((UCalendarDateFields
)i
)) errln("FAIL: Calendar::clear/isSet failed " + fieldName((UCalendarDateFields
)i
));
382 const Locale
* loc
= Calendar::getAvailableLocales(count
);
383 if (count
< 1 || loc
== 0)
385 errln("FAIL: getAvailableLocales failed");
389 for (i
=0; i
<count
; ++i
)
391 cal
= Calendar::createInstance(loc
[i
], status
);
392 if (failure(status
, "Calendar::createInstance")) return;
397 cal
= Calendar::createInstance(TimeZone::createDefault(), Locale::getEnglish(), status
);
398 if (failure(status
, "Calendar::createInstance")) return;
401 cal
= Calendar::createInstance(*zone
, Locale::getEnglish(), status
);
402 if (failure(status
, "Calendar::createInstance")) return;
405 GregorianCalendar
*gc
= new GregorianCalendar(*zone
, status
);
406 if (failure(status
, "new GregorianCalendar")) return;
409 gc
= new GregorianCalendar(Locale::getEnglish(), status
);
410 if (failure(status
, "new GregorianCalendar")) return;
413 gc
= new GregorianCalendar(Locale::getEnglish(), status
);
416 gc
= new GregorianCalendar(*zone
, Locale::getEnglish(), status
);
417 if (failure(status
, "new GregorianCalendar")) return;
420 gc
= new GregorianCalendar(zone
, status
);
421 if (failure(status
, "new GregorianCalendar")) return;
424 gc
= new GregorianCalendar(1998, 10, 14, 21, 43, status
);
425 if (gc
->getTime(status
) != (d
=date(98, 10, 14, 21, 43) )|| U_FAILURE(status
))
426 errln("FAIL: new GregorianCalendar(ymdhm) failed with " + UnicodeString(u_errorName(status
)) + ", cal=" + gc
->getTime(status
) + UnicodeString(calToStr(*gc
)) + ", d=" + d
);
428 logln(UnicodeString("GOOD: cal=") +gc
->getTime(status
) + UnicodeString(calToStr(*gc
)) + ", d=" + d
);
431 gc
= new GregorianCalendar(1998, 10, 14, 21, 43, 55, status
);
432 if (gc
->getTime(status
) != (d
=date(98, 10, 14, 21, 43, 55)) || U_FAILURE(status
))
433 errln("FAIL: new GregorianCalendar(ymdhms) failed with " + UnicodeString(u_errorName(status
)));
435 GregorianCalendar
gc2(Locale::getEnglish(), status
);
436 if (failure(status
, "new GregorianCalendar")) return;
438 if (gc2
!= *gc
|| !(gc2
== *gc
)) errln("FAIL: GregorianCalendar assignment/operator==/operator!= failed");
443 // -------------------------------------
446 * This test confirms the correct behavior of add when incrementing
447 * through subsequent days.
450 CalendarTest::TestRog()
452 UErrorCode status
= U_ZERO_ERROR
;
453 GregorianCalendar
* gc
= new GregorianCalendar(status
);
454 if (U_FAILURE(status
)) { errln("Couldn't create GregorianCalendar"); return; }
455 int32_t year
= 1997, month
= UCAL_APRIL
, date
= 1;
456 gc
->set(year
, month
, date
);
457 gc
->set(UCAL_HOUR_OF_DAY
, 23);
458 gc
->set(UCAL_MINUTE
, 0);
459 gc
->set(UCAL_SECOND
, 0);
460 gc
->set(UCAL_MILLISECOND
, 0);
461 for (int32_t i
= 0; i
< 9; i
++, gc
->add(UCAL_DATE
, 1, status
)) {
462 if (U_FAILURE(status
)) { errln("Calendar::add failed"); return; }
463 if (gc
->get(UCAL_YEAR
, status
) != year
||
464 gc
->get(UCAL_MONTH
, status
) != month
||
465 gc
->get(UCAL_DATE
, status
) != (date
+ i
)) errln("FAIL: Date wrong");
466 if (U_FAILURE(status
)) { errln("Calendar::get failed"); return; }
471 // -------------------------------------
474 * Test the handling of the day of the week, checking for correctness and
475 * for correct minimum and maximum values.
478 CalendarTest::TestDOW943()
484 void CalendarTest::dowTest(UBool lenient
)
486 UErrorCode status
= U_ZERO_ERROR
;
487 GregorianCalendar
* cal
= new GregorianCalendar(status
);
488 if (U_FAILURE(status
)) { errln("Couldn't create GregorianCalendar"); return; }
489 logln("cal - Aug 12, 1997\n");
490 cal
->set(1997, UCAL_AUGUST
, 12);
491 cal
->getTime(status
);
492 if (U_FAILURE(status
)) { errln("Calendar::getTime failed"); return; }
493 logln((lenient
?UnicodeString("LENIENT0: "):UnicodeString("nonlenient0: ")) + UnicodeString(calToStr(*cal
)));
494 cal
->setLenient(lenient
);
495 logln("cal - Dec 1, 1996\n");
496 cal
->set(1996, UCAL_DECEMBER
, 1);
497 logln((lenient
?UnicodeString("LENIENT: "):UnicodeString("nonlenient: ")) + UnicodeString(calToStr(*cal
)));
498 int32_t dow
= cal
->get(UCAL_DAY_OF_WEEK
, status
);
499 if (U_FAILURE(status
)) { errln("Calendar::get failed [%s]", u_errorName(status
)); return; }
500 int32_t min
= cal
->getMinimum(UCAL_DAY_OF_WEEK
);
501 int32_t max
= cal
->getMaximum(UCAL_DAY_OF_WEEK
);
503 dow
> max
) errln(UnicodeString("FAIL: Day of week ") + (int32_t)dow
+ " out of range");
504 if (dow
!= UCAL_SUNDAY
) errln("FAIL: Day of week should be SUNDAY[%d] not %d", UCAL_SUNDAY
, dow
);
505 if (min
!= UCAL_SUNDAY
||
506 max
!= UCAL_SATURDAY
) errln("FAIL: Min/max bad");
510 // -------------------------------------
513 * Confirm that cloned Calendar objects do not inadvertently share substructures.
516 CalendarTest::TestClonesUnique908()
518 UErrorCode status
= U_ZERO_ERROR
;
519 Calendar
*c
= Calendar::createInstance(status
);
520 if (U_FAILURE(status
)) { errln("Calendar::createInstance failed"); return; }
521 Calendar
*d
= (Calendar
*) c
->clone();
522 c
->set(UCAL_MILLISECOND
, 123);
523 d
->set(UCAL_MILLISECOND
, 456);
524 if (c
->get(UCAL_MILLISECOND
, status
) != 123 ||
525 d
->get(UCAL_MILLISECOND
, status
) != 456) {
526 errln("FAIL: Clones share fields");
528 if (U_FAILURE(status
)) { errln("Calendar::get failed"); return; }
533 // -------------------------------------
536 * Confirm that the Gregorian cutoff value works as advertised.
539 CalendarTest::TestGregorianChange768()
542 UErrorCode status
= U_ZERO_ERROR
;
544 GregorianCalendar
* c
= new GregorianCalendar(status
);
545 if (U_FAILURE(status
)) { errln("Couldn't create GregorianCalendar"); return; }
546 logln(UnicodeString("With cutoff ") + dateToString(c
->getGregorianChange(), str
));
547 b
= c
->isLeapYear(1800);
548 logln(UnicodeString(" isLeapYear(1800) = ") + (b
? "true" : "false"));
549 logln(UnicodeString(" (should be FALSE)"));
550 if (b
) errln("FAIL");
551 c
->setGregorianChange(date(0, 0, 1), status
);
552 if (U_FAILURE(status
)) { errln("GregorianCalendar::setGregorianChange failed"); return; }
553 logln(UnicodeString("With cutoff ") + dateToString(c
->getGregorianChange(), str
));
554 b
= c
->isLeapYear(1800);
555 logln(UnicodeString(" isLeapYear(1800) = ") + (b
? "true" : "false"));
556 logln(UnicodeString(" (should be TRUE)"));
557 if (!b
) errln("FAIL");
561 // -------------------------------------
564 * Confirm the functioning of the field disambiguation algorithm.
567 CalendarTest::TestDisambiguation765()
569 UErrorCode status
= U_ZERO_ERROR
;
570 Calendar
*c
= Calendar::createInstance(status
);
571 if (U_FAILURE(status
)) { errln("Calendar::createInstance failed"); return; }
572 c
->setLenient(FALSE
);
574 c
->set(UCAL_YEAR
, 1997);
575 c
->set(UCAL_MONTH
, UCAL_JUNE
);
576 c
->set(UCAL_DATE
, 3);
577 verify765("1997 third day of June = ", c
, 1997, UCAL_JUNE
, 3);
579 c
->set(UCAL_YEAR
, 1997);
580 c
->set(UCAL_DAY_OF_WEEK
, UCAL_TUESDAY
);
581 c
->set(UCAL_MONTH
, UCAL_JUNE
);
582 c
->set(UCAL_DAY_OF_WEEK_IN_MONTH
, 1);
583 verify765("1997 first Tuesday in June = ", c
, 1997, UCAL_JUNE
, 3);
585 c
->set(UCAL_YEAR
, 1997);
586 c
->set(UCAL_DAY_OF_WEEK
, UCAL_TUESDAY
);
587 c
->set(UCAL_MONTH
, UCAL_JUNE
);
588 c
->set(UCAL_DAY_OF_WEEK_IN_MONTH
, - 1);
589 verify765("1997 last Tuesday in June = ", c
, 1997, UCAL_JUNE
, 24);
590 // IllegalArgumentException e = null;
591 status
= U_ZERO_ERROR
;
594 c
->set(UCAL_YEAR
, 1997);
595 c
->set(UCAL_DAY_OF_WEEK
, UCAL_TUESDAY
);
596 c
->set(UCAL_MONTH
, UCAL_JUNE
);
597 c
->set(UCAL_DAY_OF_WEEK_IN_MONTH
, 0);
600 //catch(IllegalArgumentException ex) {
603 verify765("1997 zero-th Tuesday in June = ", status
);
605 c
->set(UCAL_YEAR
, 1997);
606 c
->set(UCAL_DAY_OF_WEEK
, UCAL_TUESDAY
);
607 c
->set(UCAL_MONTH
, UCAL_JUNE
);
608 c
->set(UCAL_WEEK_OF_MONTH
, 1);
609 verify765("1997 Tuesday in week 1 of June = ", c
, 1997, UCAL_JUNE
, 3);
611 c
->set(UCAL_YEAR
, 1997);
612 c
->set(UCAL_DAY_OF_WEEK
, UCAL_TUESDAY
);
613 c
->set(UCAL_MONTH
, UCAL_JUNE
);
614 c
->set(UCAL_WEEK_OF_MONTH
, 5);
615 verify765("1997 Tuesday in week 5 of June = ", c
, 1997, UCAL_JULY
, 1);
616 status
= U_ZERO_ERROR
;
619 c
->set(UCAL_YEAR
, 1997);
620 c
->set(UCAL_DAY_OF_WEEK
, UCAL_TUESDAY
);
621 c
->set(UCAL_MONTH
, UCAL_JUNE
);
622 c
->set(UCAL_WEEK_OF_MONTH
, 0);
623 verify765("1997 Tuesday in week 0 of June = ", c
, 1997, UCAL_MAY
, 27);
625 //catch(IllegalArgumentException ex) {
626 // errln("FAIL: Exception seen:");
627 // ex.printStackTrace(log);
629 /* Note: The following test used to expect YEAR 1997, WOY 1 to
630 * resolve to a date in Dec 1996; that is, to behave as if
631 * YEAR_WOY were 1997. With the addition of a new explicit
632 * YEAR_WOY field, YEAR_WOY must itself be set if that is what is
633 * desired. Using YEAR in combination with WOY is ambiguous, and
634 * results in the first WOY/DOW day of the year satisfying the
635 * given fields (there may be up to two such days). In this case,
636 * it propertly resolves to Tue Dec 30 1997, which has a WOY value
637 * of 1 (for YEAR_WOY 1998) and a DOW of Tuesday, and falls in the
638 * _calendar_ year 1997, as specified. - aliu */
640 c
->set(UCAL_YEAR_WOY
, 1997); // aliu
641 c
->set(UCAL_DAY_OF_WEEK
, UCAL_TUESDAY
);
642 c
->set(UCAL_WEEK_OF_YEAR
, 1);
643 verify765("1997 Tuesday in week 1 of yearWOY = ", c
, 1996, UCAL_DECEMBER
, 31);
644 c
->clear(); // - add test for YEAR
645 c
->set(UCAL_YEAR
, 1997);
646 c
->set(UCAL_DAY_OF_WEEK
, UCAL_TUESDAY
);
647 c
->set(UCAL_WEEK_OF_YEAR
, 1);
648 verify765("1997 Tuesday in week 1 of year = ", c
, 1997, UCAL_DECEMBER
, 30);
650 c
->set(UCAL_YEAR
, 1997);
651 c
->set(UCAL_DAY_OF_WEEK
, UCAL_TUESDAY
);
652 c
->set(UCAL_WEEK_OF_YEAR
, 10);
653 verify765("1997 Tuesday in week 10 of year = ", c
, 1997, UCAL_MARCH
, 4);
656 // {sfb} week 0 is no longer a valid week of year
658 c->set(Calendar::YEAR, 1997);
659 c->set(Calendar::DAY_OF_WEEK, Calendar::TUESDAY);
660 //c->set(Calendar::WEEK_OF_YEAR, 0);
661 c->set(Calendar::WEEK_OF_YEAR, 1);
662 verify765("1997 Tuesday in week 0 of year = ", c, 1996, Calendar::DECEMBER, 24);*/
665 //catch(IllegalArgumentException ex) {
666 // errln("FAIL: Exception seen:");
667 // ex.printStackTrace(log);
672 // -------------------------------------
675 CalendarTest::verify765(const UnicodeString
& msg
, Calendar
* c
, int32_t year
, int32_t month
, int32_t day
)
678 UErrorCode status
= U_ZERO_ERROR
;
679 if (c
->get(UCAL_YEAR
, status
) == year
&&
680 c
->get(UCAL_MONTH
, status
) == month
&&
681 c
->get(UCAL_DATE
, status
) == day
) {
682 if (U_FAILURE(status
)) { errln("FAIL: Calendar::get failed"); return; }
683 logln("PASS: " + msg
+ dateToString(c
->getTime(status
), str
));
684 if (U_FAILURE(status
)) { errln("Calendar::getTime failed"); return; }
687 errln("FAIL: " + msg
+ dateToString(c
->getTime(status
), str
) + "; expected " + (int32_t)year
+ "/" + (int32_t)(month
+ 1) + "/" + (int32_t)day
);
688 if (U_FAILURE(status
)) { errln("Calendar::getTime failed"); return; }
692 // -------------------------------------
695 CalendarTest::verify765(const UnicodeString
& msg
/*, IllegalArgumentException e*/, UErrorCode status
)
697 if (status
!= U_ILLEGAL_ARGUMENT_ERROR
) errln("FAIL: No IllegalArgumentException for " + msg
);
698 else logln("PASS: " + msg
+ "IllegalArgument as expected");
701 // -------------------------------------
704 * Confirm that the offset between local time and GMT behaves as expected.
707 CalendarTest::TestGMTvsLocal4064654()
709 test4064654(1997, 1, 1, 12, 0, 0);
710 test4064654(1997, 4, 16, 18, 30, 0);
713 // -------------------------------------
716 CalendarTest::test4064654(int32_t yr
, int32_t mo
, int32_t dt
, int32_t hr
, int32_t mn
, int32_t sc
)
719 UErrorCode status
= U_ZERO_ERROR
;
721 Calendar
*gmtcal
= Calendar::createInstance(status
);
722 if (U_FAILURE(status
)) { errln("Calendar::createInstance failed"); return; }
723 gmtcal
->adoptTimeZone(TimeZone::createTimeZone("Africa/Casablanca"));
724 gmtcal
->set(yr
, mo
- 1, dt
, hr
, mn
, sc
);
725 gmtcal
->set(UCAL_MILLISECOND
, 0);
726 date
= gmtcal
->getTime(status
);
727 if (U_FAILURE(status
)) { errln("Calendar::getTime failed"); return; }
728 logln("date = " + dateToString(date
, str
));
729 Calendar
*cal
= Calendar::createInstance(status
);
730 if (U_FAILURE(status
)) { errln("Calendar::createInstance failed"); return; }
731 cal
->setTime(date
, status
);
732 if (U_FAILURE(status
)) { errln("Calendar::setTime failed"); return; }
733 int32_t offset
= cal
->getTimeZone().getOffset((uint8_t)cal
->get(UCAL_ERA
, status
),
734 cal
->get(UCAL_YEAR
, status
),
735 cal
->get(UCAL_MONTH
, status
),
736 cal
->get(UCAL_DATE
, status
),
737 (uint8_t)cal
->get(UCAL_DAY_OF_WEEK
, status
),
738 cal
->get(UCAL_MILLISECOND
, status
), status
);
739 if (U_FAILURE(status
)) { errln("Calendar::get failed"); return; }
740 logln("offset for " + dateToString(date
, str
) + "= " + (offset
/ 1000 / 60 / 60.0) + "hr");
741 int32_t utc
= ((cal
->get(UCAL_HOUR_OF_DAY
, status
) * 60 +
742 cal
->get(UCAL_MINUTE
, status
)) * 60 +
743 cal
->get(UCAL_SECOND
, status
)) * 1000 +
744 cal
->get(UCAL_MILLISECOND
, status
) - offset
;
745 if (U_FAILURE(status
)) { errln("Calendar::get failed"); return; }
746 int32_t expected
= ((hr
* 60 + mn
) * 60 + sc
) * 1000;
747 if (utc
!= expected
) errln(UnicodeString("FAIL: Discrepancy of ") + (utc
- expected
) +
748 " millis = " + ((utc
- expected
) / 1000 / 60 / 60.0) + " hr");
753 // -------------------------------------
756 * The operations of adding and setting should not exhibit pathological
757 * dependence on the order of operations. This test checks for this.
760 CalendarTest::TestAddSetOrder621()
762 UDate d
= date(97, 4, 14, 13, 23, 45);
763 UErrorCode status
= U_ZERO_ERROR
;
764 Calendar
*cal
= Calendar::createInstance(status
);
765 if (U_FAILURE(status
)) {
766 errln("Calendar::createInstance failed");
770 cal
->setTime(d
, status
);
771 if (U_FAILURE(status
)) {
772 errln("Calendar::setTime failed");
776 cal
->add(UCAL_DATE
, - 5, status
);
777 if (U_FAILURE(status
)) {
778 errln("Calendar::add failed");
782 cal
->set(UCAL_HOUR_OF_DAY
, 0);
783 cal
->set(UCAL_MINUTE
, 0);
784 cal
->set(UCAL_SECOND
, 0);
786 dateToString(cal
->getTime(status
), s
);
787 if (U_FAILURE(status
)) {
788 errln("Calendar::getTime failed");
794 cal
= Calendar::createInstance(status
);
795 if (U_FAILURE(status
)) {
796 errln("Calendar::createInstance failed");
800 cal
->setTime(d
, status
);
801 if (U_FAILURE(status
)) {
802 errln("Calendar::setTime failed");
806 cal
->set(UCAL_HOUR_OF_DAY
, 0);
807 cal
->set(UCAL_MINUTE
, 0);
808 cal
->set(UCAL_SECOND
, 0);
809 cal
->add(UCAL_DATE
, - 5, status
);
810 if (U_FAILURE(status
)) {
811 errln("Calendar::add failed");
816 dateToString(cal
->getTime(status
), s2
);
817 if (U_FAILURE(status
)) {
818 errln("Calendar::getTime failed");
823 logln("Pass: " + s
+ " == " + s2
);
825 errln("FAIL: " + s
+ " != " + s2
);
829 // -------------------------------------
832 * Confirm that adding to various fields works.
835 CalendarTest::TestAdd520()
837 int32_t y
= 1997, m
= UCAL_FEBRUARY
, d
= 1;
838 UErrorCode status
= U_ZERO_ERROR
;
839 GregorianCalendar
*temp
= new GregorianCalendar(y
, m
, d
, status
);
840 if (U_FAILURE(status
)) { errln("Couldn't create GregorianCalendar"); return; }
841 check520(temp
, y
, m
, d
);
842 temp
->add(UCAL_YEAR
, 1, status
);
843 if (U_FAILURE(status
)) { errln("Calendar::add failed"); return; }
845 check520(temp
, y
, m
, d
);
846 temp
->add(UCAL_MONTH
, 1, status
);
847 if (U_FAILURE(status
)) { errln("Calendar::add failed"); return; }
849 check520(temp
, y
, m
, d
);
850 temp
->add(UCAL_DATE
, 1, status
);
851 if (U_FAILURE(status
)) { errln("Calendar::add failed"); return; }
853 check520(temp
, y
, m
, d
);
854 temp
->add(UCAL_DATE
, 2, status
);
855 if (U_FAILURE(status
)) { errln("Calendar::add failed"); return; }
857 check520(temp
, y
, m
, d
);
858 temp
->add(UCAL_DATE
, 28, status
);
859 if (U_FAILURE(status
)) { errln("Calendar::add failed"); return; }
861 check520(temp
, y
, m
, d
);
865 // -------------------------------------
868 * Execute adding and rolling in GregorianCalendar extensively,
871 CalendarTest::TestAddRollExtensive()
873 int32_t maxlimit
= 40;
874 int32_t y
= 1997, m
= UCAL_FEBRUARY
, d
= 1, hr
= 1, min
= 1, sec
= 0, ms
= 0;
875 UErrorCode status
= U_ZERO_ERROR
;
876 GregorianCalendar
*temp
= new GregorianCalendar(y
, m
, d
, status
);
877 if (U_FAILURE(status
)) { errln("Couldn't create GregorianCalendar"); return; }
879 temp
->set(UCAL_HOUR
, hr
);
880 temp
->set(UCAL_MINUTE
, min
);
881 temp
->set(UCAL_SECOND
, sec
);
882 temp
->set(UCAL_MILLISECOND
, ms
);
884 UCalendarDateFields e
;
886 logln("Testing GregorianCalendar add...");
888 while (e
< UCAL_FIELD_COUNT
) {
890 int32_t limit
= maxlimit
;
891 status
= U_ZERO_ERROR
;
892 for (i
= 0; i
< limit
; i
++) {
893 temp
->add(e
, 1, status
);
894 if (U_FAILURE(status
)) { limit
= i
; status
= U_ZERO_ERROR
; }
896 for (i
= 0; i
< limit
; i
++) {
897 temp
->add(e
, -1, status
);
898 if (U_FAILURE(status
)) { errln("GregorianCalendar::add -1 failed"); return; }
900 check520(temp
, y
, m
, d
, hr
, min
, sec
, ms
, e
);
902 e
= (UCalendarDateFields
) ((int32_t) e
+ 1);
905 logln("Testing GregorianCalendar roll...");
907 while (e
< UCAL_FIELD_COUNT
) {
909 int32_t limit
= maxlimit
;
910 status
= U_ZERO_ERROR
;
911 for (i
= 0; i
< limit
; i
++) {
912 logln(calToStr(*temp
) + UnicodeString(" " ) + fieldName(e
) + UnicodeString("++") );
913 temp
->roll(e
, 1, status
);
914 if (U_FAILURE(status
)) {
915 logln("caltest.cpp:%d e=%d, i=%d - roll(+) err %s\n", __LINE__
, (int) e
, (int) i
, u_errorName(status
));
916 logln(calToStr(*temp
));
917 limit
= i
; status
= U_ZERO_ERROR
;
920 for (i
= 0; i
< limit
; i
++) {
921 logln("caltest.cpp:%d e=%d, i=%d\n", __LINE__
, (int) e
, (int) i
);
922 logln(calToStr(*temp
) + UnicodeString(" " ) + fieldName(e
) + UnicodeString("--") );
923 temp
->roll(e
, -1, status
);
924 if (U_FAILURE(status
)) { errln(UnicodeString("GregorianCalendar::roll ") + CalendarTest::fieldName(e
) + " count=" + UnicodeString('@'+i
) + " by -1 failed with " + u_errorName(status
) ); return; }
926 check520(temp
, y
, m
, d
, hr
, min
, sec
, ms
, e
);
928 e
= (UCalendarDateFields
) ((int32_t) e
+ 1);
934 // -------------------------------------
936 CalendarTest::check520(Calendar
* c
,
937 int32_t y
, int32_t m
, int32_t d
,
938 int32_t hr
, int32_t min
, int32_t sec
,
939 int32_t ms
, UCalendarDateFields field
)
942 UErrorCode status
= U_ZERO_ERROR
;
943 if (c
->get(UCAL_YEAR
, status
) != y
||
944 c
->get(UCAL_MONTH
, status
) != m
||
945 c
->get(UCAL_DATE
, status
) != d
||
946 c
->get(UCAL_HOUR
, status
) != hr
||
947 c
->get(UCAL_MINUTE
, status
) != min
||
948 c
->get(UCAL_SECOND
, status
) != sec
||
949 c
->get(UCAL_MILLISECOND
, status
) != ms
) {
950 errln(UnicodeString("U_FAILURE for field ") + (int32_t)field
+
951 ": Expected y/m/d h:m:s:ms of " +
952 y
+ "/" + (m
+ 1) + "/" + d
+ " " +
953 hr
+ ":" + min
+ ":" + sec
+ ":" + ms
+
954 "; got " + c
->get(UCAL_YEAR
, status
) +
955 "/" + (c
->get(UCAL_MONTH
, status
) + 1) +
956 "/" + c
->get(UCAL_DATE
, status
) +
957 " " + c
->get(UCAL_HOUR
, status
) + ":" +
958 c
->get(UCAL_MINUTE
, status
) + ":" +
959 c
->get(UCAL_SECOND
, status
) + ":" +
960 c
->get(UCAL_MILLISECOND
, status
)
963 if (U_FAILURE(status
)) { errln("Calendar::get failed"); return; }
966 logln(UnicodeString("Confirmed: ") + y
+ "/" +
967 (m
+ 1) + "/" + d
+ " " +
968 hr
+ ":" + min
+ ":" + sec
+ ":" + ms
);
971 // -------------------------------------
973 CalendarTest::check520(Calendar
* c
,
974 int32_t y
, int32_t m
, int32_t d
)
977 UErrorCode status
= U_ZERO_ERROR
;
978 if (c
->get(UCAL_YEAR
, status
) != y
||
979 c
->get(UCAL_MONTH
, status
) != m
||
980 c
->get(UCAL_DATE
, status
) != d
) {
981 errln(UnicodeString("FAILURE: Expected y/m/d of ") +
982 y
+ "/" + (m
+ 1) + "/" + d
+ " " +
983 "; got " + c
->get(UCAL_YEAR
, status
) +
984 "/" + (c
->get(UCAL_MONTH
, status
) + 1) +
985 "/" + c
->get(UCAL_DATE
, status
)
988 if (U_FAILURE(status
)) { errln("Calendar::get failed"); return; }
991 logln(UnicodeString("Confirmed: ") + y
+ "/" +
995 // -------------------------------------
998 * Test that setting of fields works. In particular, make sure that all instances
999 * of GregorianCalendar don't share a static instance of the fields array.
1002 CalendarTest::TestFieldSet4781()
1005 UErrorCode status
= U_ZERO_ERROR
;
1006 GregorianCalendar
*g
= new GregorianCalendar(status
);
1007 if (U_FAILURE(status
)) { errln("Couldn't create GregorianCalendar"); return; }
1008 GregorianCalendar
*g2
= new GregorianCalendar(status
);
1009 if (U_FAILURE(status
)) { errln("Couldn't create GregorianCalendar"); return; }
1010 g2
->set(UCAL_HOUR
, 12, status
);
1011 g2
->set(UCAL_MINUTE
, 0, status
);
1012 g2
->set(UCAL_SECOND
, 0, status
);
1013 if (U_FAILURE(status
)) { errln("Calendar::set failed"); return; }
1014 if (*g
== *g2
) logln("Same");
1015 else logln("Different");
1017 //catch(IllegalArgumentException e) {
1018 //errln("Unexpected exception seen: " + e);
1024 // -------------------------------------
1026 /* We don't support serialization on C++
1028 CalendarTest::TestSerialize337()
1030 Calendar cal = Calendar::getInstance();
1033 FileOutputStream f = new FileOutputStream(FILENAME);
1034 ObjectOutput s = new ObjectOutputStream(f);
1035 s.writeObject(PREFIX);
1037 s.writeObject(POSTFIX);
1039 FileInputStream in = new FileInputStream(FILENAME);
1040 ObjectInputStream t = new ObjectInputStream(in);
1041 UnicodeString& pre = (UnicodeString&) t.readObject();
1042 Calendar c = (Calendar) t.readObject();
1043 UnicodeString& post = (UnicodeString&) t.readObject();
1045 ok = pre.equals(PREFIX) &&
1046 post.equals(POSTFIX) &&
1048 File fl = new File(FILENAME);
1051 catch(IOException e) {
1052 errln("FAIL: Exception received:");
1053 e.printStackTrace(log);
1055 catch(ClassNotFoundException e) {
1056 errln("FAIL: Exception received:");
1057 e.printStackTrace(log);
1059 if (!ok) errln("Serialization of Calendar object failed.");
1062 UnicodeString& CalendarTest::PREFIX = "abc";
1064 UnicodeString& CalendarTest::POSTFIX = "def";
1066 UnicodeString& CalendarTest::FILENAME = "tmp337.bin";
1069 // -------------------------------------
1072 * Verify that the seconds of a Calendar can be zeroed out through the
1073 * expected sequence of operations.
1076 CalendarTest::TestSecondsZero121()
1078 UErrorCode status
= U_ZERO_ERROR
;
1079 Calendar
*cal
= new GregorianCalendar(status
);
1080 if (U_FAILURE(status
)) { errln("Couldn't create GregorianCalendar"); return; }
1081 cal
->setTime(Calendar::getNow(), status
);
1082 if (U_FAILURE(status
)) { errln("Calendar::setTime failed"); return; }
1083 cal
->set(UCAL_SECOND
, 0);
1084 if (U_FAILURE(status
)) { errln("Calendar::set failed"); return; }
1085 UDate d
= cal
->getTime(status
);
1086 if (U_FAILURE(status
)) { errln("Calendar::getTime failed"); return; }
1089 if (s
.indexOf(":00 ") < 0) errln("Expected to see :00 in " + s
);
1093 // -------------------------------------
1096 * Verify that a specific sequence of adding and setting works as expected;
1097 * it should not vary depending on when and whether the get method is
1101 CalendarTest::TestAddSetGet0610()
1103 UnicodeString
EXPECTED_0610("1993/0/5", "");
1104 UErrorCode status
= U_ZERO_ERROR
;
1106 Calendar
*calendar
= new GregorianCalendar(status
);
1107 if (U_FAILURE(status
)) { errln("Couldn't create GregorianCalendar"); return; }
1108 calendar
->set(1993, UCAL_JANUARY
, 4);
1109 logln("1A) " + value(calendar
));
1110 calendar
->add(UCAL_DATE
, 1, status
);
1111 if (U_FAILURE(status
)) { errln("Calendar::add failed"); return; }
1112 UnicodeString v
= value(calendar
);
1114 logln("--) 1993/0/5");
1115 if (!(v
== EXPECTED_0610
)) errln("Expected " + EXPECTED_0610
+ "; saw " + v
);
1119 Calendar
*calendar
= new GregorianCalendar(1993, UCAL_JANUARY
, 4, status
);
1120 if (U_FAILURE(status
)) { errln("Couldn't create GregorianCalendar"); return; }
1121 logln("2A) " + value(calendar
));
1122 calendar
->add(UCAL_DATE
, 1, status
);
1123 if (U_FAILURE(status
)) { errln("Calendar::add failed"); return; }
1124 UnicodeString v
= value(calendar
);
1126 logln("--) 1993/0/5");
1127 if (!(v
== EXPECTED_0610
)) errln("Expected " + EXPECTED_0610
+ "; saw " + v
);
1131 Calendar
*calendar
= new GregorianCalendar(1993, UCAL_JANUARY
, 4, status
);
1132 if (U_FAILURE(status
)) { errln("Couldn't create GregorianCalendar"); return; }
1133 logln("3A) " + value(calendar
));
1134 calendar
->getTime(status
);
1135 if (U_FAILURE(status
)) { errln("Calendar::getTime failed"); return; }
1136 calendar
->add(UCAL_DATE
, 1, status
);
1137 if (U_FAILURE(status
)) { errln("Calendar::add failed"); return; }
1138 UnicodeString v
= value(calendar
);
1140 logln("--) 1993/0/5");
1141 if (!(v
== EXPECTED_0610
)) errln("Expected " + EXPECTED_0610
+ "; saw " + v
);
1146 // -------------------------------------
1149 CalendarTest::value(Calendar
* calendar
)
1151 UErrorCode status
= U_ZERO_ERROR
;
1152 return UnicodeString("") + (int32_t)calendar
->get(UCAL_YEAR
, status
) +
1153 "/" + (int32_t)calendar
->get(UCAL_MONTH
, status
) +
1154 "/" + (int32_t)calendar
->get(UCAL_DATE
, status
) +
1155 (U_FAILURE(status
) ? " FAIL: Calendar::get failed" : "");
1159 // -------------------------------------
1162 * Verify that various fields on a known date are set correctly.
1165 CalendarTest::TestFields060()
1167 UErrorCode status
= U_ZERO_ERROR
;
1168 int32_t year
= 1997;
1169 int32_t month
= UCAL_OCTOBER
;
1171 GregorianCalendar
*calendar
= 0;
1172 calendar
= new GregorianCalendar(year
, month
, dDate
, status
);
1173 if (U_FAILURE(status
)) { errln("Couldn't create GregorianCalendar"); return; }
1174 for (int32_t i
= 0; i
< EXPECTED_FIELDS_length
;) {
1175 UCalendarDateFields field
= (UCalendarDateFields
)EXPECTED_FIELDS
[i
++];
1176 int32_t expected
= EXPECTED_FIELDS
[i
++];
1177 if (calendar
->get(field
, status
) != expected
) {
1178 errln(UnicodeString("Expected field ") + (int32_t)field
+ " to have value " + (int32_t)expected
+
1179 "; received " + (int32_t)calendar
->get(field
, status
) + " instead");
1180 if (U_FAILURE(status
)) { errln("Calendar::get failed"); return; }
1186 int32_t CalendarTest::EXPECTED_FIELDS
[] = {
1188 UCAL_MONTH
, UCAL_OCTOBER
,
1190 UCAL_DAY_OF_WEEK
, UCAL_WEDNESDAY
,
1191 UCAL_DAY_OF_WEEK_IN_MONTH
, 4,
1192 UCAL_DAY_OF_YEAR
, 295
1195 const int32_t CalendarTest::EXPECTED_FIELDS_length
= (int32_t)(sizeof(CalendarTest::EXPECTED_FIELDS
) /
1196 sizeof(CalendarTest::EXPECTED_FIELDS
[0]));
1198 // -------------------------------------
1201 * Verify that various fields on a known date are set correctly. In this
1202 * case, the start of the epoch (January 1 1970).
1205 CalendarTest::TestEpochStartFields()
1207 UErrorCode status
= U_ZERO_ERROR
;
1208 TimeZone
*z
= TimeZone::createDefault();
1209 Calendar
*c
= Calendar::createInstance(status
);
1210 if (U_FAILURE(status
)) { errln("Calendar::createInstance failed"); return; }
1211 UDate d
= - z
->getRawOffset();
1212 GregorianCalendar
*gc
= new GregorianCalendar(status
);
1213 if (U_FAILURE(status
)) { errln("Couldn't create GregorianCalendar"); return; }
1214 gc
->setTimeZone(*z
);
1215 gc
->setTime(d
, status
);
1216 if (U_FAILURE(status
)) { errln("Calendar::setTime failed"); return; }
1217 UBool idt
= gc
->inDaylightTime(status
);
1218 if (U_FAILURE(status
)) { errln("GregorianCalendar::inDaylightTime failed"); return; }
1221 logln("Warning: Skipping test because " + dateToString(d
, str
) + " is in DST.");
1224 c
->setTime(d
, status
);
1225 if (U_FAILURE(status
)) { errln("Calendar::setTime failed"); return; }
1226 for (int32_t i
= 0; i
< UCAL_ZONE_OFFSET
;++i
) {
1227 if (c
->get((UCalendarDateFields
)i
, status
) != EPOCH_FIELDS
[i
])
1228 errln(UnicodeString("Expected field ") + i
+ " to have value " + EPOCH_FIELDS
[i
] +
1229 "; saw " + c
->get((UCalendarDateFields
)i
, status
) + " instead");
1230 if (U_FAILURE(status
)) { errln("Calendar::get failed"); return; }
1232 if (c
->get(UCAL_ZONE_OFFSET
, status
) != z
->getRawOffset())
1234 errln(UnicodeString("Expected field ZONE_OFFSET to have value ") + z
->getRawOffset() +
1235 "; saw " + c
->get(UCAL_ZONE_OFFSET
, status
) + " instead");
1236 if (U_FAILURE(status
)) { errln("Calendar::get failed"); return; }
1238 if (c
->get(UCAL_DST_OFFSET
, status
) != 0)
1240 errln(UnicodeString("Expected field DST_OFFSET to have value 0") +
1241 "; saw " + c
->get(UCAL_DST_OFFSET
, status
) + " instead");
1242 if (U_FAILURE(status
)) { errln("Calendar::get failed"); return; }
1250 int32_t CalendarTest::EPOCH_FIELDS
[] = {
1251 1, 1970, 0, 1, 1, 1, 1, 5, 1, 0, 0, 0, 0, 0, 0, - 28800000, 0
1254 // -------------------------------------
1257 * Test that the days of the week progress properly when add is called repeatedly
1258 * for increments of 24 days.
1261 CalendarTest::TestDOWProgression()
1263 UErrorCode status
= U_ZERO_ERROR
;
1264 Calendar
*cal
= new GregorianCalendar(1972, UCAL_OCTOBER
, 26, status
);
1265 if (U_FAILURE(status
)) { errln("Couldn't create GregorianCalendar"); return; }
1266 marchByDelta(cal
, 24);
1270 // -------------------------------------
1273 CalendarTest::TestDOW_LOCALandYEAR_WOY()
1275 /* Note: I've commented out the loop_addroll tests for YEAR and
1276 * YEAR_WOY below because these two fields should NOT behave
1277 * identically when adding. YEAR should keep the month/dom
1278 * invariant. YEAR_WOY should keep the woy/dow invariant. I've
1279 * added a new test that checks for this in place of the old call
1280 * to loop_addroll. - aliu */
1281 UErrorCode status
= U_ZERO_ERROR
;
1283 Calendar
*cal
=Calendar::createInstance(Locale::getGermany(), status
);
1284 if (U_FAILURE(status
)) { errln("Couldn't create GregorianCalendar"); return; }
1285 SimpleDateFormat
*sdf
=new SimpleDateFormat(UnicodeString("YYYY'-W'ww-ee"), Locale::getGermany(), status
);
1286 if (U_FAILURE(status
)) { errln("Couldn't create SimpleDateFormat"); return; }
1287 sdf
->applyLocalizedPattern(UnicodeString("JJJJ'-W'ww-ee"), status
);
1288 if (U_FAILURE(status
)) { errln("Couldn't apply localized pattern"); return; }
1290 cal
->set(1997, UCAL_DECEMBER
, 25);
1291 doYEAR_WOYLoop(cal
, sdf
, times
, status
);
1292 //loop_addroll(cal, /*sdf,*/ times, UCAL_YEAR_WOY, UCAL_YEAR, status);
1293 yearAddTest(*cal
, status
); // aliu
1294 loop_addroll(cal
, /*sdf,*/ times
, UCAL_DOW_LOCAL
, UCAL_DAY_OF_WEEK
, status
);
1295 if (U_FAILURE(status
)) { errln("Error in parse/calculate test for 1997"); return; }
1298 cal
->set(1998, UCAL_DECEMBER
, 25);
1299 doYEAR_WOYLoop(cal
, sdf
, times
, status
);
1300 //loop_addroll(cal, /*sdf,*/ times, UCAL_YEAR_WOY, UCAL_YEAR, status);
1301 yearAddTest(*cal
, status
); // aliu
1302 loop_addroll(cal
, /*sdf,*/ times
, UCAL_DOW_LOCAL
, UCAL_DAY_OF_WEEK
, status
);
1303 if (U_FAILURE(status
)) { errln("Error in parse/calculate test for 1998"); return; }
1306 cal
->set(1582, UCAL_OCTOBER
, 1);
1307 doYEAR_WOYLoop(cal
, sdf
, times
, status
);
1308 //loop_addroll(cal, /*sdf,*/ times, Calendar::YEAR_WOY, Calendar::YEAR, status);
1309 yearAddTest(*cal
, status
); // aliu
1310 loop_addroll(cal
, /*sdf,*/ times
, UCAL_DOW_LOCAL
, UCAL_DAY_OF_WEEK
, status
);
1311 if (U_FAILURE(status
)) { errln("Error in parse/calculate test for 1582"); return; }
1319 * Confirm that adding a YEAR and adding a YEAR_WOY work properly for
1320 * the given Calendar at its current setting.
1322 void CalendarTest::yearAddTest(Calendar
& cal
, UErrorCode
& status
) {
1324 * When adding the YEAR, the month and day should remain constant.
1325 * When adding the YEAR_WOY, the WOY and DOW should remain constant. - aliu
1327 * Wed Jan 14 1998 / 1998-W03-03 Add(YEAR_WOY, 1) -> Wed Jan 20 1999 / 1999-W03-03
1328 * Add(YEAR, 1) -> Thu Jan 14 1999 / 1999-W02-04
1329 * Thu Jan 14 1999 / 1999-W02-04 Add(YEAR_WOY, 1) -> Thu Jan 13 2000 / 2000-W02-04
1330 * Add(YEAR, 1) -> Fri Jan 14 2000 / 2000-W02-05
1331 * Sun Oct 31 1582 / 1582-W42-07 Add(YEAR_WOY, 1) -> Sun Oct 23 1583 / 1583-W42-07
1332 * Add(YEAR, 1) -> Mon Oct 31 1583 / 1583-W44-01
1334 int32_t y
= cal
.get(UCAL_YEAR
, status
);
1335 int32_t mon
= cal
.get(UCAL_MONTH
, status
);
1336 int32_t day
= cal
.get(UCAL_DATE
, status
);
1337 int32_t ywy
= cal
.get(UCAL_YEAR_WOY
, status
);
1338 int32_t woy
= cal
.get(UCAL_WEEK_OF_YEAR
, status
);
1339 int32_t dow
= cal
.get(UCAL_DOW_LOCAL
, status
);
1340 UDate t
= cal
.getTime(status
);
1342 UnicodeString str
, str2
;
1343 SimpleDateFormat
fmt(UnicodeString("EEE MMM dd yyyy / YYYY'-W'ww-ee"), status
);
1344 fmt
.setCalendar(cal
);
1346 fmt
.format(t
, str
.remove());
1347 str
+= ".add(YEAR, 1) =>";
1348 cal
.add(UCAL_YEAR
, 1, status
);
1349 int32_t y2
= cal
.get(UCAL_YEAR
, status
);
1350 int32_t mon2
= cal
.get(UCAL_MONTH
, status
);
1351 int32_t day2
= cal
.get(UCAL_DATE
, status
);
1352 fmt
.format(cal
.getTime(status
), str
);
1353 if (y2
!= (y
+1) || mon2
!= mon
|| day2
!= day
) {
1354 str
+= (UnicodeString
)", expected year " +
1355 (y
+1) + ", month " + (mon
+1) + ", day " + day
;
1356 errln((UnicodeString
)"FAIL: " + str
);
1357 logln( UnicodeString(" -> ") + CalendarTest::calToStr(cal
) );
1362 fmt
.format(t
, str
.remove());
1363 str
+= ".add(YEAR_WOY, 1)=>";
1364 cal
.setTime(t
, status
);
1365 logln( UnicodeString(" <- ") + CalendarTest::calToStr(cal
) );
1366 cal
.add(UCAL_YEAR_WOY
, 1, status
);
1367 int32_t ywy2
= cal
.get(UCAL_YEAR_WOY
, status
);
1368 int32_t woy2
= cal
.get(UCAL_WEEK_OF_YEAR
, status
);
1369 int32_t dow2
= cal
.get(UCAL_DOW_LOCAL
, status
);
1370 fmt
.format(cal
.getTime(status
), str
);
1371 if (ywy2
!= (ywy
+1) || woy2
!= woy
|| dow2
!= dow
) {
1372 str
+= (UnicodeString
)", expected yearWOY " +
1373 (ywy
+1) + ", woy " + woy
+ ", dowLocal " + dow
;
1374 errln((UnicodeString
)"FAIL: " + str
);
1375 logln( UnicodeString(" -> ") + CalendarTest::calToStr(cal
) );
1381 // -------------------------------------
1383 void CalendarTest::loop_addroll(Calendar
*cal
, /*SimpleDateFormat *sdf,*/ int times
, UCalendarDateFields field
, UCalendarDateFields field2
, UErrorCode
& errorCode
) {
1385 SimpleDateFormat
fmt(UnicodeString("EEE MMM dd yyyy / YYYY'-W'ww-ee"), errorCode
);
1386 fmt
.setCalendar(*cal
);
1389 for(i
= 0; i
<times
; i
++) {
1390 calclone
= cal
->clone();
1391 UDate start
= cal
->getTime(errorCode
);
1392 cal
->add(field
,1,errorCode
);
1393 if (U_FAILURE(errorCode
)) { errln("Error in add"); delete calclone
; return; }
1394 calclone
->add(field2
,1,errorCode
);
1395 if (U_FAILURE(errorCode
)) { errln("Error in add"); delete calclone
; return; }
1396 if(cal
->getTime(errorCode
) != calclone
->getTime(errorCode
)) {
1397 UnicodeString
str("FAIL: Results of add differ. "), str2
;
1398 str
+= fmt
.format(start
, str2
) + " ";
1399 str
+= UnicodeString("Add(") + fieldName(field
) + ", 1) -> " +
1400 fmt
.format(cal
->getTime(errorCode
), str2
.remove()) + "; ";
1401 str
+= UnicodeString("Add(") + fieldName(field2
) + ", 1) -> " +
1402 fmt
.format(calclone
->getTime(errorCode
), str2
.remove());
1410 for(i
= 0; i
<times
; i
++) {
1411 calclone
= cal
->clone();
1412 cal
->roll(field
,(int32_t)1,errorCode
);
1413 if (U_FAILURE(errorCode
)) { errln("Error in roll"); delete calclone
; return; }
1414 calclone
->roll(field2
,(int32_t)1,errorCode
);
1415 if (U_FAILURE(errorCode
)) { errln("Error in roll"); delete calclone
; return; }
1416 if(cal
->getTime(errorCode
) != calclone
->getTime(errorCode
)) {
1418 errln("Results of roll differ!");
1425 // -------------------------------------
1428 CalendarTest::doYEAR_WOYLoop(Calendar
*cal
, SimpleDateFormat
*sdf
,
1429 int32_t times
, UErrorCode
& errorCode
) {
1432 UDate tst
, original
;
1433 Calendar
*tstres
= new GregorianCalendar(Locale::getGermany(), errorCode
);
1434 for(int i
=0; i
<times
; ++i
) {
1435 sdf
->format(Formattable(cal
->getTime(errorCode
),Formattable::kIsDate
), us
, errorCode
);
1436 //logln("expected: "+us);
1437 if (U_FAILURE(errorCode
)) { errln("Format error"); return; }
1438 tst
=sdf
->parse(us
,errorCode
);
1439 if (U_FAILURE(errorCode
)) { errln("Parse error"); return; }
1441 tstres
->setTime(tst
, errorCode
);
1442 //logln((UnicodeString)"Parsed week of year is "+tstres->get(UCAL_WEEK_OF_YEAR, errorCode));
1443 if (U_FAILURE(errorCode
)) { errln("Set time error"); return; }
1444 original
= cal
->getTime(errorCode
);
1446 sdf
->format(Formattable(tst
,Formattable::kIsDate
), us
, errorCode
);
1447 //logln("got: "+us);
1448 if (U_FAILURE(errorCode
)) { errln("Get time error"); return; }
1451 sdf
->format(Formattable(original
, Formattable::kIsDate
), us
, errorCode
);
1452 errln("FAIL: Parsed time doesn't match with regular");
1453 logln("expected "+us
+ " " + calToStr(*cal
));
1455 sdf
->format(Formattable(tst
, Formattable::kIsDate
), us
, errorCode
);
1456 logln("got "+us
+ " " + calToStr(*tstres
));
1459 tstres
->set(UCAL_YEAR_WOY
, cal
->get(UCAL_YEAR_WOY
, errorCode
));
1460 tstres
->set(UCAL_WEEK_OF_YEAR
, cal
->get(UCAL_WEEK_OF_YEAR
, errorCode
));
1461 tstres
->set(UCAL_DOW_LOCAL
, cal
->get(UCAL_DOW_LOCAL
, errorCode
));
1462 if(cal
->get(UCAL_YEAR
, errorCode
) != tstres
->get(UCAL_YEAR
, errorCode
)) {
1463 errln("FAIL: Different Year!");
1464 logln((UnicodeString
)"Expected "+cal
->get(UCAL_YEAR
, errorCode
));
1465 logln((UnicodeString
)"Got "+tstres
->get(UCAL_YEAR
, errorCode
));
1468 if(cal
->get(UCAL_DAY_OF_YEAR
, errorCode
) != tstres
->get(UCAL_DAY_OF_YEAR
, errorCode
)) {
1469 errln("FAIL: Different Day Of Year!");
1470 logln((UnicodeString
)"Expected "+cal
->get(UCAL_DAY_OF_YEAR
, errorCode
));
1471 logln((UnicodeString
)"Got "+tstres
->get(UCAL_DAY_OF_YEAR
, errorCode
));
1474 //logln(calToStr(*cal));
1475 cal
->add(UCAL_DATE
, 1, errorCode
);
1476 if (U_FAILURE(errorCode
)) { errln("Add error"); return; }
1481 // -------------------------------------
1484 CalendarTest::marchByDelta(Calendar
* cal
, int32_t delta
)
1486 UErrorCode status
= U_ZERO_ERROR
;
1487 Calendar
*cur
= (Calendar
*) cal
->clone();
1488 int32_t initialDOW
= cur
->get(UCAL_DAY_OF_WEEK
, status
);
1489 if (U_FAILURE(status
)) { errln("Calendar::get failed"); return; }
1490 int32_t DOW
, newDOW
= initialDOW
;
1494 logln(UnicodeString("DOW = ") + DOW
+ " " + dateToString(cur
->getTime(status
), str
));
1495 if (U_FAILURE(status
)) { errln("Calendar::getTime failed"); return; }
1496 cur
->add(UCAL_DAY_OF_WEEK
, delta
, status
);
1497 if (U_FAILURE(status
)) { errln("Calendar::add failed"); return; }
1498 newDOW
= cur
->get(UCAL_DAY_OF_WEEK
, status
);
1499 if (U_FAILURE(status
)) { errln("Calendar::get failed"); return; }
1500 int32_t expectedDOW
= 1 + (DOW
+ delta
- 1) % 7;
1501 if (newDOW
!= expectedDOW
) {
1502 errln(UnicodeString("Day of week should be ") + expectedDOW
+ " instead of " + newDOW
+
1503 " on " + dateToString(cur
->getTime(status
), str
));
1504 if (U_FAILURE(status
)) { errln("Calendar::getTime failed"); return; }
1508 while (newDOW
!= initialDOW
);
1512 #define CHECK(status, msg) \
1513 if (U_FAILURE(status)) { \
1518 void CalendarTest::TestWOY(void) {
1520 FDW = Mon, MDFW = 4:
1521 Sun Dec 26 1999, WOY 51
1522 Mon Dec 27 1999, WOY 52
1523 Tue Dec 28 1999, WOY 52
1524 Wed Dec 29 1999, WOY 52
1525 Thu Dec 30 1999, WOY 52
1526 Fri Dec 31 1999, WOY 52
1527 Sat Jan 01 2000, WOY 52 ***
1528 Sun Jan 02 2000, WOY 52 ***
1529 Mon Jan 03 2000, WOY 1
1530 Tue Jan 04 2000, WOY 1
1531 Wed Jan 05 2000, WOY 1
1532 Thu Jan 06 2000, WOY 1
1533 Fri Jan 07 2000, WOY 1
1534 Sat Jan 08 2000, WOY 1
1535 Sun Jan 09 2000, WOY 1
1536 Mon Jan 10 2000, WOY 2
1538 FDW = Mon, MDFW = 2:
1539 Sun Dec 26 1999, WOY 52
1540 Mon Dec 27 1999, WOY 1 ***
1541 Tue Dec 28 1999, WOY 1 ***
1542 Wed Dec 29 1999, WOY 1 ***
1543 Thu Dec 30 1999, WOY 1 ***
1544 Fri Dec 31 1999, WOY 1 ***
1545 Sat Jan 01 2000, WOY 1
1546 Sun Jan 02 2000, WOY 1
1547 Mon Jan 03 2000, WOY 2
1548 Tue Jan 04 2000, WOY 2
1549 Wed Jan 05 2000, WOY 2
1550 Thu Jan 06 2000, WOY 2
1551 Fri Jan 07 2000, WOY 2
1552 Sat Jan 08 2000, WOY 2
1553 Sun Jan 09 2000, WOY 2
1554 Mon Jan 10 2000, WOY 3
1558 UErrorCode status
= U_ZERO_ERROR
;
1561 GregorianCalendar
cal(status
);
1562 SimpleDateFormat
fmt(UnicodeString("EEE MMM dd yyyy', WOY' w"), status
);
1563 CHECK(status
, "Fail: Cannot construct calendar/format");
1565 UCalendarDaysOfWeek fdw
= (UCalendarDaysOfWeek
) 0;
1567 //for (int8_t pass=2; pass<=2; ++pass) {
1568 for (int8_t pass
=1; pass
<=2; ++pass
) {
1572 cal
.setFirstDayOfWeek(fdw
);
1573 cal
.setMinimalDaysInFirstWeek(4);
1574 fmt
.adoptCalendar(cal
.clone());
1578 cal
.setFirstDayOfWeek(fdw
);
1579 cal
.setMinimalDaysInFirstWeek(2);
1580 fmt
.adoptCalendar(cal
.clone());
1584 //for (i=2; i<=6; ++i) {
1585 for (i
=0; i
<16; ++i
) {
1587 int32_t t_y
, t_woy
, t_dow
;
1589 cal
.set(1999, UCAL_DECEMBER
, 26 + i
);
1590 fmt
.format(t
= cal
.getTime(status
), str
.remove());
1591 CHECK(status
, "Fail: getTime failed");
1592 logln(UnicodeString("* ") + str
);
1593 int32_t dow
= cal
.get(UCAL_DAY_OF_WEEK
, status
);
1594 int32_t woy
= cal
.get(UCAL_WEEK_OF_YEAR
, status
);
1595 int32_t year
= cal
.get(UCAL_YEAR
, status
);
1596 int32_t mon
= cal
.get(UCAL_MONTH
, status
);
1597 logln(calToStr(cal
));
1598 CHECK(status
, "Fail: get failed");
1599 int32_t dowLocal
= dow
- fdw
;
1600 if (dowLocal
< 0) dowLocal
+= 7;
1602 int32_t yearWoy
= year
;
1603 if (mon
== UCAL_JANUARY
) {
1604 if (woy
>= 52) --yearWoy
;
1606 if (woy
== 1) ++yearWoy
;
1609 // Basic fields->time check y/woy/dow
1610 // Since Y/WOY is ambiguous, we do a check of the fields,
1611 // not of the specific time.
1613 cal
.set(UCAL_YEAR
, year
);
1614 cal
.set(UCAL_WEEK_OF_YEAR
, woy
);
1615 cal
.set(UCAL_DAY_OF_WEEK
, dow
);
1616 t_y
= cal
.get(UCAL_YEAR
, status
);
1617 t_woy
= cal
.get(UCAL_WEEK_OF_YEAR
, status
);
1618 t_dow
= cal
.get(UCAL_DAY_OF_WEEK
, status
);
1619 CHECK(status
, "Fail: get failed");
1620 if (t_y
!= year
|| t_woy
!= woy
|| t_dow
!= dow
) {
1621 str
= "Fail: y/woy/dow fields->time => ";
1622 fmt
.format(cal
.getTime(status
), str
);
1624 logln(calToStr(cal
));
1625 logln("[get!=set] Y%d!=%d || woy%d!=%d || dow%d!=%d\n",
1626 t_y
, year
, t_woy
, woy
, t_dow
, dow
);
1628 logln("y/woy/dow fields->time OK");
1631 // Basic fields->time check y/woy/dow_local
1632 // Since Y/WOY is ambiguous, we do a check of the fields,
1633 // not of the specific time.
1635 cal
.set(UCAL_YEAR
, year
);
1636 cal
.set(UCAL_WEEK_OF_YEAR
, woy
);
1637 cal
.set(UCAL_DOW_LOCAL
, dowLocal
);
1638 t_y
= cal
.get(UCAL_YEAR
, status
);
1639 t_woy
= cal
.get(UCAL_WEEK_OF_YEAR
, status
);
1640 t_dow
= cal
.get(UCAL_DOW_LOCAL
, status
);
1641 CHECK(status
, "Fail: get failed");
1642 if (t_y
!= year
|| t_woy
!= woy
|| t_dow
!= dowLocal
) {
1643 str
= "Fail: y/woy/dow_local fields->time => ";
1644 fmt
.format(cal
.getTime(status
), str
);
1648 // Basic fields->time check y_woy/woy/dow
1650 cal
.set(UCAL_YEAR_WOY
, yearWoy
);
1651 cal
.set(UCAL_WEEK_OF_YEAR
, woy
);
1652 cal
.set(UCAL_DAY_OF_WEEK
, dow
);
1653 t2
= cal
.getTime(status
);
1654 CHECK(status
, "Fail: getTime failed");
1656 str
= "Fail: y_woy/woy/dow fields->time => ";
1657 fmt
.format(t2
, str
);
1659 logln(calToStr(cal
));
1660 logln("%.f != %.f\n", t
, t2
);
1662 logln("y_woy/woy/dow OK");
1665 // Basic fields->time check y_woy/woy/dow_local
1667 cal
.set(UCAL_YEAR_WOY
, yearWoy
);
1668 cal
.set(UCAL_WEEK_OF_YEAR
, woy
);
1669 cal
.set(UCAL_DOW_LOCAL
, dowLocal
);
1670 t2
= cal
.getTime(status
);
1671 CHECK(status
, "Fail: getTime failed");
1673 str
= "Fail: y_woy/woy/dow_local fields->time => ";
1674 fmt
.format(t2
, str
);
1678 logln("Testing DOW_LOCAL.. dow%d\n", dow
);
1679 // Make sure DOW_LOCAL disambiguates over DOW
1680 int32_t wrongDow
= dow
- 3;
1681 if (wrongDow
< 1) wrongDow
+= 7;
1682 cal
.setTime(t
, status
);
1683 cal
.set(UCAL_DAY_OF_WEEK
, wrongDow
);
1684 cal
.set(UCAL_DOW_LOCAL
, dowLocal
);
1685 t2
= cal
.getTime(status
);
1686 CHECK(status
, "Fail: set/getTime failed");
1688 str
= "Fail: DOW_LOCAL fields->time => ";
1689 fmt
.format(t2
, str
);
1691 logln(calToStr(cal
));
1692 logln("%.f : DOW%d, DOW_LOCAL%d -> %.f\n",
1693 t
, wrongDow
, dowLocal
, t2
);
1696 // Make sure DOW disambiguates over DOW_LOCAL
1697 int32_t wrongDowLocal
= dowLocal
- 3;
1698 if (wrongDowLocal
< 1) wrongDowLocal
+= 7;
1699 cal
.setTime(t
, status
);
1700 cal
.set(UCAL_DOW_LOCAL
, wrongDowLocal
);
1701 cal
.set(UCAL_DAY_OF_WEEK
, dow
);
1702 t2
= cal
.getTime(status
);
1703 CHECK(status
, "Fail: set/getTime failed");
1705 str
= "Fail: DOW fields->time => ";
1706 fmt
.format(t2
, str
);
1710 // Make sure YEAR_WOY disambiguates over YEAR
1711 cal
.setTime(t
, status
);
1712 cal
.set(UCAL_YEAR
, year
- 2);
1713 cal
.set(UCAL_YEAR_WOY
, yearWoy
);
1714 t2
= cal
.getTime(status
);
1715 CHECK(status
, "Fail: set/getTime failed");
1717 str
= "Fail: YEAR_WOY fields->time => ";
1718 fmt
.format(t2
, str
);
1722 // Make sure YEAR disambiguates over YEAR_WOY
1723 cal
.setTime(t
, status
);
1724 cal
.set(UCAL_YEAR_WOY
, yearWoy
- 2);
1725 cal
.set(UCAL_YEAR
, year
);
1726 t2
= cal
.getTime(status
);
1727 CHECK(status
, "Fail: set/getTime failed");
1729 str
= "Fail: YEAR fields->time => ";
1730 fmt
.format(t2
, str
);
1737 FDW = Mon, MDFW = 4:
1738 Sun Dec 26 1999, WOY 51
1739 Mon Dec 27 1999, WOY 52
1740 Tue Dec 28 1999, WOY 52
1741 Wed Dec 29 1999, WOY 52
1742 Thu Dec 30 1999, WOY 52
1743 Fri Dec 31 1999, WOY 52
1744 Sat Jan 01 2000, WOY 52
1745 Sun Jan 02 2000, WOY 52
1748 // Roll the DOW_LOCAL within week 52
1749 for (i
=27; i
<=33; ++i
) {
1751 for (amount
=-7; amount
<=7; ++amount
) {
1753 cal
.set(1999, UCAL_DECEMBER
, i
);
1755 fmt
.format(cal
.getTime(status
), str
);
1756 CHECK(status
, "Fail: getTime failed");
1757 str
+= UnicodeString(", ") + amount
+ ") = ";
1759 cal
.roll(UCAL_DOW_LOCAL
, amount
, status
);
1760 CHECK(status
, "Fail: roll failed");
1762 t
= cal
.getTime(status
);
1763 int32_t newDom
= i
+ amount
;
1764 while (newDom
< 27) newDom
+= 7;
1765 while (newDom
> 33) newDom
-= 7;
1766 cal
.set(1999, UCAL_DECEMBER
, newDom
);
1767 t2
= cal
.getTime(status
);
1768 CHECK(status
, "Fail: getTime failed");
1772 str
.append(", exp ");
1773 fmt
.format(t2
, str
);
1782 void CalendarTest::TestYWOY()
1785 UErrorCode status
= U_ZERO_ERROR
;
1787 GregorianCalendar
cal(status
);
1788 CHECK(status
, "Fail: Cannot construct calendar/format");
1790 cal
.setFirstDayOfWeek(UCAL_SUNDAY
);
1791 cal
.setMinimalDaysInFirstWeek(1);
1793 logln("Setting: ywoy=2004, woy=1, dow=MONDAY");
1795 cal
.set(UCAL_YEAR_WOY
,2004);
1796 cal
.set(UCAL_WEEK_OF_YEAR
,1);
1797 cal
.set(UCAL_DAY_OF_WEEK
, UCAL_MONDAY
);
1799 logln(calToStr(cal
));
1800 if(cal
.get(UCAL_YEAR
, status
) != 2003) {
1801 errln("year not 2003");
1804 logln("+ setting DOW to THURSDAY");
1806 cal
.set(UCAL_YEAR_WOY
,2004);
1807 cal
.set(UCAL_WEEK_OF_YEAR
,1);
1808 cal
.set(UCAL_DAY_OF_WEEK
, UCAL_THURSDAY
);
1810 logln(calToStr(cal
));
1811 if(cal
.get(UCAL_YEAR
, status
) != 2004) {
1812 errln("year not 2004");
1815 logln("+ setting DOW_LOCAL to 1");
1817 cal
.set(UCAL_YEAR_WOY
,2004);
1818 cal
.set(UCAL_WEEK_OF_YEAR
,1);
1819 cal
.set(UCAL_DAY_OF_WEEK
, UCAL_THURSDAY
);
1820 cal
.set(UCAL_DOW_LOCAL
, 1);
1822 logln(calToStr(cal
));
1823 if(cal
.get(UCAL_YEAR
, status
) != 2003) {
1824 errln("year not 2003");
1827 cal
.setFirstDayOfWeek(UCAL_MONDAY
);
1828 cal
.setMinimalDaysInFirstWeek(4);
1829 UDate t
= 946713600000.;
1830 cal
.setTime(t
, status
);
1831 cal
.set(UCAL_DAY_OF_WEEK
, 4);
1832 cal
.set(UCAL_DOW_LOCAL
, 6);
1833 if(cal
.getTime(status
) != t
) {
1834 logln(calToStr(cal
));
1835 errln("FAIL: DOW_LOCAL did not take precedence");
1840 void CalendarTest::TestJD()
1843 static const int32_t kEpochStartAsJulianDay
= 2440588;
1844 UErrorCode status
= U_ZERO_ERROR
;
1845 GregorianCalendar
cal(status
);
1846 cal
.setTimeZone(*TimeZone::getGMT());
1848 jd
= cal
.get(UCAL_JULIAN_DAY
, status
);
1849 if(jd
!= kEpochStartAsJulianDay
) {
1850 errln("Wanted JD of %d at time=0, [epoch 1970] but got %d\n", kEpochStartAsJulianDay
, jd
);
1852 logln("Wanted JD of %d at time=0, [epoch 1970], got %d\n", kEpochStartAsJulianDay
, jd
);
1855 cal
.setTime(Calendar::getNow(), status
);
1857 cal
.set(UCAL_JULIAN_DAY
, kEpochStartAsJulianDay
);
1858 UDate epochTime
= cal
.getTime(status
);
1859 if(epochTime
!= 0) {
1860 errln("Wanted time of 0 at jd=%d, got %.1lf\n", kEpochStartAsJulianDay
, epochTime
);
1862 logln("Wanted time of 0 at jd=%d, got %.1lf\n", kEpochStartAsJulianDay
, epochTime
);
1869 // List of interesting locales
1870 const char *CalendarTest::testLocaleID(int32_t i
)
1873 case 0: return "he_IL@calendar=hebrew";
1874 case 1: return "en_US@calendar=hebrew";
1875 case 2: return "fr_FR@calendar=hebrew";
1876 case 3: return "fi_FI@calendar=hebrew";
1877 case 4: return "nl_NL@calendar=hebrew";
1878 case 5: return "hu_HU@calendar=hebrew";
1879 case 6: return "nl_BE@currency=MTL;calendar=islamic";
1880 case 7: return "th_TH_TRADITIONAL@calendar=gregorian";
1881 case 8: return "ar_JO@calendar=islamic-civil";
1882 case 9: return "fi_FI@calendar=islamic";
1883 case 10: return "fr_CH@calendar=islamic-civil";
1884 case 11: return "he_IL@calendar=islamic-civil";
1885 case 12: return "hu_HU@calendar=buddhist";
1886 case 13: return "hu_HU@calendar=islamic";
1887 case 14: return "en_US@calendar=japanese";
1888 default: return NULL
;
1892 int32_t CalendarTest::testLocaleCount()
1894 static int32_t gLocaleCount
= -1;
1895 if(gLocaleCount
< 0) {
1897 for(i
=0;testLocaleID(i
) != NULL
;i
++) {
1902 return gLocaleCount
;
1905 static UDate
doMinDateOfCalendar(Calendar
* adopt
, UBool
&isGregorian
, UErrorCode
& status
) {
1906 if(U_FAILURE(status
)) return 0.0;
1909 adopt
->set(UCAL_EXTENDED_YEAR
, adopt
->getActualMinimum(UCAL_EXTENDED_YEAR
, status
));
1910 UDate ret
= adopt
->getTime(status
);
1911 isGregorian
= (adopt
->getDynamicClassID() == GregorianCalendar::getStaticClassID());
1916 UDate
CalendarTest::minDateOfCalendar(const Locale
& locale
, UBool
&isGregorian
, UErrorCode
& status
) {
1917 if(U_FAILURE(status
)) return 0.0;
1918 return doMinDateOfCalendar(Calendar::createInstance(locale
, status
), isGregorian
, status
);
1921 UDate
CalendarTest::minDateOfCalendar(const Calendar
& cal
, UBool
&isGregorian
, UErrorCode
& status
) {
1922 if(U_FAILURE(status
)) return 0.0;
1923 return doMinDateOfCalendar(cal
.clone(), isGregorian
, status
);
1928 #endif /* #if !UCONFIG_NO_FORMATTING */