void TimeZoneBoundaryTest::verifyDST(UDate d, TimeZone* time_zone, UBool expUseDaylightTime, UBool expInDaylightTime, UDate expZoneOffset, UDate expDSTOffset) { UnicodeString str; UErrorCode status = U_ZERO_ERROR; logln("-- Verifying time " + dateToString(d) + " in zone " + time_zone->getID(str)); if (time_zone->inDaylightTime(d, status) == expInDaylightTime) logln(UnicodeString("PASS: inDaylightTime = ") + (time_zone->inDaylightTime(d, status)?"true":"false")); else dataerrln(UnicodeString("FAIL: inDaylightTime = ") + (time_zone->inDaylightTime(d, status)?"true":"false")); if (failure(status, "TimeZone::inDaylightTime", TRUE)) return; if (time_zone->useDaylightTime() == expUseDaylightTime) logln(UnicodeString("PASS: useDaylightTime = ") + (time_zone->useDaylightTime()?"true":"false")); else dataerrln(UnicodeString("FAIL: useDaylightTime = ") + (time_zone->useDaylightTime()?"true":"false")); if (time_zone->getRawOffset() == expZoneOffset) logln(UnicodeString("PASS: getRawOffset() = ") + (expZoneOffset / ONE_HOUR)); else dataerrln(UnicodeString("FAIL: getRawOffset() = ") + (time_zone->getRawOffset() / ONE_HOUR) + "; expected " + (expZoneOffset / ONE_HOUR)); GregorianCalendar *gc = new GregorianCalendar(time_zone->clone(), status); gc->setTime(d, status); if (failure(status, "GregorianCalendar::setTime")) return; int32_t offset = time_zone->getOffset((uint8_t)gc->get(UCAL_ERA, status), gc->get(UCAL_YEAR, status), gc->get(UCAL_MONTH, status), gc->get(UCAL_DATE, status), (uint8_t)gc->get(UCAL_DAY_OF_WEEK, status), ((gc->get(UCAL_HOUR_OF_DAY, status) * 60 + gc->get(UCAL_MINUTE, status)) * 60 + gc->get(UCAL_SECOND, status)) * 1000 + gc->get(UCAL_MILLISECOND, status), status); if (failure(status, "GregorianCalendar::get")) return; if (offset == expDSTOffset) logln(UnicodeString("PASS: getOffset() = ") + (offset / ONE_HOUR)); else dataerrln(UnicodeString("FAIL: getOffset() = ") + (offset / ONE_HOUR) + "; expected " + (expDSTOffset / ONE_HOUR)); delete gc; }
/** * Overrides TimeZone * Queries if the given date is in Daylight Savings Time. */ UBool SimpleTimeZone::inDaylightTime(UDate date, UErrorCode& status) const { // This method is wasteful since it creates a new GregorianCalendar and // deletes it each time it is called. However, this is a deprecated method // and provided only for Java compatibility as of 8/6/97 [LIU]. if (U_FAILURE(status)) return FALSE; GregorianCalendar *gc = new GregorianCalendar(*this, status); /* test for NULL */ if (gc == 0) { status = U_MEMORY_ALLOCATION_ERROR; return FALSE; } gc->setTime(date, status); UBool result = gc->inDaylightTime(status); delete gc; return result; }
void GregorianCalendar::setGregorianChange(UDate date, UErrorCode& status) { if (U_FAILURE(status)) return; fGregorianCutover = date; // Precompute two internal variables which we use to do the actual // cutover computations. These are the normalized cutover, which is the // midnight at or before the cutover, and the cutover year. The // normalized cutover is in pure date milliseconds; it contains no time // of day or timezone component, and it used to compare against other // pure date values. int32_t cutoverDay = (int32_t)ClockMath::floorDivide(fGregorianCutover, (double)kOneDay); fNormalizedGregorianCutover = cutoverDay * kOneDay; // Handle the rare case of numeric overflow. If the user specifies a // change of UDate(Long.MIN_VALUE), in order to get a pure Gregorian // calendar, then the epoch day is -106751991168, which when multiplied // by ONE_DAY gives 9223372036794351616 -- the negative value is too // large for 64 bits, and overflows into a positive value. We correct // this by using the next day, which for all intents is semantically // equivalent. if (cutoverDay < 0 && fNormalizedGregorianCutover > 0) { fNormalizedGregorianCutover = (cutoverDay + 1) * kOneDay; } // Normalize the year so BC values are represented as 0 and negative // values. GregorianCalendar *cal = new GregorianCalendar(getTimeZone(), status); /* test for NULL */ if (cal == 0) { status = U_MEMORY_ALLOCATION_ERROR; return; } if(U_FAILURE(status)) return; cal->setTime(date, status); fGregorianCutoverYear = cal->get(UCAL_YEAR, status); if (cal->get(UCAL_ERA, status) == BC) fGregorianCutoverYear = 1 - fGregorianCutoverYear; fCutoverJulianDay = cutoverDay; delete cal; }
/** * @bug 4109314 */ void TimeZoneRegressionTest:: Test4109314() { UErrorCode status = U_ZERO_ERROR; GregorianCalendar *testCal = (GregorianCalendar*)Calendar::createInstance(status); if(U_FAILURE(status)) { dataerrln("Error creating calendar %s", u_errorName(status)); delete testCal; return; } failure(status, "Calendar::createInstance"); TimeZone *PST = TimeZone::createTimeZone("PST"); /*Object[] testData = { PST, new Date(98,Calendar.APRIL,4,22,0), new Date(98, Calendar.APRIL, 5,6,0), PST, new Date(98,Calendar.OCTOBER,24,22,0), new Date(98,Calendar.OCTOBER,25,6,0), };*/ UDate testData [] = { CalendarRegressionTest::makeDate(98,UCAL_APRIL,4,22,0), CalendarRegressionTest::makeDate(98, UCAL_APRIL,5,6,0), CalendarRegressionTest::makeDate(98,UCAL_OCTOBER,24,22,0), CalendarRegressionTest::makeDate(98,UCAL_OCTOBER,25,6,0) }; UBool pass = TRUE; for (int32_t i = 0; i < 4; i+=2) { //testCal->setTimeZone((TimeZone) testData[i]); testCal->setTimeZone(*PST); UDate t = testData[i]; UDate end = testData[i+1]; while(testCal->getTime(status) < end) { testCal->setTime(t, status); if ( ! checkCalendar314(testCal, PST)) pass = FALSE; t += 60*60*1000.0; } } if ( ! pass) errln("Fail: TZ API inconsistent"); delete testCal; delete PST; }
/** * @bug 4106807 */ void DateFormatRegressionTest::Test4106807(void) { UDate dt; DateFormat *df = DateFormat::createDateTimeInstance(); UErrorCode status = U_ZERO_ERROR; SimpleDateFormat *sdfs [] = { new SimpleDateFormat(UnicodeString("yyyyMMddHHmmss"), status), new SimpleDateFormat(UnicodeString("yyyyMMddHHmmss'Z'"), status), new SimpleDateFormat(UnicodeString("yyyyMMddHHmmss''"), status), new SimpleDateFormat(UnicodeString("yyyyMMddHHmmss'a''a'"), status), new SimpleDateFormat(UnicodeString("yyyyMMddHHmmss %"), status) }; if(U_FAILURE(status)) { dataerrln("Couldn't create SimpleDateFormat, error %s", u_errorName(status)); delete sdfs[0]; delete sdfs[1]; delete sdfs[2]; delete sdfs[3]; delete sdfs[4]; return; } failure(status, "new SimpleDateFormat"); UnicodeString strings [] = { (UnicodeString) "19980211140000", (UnicodeString) "19980211140000", (UnicodeString) "19980211140000", (UnicodeString) "19980211140000a", (UnicodeString) "19980211140000 " }; /*Object[] data = { new SimpleDateFormat("yyyyMMddHHmmss"), "19980211140000", new SimpleDateFormat("yyyyMMddHHmmss'Z'"), "19980211140000", new SimpleDateFormat("yyyyMMddHHmmss''"), "19980211140000", new SimpleDateFormat("yyyyMMddHHmmss'a''a'"), "19980211140000a", new SimpleDateFormat("yyyyMMddHHmmss %"), "19980211140000 ", };*/ GregorianCalendar *gc = new GregorianCalendar(status); failure(status, "new GregorianCalendar"); TimeZone *timeZone = TimeZone::createDefault(); TimeZone *gmt = timeZone->clone(); gmt->setRawOffset(0); for(int32_t i = 0; i < 5; i++) { SimpleDateFormat *format = sdfs[i]; UnicodeString dateString = strings[i]; //try { format->setTimeZone(*gmt); dt = format->parse(dateString, status); // {sfb} some of these parses will fail purposely if(U_FAILURE(status)) break; status = U_ZERO_ERROR; UnicodeString fmtd; FieldPosition pos(FieldPosition::DONT_CARE); fmtd = df->format(dt, fmtd, pos); logln(fmtd); //logln(df->format(dt)); gc->setTime(dt, status); failure(status, "gc->getTime"); logln(UnicodeString("") + gc->get(UCAL_ZONE_OFFSET, status)); failure(status, "gc->get"); UnicodeString s; s = format->format(dt, s, pos); logln(s); /*} catch (ParseException e) { logln("No way Jose"); }*/ } delete timeZone; delete df; for(int32_t j = 0; j < 5; j++) delete sdfs [j]; delete gc; delete gmt; }