X-Git-Url: https://git.saurik.com/apple/icu.git/blobdiff_plain/73c04bcfe1096173b00431f0cdc742894b15eef0..0f5d89e82340278ed3d7d50029f37cab2c41a57e:/icuSources/i18n/windtfmt.cpp diff --git a/icuSources/i18n/windtfmt.cpp b/icuSources/i18n/windtfmt.cpp index 19766b1f..6a3779a2 100644 --- a/icuSources/i18n/windtfmt.cpp +++ b/icuSources/i18n/windtfmt.cpp @@ -1,6 +1,8 @@ +// © 2016 and later: Unicode, Inc. and others. +// License & terms of use: http://www.unicode.org/copyright.html /* ******************************************************************************** -* Copyright (C) 2005-2006, International Business Machines +* Copyright (C) 2005-2016, International Business Machines * Corporation and others. All Rights Reserved. ******************************************************************************** * @@ -11,7 +13,7 @@ #include "unicode/utypes.h" -#ifdef U_WINDOWS +#if U_PLATFORM_USES_ONLY_WIN32_API #if !UCONFIG_NO_FORMATTING @@ -19,7 +21,7 @@ #include "unicode/format.h" #include "unicode/fmtable.h" #include "unicode/datefmt.h" -#include "unicode/msgfmt.h" +#include "unicode/simpleformatter.h" #include "unicode/calendar.h" #include "unicode/gregocal.h" #include "unicode/locid.h" @@ -31,9 +33,11 @@ #include "cmemory.h" #include "uresimp.h" #include "windtfmt.h" -#include "wintz.h" +#include "wintzimpl.h" +#ifndef WIN32_LEAN_AND_MEAN # define WIN32_LEAN_AND_MEAN +#endif # define VC_EXTRALEAN # define NOUSER # define NOSERVICE @@ -45,14 +49,12 @@ U_NAMESPACE_BEGIN UOBJECT_DEFINE_RTTI_IMPLEMENTATION(Win32DateFormat) -#define ARRAY_SIZE(array) (sizeof array / sizeof array[0]) - #define NEW_ARRAY(type,count) (type *) uprv_malloc((count) * sizeof(type)) #define DELETE_ARRAY(array) uprv_free((void *) (array)) #define STACK_BUFFER_SIZE 64 -UnicodeString *getTimeDateFormat(const Calendar *cal, const Locale *locale, UErrorCode &status) +UnicodeString* Win32DateFormat::getTimeDateFormat(const Calendar *cal, const Locale *locale, UErrorCode &status) const { UnicodeString *result = NULL; const char *type = cal->getType(); @@ -69,12 +71,18 @@ UnicodeString *getTimeDateFormat(const Calendar *cal, const Locale *locale, UErr } if (U_FAILURE(status)) { - UChar defaultPattern[] = {0x007B, 0x0031, 0x007D, 0x0020, 0x007B, 0x0030, 0x007D, 0x0000}; // "{1} {0}" - return new UnicodeString(defaultPattern, ARRAY_SIZE(defaultPattern)); + static const UChar defaultPattern[] = {0x007B, 0x0031, 0x007D, 0x0020, 0x007B, 0x0030, 0x007D, 0x0000}; // "{1} {0}" + return new UnicodeString(defaultPattern, UPRV_LENGTHOF(defaultPattern)); } int32_t resStrLen = 0; - const UChar *resStr = ures_getStringByIndex(patBundle, (int32_t)DateFormat::kDateTime, &resStrLen, &status); + int32_t glueIndex = DateFormat::kDateTime; + int32_t patSize = ures_getSize(patBundle); + if (patSize >= (DateFormat::kDateTimeOffset + DateFormat::kShort + 1)) { + // Get proper date time format + glueIndex = (int32_t)(DateFormat::kDateTimeOffset + (fDateStyle - DateFormat::kDateOffset)); + } + const UChar *resStr = ures_getStringByIndex(patBundle, glueIndex, &resStrLen, &status); result = new UnicodeString(TRUE, resStr, resStrLen); @@ -86,12 +94,83 @@ UnicodeString *getTimeDateFormat(const Calendar *cal, const Locale *locale, UErr return result; } +// TODO: This is copied in both winnmfmt.cpp and windtfmt.cpp, but really should +// be factored out into a common helper for both. +static UErrorCode GetEquivalentWindowsLocaleName(const Locale& locale, UnicodeString** buffer) +{ + UErrorCode status = U_ZERO_ERROR; + char asciiBCP47Tag[LOCALE_NAME_MAX_LENGTH] = {}; + + // Convert from names like "en_CA" and "de_DE@collation=phonebook" to "en-CA" and "de-DE-u-co-phonebk". + (void)uloc_toLanguageTag(locale.getName(), asciiBCP47Tag, UPRV_LENGTHOF(asciiBCP47Tag), FALSE, &status); + + if (U_SUCCESS(status)) + { + // Need it to be UTF-16, not 8-bit + // TODO: This seems like a good thing for a helper + wchar_t bcp47Tag[LOCALE_NAME_MAX_LENGTH] = {}; + int32_t i; + for (i = 0; i < UPRV_LENGTHOF(bcp47Tag); i++) + { + if (asciiBCP47Tag[i] == '\0') + { + break; + } + else + { + // normally just copy the character + bcp47Tag[i] = static_cast(asciiBCP47Tag[i]); + } + } + + // Ensure it's null terminated + if (i < (UPRV_LENGTHOF(bcp47Tag) - 1)) + { + bcp47Tag[i] = L'\0'; + } + else + { + // Ran out of room. + bcp47Tag[UPRV_LENGTHOF(bcp47Tag) - 1] = L'\0'; + } + + + wchar_t windowsLocaleName[LOCALE_NAME_MAX_LENGTH] = {}; + + // Note: On Windows versions below 10, there is no support for locale name aliases. + // This means that it will fail for locales where ICU has a completely different + // name (like ku vs ckb), and it will also not work for alternate sort locale + // names like "de-DE-u-co-phonebk". + + // TODO: We could add some sort of exception table for cases like ku vs ckb. + + int length = ResolveLocaleName(bcp47Tag, windowsLocaleName, UPRV_LENGTHOF(windowsLocaleName)); + + if (length > 0) + { + *buffer = new UnicodeString(windowsLocaleName); + } + else + { + status = U_UNSUPPORTED_ERROR; + } + } + return status; +} + // TODO: Range-check timeStyle, dateStyle Win32DateFormat::Win32DateFormat(DateFormat::EStyle timeStyle, DateFormat::EStyle dateStyle, const Locale &locale, UErrorCode &status) - : DateFormat(), fDateTimeMsg(NULL), fTimeStyle(timeStyle), fDateStyle(dateStyle), fLocale(&locale), fZoneID() + : DateFormat(), fDateTimeMsg(NULL), fTimeStyle(timeStyle), fDateStyle(dateStyle), fLocale(locale), fZoneID(), fWindowsLocaleName(nullptr) { if (U_SUCCESS(status)) { - fLCID = locale.getLCID(); + GetEquivalentWindowsLocaleName(locale, &fWindowsLocaleName); + // Note: In the previous code, it would look up the LCID for the locale, and if + // the locale was not recognized then it would get an LCID of 0, which is a + // synonym for LOCALE_USER_DEFAULT on Windows. + // If the above method fails, then fWindowsLocaleName will remain as nullptr, and + // then we will pass nullptr to API GetLocaleInfoEx, which is the same as passing + // LOCALE_USER_DEFAULT. + fTZI = NEW_ARRAY(TIME_ZONE_INFORMATION, 1); uprv_memset(fTZI, 0, sizeof(TIME_ZONE_INFORMATION)); adoptCalendar(Calendar::createInstance(locale, status)); @@ -109,6 +188,7 @@ Win32DateFormat::~Win32DateFormat() // delete fCalendar; uprv_free(fTZI); delete fDateTimeMsg; + delete fWindowsLocaleName; } Win32DateFormat &Win32DateFormat::operator=(const Win32DateFormat &other) @@ -118,16 +198,18 @@ Win32DateFormat &Win32DateFormat::operator=(const Win32DateFormat &other) // delete fCalendar; - this->fDateTimeMsg = other.fDateTimeMsg; + this->fDateTimeMsg = other.fDateTimeMsg == NULL ? NULL : new UnicodeString(*other.fDateTimeMsg); this->fTimeStyle = other.fTimeStyle; this->fDateStyle = other.fDateStyle; - this->fLCID = other.fLCID; + this->fLocale = other.fLocale; // this->fCalendar = other.fCalendar->clone(); this->fZoneID = other.fZoneID; this->fTZI = NEW_ARRAY(TIME_ZONE_INFORMATION, 1); *this->fTZI = *other.fTZI; + this->fWindowsLocaleName = other.fWindowsLocaleName == NULL ? NULL : new UnicodeString(*other.fWindowsLocaleName); + return *this; } @@ -137,7 +219,7 @@ Format *Win32DateFormat::clone(void) const } // TODO: Is just ignoring pos the right thing? -UnicodeString &Win32DateFormat::format(Calendar &cal, UnicodeString &appendTo, FieldPosition &pos) const +UnicodeString &Win32DateFormat::format(Calendar &cal, UnicodeString &appendTo, FieldPosition & /* pos */) const { FILETIME ft; SYSTEMTIME st_gmt; @@ -160,22 +242,18 @@ UnicodeString &Win32DateFormat::format(Calendar &cal, UnicodeString &appendTo, F if (fDateStyle != DateFormat::kNone && fTimeStyle != DateFormat::kNone) { - UnicodeString *date = new UnicodeString(); - UnicodeString *time = new UnicodeString(); + UnicodeString date; + UnicodeString time; UnicodeString *pattern = fDateTimeMsg; - Formattable timeDateArray[2]; - formatDate(&st_local, *date); - formatTime(&st_local, *time); - - timeDateArray[0].adoptString(time); - timeDateArray[1].adoptString(date); + formatDate(&st_local, date); + formatTime(&st_local, time); if (strcmp(fCalendar->getType(), cal.getType()) != 0) { - pattern = getTimeDateFormat(&cal, fLocale, status); + pattern = getTimeDateFormat(&cal, &fLocale, status); } - MessageFormat::format(*pattern, timeDateArray, 2, appendTo, status); + SimpleFormatter(*pattern, 2, 2, status).format(time, date, appendTo, status); } else if (fDateStyle != DateFormat::kNone) { formatDate(&st_local, appendTo); } else if (fTimeStyle != DateFormat::kNone) { @@ -185,7 +263,7 @@ UnicodeString &Win32DateFormat::format(Calendar &cal, UnicodeString &appendTo, F return appendTo; } -void Win32DateFormat::parse(const UnicodeString& text, Calendar& cal, ParsePosition& pos) const +void Win32DateFormat::parse(const UnicodeString& /* text */, Calendar& /* cal */, ParsePosition& pos) const { pos.setErrorIndex(pos.getIndex()); } @@ -197,7 +275,7 @@ void Win32DateFormat::adoptCalendar(Calendar *newCalendar) if (fDateStyle != DateFormat::kNone && fTimeStyle != DateFormat::kNone) { delete fDateTimeMsg; - fDateTimeMsg = getTimeDateFormat(newCalendar, fLocale, status); + fDateTimeMsg = getTimeDateFormat(newCalendar, &fLocale, status); } } @@ -228,22 +306,29 @@ static const DWORD dfFlags[] = {DATE_LONGDATE, DATE_LONGDATE, DATE_SHORTDATE, DA void Win32DateFormat::formatDate(const SYSTEMTIME *st, UnicodeString &appendTo) const { - int result; - UChar stackBuffer[STACK_BUFFER_SIZE]; - UChar *buffer = stackBuffer; + int result=0; + wchar_t stackBuffer[STACK_BUFFER_SIZE]; + wchar_t *buffer = stackBuffer; + const wchar_t *localeName = nullptr; + + if (fWindowsLocaleName != nullptr) + { + localeName = reinterpret_cast(toOldUCharPtr(fWindowsLocaleName->getTerminatedBuffer())); + } - result = GetDateFormatW(fLCID, dfFlags[fDateStyle - kDateOffset], st, NULL, buffer, STACK_BUFFER_SIZE); + result = GetDateFormatEx(localeName, dfFlags[fDateStyle - kDateOffset], st, NULL, buffer, STACK_BUFFER_SIZE, NULL); if (result == 0) { if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) { - int newLength = GetDateFormatW(fLCID, dfFlags[fDateStyle - kDateOffset], st, NULL, NULL, 0); + int newLength = GetDateFormatEx(localeName, dfFlags[fDateStyle - kDateOffset], st, NULL, NULL, 0, NULL); + + buffer = NEW_ARRAY(wchar_t, newLength); - buffer = NEW_ARRAY(UChar, newLength); - GetDateFormatW(fLCID, dfFlags[fDateStyle - kDateOffset], st, NULL, buffer, newLength); + GetDateFormatEx(localeName, dfFlags[fDateStyle - kDateOffset], st, NULL, buffer, newLength, NULL); } } - appendTo.append(buffer, (int32_t) wcslen(buffer)); + appendTo.append((const UChar *)buffer, (int32_t) wcslen(buffer)); if (buffer != stackBuffer) { DELETE_ARRAY(buffer); @@ -255,21 +340,28 @@ static const DWORD tfFlags[] = {0, 0, 0, TIME_NOSECONDS}; void Win32DateFormat::formatTime(const SYSTEMTIME *st, UnicodeString &appendTo) const { int result; - UChar stackBuffer[STACK_BUFFER_SIZE]; - UChar *buffer = stackBuffer; + wchar_t stackBuffer[STACK_BUFFER_SIZE]; + wchar_t *buffer = stackBuffer; + const wchar_t *localeName = nullptr; - result = GetTimeFormatW(fLCID, tfFlags[fTimeStyle], st, NULL, buffer, STACK_BUFFER_SIZE); + if (fWindowsLocaleName != nullptr) + { + localeName = reinterpret_cast(toOldUCharPtr(fWindowsLocaleName->getTerminatedBuffer())); + } + + result = GetTimeFormatEx(localeName, tfFlags[fTimeStyle], st, NULL, buffer, STACK_BUFFER_SIZE); if (result == 0) { if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) { - int newLength = GetTimeFormatW(fLCID, tfFlags[fTimeStyle], st, NULL, NULL, 0); + int newLength = GetTimeFormatEx(localeName, tfFlags[fTimeStyle], st, NULL, NULL, 0); + + buffer = NEW_ARRAY(wchar_t, newLength); - buffer = NEW_ARRAY(UChar, newLength); - GetDateFormatW(fLCID, tfFlags[fTimeStyle], st, NULL, buffer, newLength); + GetTimeFormatEx(localeName, tfFlags[fTimeStyle], st, NULL, buffer, newLength); } } - appendTo.append(buffer, (int32_t) wcslen(buffer)); + appendTo.append((const UChar *)buffer, (int32_t) wcslen(buffer)); if (buffer != stackBuffer) { DELETE_ARRAY(buffer); @@ -293,7 +385,8 @@ UnicodeString Win32DateFormat::setTimeZoneInfo(TIME_ZONE_INFORMATION *tzi, const for (int z = 0; z < ec; z += 1) { UnicodeString equiv = TimeZone::getEquivalentID(icuid, z); - if (found = uprv_getWindowsTimeZoneInfo(tzi, equiv.getBuffer(), equiv.length())) { + found = uprv_getWindowsTimeZoneInfo(tzi, equiv.getBuffer(), equiv.length()); + if (found) { break; } } @@ -311,5 +404,5 @@ U_NAMESPACE_END #endif /* #if !UCONFIG_NO_FORMATTING */ -#endif // #ifdef U_WINDOWS +#endif // U_PLATFORM_USES_ONLY_WIN32_API