/*
*******************************************************************************
-* Copyright (C) 2011-2012, International Business Machines Corporation and
+* Copyright (C) 2011-2013, International Business Machines Corporation and
* others. All Rights Reserved.
*******************************************************************************
*/
return results;
}
+static UMutex gLock = U_MUTEX_INITIALIZER;
+
class TZGNCore : public UMemory {
public:
TZGNCore(const Locale& locale, UErrorCode& status);
private:
Locale fLocale;
- UMTX fLock;
const TimeZoneNames* fTimeZoneNames;
UHashtable* fLocationNamesMap;
UHashtable* fPartialLocationNamesMap;
MessageFormat* fRegionFormat;
- MessageFormat* fFallbackRegionFormat;
MessageFormat* fFallbackFormat;
LocaleDisplayNames* fLocaleDisplayNames;
// ---------------------------------------------------
TZGNCore::TZGNCore(const Locale& locale, UErrorCode& status)
: fLocale(locale),
- fLock(NULL),
fTimeZoneNames(NULL),
fLocationNamesMap(NULL),
fPartialLocationNamesMap(NULL),
fRegionFormat(NULL),
- fFallbackRegionFormat(NULL),
fFallbackFormat(NULL),
fLocaleDisplayNames(NULL),
fStringPool(status),
TZGNCore::~TZGNCore() {
cleanup();
- umtx_destroy(&fLock);
}
void
if (fRegionFormat == NULL) {
status = U_MEMORY_ALLOCATION_ERROR;
}
- fFallbackRegionFormat = new MessageFormat(frpat, status);
- if (fFallbackRegionFormat == NULL) {
- status = U_MEMORY_ALLOCATION_ERROR;
- }
fFallbackFormat = new MessageFormat(fpat, status);
if (fFallbackFormat == NULL) {
status = U_MEMORY_ALLOCATION_ERROR;
if (fRegionFormat != NULL) {
delete fRegionFormat;
}
- if (fFallbackRegionFormat != NULL) {
- delete fFallbackRegionFormat;
- }
if (fFallbackFormat != NULL) {
delete fFallbackFormat;
}
const UChar *locname = NULL;
TZGNCore *nonConstThis = const_cast<TZGNCore *>(this);
- umtx_lock(&nonConstThis->fLock);
+ umtx_lock(&gLock);
{
locname = nonConstThis->getGenericLocationName(tzCanonicalID);
}
- umtx_unlock(&nonConstThis->fLock);
+ umtx_unlock(&gLock);
if (locname == NULL) {
name.setToBogus();
// Construct location name
UnicodeString name;
- UBool isSingleCountry = FALSE;
UnicodeString usCountryCode;
- ZoneMeta::getSingleCountry(tzCanonicalID, usCountryCode);
- if (!usCountryCode.isEmpty()) {
- isSingleCountry = TRUE;
- } else {
- ZoneMeta::getCanonicalCountry(tzCanonicalID, usCountryCode);
- }
+ UBool isPrimary = FALSE;
+
+ ZoneMeta::getCanonicalCountry(tzCanonicalID, usCountryCode, &isPrimary);
if (!usCountryCode.isEmpty()) {
- char countryCode[ULOC_COUNTRY_CAPACITY];
- U_ASSERT(usCountryCode.length() < ULOC_COUNTRY_CAPACITY);
- int32_t ccLen = usCountryCode.extract(0, usCountryCode.length(), countryCode, sizeof(countryCode), US_INV);
- countryCode[ccLen] = 0;
+ FieldPosition fpos;
- UnicodeString country;
- fLocaleDisplayNames->regionDisplayName(countryCode, country);
+ if (isPrimary) {
+ // If this is the primary zone in the country, use the country name.
+ char countryCode[ULOC_COUNTRY_CAPACITY];
+ U_ASSERT(usCountryCode.length() < ULOC_COUNTRY_CAPACITY);
+ int32_t ccLen = usCountryCode.extract(0, usCountryCode.length(), countryCode, sizeof(countryCode), US_INV);
+ countryCode[ccLen] = 0;
+
+ UnicodeString country;
+ fLocaleDisplayNames->regionDisplayName(countryCode, country);
- // Format
- FieldPosition fpos;
- if (isSingleCountry) {
- // If the zone is only one zone in the country, do not add city
Formattable param[] = {
Formattable(country)
};
+
fRegionFormat->format(param, 1, name, fpos, status);
} else {
+ // If this is not the primary zone in the country,
+ // use the exemplar city name.
+
// getExemplarLocationName should retur non-empty string
// if the time zone is associated with a region
+
UnicodeString city;
fTimeZoneNames->getExemplarLocationName(tzCanonicalID, city);
- Formattable params[] = {
+ Formattable param[] = {
Formattable(city),
- Formattable(country)
};
- fFallbackRegionFormat->format(params, 2, name, fpos, status);
+
+ fRegionFormat->format(param, 1, name, fpos, status);
}
if (U_FAILURE(status)) {
return NULL;
const UChar *uplname = NULL;
TZGNCore *nonConstThis = const_cast<TZGNCore *>(this);
- umtx_lock(&nonConstThis->fLock);
+ umtx_lock(&gLock);
{
uplname = nonConstThis->getPartialLocationName(tzCanonicalID, mzID, isLong, mzDisplayName);
}
- umtx_unlock(&nonConstThis->fLock);
+ umtx_unlock(&gLock);
if (uplname == NULL) {
name.setToBogus();
TZGNCore *nonConstThis = const_cast<TZGNCore *>(this);
- umtx_lock(&nonConstThis->fLock);
+ umtx_lock(&gLock);
{
fGNamesTrie.search(text, start, (TextTrieMapSearchResultHandler *)&handler, status);
}
- umtx_unlock(&nonConstThis->fLock);
+ umtx_unlock(&gLock);
if (U_FAILURE(status)) {
return NULL;
// All names are not yet loaded into the local trie.
// Load all available names into the trie. This could be very heavy.
- umtx_lock(&nonConstThis->fLock);
+ umtx_lock(&gLock);
{
if (!fGNamesTrieFullyLoaded) {
StringEnumeration *tzIDs = TimeZone::createTimeZoneIDEnumeration(UCAL_ZONE_TYPE_CANONICAL, NULL, NULL, status);
}
}
}
- umtx_unlock(&nonConstThis->fLock);
+ umtx_unlock(&gLock);
if (U_FAILURE(status)) {
return NULL;
}
- umtx_lock(&nonConstThis->fLock);
+ umtx_lock(&gLock);
{
// now try it again
fGNamesTrie.search(text, start, (TextTrieMapSearchResultHandler *)&handler, status);
}
- umtx_unlock(&nonConstThis->fLock);
+ umtx_unlock(&gLock);
results = handler.getMatches(maxLen);
if (results != NULL && maxLen > 0) {
} TZGNCoreRef;
// TZGNCore object cache handling
-static UMTX gTZGNLock = NULL;
+static UMutex gTZGNLock = U_MUTEX_INITIALIZER;
static UHashtable *gTZGNCoreCache = NULL;
static UBool gTZGNCoreCacheInitialized = FALSE;
*/
static UBool U_CALLCONV tzgnCore_cleanup(void)
{
- umtx_destroy(&gTZGNLock);
-
if (gTZGNCoreCache != NULL) {
uhash_close(gTZGNCoreCache);
gTZGNCoreCache = NULL;