/********************************************************************
* COPYRIGHT:
- * Copyright (c) 1997-2010, International Business Machines Corporation
+ * Copyright (c) 1997-2016, International Business Machines Corporation
* and others. All Rights Reserved.
********************************************************************/
#include "unicode/strenum.h"
#include "cmemory.h"
#include "caltest.h"
+#include "unicode/localpointer.h"
#include <float.h>
CASE(44,TestDeprecates);
CASE(45,TestT5555);
CASE(46,TestT6745);
+ CASE(47,TestT8057);
+ CASE(48,TestT8596);
+ CASE(49,Test9019);
+ CASE(50,TestT9452);
default: name = ""; break;
}
}
delete cal2;
}
+
+void
+CalendarRegressionTest::Test9019()
+{
+ UErrorCode status = U_ZERO_ERROR;
+ LocalPointer<GregorianCalendar> cal1(new GregorianCalendar(status), status);
+ LocalPointer<GregorianCalendar> cal2(new GregorianCalendar(status), status);
+ if(U_FAILURE(status)) {
+ dataerrln("Error creating Calendar: %s", u_errorName(status));
+ return;
+ }
+ cal1->set(UCAL_HOUR, 1);
+ cal2->set(UCAL_HOUR,2);
+ cal1->clear();
+ cal2->clear();
+ failure(status, "new GregorianCalendar");
+ cal1->set(2011,UCAL_MAY,06);
+ cal2->set(2012,UCAL_JANUARY,06);
+ printdate(cal1.getAlias(), "cal1: ") ;
+ cal1->setLenient(FALSE);
+ cal1->add(UCAL_MONTH,8,status);
+ failure(status, "->add(UCAL_MONTH,8)");
+ printdate(cal1.getAlias(), "cal1 (lenient) after adding 8 months:") ;
+ printdate(cal2.getAlias(), "cal2 (expected date):") ;
+
+ if(!cal1->equals(*cal2,status)) {
+ errln("Error: cal1 != cal2.\n");
+ }
+ failure(status, "equals");
+}
+
void
CalendarRegressionTest::printdate(GregorianCalendar *cal, const char *string)
{
// require the host zone to be set; it can be set in Java.
UErrorCode status = U_ZERO_ERROR;
StringEnumeration* ids = TimeZone::createEnumeration();
+ if (ids == NULL) {
+ dataerrln("Unable to create TimeZone Enumeration.");
+ return;
+ }
UBool bad = FALSE;
TimeZone* tz =TimeZone::createTimeZone("Asia/Riyadh87");
failure(status, "new TimeZone");
{
int32_t count = 0;
StringEnumeration* ids = TimeZone::createEnumeration(-8 * 60 * 60 * 1000);
+ if (ids == NULL) {
+ dataerrln("Unable to create TimeZone enumeration.");
+ return;
+ }
UErrorCode status = U_ZERO_ERROR;
count = ids->count(status);
+ (void)count; // Suppress set but not used warning.
SimpleTimeZone *pdt = new SimpleTimeZone(-8 * 60 * 60 * 1000, *ids->snext(status));
pdt->setStartRule(UCAL_APRIL, 1, UCAL_SUNDAY, 2 * 60 * 60 * 1000, status);
pdt->setEndRule(UCAL_OCTOBER, -1, UCAL_SUNDAY, 2 * 60 * 60 * 1000, status);
if (dow < min || dow > max)
errln("FAIL: Day of week %d out of range [%d,%d]\n", dow, min, max);
if (dow != UCAL_SUNDAY)
- errln("FAIL: Day of week should be SUNDAY Got " + dow);
+ errln(UnicodeString("FAIL: Day of week should be SUNDAY Got ") + dow);
if(U_FAILURE(status)) {
errln("Error checking Calendar: %s", u_errorName(status));
logln( UnicodeString(" Cal1 = ") + cal1->getTime(status) );
logln( UnicodeString(" Cal1 time in ms = ") + cal1->get(UCAL_MILLISECOND,status) );
- for( int32_t k = 0; k < 100 ; k++ );
+ for (int32_t k = 0; k < 100 ; k++)
+ ;
GregorianCalendar *cal2 = new GregorianCalendar(1997, 10, 11, 10, 20, 40,status);
/*cal2.set( Calendar::YEAR, 1997 );
}
int32_t dow = a->get(UCAL_DAY_OF_WEEK, status);
if (dow != UCAL_THURSDAY)
- errln("Fail: Want THURSDAY Got " + dow);
+ errln(UnicodeString("Fail: Want THURSDAY Got ") + dow);
delete a;
}
return;
}
DateFormat *fmt = new SimpleDateFormat(UnicodeString("MMMM d, yyyy G"),status);
- if(!assertSuccess("trying to construct", status))return;
+ if(U_FAILURE(status)) {
+ dataerrln("Error creating SimpleDateFormat - %s", u_errorName(status));
+ delete cal;
+ return;
+ }
cal->clear();
for (int32_t y=-20; y<=10; ++y) {
cal->set(UCAL_ERA, y < 1 ? GregorianCalendar::BC : GregorianCalendar::AD);
return;
}
DateFormat *fmt = new SimpleDateFormat(UnicodeString("MMMM d, yyyy G"),status);
- if(!assertSuccess("trying to construct", status))return;
+ if(U_FAILURE(status)) {
+ dataerrln("Error creating SimpleDateFormat - %s", u_errorName(status));
+ delete cal;
+ return;
+ }
cal->clear();
cal->set(UCAL_ERA, GregorianCalendar::BC);
cal->set(UCAL_YEAR, 81); // 81 BC is a leap year (proleptically)
UnicodeString temp;
errln("test failed with zone " + zones[i]->getID(temp));
errln(" cutover date is Date(Long.MAX_VALUE)");
- errln(" isLeapYear(100) returns: " + is100Leap);
+ errln(UnicodeString(" isLeapYear(100) returns: ") + is100Leap);
}
delete calendar;
}
2000, UCAL_JANUARY, 1, 52,
2001, UCAL_DECEMBER, 31, 1,
};
- int32_t DATA_length = (int32_t)(sizeof(DATA) / sizeof(DATA[0]));
+ int32_t DATA_length = UPRV_LENGTHOF(DATA);
UnicodeString str;
DateFormat& dfmt = *(DateFormat*)&fmt;
30, 42, 293, UCAL_SATURDAY,
31, 43, 294, UCAL_SUNDAY
};
- int32_t DOY_DATA_length = (int32_t)(sizeof(DOY_DATA) / sizeof(DOY_DATA[0]));
+ int32_t DOY_DATA_length = UPRV_LENGTHOF(DOY_DATA);
for (i=0; i<DOY_DATA_length; i+=4) {
// Test time->fields
// { Calendar::YEAR_WOY, ADD|ROLL, 1, -ONE_DAY, +6*ONE_DAY },
// { Calendar::DOW_LOCAL, ADD|ROLL, 2, -ONE_DAY, +1*ONE_DAY }
};
- int32_t DATA_length = (int32_t)(sizeof(DATA) / sizeof(DATA[0]));
+ int32_t DATA_length = UPRV_LENGTHOF(DATA);
// Now run the tests
for (i=0; i<DATA_length; ++i) {
1964, UCAL_SEPTEMBER, 7, 1999, UCAL_JUNE, 4,
1999, UCAL_JUNE, 4, 1964, UCAL_SEPTEMBER, 7,
};
- int32_t DATA_length = (int32_t)(sizeof(DATA)/sizeof(DATA[0]));
+ int32_t DATA_length = UPRV_LENGTHOF(DATA);
Calendar* pcal = Calendar::createInstance(Locale::getUS(), ec);
if(U_FAILURE(ec)) {
dataerrln("Error creating calendar %s", u_errorName(ec));
SimpleDateFormat fmt(UnicodeString("MMM dd yyyy",""), ec);
fmt.setCalendar(cal);
UnicodeString s, t, u;
- if (!failure(ec, "setup")) {
+ if (U_SUCCESS(ec)) {
for (i=0; i<DATA_length; i+=6) {
int32_t y1 = DATA[i];
int32_t m1 = DATA[i+1];
fmt.format(date22, t.remove()));
}
}
+ } else {
+ dataerrln("Error creating SimpleDateFormat - %s", u_errorName(ec));
}
delete pcal;
}
StringEnumeration *tz = TimeZone::createEnumeration();
if (tz == NULL) {
- errln("FAIL: TimeZone::createEnumeration");
+ dataerrln("FAIL: TimeZone::createEnumeration");
return;
}
delete c1;
delete c2;
+
}
+void CalendarRegressionTest::TestT8057(void) {
+ // Set the calendar to the last day in a leap year
+ UErrorCode status = U_ZERO_ERROR;
+ GregorianCalendar *cal = (GregorianCalendar*)Calendar::createInstance(status);
+ if(U_FAILURE(status)) {
+ errln("Error creating Calendar: %s", u_errorName(status));
+ delete cal;
+ return;
+ }
+ cal->setLenient(FALSE);
+ cal->clear();
+ cal->set(2008, UCAL_DECEMBER, 31);
+
+ // Force calculating then fields once.
+ UDate t = cal->getTime(status);
+ if(U_FAILURE(status)) {
+ errln("Error while calculating the date");
+ delete cal;
+ return;
+ }
+
+ UDate expected = 1262246400000.0; // 2009-12-31 00:00 PST
+
+ cal->add(UCAL_YEAR, 1, status);
+ t = cal->getTime(status);
+ if (U_SUCCESS(status)) {
+ if (t != expected) {
+ dataerrln((UnicodeString)"FAIL: wrong date after add: expected=" + expected + " returned=" + t);
+ }
+ } else {
+ errln("FAIL: error while adding one year");
+ }
+
+ delete cal;
+}
+
+// Test case for ticket#8596.
+// Setting an year followed by getActualMaximum(Calendar.WEEK_OF_YEAR)
+// may result wrong maximum week.
+void CalendarRegressionTest::TestT8596(void) {
+ UErrorCode status = U_ZERO_ERROR;
+ GregorianCalendar *gc = new GregorianCalendar(*TimeZone::getGMT(), status);
+
+ if (U_FAILURE(status)) {
+ dataerrln("Error creating Calendar: %s", u_errorName(status));
+ delete gc;
+ return;
+ }
+
+ gc->setFirstDayOfWeek(UCAL_MONDAY);
+ gc->setMinimalDaysInFirstWeek(4);
+
+ // Force the calender to resolve the fields once.
+ // The maximum week number in 2011 is 52.
+ gc->set(UCAL_YEAR, 2011);
+ gc->get(UCAL_YEAR, status);
+
+ // Set a date in year 2009, but not calling get to resolve
+ // the calendar's internal field yet.
+ gc->set(2009, UCAL_JULY, 1);
+
+ // Then call getActuamMaximum for week of year.
+ // #8596 was caused by conflict between year set
+ // above and internal work calendar field resolution.
+ int32_t maxWeeks = gc->getActualMaximum(UCAL_WEEK_OF_YEAR, status);
+
+ if (U_FAILURE(status)) {
+ errln("Error calendar calculation: %s", u_errorName(status));
+ delete gc;
+ return;
+ }
+
+ if (maxWeeks != 53) {
+ errln((UnicodeString)"FAIL: Max week in 2009 in ISO calendar is 53, but got " + maxWeeks);
+ }
+
+ delete gc;
+}
+
+// Test case for ticket 9452
+// Calendar addition fall onto the missing date - 2011-12-30 in Samoa
+void CalendarRegressionTest::TestT9452(void) {
+ UErrorCode status = U_ZERO_ERROR;
+ GregorianCalendar cal(TimeZone::createTimeZone("Pacific/Apia"), status);
+ failure(status, "initializing GregorianCalendar");
+
+ SimpleDateFormat sdf(UnicodeString("y-MM-dd'T'HH:mm:ssZZZZZ"), status);
+ failure(status, "initializing SimpleDateFormat");
+ sdf.setCalendar(cal);
+
+ UnicodeString dstr;
+
+ // Set date to 2011-12-29 00:00
+ cal.clear();
+ cal.set(2011, UCAL_DECEMBER, 29, 0, 0, 0);
+
+ UDate d = cal.getTime(status);
+ if (!failure(status, "getTime for initial date")) {
+ sdf.format(d, dstr);
+ logln(UnicodeString("Initial date: ") + dstr);
+
+ // Add 1 day
+ cal.add(UCAL_DATE, 1, status);
+ failure(status, "add 1 day");
+ d = cal.getTime(status);
+ failure(status, "getTime after +1 day");
+ dstr.remove();
+ sdf.format(d, dstr);
+ logln(UnicodeString("+1 day: ") + dstr);
+ assertEquals("Add 1 day", UnicodeString("2011-12-31T00:00:00+14:00"), dstr);
+
+ // Subtract 1 day
+ cal.add(UCAL_DATE, -1, status);
+ failure(status, "subtract 1 day");
+ d = cal.getTime(status);
+ failure(status, "getTime after -1 day");
+ dstr.remove();
+ sdf.format(d, dstr);
+ logln(UnicodeString("-1 day: ") + dstr);
+ assertEquals("Subtract 1 day", UnicodeString("2011-12-29T00:00:00-10:00"), dstr);
+ }
+}
#endif /* #if !UCONFIG_NO_FORMATTING */