// Canonicalize, so that an old-style variant will be transformed to keywords. // e.g ja_JP_TRADITIONAL -> ja_JP@calendar=japanese // NOTE: Since ICU-20187, ja_JP_TRADITIONAL no longer canonicalizes, and // the Gregorian calendar is returned instead.
CharString canonicalName = ulocimp_canonicalize(locid, status); if (U_FAILURE(status)) { return CALTYPE_GREGORIAN;
}
CharString calTypeBuf = ulocimp_getKeywordValue(canonicalName.data(), "calendar", status); if (U_SUCCESS(status)) {
calType = getCalendarType(calTypeBuf.data()); if (calType != CALTYPE_UNKNOWN) { return calType;
}
}
status = U_ZERO_ERROR;
// when calendar keyword is not available or not supported, read supplementalData // to get the default calendar type for the locale's region
CharString region = ulocimp_getRegionForSupplementalData(canonicalName.data(), true, status); if (U_FAILURE(status)) { return CALTYPE_GREGORIAN;
}
calTypeBuf.clear(); if (U_SUCCESS(status) && order != nullptr) { // the first calendar type is the default for the region
int32_t len = 0; const char16_t *uCalType = ures_getStringByIndex(order, 0, &len, &status);
calTypeBuf.appendInvariantChars(uCalType, len, status);
calType = getCalendarType(calTypeBuf.data());
}
ures_close(order);
ures_close(rb);
if (calType == CALTYPE_UNKNOWN) { // final fallback
calType = CALTYPE_GREGORIAN;
} return calType;
}
// Resource bundle tags read by this class staticconstchar gCalendar[] = "calendar"; staticconstchar gMonthNames[] = "monthNames"; staticconstchar gGregorian[] = "gregorian";
// Data flow in Calendar // ---------------------
// The current time is represented in two ways by Calendar: as UTC // milliseconds from the epoch start (1 January 1970 0:00 UTC), and as local // fields such as MONTH, HOUR, AM_PM, etc. It is possible to compute the // millis from the fields, and vice versa. The data needed to do this // conversion is encapsulated by a TimeZone object owned by the Calendar. // The data provided by the TimeZone object may also be overridden if the // user sets the ZONE_OFFSET and/or DST_OFFSET fields directly. The class // keeps track of what information was most recently set by the caller, and // uses that to compute any other information as needed.
// If the user sets the fields using set(), the data flow is as follows. // This is implemented by the Calendar subclass's computeTime() method. // During this process, certain fields may be ignored. The disambiguation // algorithm for resolving which fields to pay attention to is described // above.
// local fields (YEAR, MONTH, DATE, HOUR, MINUTE, etc.) // | // | Using Calendar-specific algorithm // V // local standard millis // | // | Using TimeZone or user-set ZONE_OFFSET / DST_OFFSET // V // UTC millis (in time data member)
// If the user sets the UTC millis using setTime(), the data flow is as // follows. This is implemented by the Calendar subclass's computeFields() // method.
// UTC millis (in time data member) // | // | Using TimeZone getOffset() // V // local standard millis // | // | Using Calendar-specific algorithm // V // local fields (YEAR, MONTH, DATE, HOUR, MINUTE, etc.)
// In general, a round trip from fields, through local and UTC millis, and // back out to fields is made when necessary. This is implemented by the // complete() method. Resolving a partial set of fields into a UTC millis // value allows all remaining fields to be generated from that value. If // the Calendar is lenient, the fields are also renormalized to standard // ranges when they are regenerated.
#if !UCONFIG_NO_SERVICE if (isCalendarServiceUsed()) {
u = getCalendarService(success)->get(aLocale, LocaleKey::KIND_ANY, &actualLoc, success);
} else #endif
{
u = createStandardCalendar(getCalendarTypeForLocale(aLocale.getName()), aLocale, success);
}
Calendar* c = nullptr;
if(U_FAILURE(success) || !u) { if(U_SUCCESS(success)) { // Propagate some kind of err
success = U_INTERNAL_PROGRAM_ERROR;
} return nullptr;
}
#if !UCONFIG_NO_SERVICE const UnicodeString* str = dynamic_cast<const UnicodeString*>(u); if(str != nullptr) { // It's a unicode string telling us what type of calendar to load ("gregorian", etc) // Create a Locale over this string
Locale l("");
LocaleUtility::initLocaleFromName(*str, l);
#ifdef U_DEBUG_CALSVC
fprintf(stderr, "Calendar::createInstance(%s), looking up [%s]\n", aLocale.getName(), l.getName()); #endif
Locale actualLoc2; delete u;
u = nullptr;
// Don't overwrite actualLoc, since the actual loc from this call // may be something like "@calendar=gregorian" -- TODO investigate // further...
c = (Calendar*)getCalendarService(success)->get(l, LocaleKey::KIND_ANY, &actualLoc2, success);
str = dynamic_cast<const UnicodeString*>(c); if(str != nullptr) { // recursed! Second lookup returned a UnicodeString. // Perhaps DefaultCalendar{} was set to another locale. #ifdef U_DEBUG_CALSVC char tmp[200]; // Extract a char* out of it..
int32_t len = str->length();
int32_t actLen = sizeof(tmp)-1; if(len > actLen) {
len = actLen;
}
str->extract(0,len,tmp);
tmp[len]=0;
fprintf(stderr, "err - recursed, 2nd lookup was unistring %s\n", tmp); #endif
success = U_MISSING_RESOURCE_ERROR; // requested a calendar type which could NOT be found. delete c; return nullptr;
} #ifdef U_DEBUG_CALSVC
fprintf(stderr, "%p: setting week count data to locale %s, actual locale %s\n", c, (constchar*)aLocale.getName(), (constchar *)actualLoc.getName()); #endif
c->setWeekData(aLocale, c->getType(), success); // set the correct locale (this was an indirect calendar)
char keyword[ULOC_FULLNAME_CAPACITY] = "";
UErrorCode tmpStatus = U_ZERO_ERROR;
l.getKeywordValue("calendar", keyword, ULOC_FULLNAME_CAPACITY, tmpStatus); if (U_SUCCESS(tmpStatus) && uprv_strcmp(keyword, "iso8601") == 0) {
c->setFirstDayOfWeek(UCAL_MONDAY);
c->setMinimalDaysInFirstWeek(4);
}
} else #endif/* UCONFIG_NO_SERVICE */
{ // a calendar was returned - we assume the factory did the right thing.
c = (Calendar*)u;
}
// Now, reset calendar to default state:
c->adoptTimeZone(zonePtr.orphan()); // Set the correct time zone
c->setTimeInMillis(getNow(), success); // let the new calendar have the current time.
int32_t
Calendar::get(UCalendarDateFields field, UErrorCode& status) const
{ if (U_FAILURE(status)) { return0;
} if (field < 0 || field >= UCAL_FIELD_COUNT) {
status = U_ILLEGAL_ARGUMENT_ERROR; return0;
} // field values are only computed when actually requested; for more on when computation // of various things happens, see the "data flow in Calendar" description at the top // of this file if (U_SUCCESS(status)) const_cast<Calendar*>(this)->complete(status); // Cast away const return U_SUCCESS(status) ? fFields[field] : 0;
}
// -------------------------------------
void
Calendar::set(UCalendarDateFields field, int32_t value)
{ if (field < 0 || field >= UCAL_FIELD_COUNT) { return;
} if (fAreFieldsVirtuallySet) {
UErrorCode ec = U_ZERO_ERROR;
computeFields(ec);
}
fFields[field] = value; /* Ensure that the fNextStamp value doesn't go pass max value for int8_t */ if (fNextStamp == STAMP_MAX) {
recalculateStamp();
}
fStamp[field] = fNextStamp++;
fIsTimeSet = fAreFieldsSet = fAreFieldsVirtuallySet = false;
}
void
Calendar::clear()
{
uprv_memset(fFields, 0, sizeof(fFields));
uprv_memset(fStamp, kUnset, sizeof(fStamp));
fNextStamp = kMinimumUserStamp;
fIsTimeSet = fAreFieldsSet = fAreAllFieldsSet = fAreFieldsVirtuallySet = false; // fTime is not 'cleared' - may be used if no fields are set.
}
int32_t Calendar::newestStamp(UCalendarDateFields first, UCalendarDateFields last, int32_t bestStampSoFar) const
{
int32_t bestStamp = bestStampSoFar; for (int32_t i = static_cast<int32_t>(first); i <= static_cast<int32_t>(last); ++i) { if (fStamp[i] > bestStamp) {
bestStamp = fStamp[i];
}
} return bestStamp;
}
// -------------------------------------
void
Calendar::complete(UErrorCode& status)
{ if (U_FAILURE(status)) { return;
} if (!fIsTimeSet) {
updateTime(status); /* Test for buffer overflows */ if(U_FAILURE(status)) { return;
}
} if (!fAreFieldsSet) {
computeFields(status); // fills in unset fields /* Test for buffer overflows */ if(U_FAILURE(status)) { return;
}
fAreFieldsSet = true;
fAreAllFieldsSet = true;
}
}
//------------------------------------------------------------------------- // Protected utility methods for use by subclasses. These are very handy // for implementing add, roll, and computeFields. //-------------------------------------------------------------------------
// We used to check for and correct extreme millis values (near // Long.MIN_VALUE or Long.MAX_VALUE) here. Such values would cause // overflows from positive to negative (or vice versa) and had to // be manually tweaked. We no longer need to do this because we // have limited the range of supported dates to those that have a // Julian day that fits into an int. This allows us to implement a // JULIAN_DAY field and also removes some inelegant code. - Liu // 11/6/00
int32_t millisInDay; double days = ClockMath::floorDivide(
localMillis, U_MILLIS_PER_DAY, &millisInDay) +
kEpochStartAsJulianDay; if (days > INT32_MAX || days < INT32_MIN) {
ec = U_ILLEGAL_ARGUMENT_ERROR; return;
}
// Call framework method to have subclass compute its fields. // These must include, at a minimum, MONTH, DAY_OF_MONTH, // EXTENDED_YEAR, YEAR, DAY_OF_YEAR. This method will call internalSet(), // which will update stamp[].
handleComputeFields(fFields[UCAL_JULIAN_DAY], ec);
// Compute week-related fields, based on the subclass-computed // fields computed by handleComputeFields().
computeWeekFields(ec);
// Compute time-related fields. These are independent of the date and // of the subclass algorithm. They depend only on the local zone // wall milliseconds in day. if (U_FAILURE(ec)) { return;
}
uint8_t Calendar::julianDayToDayOfWeek(int32_t julian)
{ // If julian is negative, then julian%7 will be negative, so we adjust // accordingly. We add 1 because Julian day 0 is Monday.
int8_t dayOfWeek = static_cast<int8_t>((julian + 1LL) % 7);
// WEEK_OF_YEAR start // Compute the week of the year. For the Gregorian calendar, valid week // numbers run from 1 to 52 or 53, depending on the year, the first day // of the week, and the minimal days in the first week. For other // calendars, the valid range may be different -- it depends on the year // length. Days at the start of the year may fall into the last week of // the previous year; days at the end of the year may fall into the // first week of the next year. ASSUME that the year length is less than // 7000 days.
int32_t yearOfWeekOfYear = eyear;
int32_t relDow = (dayOfWeek + 7 - firstDayOfWeek) % 7; // 0..6
int32_t relDowJan1 = (dayOfWeek - dayOfYear + 7001 - firstDayOfWeek) % 7; // 0..6
int32_t woy = (dayOfYear - 1 + relDowJan1) / 7; // 0..53
int32_t minimalDaysInFirstWeek = getMinimalDaysInFirstWeek(); if ((7 - relDowJan1) >= minimalDaysInFirstWeek) {
++woy;
}
// Adjust for weeks at the year end that overlap into the previous or // next calendar year. if (woy == 0) { // We are the last week of the previous year. // Check to see if we are in the last week; if so, we need // to handle the case in which we are the first week of the // next year.
int32_t prevDoy = dayOfYear + handleGetYearLength(eyear - 1, ec); if(U_FAILURE(ec)) return;
woy = weekNumber(prevDoy, dayOfWeek);
yearOfWeekOfYear--;
} else {
int32_t lastDoy = handleGetYearLength(eyear, ec); if(U_FAILURE(ec)) return; // Fast check: For it to be week 1 of the next year, the DOY // must be on or after L-5, where L is yearLength(), then it // cannot possibly be week 1 of the next year: // L-5 L // doy: 359 360 361 362 363 364 365 001 // dow: 1 2 3 4 5 6 7 if (dayOfYear >= (lastDoy - 5)) {
int32_t lastRelDow = (relDow + lastDoy - dayOfYear) % 7; if (lastRelDow < 0) {
lastRelDow += 7;
} if (((6 - lastRelDow) >= minimalDaysInFirstWeek) &&
((dayOfYear + 7 - relDow) > lastDoy)) {
woy = 1;
yearOfWeekOfYear++;
}
}
}
fFields[UCAL_WEEK_OF_YEAR] = woy;
fFields[UCAL_YEAR_WOY] = yearOfWeekOfYear; // min/max of years are not constrains for caller, so not assert here. // WEEK_OF_YEAR end
int32_t Calendar::weekNumber(int32_t desiredDay, int32_t dayOfPeriod, int32_t dayOfWeek)
{ // Determine the day of the week of the first day of the period // in question (either a year or a month). Zero represents the // first day of the week on this calendar.
int32_t periodStartDayOfWeek = (dayOfWeek - getFirstDayOfWeek() - dayOfPeriod + 1) % 7; if (periodStartDayOfWeek < 0) periodStartDayOfWeek += 7;
// Compute the week number. Initially, ignore the first week, which // may be fractional (or may not be). We add periodStartDayOfWeek in // order to fill out the first week, if it is fractional.
int32_t weekNo = (desiredDay + periodStartDayOfWeek - 1)/7;
// If the first week is long enough, then count it. If // the minimal days in the first week is one, or if the period start // is zero, we always increment weekNo. if ((7 - periodStartDayOfWeek) >= getMinimalDaysInFirstWeek()) ++weekNo;
void Calendar::roll(UCalendarDateFields field, int32_t amount, UErrorCode& status) UPRV_NO_SANITIZE_UNDEFINED { if (amount == 0) { return; // Nothing to do
}
complete(status);
if(U_FAILURE(status)) { return;
} if (field < 0 || field >= UCAL_FIELD_COUNT) {
status = U_ILLEGAL_ARGUMENT_ERROR; return;
} switch (field) { case UCAL_DAY_OF_MONTH: case UCAL_AM_PM: case UCAL_MINUTE: case UCAL_SECOND: case UCAL_MILLISECOND: case UCAL_MILLISECONDS_IN_DAY: case UCAL_ERA: // These are the standard roll instructions. These work for all // simple cases, that is, cases in which the limits are fixed, such // as the hour, the day of the month, and the era.
{
int32_t min = getActualMinimum(field,status);
int32_t max = getActualMaximum(field,status); if (U_FAILURE(status)) { return;
}
int32_t gap = max - min + 1;
int64_t value = internalGet(field);
value = (value + amount - min) % gap; if (value < 0) {
value += gap;
}
value += min;
set(field, value); return;
}
case UCAL_HOUR: case UCAL_HOUR_OF_DAY: // Rolling the hour is difficult on the ONSET and CEASE days of // daylight savings. For example, if the change occurs at // 2 AM, we have the following progression: // ONSET: 12 Std -> 1 Std -> 3 Dst -> 4 Dst // CEASE: 12 Dst -> 1 Dst -> 1 Std -> 2 Std // To get around this problem we don't use fields; we manipulate // the time in millis directly.
{ // Assume min == 0 in calculations below double start = getTimeInMillis(status);
int64_t oldHour = internalGet(field);
int32_t max = getMaximum(field);
int32_t newHour = (oldHour + amount) % (max + 1); if (newHour < 0) {
newHour += max + 1;
}
setTimeInMillis(start + kOneHour * (newHour - oldHour),status); return;
}
case UCAL_MONTH: case UCAL_ORDINAL_MONTH: // Rolling the month involves both pinning the final value // and adjusting the DAY_OF_MONTH if necessary. We only adjust the // DAY_OF_MONTH if, after updating the MONTH field, it is illegal. // E.g., <jan31>.roll(MONTH, 1) -> <feb28> or <feb29>.
{
int32_t max = getActualMaximum(UCAL_MONTH, status) + 1;
int64_t mon = internalGet(UCAL_MONTH);
mon = (mon + amount) % max;
// Keep the day of month in range. We don't want to spill over // into the next month; e.g., we don't want jan31 + 1 mo -> feb31 -> // mar3.
pinField(UCAL_DAY_OF_MONTH,status); return;
}
case UCAL_YEAR: case UCAL_YEAR_WOY:
{ // * If era==0 and years go backwards in time, change sign of amount. // * Until we have new API per #9393, we temporarily hardcode knowledge of // which calendars have era 0 years that go backwards.
int32_t era = internalGet(UCAL_ERA); if (era == 0 && isEra0CountingBackward()) { if (uprv_mul32_overflow(amount, -1, &amount)) {
status = U_ILLEGAL_ARGUMENT_ERROR; return;
}
}
int32_t newYear; if (uprv_add32_overflow(
amount, internalGet(field), &newYear)) {
status = U_ILLEGAL_ARGUMENT_ERROR; return;
} if (era > 0 || newYear >= 1) {
int32_t maxYear = getActualMaximum(field, status); if (maxYear < 32768) { // this era has real bounds, roll should wrap years if (newYear < 1) {
newYear = maxYear - ((-newYear) % maxYear);
} elseif (newYear > maxYear) {
newYear = ((newYear - 1) % maxYear) + 1;
} // else era is unbounded, just pin low year instead of wrapping
} elseif (newYear < 1) {
newYear = 1;
} // else we are in era 0 with newYear < 1; // calendars with years that go backwards must pin the year value at 0, // other calendars can have years < 0 in era 0
} elseif (era == 0 && isEra0CountingBackward()) {
newYear = 1;
}
set(field, newYear);
pinField(UCAL_MONTH,status);
pinField(UCAL_DAY_OF_MONTH,status); return;
}
case UCAL_EXTENDED_YEAR: // Rolling the year can involve pinning the DAY_OF_MONTH. if (uprv_add32_overflow(
amount, internalGet(field), &amount)) {
status = U_ILLEGAL_ARGUMENT_ERROR; return;
}
set(field, amount);
pinField(UCAL_MONTH,status);
pinField(UCAL_DAY_OF_MONTH,status); return;
case UCAL_WEEK_OF_MONTH:
{ // 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?
// 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 the week (normalized for locale) for the first // of the month.
int32_t fdm = (dow - internalGet(UCAL_DAY_OF_MONTH) + 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 = getActualMaximum(UCAL_DAY_OF_MONTH, status);
int32_t ldm = (monthLen - internalGet(UCAL_DAY_OF_MONTH) + 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; if (gap == 0) {
status = U_INTERNAL_PROGRAM_ERROR; return;
}
int32_t day_of_month = (internalGet(UCAL_DAY_OF_MONTH) + amount*7LL -
start) % gap; if (day_of_month < 0) day_of_month += gap;
day_of_month += start;
// Finally, pin to the real start and end of the month. if (day_of_month < 1) day_of_month = 1; if (day_of_month > monthLen) day_of_month = 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.
set(UCAL_DAY_OF_MONTH, day_of_month); return;
} case UCAL_WEEK_OF_YEAR:
{ // This follows the outline of WEEK_OF_MONTH, except it applies // to the whole year. Please see the comment for WEEK_OF_MONTH // for general notes.
// 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 the week (normalized for locale) for the first // of the year.
int32_t fdy = (dow - internalGet(UCAL_DAY_OF_YEAR) + 1) % 7; if (fdy < 0) fdy += 7;
// Get the first day of the first full week of the year, // 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 - fdy) < getMinimalDaysInFirstWeek())
start = 8 - fdy; // Skip the first partial week else
start = 1 - fdy; // This may be zero or negative
// Get the day of the week (normalized for locale) for the last // day of the year.
int32_t yearLen = getActualMaximum(UCAL_DAY_OF_YEAR,status);
int32_t ldy = (yearLen - internalGet(UCAL_DAY_OF_YEAR) + dow) % 7; // We know yearLen >= DAY_OF_YEAR so we skip the += 7 step here.
// Get the limit day for the blocked-off rectangular year; that // is, the day which is one past the last day of the year, // after the year 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 = yearLen + 7 - ldy;
// Now roll between start and (limit - 1).
int32_t gap = limit - start; if (gap == 0) {
status = U_INTERNAL_PROGRAM_ERROR; return;
}
int32_t day_of_year = (internalGet(UCAL_DAY_OF_YEAR) + amount*7LL -
start) % gap; if (day_of_year < 0) day_of_year += gap;
day_of_year += start;
// Finally, pin to the real start and end of the month. if (day_of_year < 1) day_of_year = 1; if (day_of_year > yearLen) day_of_year = yearLen;
// Make sure that the year and day of year are attended to by // clearing other fields which would normally take precedence. // If the disambiguation algorithm is changed, this section will // have to be updated as well.
set(UCAL_DAY_OF_YEAR, day_of_year);
clear(UCAL_MONTH);
clear(UCAL_ORDINAL_MONTH); return;
} case UCAL_DAY_OF_YEAR:
{ // Roll the day of year using millis. Compute the millis for // the start of the year, and get the length of the year. double delta = amount * kOneDay; // Scale up from days to millis double min2 = internalGet(UCAL_DAY_OF_YEAR)-1;
min2 *= kOneDay;
min2 = internalGetTime() - min2;
double yearLength = getActualMaximum(UCAL_DAY_OF_YEAR,status); double oneYear = yearLength;
oneYear *= kOneDay;
newtime = uprv_fmod((internalGetTime() + delta - min2), oneYear); if (newtime < 0) newtime += oneYear;
setTimeInMillis(newtime + min2, status); return;
} case UCAL_DAY_OF_WEEK: case UCAL_DOW_LOCAL:
{ // Roll the day of week using millis. Compute the millis for // the start of the week, using the first day of week setting. // Restrict the millis to [start, start+7days). double delta = amount * kOneDay; // Scale up from days to millis // Compute the number of days before the current day in this // week. This will be a value 0..6.
int32_t leadDays = internalGet(field);
leadDays -= (field == UCAL_DAY_OF_WEEK) ? getFirstDayOfWeek() : 1; if (leadDays < 0) leadDays += 7; double min2 = internalGetTime() - leadDays * kOneDay; double newtime = uprv_fmod((internalGetTime() + delta - min2), kOneWeek); if (newtime < 0) newtime += kOneWeek;
setTimeInMillis(newtime + min2, status); return;
} case UCAL_DAY_OF_WEEK_IN_MONTH:
{ // Roll the day of week in the month using millis. Determine // the first day of the week in the month, and then the last, // and then roll within that range. double delta = amount * kOneWeek; // Scale up from weeks to millis // Find the number of same days of the week before this one // in this month.
int32_t preWeeks = (internalGet(UCAL_DAY_OF_MONTH) - 1) / 7; // Find the number of same days of the week after this one // in this month.
int32_t postWeeks = (getActualMaximum(UCAL_DAY_OF_MONTH,status) -
internalGet(UCAL_DAY_OF_MONTH)) / 7; // From these compute the min and gap millis for rolling. double min2 = internalGetTime() - preWeeks * kOneWeek; double gap2 = kOneWeek * (preWeeks + postWeeks + 1); // Must add 1! // Roll within this range double newtime = uprv_fmod((internalGetTime() + delta - min2), gap2); if (newtime < 0) newtime += gap2;
setTimeInMillis(newtime + min2, status); return;
} case UCAL_JULIAN_DAY: if (uprv_add32_overflow(
amount, internalGet(field), &amount)) {
status = U_ILLEGAL_ARGUMENT_ERROR; return;
}
set(field, amount); return; default: // Other fields cannot be rolled by this method #ifdefined (U_DEBUG_CAL)
fprintf(stderr, "%s:%d: ILLEGAL ARG because of roll on non-rollable field %s\n",
__FILE__, __LINE__,fldName(field)); #endif
status = U_ILLEGAL_ARGUMENT_ERROR;
}
}
// ------------------------------------- void Calendar::add(UCalendarDateFields field, int32_t amount, UErrorCode& status)
{ if (U_FAILURE(status)) { return;
} if (field < 0 || field >= UCAL_FIELD_COUNT) {
status = U_ILLEGAL_ARGUMENT_ERROR; return;
} if (amount == 0) { return; // Do nothing!
}
// We handle most fields in the same way. The algorithm is to add // a computed amount of millis to the current millis. The only // wrinkle is with DST (and/or a change to the zone's UTC offset, which // we'll include with DST) -- for some fields, like the DAY_OF_MONTH, // we don't want the wall time to shift due to changes in DST. If the // result of the add operation is to move from DST to Standard, or // vice versa, we need to adjust by an hour forward or back, // respectively. For such fields we set keepWallTimeInvariant to true.
// We only adjust the DST for fields larger than an hour. For // fields smaller than an hour, we cannot adjust for DST without // causing problems. for instance, if you add one hour to April 5, // 1998, 1:00 AM, in PST, the time becomes "2:00 AM PDT" (an // illegal value), but then the adjustment sees the change and // compensates by subtracting an hour. As a result the time // doesn't advance at all.
// For some fields larger than a day, such as a UCAL_MONTH, we pin the // UCAL_DAY_OF_MONTH. This allows <March 31>.add(UCAL_MONTH, 1) to be // <April 30>, rather than <April 31> => <May 1>.
double delta = amount; // delta in ms
UBool keepWallTimeInvariant = true;
switch (field) { case UCAL_ERA:
{
int32_t era = get(UCAL_ERA, status); if (U_FAILURE(status)) { return;
} if (uprv_add32_overflow(era, amount, &era)) {
status = U_ILLEGAL_ARGUMENT_ERROR; return;
}
set(UCAL_ERA, era);
pinField(UCAL_ERA, status); return;
}
case UCAL_YEAR: case UCAL_YEAR_WOY:
{ // * If era=0 and years go backwards in time, change sign of amount. // * Until we have new API per #9393, we temporarily hardcode knowledge of // which calendars have era 0 years that go backwards. // * Note that for UCAL_YEAR (but not UCAL_YEAR_WOY) we could instead handle // this by applying the amount to the UCAL_EXTENDED_YEAR field; but since // we would still need to handle UCAL_YEAR_WOY as below, might as well // also handle UCAL_YEAR the same way. if (get(UCAL_ERA, status) == 0 && isEra0CountingBackward()) { if (uprv_mul32_overflow(amount, -1, &amount)) {
status = U_ILLEGAL_ARGUMENT_ERROR; return;
}
}
} // Fall through into normal handling
U_FALLTHROUGH; case UCAL_EXTENDED_YEAR: case UCAL_MONTH: case UCAL_ORDINAL_MONTH:
{
UBool oldLenient = isLenient();
setLenient(true);
int32_t value = get(field, status); if (U_FAILURE(status)) { return;
} if (uprv_add32_overflow(value, amount, &value)) {
status = U_ILLEGAL_ARGUMENT_ERROR; return;
}
set(field, value);
case UCAL_WEEK_OF_YEAR: case UCAL_WEEK_OF_MONTH: case UCAL_DAY_OF_WEEK_IN_MONTH:
delta *= kOneWeek; break;
case UCAL_AM_PM:
delta *= 12 * kOneHour; break;
case UCAL_DAY_OF_MONTH: case UCAL_DAY_OF_YEAR: case UCAL_DAY_OF_WEEK: case UCAL_DOW_LOCAL: case UCAL_JULIAN_DAY:
delta *= kOneDay; break;
case UCAL_HOUR_OF_DAY: case UCAL_HOUR:
delta *= kOneHour;
keepWallTimeInvariant = false; break;
case UCAL_MINUTE:
delta *= kOneMinute;
keepWallTimeInvariant = false; break;
case UCAL_SECOND:
delta *= kOneSecond;
keepWallTimeInvariant = false; break;
case UCAL_MILLISECOND: case UCAL_MILLISECONDS_IN_DAY:
keepWallTimeInvariant = false; break;
default: #ifdefined (U_DEBUG_CAL)
fprintf(stderr, "%s:%d: ILLEGAL ARG because field %s not addable",
__FILE__, __LINE__, fldName(field)); #endif
status = U_ILLEGAL_ARGUMENT_ERROR; return; // throw new IllegalArgumentException("Calendar.add(" + fieldName(field) + // ") not supported");
}
// In order to keep the wall time invariant (for fields where this is // appropriate), check the combined DST & ZONE offset before and // after the add() operation. If it changes, then adjust the millis // to compensate.
int32_t prevOffset = 0;
int32_t prevWallTime = 0; if (keepWallTimeInvariant) {
prevOffset = get(UCAL_DST_OFFSET, status) + get(UCAL_ZONE_OFFSET, status);
prevWallTime = get(UCAL_MILLISECONDS_IN_DAY, status);
}
if (keepWallTimeInvariant) {
int32_t newWallTime = get(UCAL_MILLISECONDS_IN_DAY, status); if (newWallTime != prevWallTime) { // There is at least one zone transition between the base // time and the result time. As the result, wall time has // changed.
UDate t = internalGetTime();
int32_t newOffset = get(UCAL_DST_OFFSET, status) + get(UCAL_ZONE_OFFSET, status); if (newOffset != prevOffset) { // When the difference of the previous UTC offset and // the new UTC offset exceeds 1 full day, we do not want // to roll over/back the date. For now, this only happens // in Samoa (Pacific/Apia) on Dec 30, 2011. See ticket:9452.
int32_t adjAmount = prevOffset - newOffset;
adjAmount = adjAmount >= 0 ? adjAmount % static_cast<int32_t>(kOneDay) : -(-adjAmount % static_cast<int32_t>(kOneDay)); if (adjAmount != 0) {
setTimeInMillis(t + adjAmount, status);
newWallTime = get(UCAL_MILLISECONDS_IN_DAY, status);
} if (newWallTime != prevWallTime) { // The result wall time or adjusted wall time was shifted because // the target wall time does not exist on the result date. switch (fSkippedWallTime) { case UCAL_WALLTIME_FIRST: if (adjAmount > 0) {
setTimeInMillis(t, status);
} break; case UCAL_WALLTIME_LAST: if (adjAmount < 0) {
setTimeInMillis(t, status);
} break; case UCAL_WALLTIME_NEXT_VALID:
UDate tmpT = adjAmount > 0 ? internalGetTime() : t;
UDate immediatePrevTrans;
UBool hasTransition = getImmediatePreviousZoneTransition(tmpT, &immediatePrevTrans, status); if (U_SUCCESS(status) && hasTransition) {
setTimeInMillis(immediatePrevTrans, status);
} break;
}
}
}
}
}
}
int32_t Calendar::fieldDifference(UDate targetMs, UCalendarDateFields field, UErrorCode& ec) { if (U_FAILURE(ec)) { return0;
} if (field < 0 || field >= UCAL_FIELD_COUNT) {
ec = U_ILLEGAL_ARGUMENT_ERROR; return0;
}
int32_t min = 0; double startMs = getTimeInMillis(ec); // Always add from the start millis. This accommodates // operations like adding years from February 29, 2000 up to // February 29, 2004. If 1, 1, 1, 1 is added to the year // field, the DOM gets pinned to 28 and stays there, giving an // incorrect DOM difference of 1. We have to add 1, reset, 2, // reset, 3, reset, 4. if (startMs < targetMs) {
int32_t max = 1; // Find a value that is too large while (U_SUCCESS(ec)) {
setTimeInMillis(startMs, ec);
add(field, max, ec); double ms = getTimeInMillis(ec); if (ms == targetMs) { return max;
} elseif (ms > targetMs) { break;
} elseif (max < INT32_MAX) {
min = max;
max <<= 1; if (max < 0) {
max = INT32_MAX;
}
} else { // Field difference too large to fit into int32_t #ifdefined (U_DEBUG_CAL)
fprintf(stderr, "%s:%d: ILLEGAL ARG because field %s's max too large for int32_t\n",
__FILE__, __LINE__, fldName(field)); #endif
ec = U_ILLEGAL_ARGUMENT_ERROR; return0;
}
} // Do a binary search while ((max - min) > 1 && U_SUCCESS(ec)) {
int32_t t = min + (max - min)/2; // make sure intermediate values don't exceed INT32_MAX
setTimeInMillis(startMs, ec);
add(field, t, ec); double ms = getTimeInMillis(ec); if (ms == targetMs) { return t;
} elseif (ms > targetMs) {
max = t;
} else {
min = t;
}
}
} elseif (startMs > targetMs) {
int32_t max = -1; // Find a value that is too small while (U_SUCCESS(ec)) {
setTimeInMillis(startMs, ec);
add(field, max, ec); double ms = getTimeInMillis(ec); if (ms == targetMs) { return max;
} elseif (ms < targetMs) { break;
} else {
min = max;
max = static_cast<int32_t>(static_cast<uint32_t>(max) << 1); if (max == 0) { // Field difference too large to fit into int32_t #ifdefined (U_DEBUG_CAL)
fprintf(stderr, "%s:%d: ILLEGAL ARG because field %s's max too large for int32_t\n",
__FILE__, __LINE__, fldName(field)); #endif
ec = U_ILLEGAL_ARGUMENT_ERROR; return0;
}
}
} // Do a binary search while ((min - max) > 1 && U_SUCCESS(ec)) {
int32_t t = min + (max - min)/2; // make sure intermediate values don't exceed INT32_MAX
setTimeInMillis(startMs, ec);
add(field, t, ec); double ms = getTimeInMillis(ec); if (ms == targetMs) { return t;
} elseif (ms < targetMs) {
max = t;
} else {
min = t;
}
}
} // Set calendar to end point
setTimeInMillis(startMs, ec);
add(field, min, ec);
/* Test for buffer overflows */ if(U_FAILURE(ec)) { return0;
} return min;
}
// -------------------------------------
void
Calendar::adoptTimeZone(TimeZone* zone)
{ // Do nothing if passed-in zone is nullptr if (zone == nullptr) { return;
}
// fZone should always be non-null delete fZone;
fZone = zone;
// if the zone changes, we need to recompute the time fields
fAreFieldsSet = false;
}
TimeZone*
Calendar::orphanTimeZone()
{ // we let go of the time zone; the new time zone is the system default time zone
TimeZone *defaultZone = TimeZone::createDefault(); if (defaultZone == nullptr) { // No error handling available. Must keep fZone non-nullptr, there are many unchecked uses. return nullptr;
}
TimeZone *z = fZone;
fZone = defaultZone; return z;
}
void
Calendar::setMinimalDaysInFirstWeek(uint8_t value)
{ // Values less than 1 have the same effect as 1; values greater // than 7 have the same effect as 7. However, we normalize values // so operator== and so forth work. if (value < 1) {
value = 1;
} elseif (value > 7) {
value = 7;
} if (fMinimalDaysInFirstWeek != value) {
fMinimalDaysInFirstWeek = value;
fAreFieldsSet = false;
}
}
UBool
Calendar::isWeekend(UDate date, UErrorCode &status) const
{ if (U_FAILURE(status)) { returnfalse;
} // clone the calendar so we don't mess with the real one.
Calendar *work = this->clone(); if (work == nullptr) {
status = U_MEMORY_ALLOCATION_ERROR; returnfalse;
}
UBool result = false;
work->setTime(date, status); if (U_SUCCESS(status)) {
result = work->isWeekend();
} delete work; return result;
}
UBool
Calendar::isWeekend() const
{
UErrorCode status = U_ZERO_ERROR;
UCalendarDaysOfWeek dayOfWeek = static_cast<UCalendarDaysOfWeek>(get(UCAL_DAY_OF_WEEK, status));
UCalendarWeekdayType dayType = getDayOfWeekType(dayOfWeek, status); if (U_SUCCESS(status)) { switch (dayType) { case UCAL_WEEKDAY: returnfalse; case UCAL_WEEKEND: returntrue; case UCAL_WEEKEND_ONSET: case UCAL_WEEKEND_CEASE: // Use internalGet() because the above call to get() populated all fields.
{
int32_t millisInDay = internalGet(UCAL_MILLISECONDS_IN_DAY);
int32_t transitionMillis = getWeekendTransition(dayOfWeek, status); if (U_SUCCESS(status)) { return (dayType == UCAL_WEEKEND_ONSET)?
(millisInDay >= transitionMillis):
(millisInDay < transitionMillis);
} // else fall through, return false
U_FALLTHROUGH;
} default: break;
}
} returnfalse;
}
int32_t Calendar::getLimit(UCalendarDateFields field, ELimitType limitType) const { switch (field) { case UCAL_DAY_OF_WEEK: case UCAL_AM_PM: case UCAL_HOUR: case UCAL_HOUR_OF_DAY: case UCAL_MINUTE: case UCAL_SECOND: case UCAL_MILLISECOND: case UCAL_ZONE_OFFSET: case UCAL_DST_OFFSET: case UCAL_DOW_LOCAL: case UCAL_JULIAN_DAY: case UCAL_MILLISECONDS_IN_DAY: case UCAL_IS_LEAP_MONTH: return kCalendarLimits[field][limitType];
int32_t
Calendar::getActualMinimum(UCalendarDateFields field, UErrorCode& status) const
{ if (U_FAILURE(status)) { return0;
} if (field < 0 || field >= UCAL_FIELD_COUNT) {
status = U_ILLEGAL_ARGUMENT_ERROR; return0;
}
int32_t fieldValue = getGreatestMinimum(field);
int32_t endValue = getMinimum(field);
// if we know that the minimum value is always the same, just return it if (fieldValue == endValue) { return fieldValue;
}
// clone the calendar so we don't mess with the real one, and set it to // accept anything for the field values
Calendar *work = this->clone(); if (work == nullptr) {
status = U_MEMORY_ALLOCATION_ERROR; return0;
}
work->setLenient(true);
// now try each value from getLeastMaximum() to getMaximum() one by one until // we get a value that normalizes to another value. The last value that // normalizes to itself is the actual minimum for the current date
int32_t result = fieldValue;
do {
work->set(field, fieldValue); if (work->get(field, status) != fieldValue) { break;
} else {
result = fieldValue;
fieldValue--;
}
} while (fieldValue >= endValue);
delete work;
/* Test for buffer overflows */ if(U_FAILURE(status)) { return0;
} return result;
}
bool
Calendar::inTemporalLeapYear(UErrorCode& status) const
{ // Default to Gregorian based leap year rule. return getActualMaximum(UCAL_DAY_OF_YEAR, status) == 366;
}
void
Calendar::setTemporalMonthCode(constchar* code, UErrorCode& status )
{ if (U_FAILURE(status)) { return;
}
int32_t len = static_cast<int32_t>(uprv_strlen(code)); if (len == 3 && code[0] == 'M') { for (int m = 0; gTemporalMonthCodes[m] != nullptr; m++) { if (uprv_strcmp(code, gTemporalMonthCodes[m]) == 0) {
set(UCAL_MONTH, m);
set(UCAL_IS_LEAP_MONTH, 0); return;
}
}
}
status = U_ILLEGAL_ARGUMENT_ERROR;
}
// -------------------------------------
/** *Ensurethateachfieldiswithinitsvalidrangebycalling{@link *#validateField(int)}oneachfieldthathasbeenset.Thismethod *shouldonlybecalledifthiscalendarisnotlenient. *@see#isLenient *@see#validateField(int)
*/ void Calendar::validateFields(UErrorCode &status) { if (U_FAILURE(status)) { return;
} for (int32_t field = 0; U_SUCCESS(status) && (field < UCAL_FIELD_COUNT); field++) { if (fStamp[field] >= kMinimumUserStamp) {
validateField(static_cast<UCalendarDateFields>(field), status);
}
}
}
/** *Validateasinglefieldofthiscalendar.Subclassesshould *overridethismethodtovalidateanycalendar-specificfields. *Genericfieldscanbehandledby *<code>Calendar.validateField()</code>. *@see#validateField(int,int,int)
*/ void Calendar::validateField(UCalendarDateFields field, UErrorCode &status) { if (U_FAILURE(status)) { return;
} if (field < 0 || field >= UCAL_FIELD_COUNT) {
status = U_ILLEGAL_ARGUMENT_ERROR; return;
}
int32_t y; switch (field) { case UCAL_DAY_OF_MONTH:
y = handleGetExtendedYear(status); if (U_FAILURE(status)) { return;
}
validateField(field, 1, handleGetMonthLength(y, internalGetMonth(status), status), status); break; case UCAL_DAY_OF_YEAR:
y = handleGetExtendedYear(status); if (U_FAILURE(status)) { return;
}
validateField(field, 1, handleGetYearLength(y, status), status); break; case UCAL_DAY_OF_WEEK_IN_MONTH: if (internalGet(field) == 0) { #ifdefined (U_DEBUG_CAL)
fprintf(stderr, "%s:%d: ILLEGAL ARG because DOW in month cannot be 0\n",
__FILE__, __LINE__); #endif
status = U_ILLEGAL_ARGUMENT_ERROR; // "DAY_OF_WEEK_IN_MONTH cannot be zero" return;
}
validateField(field, getMinimum(field), getMaximum(field), status); break; default:
validateField(field, getMinimum(field), getMaximum(field), status); break;
}
}
/** *Validateasinglefieldofthiscalendargivenitsminimumand *maximumallowedvalue.Ifthefieldisoutofrange,throwa *descriptive<code>IllegalArgumentException</code>.Subclassesmay *usethismethodintheirimplementationof{@link *#validateField(int)}.
*/ void Calendar::validateField(UCalendarDateFields field, int32_t min, int32_t max, UErrorCode& status)
{ if (U_FAILURE(status)) { return;
} if (field < 0 || field >= UCAL_FIELD_COUNT) {
status = U_ILLEGAL_ARGUMENT_ERROR; return;
}
int32_t value = fFields[field]; if (value < min || value > max) { #ifdefined (U_DEBUG_CAL)
fprintf(stderr, "%s:%d: ILLEGAL ARG because of field %s out of range %d..%d at %d\n",
__FILE__, __LINE__,fldName(field),min,max,value); #endif
status = U_ILLEGAL_ARGUMENT_ERROR; return;
}
}
UCalendarDateFields Calendar::resolveFields(const UFieldResolutionTable* precedenceTable) const {
int32_t bestField = UCAL_FIELD_COUNT;
int32_t tempBestField; for (int32_t g=0; precedenceTable[g][0][0] != -1 && (bestField == UCAL_FIELD_COUNT); ++g) {
int32_t bestStamp = kUnset; for (int32_t l=0; precedenceTable[g][l][0] != -1; ++l) {
int32_t lineStamp = kUnset; // Skip over first entry if it is negative for (int32_t i=((precedenceTable[g][l][0]>=kResolveRemap)?1:0); precedenceTable[g][l][i]!=-1; ++i) {
U_ASSERT(precedenceTable[g][l][i] < UCAL_FIELD_COUNT);
int32_t s = fStamp[precedenceTable[g][l][i]]; // If any field is unset then don't use this line if (s == kUnset) { goto linesInGroup;
} elseif(s > lineStamp) {
lineStamp = s;
}
} // Record new maximum stamp & field no. if (lineStamp > bestStamp) {
tempBestField = precedenceTable[g][l][0]; // First field refers to entire line if (tempBestField >= kResolveRemap) {
tempBestField &= (kResolveRemap-1); // This check is needed to resolve some issues with UCAL_YEAR precedence mapping if (tempBestField != UCAL_DATE || (fStamp[UCAL_WEEK_OF_MONTH] < fStamp[tempBestField])) {
bestField = tempBestField;
}
} else {
bestField = tempBestField;
}
// We only use MILLISECONDS_IN_DAY if it has been set by the user. // This makes it possible for the caller to set the calendar to a // time and call clear(MONTH) to reset the MONTH to January. This // is legacy behavior. Without this, clear(MONTH) has no effect, // since the internally set JULIAN_DAY is used. if (fStamp[UCAL_MILLISECONDS_IN_DAY] >= static_cast<int32_t>(kMinimumUserStamp) &&
newestStamp(UCAL_AM_PM, UCAL_MILLISECOND, kUnset) <= fStamp[UCAL_MILLISECONDS_IN_DAY]) {
millisInDay = internalGet(UCAL_MILLISECONDS_IN_DAY);
} else {
millisInDay = computeMillisInDay();
}
UDate t = 0; if (fStamp[UCAL_ZONE_OFFSET] >= static_cast<int32_t>(kMinimumUserStamp) ||
fStamp[UCAL_DST_OFFSET] >= static_cast<int32_t>(kMinimumUserStamp)) {
t = millis + millisInDay - internalGet(UCAL_ZONE_OFFSET) - internalGet(UCAL_DST_OFFSET);
} else { // Compute the time zone offset and DST offset. There are two potential // ambiguities here. We'll assume a 2:00 am (wall time) switchover time // for discussion purposes here. // // 1. The positive offset change such as transition into DST. // Here, a designated time of 2:00 am - 2:59 am does not actually exist. // For this case, skippedWallTime option specifies the behavior. // For example, 2:30 am is interpreted as; // - WALLTIME_LAST(default): 3:30 am (DST) (interpreting 2:30 am as 31 minutes after 1:59 am (STD)) // - WALLTIME_FIRST: 1:30 am (STD) (interpreting 2:30 am as 30 minutes before 3:00 am (DST)) // - WALLTIME_NEXT_VALID: 3:00 am (DST) (next valid time after 2:30 am on a wall clock) // 2. The negative offset change such as transition out of DST. // Here, a designated time of 1:00 am - 1:59 am can be in standard or DST. Both are valid // representations (the rep jumps from 1:59:59 DST to 1:00:00 Std). // For this case, repeatedWallTime option specifies the behavior. // For example, 1:30 am is interpreted as; // - WALLTIME_LAST(default): 1:30 am (STD) - latter occurrence // - WALLTIME_FIRST: 1:30 am (DST) - former occurrence // // In addition to above, when calendar is strict (not default), wall time falls into // the skipped time range will be processed as an error case. // // These special cases are mostly handled in #computeZoneOffset(long), except WALLTIME_NEXT_VALID // at positive offset change. The protected method computeZoneOffset(long) is exposed to Calendar // subclass implementations and marked as @stable. Strictly speaking, WALLTIME_NEXT_VALID // should be also handled in the same place, but we cannot change the code flow without deprecating // the protected method. // // We use the TimeZone object, unless the user has explicitly set the ZONE_OFFSET // or DST_OFFSET fields; then we use those fields.
if (!isLenient() || fSkippedWallTime == UCAL_WALLTIME_NEXT_VALID) { // When strict, invalidate a wall time falls into a skipped wall time range. // When lenient and skipped wall time option is WALLTIME_NEXT_VALID, // the result time will be adjusted to the next valid time (on wall clock).
int32_t zoneOffset = computeZoneOffset(millis, millisInDay, status);
UDate tmpTime = millis + millisInDay - zoneOffset;
if (U_SUCCESS(status)) { // zoneOffset != (raw + dst) only when the given wall time fall into // a skipped wall time range caused by positive zone offset transition. if (zoneOffset != (raw + dst)) { if (!isLenient()) {
status = U_ILLEGAL_ARGUMENT_ERROR;
} else {
U_ASSERT(fSkippedWallTime == UCAL_WALLTIME_NEXT_VALID); // Adjust time to the next valid wall clock time. // At this point, tmpTime is on or after the zone offset transition causing // the skipped time range.
UDate immediatePrevTransition;
UBool hasTransition = getImmediatePreviousZoneTransition(tmpTime, &immediatePrevTransition, status); if (U_SUCCESS(status) && hasTransition) {
t = immediatePrevTransition;
}
}
} else {
t = tmpTime;
}
}
} else {
t = millis + millisInDay - computeZoneOffset(millis, millisInDay, status);
}
} if (U_SUCCESS(status)) {
internalSetTime(t);
}
}
/** *Findthepreviouszonetransitionnearthegiventime.
*/
UBool Calendar::getImmediatePreviousZoneTransition(UDate base, UDate *transitionTime, UErrorCode& status) const { if (U_FAILURE(status)) { returnfalse;
}
BasicTimeZone *btz = getBasicTimeZone(); if (btz) {
TimeZoneTransition trans;
UBool hasTransition = btz->getPreviousTransition(base, true, trans); if (hasTransition) {
*transitionTime = trans.getTime(); returntrue;
} else { // Could not find any transitions. // Note: This should never happen.
status = U_INTERNAL_PROGRAM_ERROR;
}
} else { // If not BasicTimeZone, return unsupported error for now. // TODO: We may support non-BasicTimeZone in future.
status = U_UNSUPPORTED_ERROR;
} returnfalse;
}
/** *Computethemillisecondsinthedayfromthefields.Thisisa *valuefrom0to23:59:59.999inclusive,unlessfieldsareoutof *range,inwhichcaseitcanbeanarbitraryvalue.Thisvalue *reflectslocalzonewalltime. *@stableICU2.0
*/ double Calendar::computeMillisInDay() { // Do the time portion of the conversion.
double millisInDay = 0;
// Find the best set of fields specifying the time of day. There // are only two possibilities here; the HOUR_OF_DAY or the // AM_PM and the HOUR.
int32_t hourOfDayStamp = fStamp[UCAL_HOUR_OF_DAY];
int32_t hourStamp = (fStamp[UCAL_HOUR] > fStamp[UCAL_AM_PM])?fStamp[UCAL_HOUR]:fStamp[UCAL_AM_PM];
int32_t bestStamp = (hourStamp > hourOfDayStamp) ? hourStamp : hourOfDayStamp;
// Hours if (bestStamp != kUnset) { if (bestStamp == hourOfDayStamp) { // Don't normalize here; let overflow bump into the next period. // This is consistent with how we handle other fields.
millisInDay += internalGet(UCAL_HOUR_OF_DAY);
} else { // Don't normalize here; let overflow bump into the next period. // This is consistent with how we handle other fields.
millisInDay += internalGet(UCAL_HOUR); // Treat even number as AM and odd nubmber as PM to align with the // logic in roll()
millisInDay += (internalGet(UCAL_AM_PM) % 2 == 0) ? 0 : 12;
}
}
// We use the fact that unset == 0; we start with millisInDay // == HOUR_OF_DAY.
millisInDay *= 60;
millisInDay += internalGet(UCAL_MINUTE); // now have minutes
millisInDay *= 60;
millisInDay += internalGet(UCAL_SECOND); // now have seconds
millisInDay *= 1000;
millisInDay += internalGet(UCAL_MILLISECOND); // now have millis
UBool sawRecentNegativeShift = false; if (fRepeatedWallTime == UCAL_WALLTIME_FIRST) { // Check if the given wall time falls into repeated time range
UDate tgmt = wall - (rawOffset + dstOffset);
// Any negative zone transition within last 6 hours? // Note: The maximum historic negative zone transition is -3 hours in the tz database. // 6 hour window would be sufficient for this purpose.
int32_t tmpRaw, tmpDst;
tz.getOffset(tgmt - 6*60*60*1000, false, tmpRaw, tmpDst, ec);
int32_t offsetDelta = (rawOffset + dstOffset) - (tmpRaw + tmpDst);
U_ASSERT(offsetDelta < -6*60*60*1000); if (offsetDelta < 0) {
sawRecentNegativeShift = true; // Negative shift within last 6 hours. When UCAL_WALLTIME_FIRST is used and the given wall time falls // into the repeated time range, use offsets before the transition. // Note: If it does not fall into the repeated time range, offsets remain unchanged below.
tz.getOffset(wall + offsetDelta, true, rawOffset, dstOffset, ec);
}
} if (!sawRecentNegativeShift && fSkippedWallTime == UCAL_WALLTIME_FIRST) { // When skipped wall time option is WALLTIME_FIRST, // recalculate offsets from the resolved time (non-wall). // When the given wall time falls into skipped wall time, // the offsets will be based on the zone offsets AFTER // the transition (which means, earliest possible interpretation).
UDate tgmt = wall - (rawOffset + dstOffset);
tz.getOffset(tgmt, false, rawOffset, dstOffset, ec);
}
} return rawOffset + dstOffset;
}
int32_t Calendar::computeJulianDay(UErrorCode &status)
{ // We want to see if any of the date fields is newer than the // JULIAN_DAY. If not, then we use JULIAN_DAY. If so, then we do // the normal resolution. We only use JULIAN_DAY if it has been // set by the user. This makes it possible for the caller to set // the calendar to a time and call clear(MONTH) to reset the MONTH // to January. This is legacy behavior. Without this, // clear(MONTH) has no effect, since the internally set JULIAN_DAY // is used. if (fStamp[UCAL_JULIAN_DAY] >= static_cast<int32_t>(kMinimumUserStamp)) {
int32_t bestStamp = newestStamp(UCAL_ERA, UCAL_DAY_OF_WEEK_IN_MONTH, kUnset);
bestStamp = newestStamp(UCAL_YEAR_WOY, UCAL_EXTENDED_YEAR, bestStamp);
bestStamp = newestStamp(UCAL_ORDINAL_MONTH, UCAL_ORDINAL_MONTH, bestStamp); if (bestStamp <= fStamp[UCAL_JULIAN_DAY]) { return internalGet(UCAL_JULIAN_DAY);
}
}
if (bestField == UCAL_WEEK_OF_YEAR && newerField(UCAL_YEAR_WOY, UCAL_YEAR) == UCAL_YEAR_WOY) {
year = internalGet(UCAL_YEAR_WOY);
} else {
year = handleGetExtendedYear(status); if (U_FAILURE(status)) { return0;
}
}
internalSet(UCAL_EXTENDED_YEAR, year); // Return U_ILLEGAL_ARGUMENT_ERROR if year is too large that may cuase int32_t overflow // later. if (year > INT32_MAX / 400) {
status = U_ILLEGAL_ARGUMENT_ERROR; return0;
}
// At this point julianDay is the 0-based day BEFORE the first day of // January 1, year 1 of the given calendar. If julianDay == 0, it // specifies (Jan. 1, 1) - 1, in whatever calendar we are using (Julian // or Gregorian). (or it is before the month we are in, if useMonth is True)
// At this point we need to process the WEEK_OF_MONTH or // WEEK_OF_YEAR, which are similar, or the DAY_OF_WEEK_IN_MONTH. // First, perform initial shared computations. These locate the // first week of the period.
// Get the 0-based localized DOW of day one of the month or year. // Valid range 0..6.
int32_t first = julianDayToDayOfWeek(julianDay + 1) - firstDayOfWeek; if (first < 0) {
first += 7;
}
int32_t dowLocal = getLocalDOW(status); if (U_FAILURE(status)) { return0;
}
// Find the first target DOW (dowLocal) in the month or year. // Actually, it may be just before the first of the month or year. // It will be an integer from -5..7.
int32_t date = 1 - first + dowLocal;
if (bestField == UCAL_DAY_OF_WEEK_IN_MONTH) { // Adjust the target DOW to be in the month or year. if (date < 1) {
date += 7;
}
// The only trickiness occurs if the day-of-week-in-month is // negative.
int32_t dim = internalGet(UCAL_DAY_OF_WEEK_IN_MONTH, 1); if (dim >= 0) {
int32_t temp; if (uprv_mul32_overflow(7, dim - 1, &temp) ||
uprv_add32_overflow(date, temp, &date)) {
status = U_ILLEGAL_ARGUMENT_ERROR; return0;
}
} else { // Move date to the last of this day-of-week in this month, // then back up as needed. If dim==-1, we don't back up at // all. If dim==-2, we back up once, etc. Don't back up // past the first of the given day-of-week in this month. // Note that we handle -2, -3, etc. correctly, even though // values < -1 are technically disallowed.
int32_t m = internalGetMonth(UCAL_JANUARY, status);
int32_t monthLength = handleGetMonthLength(year, m, status); if (U_FAILURE(status)) { return0;
}
int32_t temp; if (uprv_add32_overflow((monthLength - date) / 7, dim+1, &temp) ||
uprv_mul32_overflow(temp, 7, &temp) ||
uprv_add32_overflow(date, temp, &date)) {
status = U_ILLEGAL_ARGUMENT_ERROR; return0;
}
}
} else { #ifdefined (U_DEBUG_CAL)
fprintf(stderr, "%s:%d - bf= %s\n", __FILE__, __LINE__, fldName(bestField)); #endif
if(bestField == UCAL_WEEK_OF_YEAR) { // ------------------------------------- WOY ------------- if(!isSet(UCAL_YEAR_WOY) || // YWOY not set at all or
( (resolveFields(kYearPrecedence) != UCAL_YEAR_WOY) // YWOY doesn't have precedence
&& (fStamp[UCAL_YEAR_WOY]!=kInternallySet) ) ) // (excluding where all fields are internally set - then YWOY is used)
{ // need to be sure to stay in 'real' year.
int32_t woy = internalGet(bestField);
int32_t nextJulianDay = handleComputeMonthStart(year+1, 0, false, status); // jd of day before jan 1 if (U_FAILURE(status)) { return0;
}
int32_t nextFirst = julianDayToDayOfWeek(nextJulianDay + 1) - firstDayOfWeek;
if (nextFirst < 0) { // 0..6 ldow of Jan 1
nextFirst += 7;
}
// nextFirst is now the localized DOW of Jan 1 of y-woy+1 if((nextFirst > 0) && // Jan 1 starts on FDOW
(7-nextFirst) >= getMinimalDaysInFirstWeek()) // or enough days in the week
{ // Jan 1 of (yearWoy+1) is in yearWoy+1 - recalculate JD to next year #ifdefined (U_DEBUG_CAL)
fprintf(stderr, "%s:%d - was going to move JD from %d to %d [d%d]\n", __FILE__, __LINE__,
julianDay, nextJulianDay, (nextJulianDay-julianDay)); #endif
julianDay = nextJulianDay;
// recalculate 'first' [0-based local dow of jan 1]
first = julianDayToDayOfWeek(julianDay + 1) - firstDayOfWeek; if (first < 0) {
first += 7;
} // recalculate date.
date = 1 - first + dowLocal;
}
} elseif(woy>=getLeastMaximum(bestField)) { // could be in the last week- find out if this JD would overstep
int32_t testDate = date; if ((7 - first) < getMinimalDaysInFirstWeek()) {
testDate += 7;
}
// Now adjust for the week number.
int32_t weeks; if (uprv_mul32_overflow(woy-1, 7, &weeks) ||
uprv_add32_overflow(weeks, testDate, &testDate)) {
status = U_ILLEGAL_ARGUMENT_ERROR; return0;
}
if(testDate > nextJulianDay) { // is it past Dec 31? (nextJulianDay is day BEFORE year+1's Jan 1) // Fire up the calculating engines.. retry YWOY = (year-1)
int32_t prevYear; if (uprv_add32_overflow(year, -1, &prevYear)) {
status = U_ILLEGAL_ARGUMENT_ERROR; return0;
}
julianDay = handleComputeMonthStart(prevYear, 0, false, status); // jd before Jan 1 of previous year if (U_FAILURE(status)) { return0;
}
first = julianDayToDayOfWeek(julianDay + 1) - firstDayOfWeek; // 0 based local dow of first week
if(first < 0) { // 0..6
first += 7;
}
date = 1 - first + dowLocal;
#ifdefined (U_DEBUG_CAL)
fprintf(stderr, "%s:%d - date now %d, jd%d, ywoy%d\n",
__FILE__, __LINE__, date, julianDay, year-1); #endif
// assert(bestField == WEEK_OF_MONTH || bestField == WEEK_OF_YEAR) // Adjust for minimal days in first week if ((7 - first) < getMinimalDaysInFirstWeek()) {
date += 7;
}
// Now adjust for the week number.
int32_t weeks = internalGet(bestField); if (uprv_add32_overflow(weeks, -1, &weeks) ||
uprv_mul32_overflow(7, weeks, &weeks) ||
uprv_add32_overflow(date, weeks, &date)) {
status = U_ILLEGAL_ARGUMENT_ERROR; return0;
}
}
if (uprv_add32_overflow(julianDay, date, &julianDay)) {
status = U_ILLEGAL_ARGUMENT_ERROR; return0;
} return julianDay;
}
int32_t
Calendar::getDefaultMonthInYear(int32_t /*eyear*/, UErrorCode& /* status */)
{ return0;
}
int32_t Calendar::getLocalDOW(UErrorCode& status)
{ if (U_FAILURE(status)) { return0;
} // Get zero-based localized DOW, valid range 0..6. This is the DOW // we are looking for.
int32_t dowLocal = 0; switch (resolveFields(kDOWPrecedence)) { case UCAL_DAY_OF_WEEK:
dowLocal = internalGet(UCAL_DAY_OF_WEEK); if (uprv_add32_overflow(dowLocal, -fFirstDayOfWeek, &dowLocal)) {
status = U_ILLEGAL_ARGUMENT_ERROR; return0;
} break; case UCAL_DOW_LOCAL:
dowLocal = internalGet(UCAL_DOW_LOCAL); if (uprv_add32_overflow(dowLocal, -1, &dowLocal)) {
status = U_ILLEGAL_ARGUMENT_ERROR; return0;
} break; default: break;
}
dowLocal = dowLocal % 7; if (dowLocal < 0) {
dowLocal += 7;
} return dowLocal;
}
int32_t Calendar::handleGetExtendedYearFromWeekFields(int32_t yearWoy, int32_t woy, UErrorCode& status)
{ if (U_FAILURE(status)) { return0;
} // We have UCAL_YEAR_WOY and UCAL_WEEK_OF_YEAR - from those, determine // what year we fall in, so that other code can set it properly. // (code borrowed from computeWeekFields and handleComputeJulianDay) //return yearWoy;
// First, we need a reliable DOW.
UCalendarDateFields bestField = resolveFields(kDatePrecedence); // !! Note: if subclasses have a different table, they should override handleGetExtendedYearFromWeekFields
// Now, a local DOW
int32_t dowLocal = getLocalDOW(status); // 0..6 if (U_FAILURE(status)) { return0;
}
int32_t firstDayOfWeek = getFirstDayOfWeek(); // Localized fdw
int32_t jan1Start = handleComputeMonthStart(yearWoy, 0, false, status);
int32_t yearWoyPlus1; if (uprv_add32_overflow(yearWoy, 1, &yearWoyPlus1)) {
status = U_ILLEGAL_ARGUMENT_ERROR; return0;
}
int32_t nextJan1Start = handleComputeMonthStart(yearWoyPlus1, 0, false, status); // next year's Jan1 start if (U_FAILURE(status)) { return0;
}
// At this point julianDay is the 0-based day BEFORE the first day of // January 1, year 1 of the given calendar. If julianDay == 0, it // specifies (Jan. 1, 1) - 1, in whatever calendar we are using (Julian // or Gregorian). (or it is before the month we are in, if useMonth is True)
// At this point we need to process the WEEK_OF_MONTH or // WEEK_OF_YEAR, which are similar, or the DAY_OF_WEEK_IN_MONTH. // First, perform initial shared computations. These locate the // first week of the period.
// Get the 0-based localized DOW of day one of the month or year. // Valid range 0..6.
int32_t first = julianDayToDayOfWeek(jan1Start + 1) - firstDayOfWeek; if (first < 0) {
first += 7;
}
//// (nextFirst was not used below) // int32_t nextFirst = julianDayToDayOfWeek(nextJan1Start + 1) - firstDayOfWeek; // if (nextFirst < 0) { // nextFirst += 7; //}
int32_t minDays = getMinimalDaysInFirstWeek();
UBool jan1InPrevYear = false; // January 1st in the year of WOY is the 1st week? (i.e. first week is < minimal ) //UBool nextJan1InPrevYear = false; // January 1st of Year of WOY + 1 is in the first week?
switch(bestField) { case UCAL_WEEK_OF_YEAR: if(woy == 1) { if(jan1InPrevYear) { // the first week of January is in the previous year // therefore WOY1 is always solidly within yearWoy return yearWoy;
} else { // First WOY is split between two years if( dowLocal < first) { // we are prior to Jan 1 return yearWoy-1; // previous year
} else { return yearWoy; // in this year
}
}
} elseif(woy >= getLeastMaximum(bestField)) { // we _might_ be in the last week..
int32_t jd = // Calculate JD of our target day:
jan1Start + // JD of Jan 1
(7-first) + // days in the first week (Jan 1.. )
(woy-1)*7 + // add the weeks of the year
dowLocal; // the local dow (0..6) of last week if(jan1InPrevYear==false) {
jd -= 7; // woy already includes Jan 1's week.
}
if( (jd+1) >= nextJan1Start ) { // we are in week 52 or 53 etc. - actual year is yearWoy+1 return yearWoy+1;
} else { // still in yearWoy; return yearWoy;
}
} else { // we're not possibly in the last week -must be ywoy return yearWoy;
}
case UCAL_DATE:
{
int32_t m = internalGetMonth(status); if (U_FAILURE(status)) { return0;
} if((m == 0) &&
(woy >= getLeastMaximum(UCAL_WEEK_OF_YEAR))) { return yearWoy+1; // month 0, late woy = in the next year
} elseif(woy==1) { //if(nextJan1InPrevYear) { if(m == 0) { return yearWoy;
} else { return yearWoy-1;
} //}
}
} //(internalGet(UCAL_DATE) <= (7-first)) /* && in minDow */ ) { //within 1st week and in this month.. //return yearWoy+1; return yearWoy;
default: // assume the year is appropriate return yearWoy;
}
}
int32_t
Calendar::getActualMaximum(UCalendarDateFields field, UErrorCode& status) const
{ if (U_FAILURE(status)) { return0;
} if (field < 0 || field >= UCAL_FIELD_COUNT) {
status = U_ILLEGAL_ARGUMENT_ERROR; return0;
}
int32_t result; switch (field) { case UCAL_DATE:
{
Calendar *cal = clone(); if(!cal) {
status = U_MEMORY_ALLOCATION_ERROR; return0;
}
cal->setLenient(true);
cal->prepareGetActual(field,false,status);
result = handleGetMonthLength(cal->get(UCAL_EXTENDED_YEAR, status), cal->get(UCAL_MONTH, status), status); delete cal;
} break;
case UCAL_DAY_OF_YEAR:
{
Calendar *cal = clone(); if(!cal) {
status = U_MEMORY_ALLOCATION_ERROR; return0;
}
cal->setLenient(true);
cal->prepareGetActual(field,false,status);
result = handleGetYearLength(cal->get(UCAL_EXTENDED_YEAR, status), status); delete cal;
} break;
case UCAL_DAY_OF_WEEK: case UCAL_AM_PM: case UCAL_HOUR: case UCAL_HOUR_OF_DAY: case UCAL_MINUTE: case UCAL_SECOND: case UCAL_MILLISECOND: case UCAL_ZONE_OFFSET: case UCAL_DST_OFFSET: case UCAL_DOW_LOCAL: case UCAL_JULIAN_DAY: case UCAL_MILLISECONDS_IN_DAY: // These fields all have fixed minima/maxima
result = getMaximum(field); break;
case UCAL_ORDINAL_MONTH:
result = inTemporalLeapYear(status) ? getMaximum(UCAL_ORDINAL_MONTH) : getLeastMaximum(UCAL_ORDINAL_MONTH); break;
default: // For all other fields, do it the hard way....
result = getActualHelper(field, getLeastMaximum(field), getMaximum(field),status); break;
} return result;
}
switch (field) { case UCAL_YEAR: case UCAL_EXTENDED_YEAR:
set(UCAL_DAY_OF_YEAR, getGreatestMinimum(UCAL_DAY_OF_YEAR)); break;
case UCAL_YEAR_WOY:
set(UCAL_WEEK_OF_YEAR, getGreatestMinimum(UCAL_WEEK_OF_YEAR));
U_FALLTHROUGH; case UCAL_MONTH:
set(UCAL_DATE, getGreatestMinimum(UCAL_DATE)); break;
case UCAL_DAY_OF_WEEK_IN_MONTH: // For dowim, the maximum occurs for the DOW of the first of the // month.
set(UCAL_DATE, 1);
set(UCAL_DAY_OF_WEEK, get(UCAL_DAY_OF_WEEK, status)); // Make this user set break;
case UCAL_WEEK_OF_MONTH: case UCAL_WEEK_OF_YEAR: // If we're counting weeks, set the day of the week to either the // first or last localized DOW. We know the last week of a month // or year will contain the first day of the week, and that the // first week will contain the last DOW.
{
int32_t dow = fFirstDayOfWeek; if (isMinimum) {
dow = (dow + 6) % 7; // set to last DOW if (dow < UCAL_SUNDAY) {
dow += 7;
}
} #ifdefined (U_DEBUG_CAL)
fprintf(stderr, "prepareGetActualHelper(WOM/WOY) - dow=%d\n", dow); #endif
set(UCAL_DAY_OF_WEEK, dow);
} break; default: break;
}
// Do this last to give it the newest time stamp
set(field, getGreatestMinimum(field));
}
int32_t Calendar::getActualHelper(UCalendarDateFields field, int32_t startValue, int32_t endValue, UErrorCode &status) const
{ #ifdefined (U_DEBUG_CAL)
fprintf(stderr, "getActualHelper(%d,%d .. %d, %s)\n", field, startValue, endValue, u_errorName(status)); #endif if (U_FAILURE(status)) { return0;
} if (field < 0 || field >= UCAL_FIELD_COUNT) {
status = U_ILLEGAL_ARGUMENT_ERROR; return0;
} if (startValue == endValue) { // if we know that the maximum value is always the same, just return it return startValue;
}
int32_t delta = (endValue > startValue) ? 1 : -1;
// clone the calendar so we don't mess with the real one, and set it to // accept anything for the field values if(U_FAILURE(status)) { return startValue;
}
Calendar *work = clone(); if(!work) {
status = U_MEMORY_ALLOCATION_ERROR; return startValue;
}
// need to resolve time here, otherwise, fields set for actual limit // may cause conflict with fields previously set (but not yet resolved).
work->complete(status);
// now try each value from the start to the end one by one until // we get a value that normalizes to another value. The last value that // normalizes to itself is the actual maximum for the current date
work->set(field, startValue);
// prepareGetActual sets the first day of week in the same week with // the first day of a month. Unlike WEEK_OF_YEAR, week number for the // week which contains days from both previous and current month is // not unique. For example, last several days in the previous month // is week 5, and the rest of week is week 1.
int32_t result = startValue; if ((work->get(field, status) != startValue
&& field != UCAL_WEEK_OF_MONTH && delta > 0 ) || U_FAILURE(status)) { #ifdefined (U_DEBUG_CAL)
fprintf(stderr, "getActualHelper(fld %d) - got %d (not %d) - %s\n", field, work->get(field,status), startValue, u_errorName(status)); #endif
} else { do {
startValue += delta;
work->add(field, delta, status); if (work->get(field, status) != startValue || U_FAILURE(status)) { #ifdefined (U_DEBUG_CAL)
fprintf(stderr, "getActualHelper(fld %d) - got %d (not %d), BREAK - %s\n", field, work->get(field,status), startValue, u_errorName(status)); #endif break;
}
result = startValue;
} while (startValue != endValue);
} delete work; #ifdefined (U_DEBUG_CAL)
fprintf(stderr, "getActualHelper(%d) = %d\n", field, result); #endif return result;
}
// Since week and weekend data is territory based instead of language based, // we may need to tweak the locale that we are using to try to get the appropriate // values, using the following logic: // 1). If the locale has a language but no territory, use the territory as defined by // the likely subtags. // 2). If the locale has a script designation then we ignore it, // then remove it ( i.e. "en_Latn_US" becomes "en_US" )
/* The code here is somewhat of a hack, since week data and weekend data aren't really tied to aspecificcalendar,theyaren'ttrulylocaledata.Butthisistheonlyplacewherevalidand actuallocalecanbeset,sowetakeashotatitherebyloadingarepresentativeresource fromthecalendardata.ThecodeusedtousethedateTimeElementsresourcetogetfirstday
of week data, but this was moved to supplemental data under ticket 7755. (JCE) */
// Get the monthNames resource bundle for the calendar 'type'. Fallback to gregorian if the resource is not // found.
LocalUResourceBundlePointer calData(ures_open(nullptr, useLocale.getBaseName(), &status));
ures_getByKey(calData.getAlias(), gCalendar, calData.getAlias(), &status);
// If we are lenient, we need to recompute the fields to normalize // the values. Also, if we haven't set all the fields yet (i.e., // in a newly-created object), we need to fill in the fields. [LIU] if (isLenient() || ! fAreAllFieldsSet)
fAreFieldsSet = false;
// Deprecated function. This doesn't need to be inline. void
Calendar::internalSet(EDateFields field, int32_t value)
{
internalSet(static_cast<UCalendarDateFields>(field), value);
}
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.