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; }
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; }
void cpp_main() { UErrorCode status = U_ZERO_ERROR; puts("C++ sample"); GregorianCalendar* gc = new GregorianCalendar(status); if (U_FAILURE(status)) { puts("Couldn't create GregorianCalendar"); return; } /* set up the date */ gc->set(2000, UCAL_FEBRUARY, 26); gc->set(UCAL_HOUR_OF_DAY, 23); gc->set(UCAL_MINUTE, 0); gc->set(UCAL_SECOND, 0); gc->set(UCAL_MILLISECOND, 0); /* Iterate through the days and print it out. */ for (int32_t i = 0; i < 30; i++) { /* print out the date. */ /* You should use the DateFormat to properly format it */ printf("year: %d, month: %d (%d in the implementation), day: %d\n", gc->get(UCAL_YEAR, status), gc->get(UCAL_MONTH, status) + 1, gc->get(UCAL_MONTH, status), gc->get(UCAL_DATE, status)); if (U_FAILURE(status)) { puts("Calendar::get failed"); return; } /* Add a day to the date */ gc->add(UCAL_DATE, 1, status); if (U_FAILURE(status)) { puts("Calendar::add failed"); return; } } delete gc; }
/** * @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; }