-/*
- Mapping H<->h and K<->k is not correct in all locales; Japanese 12 hour, for example, uses H<->k
- This gets an approximately correct replacement character for the locale and forcing direction in question
- <rdar://problem/14062096> [iCal] Incorrect time format is used for current time indicator line
- */
-static UChar __CFDateFormatterForcedHourCharacterForLocale(CFLocaleRef locale, Boolean doForce24, Boolean doForce12, Boolean *succeeded) {
- if (doForce24) doForce12 = false; // if both are set, Force24 wins, period
- if (!locale || (!doForce24 && !doForce12)) {
- *succeeded = false;
- return '\0';
- }
- CFStringRef localeName = locale ? CFLocaleGetIdentifier(locale) : CFSTR("");
- char buffer[BUFFER_SIZE] = {0};
- const char *cstr = CFStringGetCStringPtr(localeName, kCFStringEncodingASCII);
- if (NULL == cstr) {
- if (CFStringGetCString(localeName, buffer, BUFFER_SIZE, kCFStringEncodingASCII)) cstr = buffer;
- }
-
- __block UChar hourPatternChar = '\0';
-
- useTemplatePatternGenerator(cstr, ^(UDateTimePatternGenerator *ptg) {
- UChar hourPattern[256] = {0};
- int32_t patternLen = -1;
- if (ptg) {
- UErrorCode errorCode = U_ZERO_ERROR;
- patternLen = __cficu_udatpg_getBestPattern(ptg, (const UChar *)(doForce12 ? "h" : "H"), 1, hourPattern, sizeof(hourPattern) / sizeof(UChar), &errorCode);
- if (errorCode != U_ZERO_ERROR) {
- memset(hourPattern, 0, sizeof(hourPattern)); //make sure there's nothing there if we failed
- patternLen = -1;
- }
- }
-
- /*
- Blindly replacing HHHH with four copies of the entire udatpg_getBestPattern result is not going to work. Search for the first [hHkK] in the result and use just that
- */
- if (patternLen > 0) {
- for (CFIndex idx = 0; hourPattern[idx] != '\0' && idx < patternLen && idx < sizeof(hourPattern) / sizeof(UChar); idx++) {
- if (hourPattern[idx] == 'k' || hourPattern[idx] == 'K' || hourPattern[idx] == 'h' || hourPattern[idx] == 'H') {
- hourPatternChar = hourPattern[idx];
- break;
- }
- }
- }
- });
-
- *succeeded = hourPatternChar != '\0';
- return hourPatternChar;
-}
-
-#define FORCE_CHAR(replacement, oldType, newType) do { \
- if (!isInQuote) {\
- if (-1 == firstHour) {\
- firstHour = CFStringGetLength(outString);\
- }\
- had##oldType##Hour = true;\
- if (doForce##newType) {\
- ch = useSpecialHourChar ? hourPatternChar : replacement;\
- }\
- }\
-}while(0)
-#define FORCE_CHAR_12(replacement) FORCE_CHAR(replacement, 12, 24)
-#define FORCE_CHAR_24(replacement) FORCE_CHAR(replacement, 24, 12)
-