// setTimeInMillis() limits the Julian day range to +/-7F000000. // This would seem to limit the year range to: // ms=+183882168921600000 jd=7f000000 December 20, 5828963 AD // ms=-184303902528000000 jd=81000000 September 20, 5838270 BC // HOWEVER, CalendarRegressionTest/Test4167060 shows that the actual // range limit on the year field is smaller (~ +/-140000). [alan 3.0]
// 00:00:00 UTC, October 15, 1582, expressed in ms from the epoch. // Note that only Italy and other Catholic countries actually // observed this cutover. Most other countries followed in // the next few centuries, some as late as 1928. [LIU] // in Java, -12219292800000L //const UDate GregorianCalendar::kPapalCutover = -12219292800000L; staticconst uint32_t kCutoverJulianDay = 2299161; staticconst int32_t kDefaultCutoverYear = 1582; staticconst UDate kPapalCutover = (2299161.0 - kEpochStartAsJulianDay) * U_MILLIS_PER_DAY; //static const UDate kPapalCutoverJulian = (2299161.0 - kEpochStartAsJulianDay);
void
GregorianCalendar::setGregorianChange(UDate date, UErrorCode& status)
{ if (U_FAILURE(status)) return;
// 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. double cutoverDay = ClockMath::floorDivide(date, kOneDay);
// Handle the rare case of numeric overflow where the user specifies a time // outside of INT32_MIN .. INT32_MAX number of days.
// Normalize the year so BC values are represented as 0 and negative // values.
GregorianCalendar *cal = new GregorianCalendar(getTimeZone(), status); /* test for nullptr */ if (cal == nullptr) {
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 = static_cast<int32_t>(cutoverDay); delete cal;
}
if (julianDay >= fCutoverJulianDay) {
month = getGregorianMonth();
dayOfMonth = getGregorianDayOfMonth();
dayOfYear = getGregorianDayOfYear();
eyear = getGregorianYear();
} else { // The Julian epoch day (not the same as Julian Day) // is zero on Saturday December 30, 0 (Gregorian).
int32_t julianEpochDay = julianDay - (kJan1_1JulianDay - 2);
eyear = static_cast<int32_t>(ClockMath::floorDivide((4.0 * julianEpochDay) + 1464.0, static_cast<int32_t>(1461), &unusedRemainder));
// Compute the Julian calendar day number for January 1, eyear
int32_t january1 = 365 * (eyear - 1) + ClockMath::floorDivide(eyear - 1, static_cast<int32_t>(4));
dayOfYear = (julianEpochDay - january1); // 0-based
// Julian leap years occurred historically every 4 years starting // with 8 AD. Before 8 AD the spacing is irregular; every 3 years // from 45 BC to 9 BC, and then none until 8 AD. However, we don't // implement this historical detail; instead, we implement the // computationally cleaner proleptic calendar, which assumes // consistent 4-year cycles throughout time.
UBool isLeap = ((eyear&0x3) == 0); // equiv. to (eyear%4 == 0)
// [j81] if we are after the cutover in its year, shift the day of the year if((eyear == fGregorianCutoverYear) && (julianDay >= fCutoverJulianDay)) { //from handleComputeMonthStart
int32_t gregShift = Grego::gregorianShift(eyear); #ifdefined (U_DEBUG_CAL)
fprintf(stderr, "%s:%d: gregorian shift %d ::: doy%d => %d [cut=%d]\n",
__FILE__, __LINE__,gregShift, dayOfYear, dayOfYear+gregShift, fCutoverJulianDay); #endif
dayOfYear += gregShift;
}
int32_t jd = Calendar::handleComputeJulianDay(bestField, status); if (U_FAILURE(status)) { return0;
}
if((bestField == UCAL_WEEK_OF_YEAR) && // if we are doing WOY calculations, we are counting relative to Jan 1 *julian*
(internalGet(UCAL_EXTENDED_YEAR)==fGregorianCutoverYear) &&
jd >= fCutoverJulianDay) {
fInvertGregorian = true; // So that the Julian Jan 1 will be used in handleComputeMonthStart return Calendar::handleComputeJulianDay(bestField, status);
}
// If the month is out of range, adjust it into range, and // modify the extended year value accordingly. if (month < 0 || month > 11) { if (uprv_add32_overflow(ClockMath::floorDivide(month, 12, &month),
eyear, &eyear)) {
status = U_ILLEGAL_ARGUMENT_ERROR; return0;
}
}
nonConstThis->fIsGregorian = (eyear >= fGregorianCutoverYear); #ifdefined (U_DEBUG_CAL)
fprintf(stderr, "%s:%d: (hcms%d/%d) fIsGregorian %s, fInvertGregorian %s\n",
__FILE__, __LINE__, eyear,month, fIsGregorian?"T":"F", fInvertGregorian?"T":"F"); #endif if (fInvertGregorian) {
nonConstThis->fIsGregorian = !fIsGregorian;
} if (fIsGregorian) {
isLeap = isLeap && ((eyear%100 != 0) || (eyear%400 == 0)); // Add 2 because Gregorian calendar starts 2 days after // Julian calendar
int32_t gregShift = Grego::gregorianShift(eyear); #ifdefined (U_DEBUG_CAL)
fprintf(stderr, "%s:%d: (hcms%d/%d) gregorian shift of %d += %d\n",
__FILE__, __LINE__, eyear, month, julianDay, gregShift); #endif
julianDay += gregShift;
}
// At this point julianDay indicates the day BEFORE the first // day of January 1, <eyear> of either the Julian or Gregorian // calendar.
if (month != 0) {
julianDay += isLeap?kLeapNumDays[month]:kNumDays[month];
}
return julianDay;
}
int32_t GregorianCalendar::handleGetMonthLength(int32_t extendedYear, int32_t month, UErrorCode& /* status */) const
{ // If the month is out of range, adjust it into range, and // modify the extended year value accordingly. if (month < 0 || month > 11) {
extendedYear += ClockMath::floorDivide(month, 12, &month);
}
UBool
GregorianCalendar::validateFields() const
{ for (int32_t field = 0; field < UCAL_FIELD_COUNT; field++) { // Ignore DATE and DAY_OF_YEAR which are handled below if (field != UCAL_DATE &&
field != UCAL_DAY_OF_YEAR &&
isSet(static_cast<UCalendarDateFields>(field)) &&
!boundsCheck(internalGet(static_cast<UCalendarDateFields>(field)), static_cast<UCalendarDateFields>(field))) returnfalse;
}
// Values differ in Least-Maximum and Maximum should be handled // specially. if (isSet(UCAL_DATE)) {
int32_t date = internalGet(UCAL_DATE);
UErrorCode internalStatus = U_ZERO_ERROR; if (date < getMinimum(UCAL_DATE) ||
date > monthLength(internalGetMonth(internalStatus), internalStatus) ||
U_FAILURE(internalStatus)) { returnfalse;
}
}
if (isSet(UCAL_DAY_OF_YEAR)) {
int32_t days = internalGet(UCAL_DAY_OF_YEAR); if (days < 1 || days > yearLength()) { returnfalse;
}
}
// Handle DAY_OF_WEEK_IN_MONTH, which must not have the value zero. // We've checked against minimum and maximum above already. if (isSet(UCAL_DAY_OF_WEEK_IN_MONTH) && 0 == internalGet(UCAL_DAY_OF_WEEK_IN_MONTH)) { returnfalse;
}
return true;
}
// -------------------------------------
UBool
GregorianCalendar::boundsCheck(int32_t value, UCalendarDateFields field) const
{ return value >= getMinimum(field) && value <= getMaximum(field);
}
// -------------------------------------
UDate
GregorianCalendar::getEpochDay(UErrorCode& status)
{
complete(status); // Divide by 1000 (convert to seconds) in order to prevent overflow when // dealing with UDate(Long.MIN_VALUE) and UDate(Long.MAX_VALUE). double wallSec = internalGetTime()/1000 + (internalGet(UCAL_ZONE_OFFSET) + internalGet(UCAL_DST_OFFSET))/1000;
if (isGregorian) {
isLeap = isLeap && ((year%100 != 0) || (year%400 == 0)); // Add 2 because Gregorian calendar starts 2 days after Julian calendar
julianDay += Grego::gregorianShift(year);
}
return julianDay;
}
// /** // * Compute the day of week, relative to the first day of week, from // * 0..6, of the current DOW_LOCAL or DAY_OF_WEEK fields. This is // * equivalent to get(DOW_LOCAL) - 1. // */ // int32_t GregorianCalendar::computeRelativeDOW() const { // int32_t relDow = 0; // if (fStamp[UCAL_DOW_LOCAL] > fStamp[UCAL_DAY_OF_WEEK]) { // relDow = internalGet(UCAL_DOW_LOCAL) - 1; // 1-based // } else if (fStamp[UCAL_DAY_OF_WEEK] != kUnset) { // relDow = internalGet(UCAL_DAY_OF_WEEK) - getFirstDayOfWeek(); // if (relDow < 0) relDow += 7; // } // return relDow; // }
// /** // * Compute the day of week, relative to the first day of week, // * from 0..6 of the given julian day. // */ // int32_t GregorianCalendar::computeRelativeDOW(double julianDay) const { // int32_t relDow = julianDayToDayOfWeek(julianDay) - getFirstDayOfWeek(); // if (relDow < 0) { // relDow += 7; // } // return relDow; // }
// /** // * Compute the DOY using the WEEK_OF_YEAR field and the julian day // * of the day BEFORE January 1 of a year (a return value from // * computeJulianDayOfYear). // */ // int32_t GregorianCalendar::computeDOYfromWOY(double julianDayOfYear) const { // // Compute DOY from day of week plus week of year
// // Find the day of the week for the first of this year. This // // is zero-based, with 0 being the locale-specific first day of // // the week. Add 1 to get first day of year. // int32_t fdy = computeRelativeDOW(julianDayOfYear + 1);
// return // // Compute doy of first (relative) DOW of WOY 1 // (((7 - fdy) < getMinimalDaysInFirstWeek()) // ? (8 - fdy) : (1 - fdy))
// // Adjust for the week number. // + (7 * (internalGet(UCAL_WEEK_OF_YEAR) - 1))
// // Adjust for the DOW // + computeRelativeDOW(); // }
// J81 processing. (gregorian cutover)
UBool inCutoverMonth = false;
int32_t cMonthLen=0; // 'c' for cutover; in days
int32_t cDayOfMonth=0; // no discontinuity: [0, cMonthLen) double cMonthStart=0.0; // in ms
// Common code - see if we're in the cutover month of the cutover year if(get(UCAL_EXTENDED_YEAR, status) == fGregorianCutoverYear) { switch (field) { case UCAL_DAY_OF_MONTH: case UCAL_WEEK_OF_MONTH:
{
int32_t max = monthLength(internalGetMonth(status), status); if (U_FAILURE(status)) { return;
}
UDate t = internalGetTime(); // We subtract 1 from the DAY_OF_MONTH to make it zero-based, and an // additional 10 if we are after the cutover. Thus the monthStart // value will be correct iff we actually are in the cutover month.
cDayOfMonth = internalGet(UCAL_DAY_OF_MONTH) - ((t >= fGregorianCutover) ? 10 : 0);
cMonthStart = t - ((cDayOfMonth - 1) * kOneDay); // A month containing the cutover is 10 days shorter. if ((cMonthStart < fGregorianCutover) &&
(cMonthStart + (cMonthLen=(max-10))*kOneDay >= fGregorianCutover)) {
inCutoverMonth = true;
}
} break; default:
;
}
}
switch (field) { case UCAL_WEEK_OF_YEAR: { // Unlike WEEK_OF_MONTH, WEEK_OF_YEAR never shifts the day of the // week. Also, rolling the week of the year can have seemingly // strange effects simply because the year of the week of year // may be different from the calendar year. For example, the // date Dec 28, 1997 is the first day of week 1 of 1998 (if // weeks start on Sunday and the minimal days in first week is // <= 3).
int32_t woy = get(UCAL_WEEK_OF_YEAR, status); // Get the ISO year, which matches the week of year. This // may be one year before or after the calendar year.
int32_t isoYear = get(UCAL_YEAR_WOY, status);
int32_t isoDoy = internalGet(UCAL_DAY_OF_YEAR);
int32_t month = internalGetMonth(status); if (U_FAILURE(status)) { return;
} if (month == UCAL_JANUARY) { if (woy >= 52) {
isoDoy += handleGetYearLength(isoYear, status);
}
} else { if (woy == 1) {
isoDoy -= handleGetYearLength(isoYear - 1, status);
}
} if (U_FAILURE(status)) return; if (uprv_add32_overflow(woy, amount, &woy)) {
status = U_ILLEGAL_ARGUMENT_ERROR; return;
} // Do fast checks to avoid unnecessary computation: if (woy < 1 || woy > 52) { // Determine the last week of the ISO year. // We do this using the standard formula we use // everywhere in this file. If we can see that the // days at the end of the year are going to fall into // week 1 of the next year, we drop the last week by // subtracting 7 from the last day of the year.
int32_t lastDoy = handleGetYearLength(isoYear, status); if (U_FAILURE(status)) return;
int32_t lastRelDow = (lastDoy - isoDoy + internalGet(UCAL_DAY_OF_WEEK) -
getFirstDayOfWeek()) % 7; if (lastRelDow < 0) lastRelDow += 7; if ((6 - lastRelDow) >= getMinimalDaysInFirstWeek()) lastDoy -= 7;
int32_t lastWoy = weekNumber(lastDoy, lastRelDow + 1);
woy = ((woy + lastWoy - 1) % lastWoy) + 1;
}
set(UCAL_WEEK_OF_YEAR, woy);
set(UCAL_YEAR_WOY,isoYear); return;
}
case UCAL_DAY_OF_MONTH: if( !inCutoverMonth ) {
Calendar::roll(field, amount, status); return;
}
{ // [j81] 1582 special case for DOM // The default computation works except when the current month // contains the Gregorian cutover. We handle this special case // here. [j81 - aliu] double monthLen = cMonthLen * kOneDay; double msIntoMonth = uprv_fmod(internalGetTime() - cMonthStart +
amount * kOneDay, monthLen); if (msIntoMonth < 0) {
msIntoMonth += monthLen;
} #ifdefined (U_DEBUG_CAL)
fprintf(stderr, "%s:%d: roll DOM %d -> %.0lf ms \n",
__FILE__, __LINE__,amount, cMonthLen, cMonthStart+msIntoMonth); #endif
setTimeInMillis(cMonthStart + msIntoMonth, status); return;
}
case UCAL_WEEK_OF_MONTH: if( !inCutoverMonth ) {
Calendar::roll(field, amount, status); return;
}
{ #ifdefined (U_DEBUG_CAL)
fprintf(stderr, "%s:%d: roll WOM %d ??????????????????? \n",
__FILE__, __LINE__,amount); #endif // NOTE: following copied from the old // GregorianCalendar::roll( WEEK_OF_MONTH ) code
// This is tricky, because during the roll we may have to shift // to a different day of the week. For example:
// s m t w r f s // 1 2 3 4 5 // 6 7 8 9 10 11 12
// When rolling from the 6th or 7th back one week, we go to the // 1st (assuming that the first partial week counts). The same // thing happens at the end of the month.
// The other tricky thing is that we have to figure out whether // the first partial week actually counts or not, based on the // minimal first days in the week. And we have to use the // correct first day of the week to delineate the week // boundaries.
// Here's our algorithm. First, we find the real boundaries of // the month. Then we discard the first partial week if it // doesn't count in this locale. Then we fill in the ends with // phantom days, so that the first partial week and the last // partial week are full weeks. We then have a nice square // block of weeks. We do the usual rolling within this block, // as is done elsewhere in this method. If we wind up on one of // the phantom days that we added, we recognize this and pin to // the first or the last day of the month. Easy, eh?
// Another wrinkle: To fix jitterbug 81, we have to make all this // work in the oddball month containing the Gregorian cutover. // This month is 10 days shorter than usual, and also contains // a discontinuity in the days; e.g., the default cutover month // is Oct 1582, and goes from day of month 4 to day of month 15.
// Normalize the DAY_OF_WEEK so that 0 is the first day of the week // in this locale. We have dow in 0..6.
int32_t dow = internalGet(UCAL_DAY_OF_WEEK) - getFirstDayOfWeek(); if (dow < 0)
dow += 7;
// Find the day of month, compensating for cutover discontinuity.
int32_t dom = cDayOfMonth;
// Find the day of the week (normalized for locale) for the first // of the month.
int32_t fdm = (dow - dom + 1) % 7; if (fdm < 0)
fdm += 7;
// Get the first day of the first full week of the month, // including phantom days, if any. Figure out if the first week // counts or not; if it counts, then fill in phantom days. If // not, advance to the first real full week (skip the partial week).
int32_t start; if ((7 - fdm) < getMinimalDaysInFirstWeek())
start = 8 - fdm; // Skip the first partial week else
start = 1 - fdm; // This may be zero or negative
// Get the day of the week (normalized for locale) for the last // day of the month.
int32_t monthLen = cMonthLen;
int32_t ldm = (monthLen - dom + dow) % 7; // We know monthLen >= DAY_OF_MONTH so we skip the += 7 step here.
// Get the limit day for the blocked-off rectangular month; that // is, the day which is one past the last day of the month, // after the month has already been filled in with phantom days // to fill out the last week. This day has a normalized DOW of 0.
int32_t limit = monthLen + 7 - ldm;
// Now roll between start and (limit - 1).
int32_t gap = limit - start;
int32_t newDom = (dom + amount*7 - start) % gap; if (newDom < 0)
newDom += gap;
newDom += start;
// Finally, pin to the real start and end of the month. if (newDom < 1)
newDom = 1; if (newDom > monthLen)
newDom = monthLen;
// Set the DAY_OF_MONTH. We rely on the fact that this field // takes precedence over everything else (since all other fields // are also set at this point). If this fact changes (if the // disambiguation algorithm changes) then we will have to unset // the appropriate fields here so that DAY_OF_MONTH is attended // to.
// If we are in the cutover month, manipulate ms directly. Don't do // this in general because it doesn't work across DST boundaries // (details, details). This takes care of the discontinuity.
setTimeInMillis(cMonthStart + (newDom-1)*kOneDay, status); return;
}
/** *Returnthemaximumvaluethatthisfieldcouldhave,giventhecurrentdate. *Forexample,withthedate"Feb3,1997"andtheDAY_OF_MONTHfield,theactual *maximumwouldbe28;for"Feb3,1996"its29.SimilarlyforaHebrewcalendar, *forsomeyearstheactualmaximumforMONTHis12,andforothers13. *@stableICU2.0
*/
int32_t GregorianCalendar::getActualMaximum(UCalendarDateFields field, UErrorCode& status) const
{ /* It is a known limitation that the code here (and in getActualMinimum) *won'tbehaveproperlyattheextremelimitsofGregorianCalendar's *representablerange(exceptforthecodethathandlestheYEAR *field).That'sbecausetheendsoftherepresentablerangeareat *oddspotsintheyear.ForcalendarswiththedefaultGregorian *cutover,theselimitsareSunDec0216:47:04GMT292269055BCtoSun *Aug1707:12:55GMT292278994AD,somewhatdifferentfornon-GMT *zones.Asaresult,ifthecalendarissettoAug1292278994AD, *theactualmaximumofDAY_OF_MONTHis17,not30.IfthedateisMar *31inthatyear,theactualmaximummonthmightbeJul,whereasis *thedateisMar15,theactualmaximummightbeAug--dependingon *theprecisesemanticsthataredesired.Similarconsiderations *affectallfields.Nonetheless,thiseffectissufficientlyarcane *thatwepermitit,ratherthancomplicatingthecodetohandlesuch *intricacies.-liu8/20/98
*UPDATE:Nolongertrue,sincewehavepulledinthelimitvalueson
* the year. - Liu 11/6/00 */
switch (field) {
case UCAL_YEAR: /* The year computation is no different, in principle, from the *others,however,therangeofpossiblemaximaislarge.In *addition,thewayweknowwe'veexceededtherangeisdifferent. *Forthesereasons,weusethespecialcasecodebelowtohandle *thisfield. * *TheactualmaximaforYEARdependonthetypeofcalendar: * *Gregorian=May17,292275056BC-Aug17,292278994AD *Julian=Dec2,292269055BC-Jan3,292272993AD *Hybrid=Dec2,292269055BC-Aug17,292278994AD * *Weknowwe'veexceededthemaximumwheneitherthemonth,date, *time,orerachangesinresponsetosettingtheyear.Wedon't *checkformonth,date,andtimeherebecausetheyearanderaare *sufficienttodetectaninvalidyearsetting.NOTE:Ifcodeis *addedtocheckthemonthanddateinthefutureforsomereason, *Feb29mustbeallowedtoshifttoMar1whensettingtheyear.
*/
{ if(U_FAILURE(status)) return0;
Calendar *cal = clone(); if(!cal) {
status = U_MEMORY_ALLOCATION_ERROR; return0;
}
cal->setLenient(true);
int32_t era = cal->get(UCAL_ERA, status);
UDate d = cal->getTime(status);
/* Perform a binary search, with the invariant that lowGood is a *validyear,andhighBadisanoutofrangeyear.
*/
int32_t lowGood = kGregorianCalendarLimits[UCAL_YEAR][1];
int32_t highBad = kGregorianCalendarLimits[UCAL_YEAR][2]+1; while ((lowGood + 1) < highBad) {
int32_t y = (lowGood + highBad) / 2;
cal->set(UCAL_YEAR, y); if (cal->get(UCAL_YEAR, status) == y && cal->get(UCAL_ERA, status) == era) {
lowGood = y;
} else {
highBad = y;
cal->setTime(d, status); // Restore original fields
}
}
int32_t GregorianCalendar::handleGetExtendedYear(UErrorCode& status) { if (U_FAILURE(status)) { return0;
} // the year to return
int32_t year = kEpochYear;
// year field to use // There are three separate fields which could be used to // derive the proper year. Use the one most recently set.
UCalendarDateFields yearField = newerField(
newerField(UCAL_EXTENDED_YEAR, UCAL_YEAR), UCAL_YEAR_WOY);
// based on the "best" year field, get the year switch(yearField) { case UCAL_EXTENDED_YEAR:
year = internalGet(UCAL_EXTENDED_YEAR, kEpochYear); break;
case UCAL_YEAR:
{ // The year defaults to the epoch start, the era to AD
int32_t era = internalGet(UCAL_ERA, AD); if (era == BC) {
year = 1 - internalGet(UCAL_YEAR, 1); // Convert to extended year
} elseif (era == AD) {
year = internalGet(UCAL_YEAR, kEpochYear);
} else {
status = U_ILLEGAL_ARGUMENT_ERROR; return0;
}
} break;
case UCAL_YEAR_WOY:
year = handleGetExtendedYearFromWeekFields(
internalGet(UCAL_YEAR_WOY), internalGet(UCAL_WEEK_OF_YEAR), status); if (U_FAILURE(status)) { return0;
} #ifdefined (U_DEBUG_CAL) // if(internalGet(UCAL_YEAR_WOY) != year) {
fprintf(stderr, "%s:%d: hGEYFWF[%d,%d] -> %d\n",
__FILE__, __LINE__,internalGet(UCAL_YEAR_WOY),internalGet(UCAL_WEEK_OF_YEAR),year); //} #endif break;
default:
year = kEpochYear;
} return year;
}
int32_t GregorianCalendar::handleGetExtendedYearFromWeekFields(int32_t yearWoy, int32_t woy, UErrorCode& status)
{ if (U_FAILURE(status)) { return0;
} // convert year to extended form
int32_t era = internalGet(UCAL_ERA, AD); if(era == BC) {
yearWoy = 1 - yearWoy;
} return Calendar::handleGetExtendedYearFromWeekFields(yearWoy, woy, status);
}
¤ Diese beiden folgenden Angebotsgruppen bietet das Unternehmen0.24Angebot
(Wie Sie bei der Firma Beratungs- und Dienstleistungen beauftragen können 2026-08-26)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.