// Rule set number must not be zero. (0 is used to indicate "not found" by hashmap.) // Currently ICU data conveniently starts numbering rule sets from 1. if (setNum == 0) {
errorCode = U_INVALID_FORMAT_ERROR; return -1;
} else { return setNum;
}
}
if (type == CUTOFF_TYPE_UNKNOWN) {
errorCode = U_INVALID_FORMAT_ERROR; return;
}
int32_t hour = parseHour(hour_str, errorCode); if (U_FAILURE(errorCode)) { return; }
cutoffs[hour] |= 1 << type;
}
// Translate the cutoffs[] array to day period rules. void setDayPeriodForHoursFromCutoffs(UErrorCode &errorCode) {
DayPeriodRules &rule = data->rules[ruleSetNum];
for (int32_t startHour = 0; startHour <= 24; ++startHour) { // AT cutoffs must be either midnight or noon. if (cutoffs[startHour] & (1 << CUTOFF_TYPE_AT)) { if (startHour == 0 && period == DayPeriodRules::DAYPERIOD_MIDNIGHT) {
rule.fHasMidnight = true;
} elseif (startHour == 12 && period == DayPeriodRules::DAYPERIOD_NOON) {
rule.fHasNoon = true;
} else {
errorCode = U_INVALID_FORMAT_ERROR; // Bad data. return;
}
}
// FROM/AFTER and BEFORE must come in a pair. if (cutoffs[startHour] & (1 << CUTOFF_TYPE_FROM) ||
cutoffs[startHour] & (1 << CUTOFF_TYPE_AFTER)) { for (int32_t hour = startHour + 1;; ++hour) { if (hour == startHour) { // We've gone around the array once and can't find a BEFORE.
errorCode = U_INVALID_FORMAT_ERROR; return;
} if (hour == 25) { hour = 0; } if (cutoffs[hour] & (1 << CUTOFF_TYPE_BEFORE)) {
rule.add(startHour, hour, period); break;
}
}
}
}
}
// Gets the numerical value of the hour from the Unicode string. static int32_t parseHour(const UnicodeString &time, UErrorCode &errorCode) { if (U_FAILURE(errorCode)) { return 0;
}
int32_t hourLimit = time.length() - 3; // `time` must look like "x:00" or "xx:00". // If length is wrong or `time` doesn't end with ":00", error out. if ((hourLimit != 1 && hourLimit != 2) ||
time[hourLimit] != 0x3A || time[hourLimit + 1] != 0x30 ||
time[hourLimit + 2] != 0x30) {
errorCode = U_INVALID_FORMAT_ERROR; return 0;
}
// If `time` doesn't begin with a number in [0, 24], error out. // Note: "24:00" is possible in "before 24:00".
int32_t hour = time[0] - 0x30; if (hour < 0 || 9 < hour) {
errorCode = U_INVALID_FORMAT_ERROR; return 0;
} if (hourLimit == 2) {
int32_t hourDigit2 = time[1] - 0x30; if (hourDigit2 < 0 || 9 < hourDigit2) {
errorCode = U_INVALID_FORMAT_ERROR; return 0;
}
hour = hour * 10 + hourDigit2; if (hour > 24) {
errorCode = U_INVALID_FORMAT_ERROR; return 0;
}
}
void U_CALLCONV DayPeriodRules::load(UErrorCode &errorCode) { if (U_FAILURE(errorCode)) { return;
}
data = new DayPeriodRulesData();
data->localeToRuleSetNumMap = uhash_open(uhash_hashChars, uhash_compareChars, nullptr, &errorCode);
LocalUResourceBundlePointer rb_dayPeriods(ures_openDirect(nullptr, "dayPeriods", &errorCode));
// Get the largest rule set number (so we allocate enough objects).
DayPeriodRulesCountSink countSink;
ures_getAllItemsWithFallback(rb_dayPeriods.getAlias(), "rules", countSink, errorCode);
// If the entire day period rules data doesn't conform to spec (even if the part we want // does), return nullptr. if(U_FAILURE(errorCode)) { return nullptr; }
int32_t ruleSetNum = 0; // NB there is no rule set 0 and 0 is returned upon lookup failure. while (*name != '\0') {
ruleSetNum = uhash_geti(data->localeToRuleSetNumMap, name); if (ruleSetNum == 0) {
CharString parent = ulocimp_getParent(name, errorCode); if (parent.isEmpty()) { // Saves a lookup in the hash table. break;
}
parent.extract(name, UPRV_LENGTHOF(name), errorCode);
} else { break;
}
}
if (ruleSetNum <= 0 || data->rules[ruleSetNum].getDayPeriodForHour(0) == DAYPERIOD_UNKNOWN) { // If day period for hour 0 is UNKNOWN then day period for all hours are UNKNOWN. // Data doesn't exist even with fallback. return nullptr;
} else { return &data->rules[ruleSetNum];
}
}
DayPeriodRules::DayPeriodRules() : fHasMidnight(false), fHasNoon(false) { for (int32_t i = 0; i < 24; ++i) {
fDayPeriodForHour[i] = DayPeriodRules::DAYPERIOD_UNKNOWN;
}
}
if (startHour > endHour) { // dayPeriod wraps around midnight. Shift midPoint by 12 hours, in the direction that // lands it in [0, 24).
midPoint += 12; if (midPoint >= 24) {
midPoint -= 24;
}
}
if (dayPeriod == DAYPERIOD_MIDNIGHT) { return 0; } if (dayPeriod == DAYPERIOD_NOON) { return 12; }
if (fDayPeriodForHour[0] == dayPeriod && fDayPeriodForHour[23] == dayPeriod) { // dayPeriod wraps around midnight. Start hour is later than end hour. for (int32_t i = 22; i >= 1; --i) { if (fDayPeriodForHour[i] != dayPeriod) { return (i + 1);
}
}
} else { for (int32_t i = 0; i <= 23; ++i) { if (fDayPeriodForHour[i] == dayPeriod) { return i;
}
}
}
// dayPeriod doesn't exist in rule set; set error and exit.
errorCode = U_ILLEGAL_ARGUMENT_ERROR; return -1;
}
if (dayPeriod == DAYPERIOD_MIDNIGHT) { return 0; } if (dayPeriod == DAYPERIOD_NOON) { return 12; }
if (fDayPeriodForHour[0] == dayPeriod && fDayPeriodForHour[23] == dayPeriod) { // dayPeriod wraps around midnight. End hour is before start hour. for (int32_t i = 1; i <= 22; ++i) { if (fDayPeriodForHour[i] != dayPeriod) { // i o'clock is when a new period starts, therefore when the old period ends. return i;
}
}
} else { for (int32_t i = 23; i >= 0; --i) { if (fDayPeriodForHour[i] == dayPeriod) { return (i + 1);
}
}
}
// dayPeriod doesn't exist in rule set; set error and exit.
errorCode = U_ILLEGAL_ARGUMENT_ERROR; return -1;
}
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.