X-Git-Url: https://git.saurik.com/apple/icu.git/blobdiff_plain/57a6839dcb3bba09e8228b822b290604668416fe..0f5d89e82340278ed3d7d50029f37cab2c41a57e:/icuSources/i18n/windtfmt.cpp diff --git a/icuSources/i18n/windtfmt.cpp b/icuSources/i18n/windtfmt.cpp index 1378491a..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-2013, International Business Machines +* Copyright (C) 2005-2016, International Business Machines * Corporation and others. All Rights Reserved. ******************************************************************************** * @@ -11,7 +13,7 @@ #include "unicode/utypes.h" -#if U_PLATFORM_HAS_WIN32_API +#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" @@ -33,7 +35,9 @@ #include "windtfmt.h" #include "wintzimpl.h" +#ifndef WIN32_LEAN_AND_MEAN # define WIN32_LEAN_AND_MEAN +#endif # define VC_EXTRALEAN # define NOUSER # define NOSERVICE @@ -45,8 +49,6 @@ 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)) @@ -70,7 +72,7 @@ UnicodeString* Win32DateFormat::getTimeDateFormat(const Calendar *cal, const Loc if (U_FAILURE(status)) { static const UChar defaultPattern[] = {0x007B, 0x0031, 0x007D, 0x0020, 0x007B, 0x0030, 0x007D, 0x0000}; // "{1} {0}" - return new UnicodeString(defaultPattern, ARRAY_SIZE(defaultPattern)); + return new UnicodeString(defaultPattern, UPRV_LENGTHOF(defaultPattern)); } int32_t resStrLen = 0; @@ -92,12 +94,83 @@ UnicodeString* Win32DateFormat::getTimeDateFormat(const Calendar *cal, const Loc 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)); @@ -115,6 +188,7 @@ Win32DateFormat::~Win32DateFormat() // delete fCalendar; uprv_free(fTZI); delete fDateTimeMsg; + delete fWindowsLocaleName; } Win32DateFormat &Win32DateFormat::operator=(const Win32DateFormat &other) @@ -124,17 +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->fLocale = other.fLocale; - this->fLCID = other.fLCID; // 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; } @@ -144,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; @@ -167,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); } - 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) { @@ -192,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()); } @@ -235,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); @@ -262,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); @@ -300,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; } } @@ -318,5 +404,5 @@ U_NAMESPACE_END #endif /* #if !UCONFIG_NO_FORMATTING */ -#endif // U_PLATFORM_HAS_WIN32_API +#endif // U_PLATFORM_USES_ONLY_WIN32_API