static UBool isValidRuleStartDate(int32_t year, int32_t month, int32_t day) { return year >= MIN_ENCODED_START_YEAR && year <= MAX_ENCODED_START_YEAR
&& month >= 1 && month <= 12 && day >=1 && day <= 31;
}
/** * Encode year/month/date to a single integer. * year is high 16 bits (-32768 to 32767), month is * next 8 bits and day of month is last 8 bits. * * @param year year * @param month month (1-base) * @param day day of month * @return an encoded date.
*/ static int32_t encodeDate(int32_t year, int32_t month, int32_t day) { returnstatic_cast<int32_t>(static_cast<uint32_t>(year) << 16) | month << 8 | day;
}
/** * Compare an encoded date with another date specified by year/month/day. * @param encoded An encoded date * @param year Year of another date * @param month Month of another date * @param day Day of another date * @return -1 when encoded date is earlier, 0 when two dates are same, * and 1 when encoded date is later.
*/ static int32_t compareEncodedDateWithYMD(int encoded, int year, int month, int day) { if (year < MIN_ENCODED_START_YEAR) { if (encoded == MIN_ENCODED_START) { if (year > MIN_INT32 || month > 1 || day > 1) { return -1;
} return 0;
} else { return 1;
}
} elseif (year > MAX_ENCODED_START_YEAR) { return -1;
} else { int tmp = encodeDate(year, month, day); if (encoded < tmp) { return -1;
} elseif (encoded == tmp) { return 0;
} else { return 1;
}
}
}
if (isSet(startDates[eraIdx])) { if (hasEnd) { // This implementation assumes either start or end is available, not both. // For now, just ignore the end rule.
}
} else { if (hasEnd) { if (eraIdx != 0) { // This implementation does not support end only rule for eras other than // the first one.
status = U_INVALID_FORMAT_ERROR; return nullptr;
}
U_ASSERT(eraIdx == 0);
startDates[eraIdx] = MIN_ENCODED_START;
} else {
status = U_INVALID_FORMAT_ERROR; return nullptr;
}
}
if (hasName) { if (eraIdx >= firstTentativeIdx) {
status = U_INVALID_FORMAT_ERROR; return nullptr;
}
} else { if (eraIdx < firstTentativeIdx) {
firstTentativeIdx = eraIdx;
}
}
}
EraRules *result; if (firstTentativeIdx < MAX_INT32 && !includeTentativeEra) {
result = new EraRules(startDates, firstTentativeIdx);
} else {
result = new EraRules(startDates, numEras);
}
if (result == nullptr) {
status = U_MEMORY_ALLOCATION_ERROR;
} return result;
}
if (month < 1 || month > 12 || day < 1 || day > 31) {
status = U_ILLEGAL_ARGUMENT_ERROR; return -1;
}
int32_t high = numEras; // last index + 1
int32_t low;
// Short circuit for recent years. Most modern computations will // occur in the last few eras. if (compareEncodedDateWithYMD(startDates[getCurrentEraIndex()], year, month, day) <= 0) {
low = getCurrentEraIndex();
} else {
low = 0;
}
// Do binary search while (low < high - 1) { int i = (low + high) / 2; if (compareEncodedDateWithYMD(startDates[i], year, month, day) <= 0) {
low = i;
} else {
high = i;
}
} return low;
}
void EraRules::initCurrentEra() { // Compute local wall time in millis using ICU's default time zone.
UErrorCode ec = U_ZERO_ERROR;
UDate localMillis = ucal_getNow();
int32_t rawOffset, dstOffset;
TimeZone* zone = TimeZone::createDefault(); // If we failed to create the default time zone, we are in a bad state and don't // really have many options. Carry on using UTC millis as a fallback. if (zone != nullptr) {
zone->getOffset(localMillis, false, rawOffset, dstOffset, ec); delete zone;
localMillis += (rawOffset + dstOffset);
}
int year, month0, dom, dow, doy, mid;
Grego::timeToFields(localMillis, year, month0, dom, dow, doy, mid, ec); if (U_FAILURE(ec)) return; int currentEncodedDate = encodeDate(year, month0 + 1 /* changes to 1-base */, dom); int eraIdx = numEras - 1; while (eraIdx > 0) { if (currentEncodedDate >= startDates[eraIdx]) { break;
}
eraIdx--;
} // Note: current era could be before the first era. // In this case, this implementation returns the first era index (0).
currentEra = eraIdx;
}
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.