michael@0: /* michael@0: ******************************************************************************* michael@0: * Copyright (C) 2007-2013, International Business Machines Corporation and michael@0: * others. All Rights Reserved. michael@0: ******************************************************************************* michael@0: */ michael@0: michael@0: #include "unicode/utypes.h" michael@0: michael@0: #if !UCONFIG_NO_FORMATTING michael@0: michael@0: #include "zonemeta.h" michael@0: michael@0: #include "unicode/timezone.h" michael@0: #include "unicode/ustring.h" michael@0: #include "unicode/putil.h" michael@0: #include "unicode/simpletz.h" michael@0: michael@0: #include "umutex.h" michael@0: #include "uvector.h" michael@0: #include "cmemory.h" michael@0: #include "gregoimp.h" michael@0: #include "cstring.h" michael@0: #include "ucln_in.h" michael@0: #include "uassert.h" michael@0: #include "uresimp.h" michael@0: #include "uhash.h" michael@0: #include "olsontz.h" michael@0: michael@0: static UMutex gZoneMetaLock = U_MUTEX_INITIALIZER; michael@0: michael@0: // CLDR Canonical ID mapping table michael@0: static UHashtable *gCanonicalIDCache = NULL; michael@0: static icu::UInitOnce gCanonicalIDCacheInitOnce = U_INITONCE_INITIALIZER; michael@0: michael@0: // Metazone mapping table michael@0: static UHashtable *gOlsonToMeta = NULL; michael@0: static icu::UInitOnce gOlsonToMetaInitOnce = U_INITONCE_INITIALIZER; michael@0: michael@0: // Available metazone IDs vector and table michael@0: static icu::UVector *gMetaZoneIDs = NULL; michael@0: static UHashtable *gMetaZoneIDTable = NULL; michael@0: static icu::UInitOnce gMetaZoneIDsInitOnce = U_INITONCE_INITIALIZER; michael@0: michael@0: // Country info vectors michael@0: static icu::UVector *gSingleZoneCountries = NULL; michael@0: static icu::UVector *gMultiZonesCountries = NULL; michael@0: static icu::UInitOnce gCountryInfoVectorsInitOnce = U_INITONCE_INITIALIZER; michael@0: michael@0: U_CDECL_BEGIN michael@0: michael@0: /** michael@0: * Cleanup callback func michael@0: */ michael@0: static UBool U_CALLCONV zoneMeta_cleanup(void) michael@0: { michael@0: if (gCanonicalIDCache != NULL) { michael@0: uhash_close(gCanonicalIDCache); michael@0: gCanonicalIDCache = NULL; michael@0: } michael@0: gCanonicalIDCacheInitOnce.reset(); michael@0: michael@0: if (gOlsonToMeta != NULL) { michael@0: uhash_close(gOlsonToMeta); michael@0: gOlsonToMeta = NULL; michael@0: } michael@0: gOlsonToMetaInitOnce.reset(); michael@0: michael@0: if (gMetaZoneIDTable != NULL) { michael@0: uhash_close(gMetaZoneIDTable); michael@0: gMetaZoneIDTable = NULL; michael@0: } michael@0: // delete after closing gMetaZoneIDTable, because it holds michael@0: // value objects held by the hashtable michael@0: delete gMetaZoneIDs; michael@0: gMetaZoneIDs = NULL; michael@0: gMetaZoneIDsInitOnce.reset(); michael@0: michael@0: delete gSingleZoneCountries; michael@0: gSingleZoneCountries = NULL; michael@0: delete gMultiZonesCountries; michael@0: gMultiZonesCountries = NULL; michael@0: gCountryInfoVectorsInitOnce.reset(); michael@0: michael@0: return TRUE; michael@0: } michael@0: michael@0: /** michael@0: * Deleter for UChar* string michael@0: */ michael@0: static void U_CALLCONV michael@0: deleteUCharString(void *obj) { michael@0: UChar *entry = (UChar*)obj; michael@0: uprv_free(entry); michael@0: } michael@0: michael@0: /** michael@0: * Deleter for UVector michael@0: */ michael@0: static void U_CALLCONV michael@0: deleteUVector(void *obj) { michael@0: delete (icu::UVector*) obj; michael@0: } michael@0: michael@0: /** michael@0: * Deleter for OlsonToMetaMappingEntry michael@0: */ michael@0: static void U_CALLCONV michael@0: deleteOlsonToMetaMappingEntry(void *obj) { michael@0: icu::OlsonToMetaMappingEntry *entry = (icu::OlsonToMetaMappingEntry*)obj; michael@0: uprv_free(entry); michael@0: } michael@0: michael@0: U_CDECL_END michael@0: michael@0: U_NAMESPACE_BEGIN michael@0: michael@0: #define ZID_KEY_MAX 128 michael@0: michael@0: static const char gMetaZones[] = "metaZones"; michael@0: static const char gMetazoneInfo[] = "metazoneInfo"; michael@0: static const char gMapTimezonesTag[] = "mapTimezones"; michael@0: michael@0: static const char gKeyTypeData[] = "keyTypeData"; michael@0: static const char gTypeAliasTag[] = "typeAlias"; michael@0: static const char gTypeMapTag[] = "typeMap"; michael@0: static const char gTimezoneTag[] = "timezone"; michael@0: michael@0: static const char gPrimaryZonesTag[] = "primaryZones"; michael@0: michael@0: static const char gWorldTag[] = "001"; michael@0: michael@0: static const UChar gWorld[] = {0x30, 0x30, 0x31, 0x00}; // "001" michael@0: michael@0: static const UChar gDefaultFrom[] = {0x31, 0x39, 0x37, 0x30, 0x2D, 0x30, 0x31, 0x2D, 0x30, 0x31, michael@0: 0x20, 0x30, 0x30, 0x3A, 0x30, 0x30, 0x00}; // "1970-01-01 00:00" michael@0: static const UChar gDefaultTo[] = {0x39, 0x39, 0x39, 0x39, 0x2D, 0x31, 0x32, 0x2D, 0x33, 0x31, michael@0: 0x20, 0x32, 0x33, 0x3A, 0x35, 0x39, 0x00}; // "9999-12-31 23:59" michael@0: michael@0: static const UChar gCustomTzPrefix[] = {0x47, 0x4D, 0x54, 0}; // "GMT" michael@0: michael@0: #define ASCII_DIGIT(c) (((c)>=0x30 && (c)<=0x39) ? (c)-0x30 : -1) michael@0: michael@0: /* michael@0: * Convert a date string used by metazone mappings to UDate. michael@0: * The format used by CLDR metazone mapping is "yyyy-MM-dd HH:mm". michael@0: */ michael@0: static UDate michael@0: parseDate (const UChar *text, UErrorCode &status) { michael@0: if (U_FAILURE(status)) { michael@0: return 0; michael@0: } michael@0: int32_t len = u_strlen(text); michael@0: if (len != 16 && len != 10) { michael@0: // It must be yyyy-MM-dd HH:mm (length 16) or yyyy-MM-dd (length 10) michael@0: status = U_INVALID_FORMAT_ERROR; michael@0: return 0; michael@0: } michael@0: michael@0: int32_t year = 0, month = 0, day = 0, hour = 0, min = 0, n; michael@0: int32_t idx; michael@0: michael@0: // "yyyy" (0 - 3) michael@0: for (idx = 0; idx <= 3 && U_SUCCESS(status); idx++) { michael@0: n = ASCII_DIGIT((int32_t)text[idx]); michael@0: if (n >= 0) { michael@0: year = 10*year + n; michael@0: } else { michael@0: status = U_INVALID_FORMAT_ERROR; michael@0: } michael@0: } michael@0: // "MM" (5 - 6) michael@0: for (idx = 5; idx <= 6 && U_SUCCESS(status); idx++) { michael@0: n = ASCII_DIGIT((int32_t)text[idx]); michael@0: if (n >= 0) { michael@0: month = 10*month + n; michael@0: } else { michael@0: status = U_INVALID_FORMAT_ERROR; michael@0: } michael@0: } michael@0: // "dd" (8 - 9) michael@0: for (idx = 8; idx <= 9 && U_SUCCESS(status); idx++) { michael@0: n = ASCII_DIGIT((int32_t)text[idx]); michael@0: if (n >= 0) { michael@0: day = 10*day + n; michael@0: } else { michael@0: status = U_INVALID_FORMAT_ERROR; michael@0: } michael@0: } michael@0: if (len == 16) { michael@0: // "HH" (11 - 12) michael@0: for (idx = 11; idx <= 12 && U_SUCCESS(status); idx++) { michael@0: n = ASCII_DIGIT((int32_t)text[idx]); michael@0: if (n >= 0) { michael@0: hour = 10*hour + n; michael@0: } else { michael@0: status = U_INVALID_FORMAT_ERROR; michael@0: } michael@0: } michael@0: // "mm" (14 - 15) michael@0: for (idx = 14; idx <= 15 && U_SUCCESS(status); idx++) { michael@0: n = ASCII_DIGIT((int32_t)text[idx]); michael@0: if (n >= 0) { michael@0: min = 10*min + n; michael@0: } else { michael@0: status = U_INVALID_FORMAT_ERROR; michael@0: } michael@0: } michael@0: } michael@0: michael@0: if (U_SUCCESS(status)) { michael@0: UDate date = Grego::fieldsToDay(year, month - 1, day) * U_MILLIS_PER_DAY michael@0: + hour * U_MILLIS_PER_HOUR + min * U_MILLIS_PER_MINUTE; michael@0: return date; michael@0: } michael@0: return 0; michael@0: } michael@0: michael@0: static void U_CALLCONV initCanonicalIDCache(UErrorCode &status) { michael@0: gCanonicalIDCache = uhash_open(uhash_hashUChars, uhash_compareUChars, NULL, &status); michael@0: if (gCanonicalIDCache == NULL) { michael@0: status = U_MEMORY_ALLOCATION_ERROR; michael@0: } michael@0: if (U_FAILURE(status)) { michael@0: gCanonicalIDCache = NULL; michael@0: } michael@0: // No key/value deleters - keys/values are from a resource bundle michael@0: ucln_i18n_registerCleanup(UCLN_I18N_ZONEMETA, zoneMeta_cleanup); michael@0: } michael@0: michael@0: michael@0: const UChar* U_EXPORT2 michael@0: ZoneMeta::getCanonicalCLDRID(const UnicodeString &tzid, UErrorCode& status) { michael@0: if (U_FAILURE(status)) { michael@0: return NULL; michael@0: } michael@0: michael@0: int32_t len = tzid.length(); michael@0: if (len > ZID_KEY_MAX) { michael@0: status = U_ILLEGAL_ARGUMENT_ERROR; michael@0: return NULL; michael@0: } michael@0: michael@0: // Checking the cached results michael@0: umtx_initOnce(gCanonicalIDCacheInitOnce, &initCanonicalIDCache, status); michael@0: if (U_FAILURE(status)) { michael@0: return NULL; michael@0: } michael@0: michael@0: const UChar *canonicalID = NULL; michael@0: michael@0: UErrorCode tmpStatus = U_ZERO_ERROR; michael@0: UChar utzid[ZID_KEY_MAX + 1]; michael@0: tzid.extract(utzid, ZID_KEY_MAX + 1, tmpStatus); michael@0: U_ASSERT(tmpStatus == U_ZERO_ERROR); // we checked the length of tzid already michael@0: michael@0: // Check if it was already cached michael@0: umtx_lock(&gZoneMetaLock); michael@0: { michael@0: canonicalID = (const UChar *)uhash_get(gCanonicalIDCache, utzid); michael@0: } michael@0: umtx_unlock(&gZoneMetaLock); michael@0: michael@0: if (canonicalID != NULL) { michael@0: return canonicalID; michael@0: } michael@0: michael@0: // If not, resolve CLDR canonical ID with resource data michael@0: UBool isInputCanonical = FALSE; michael@0: char id[ZID_KEY_MAX + 1]; michael@0: const UChar* idChars = tzid.getBuffer(); michael@0: michael@0: u_UCharsToChars(idChars,id,len); michael@0: id[len] = (char) 0; // Make sure it is null terminated. michael@0: michael@0: // replace '/' with ':' michael@0: char *p = id; michael@0: while (*p++) { michael@0: if (*p == '/') { michael@0: *p = ':'; michael@0: } michael@0: } michael@0: michael@0: UResourceBundle *top = ures_openDirect(NULL, gKeyTypeData, &tmpStatus); michael@0: UResourceBundle *rb = ures_getByKey(top, gTypeMapTag, NULL, &tmpStatus); michael@0: ures_getByKey(rb, gTimezoneTag, rb, &tmpStatus); michael@0: ures_getByKey(rb, id, rb, &tmpStatus); michael@0: if (U_SUCCESS(tmpStatus)) { michael@0: // type entry (canonical) found michael@0: // the input is the canonical ID. resolve to const UChar* michael@0: canonicalID = TimeZone::findID(tzid); michael@0: isInputCanonical = TRUE; michael@0: } michael@0: michael@0: if (canonicalID == NULL) { michael@0: // If a map element not found, then look for an alias michael@0: tmpStatus = U_ZERO_ERROR; michael@0: ures_getByKey(top, gTypeAliasTag, rb, &tmpStatus); michael@0: ures_getByKey(rb, gTimezoneTag, rb, &tmpStatus); michael@0: const UChar *canonical = ures_getStringByKey(rb,id,NULL,&tmpStatus); michael@0: if (U_SUCCESS(tmpStatus)) { michael@0: // canonical map found michael@0: canonicalID = canonical; michael@0: } michael@0: michael@0: if (canonicalID == NULL) { michael@0: // Dereference the input ID using the tz data michael@0: const UChar *derefer = TimeZone::dereferOlsonLink(tzid); michael@0: if (derefer == NULL) { michael@0: status = U_ILLEGAL_ARGUMENT_ERROR; michael@0: } else { michael@0: len = u_strlen(derefer); michael@0: u_UCharsToChars(derefer,id,len); michael@0: id[len] = (char) 0; // Make sure it is null terminated. michael@0: michael@0: // replace '/' with ':' michael@0: char *p = id; michael@0: while (*p++) { michael@0: if (*p == '/') { michael@0: *p = ':'; michael@0: } michael@0: } michael@0: michael@0: // If a dereference turned something up then look for an alias. michael@0: // rb still points to the alias table, so we don't have to go looking michael@0: // for it. michael@0: tmpStatus = U_ZERO_ERROR; michael@0: canonical = ures_getStringByKey(rb,id,NULL,&tmpStatus); michael@0: if (U_SUCCESS(tmpStatus)) { michael@0: // canonical map for the dereferenced ID found michael@0: canonicalID = canonical; michael@0: } else { michael@0: canonicalID = derefer; michael@0: isInputCanonical = TRUE; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: ures_close(rb); michael@0: ures_close(top); michael@0: michael@0: if (U_SUCCESS(status)) { michael@0: U_ASSERT(canonicalID != NULL); // canocanilD must be non-NULL here michael@0: michael@0: // Put the resolved canonical ID to the cache michael@0: umtx_lock(&gZoneMetaLock); michael@0: { michael@0: const UChar* idInCache = (const UChar *)uhash_get(gCanonicalIDCache, utzid); michael@0: if (idInCache == NULL) { michael@0: const UChar* key = ZoneMeta::findTimeZoneID(tzid); michael@0: U_ASSERT(key != NULL); michael@0: if (key != NULL) { michael@0: idInCache = (const UChar *)uhash_put(gCanonicalIDCache, (void *)key, (void *)canonicalID, &status); michael@0: U_ASSERT(idInCache == NULL); michael@0: } michael@0: } michael@0: if (U_SUCCESS(status) && isInputCanonical) { michael@0: // Also put canonical ID itself into the cache if not exist michael@0: const UChar *canonicalInCache = (const UChar*)uhash_get(gCanonicalIDCache, canonicalID); michael@0: if (canonicalInCache == NULL) { michael@0: canonicalInCache = (const UChar *)uhash_put(gCanonicalIDCache, (void *)canonicalID, (void *)canonicalID, &status); michael@0: U_ASSERT(canonicalInCache == NULL); michael@0: } michael@0: } michael@0: } michael@0: umtx_unlock(&gZoneMetaLock); michael@0: } michael@0: michael@0: return canonicalID; michael@0: } michael@0: michael@0: UnicodeString& U_EXPORT2 michael@0: ZoneMeta::getCanonicalCLDRID(const UnicodeString &tzid, UnicodeString &systemID, UErrorCode& status) { michael@0: const UChar *canonicalID = getCanonicalCLDRID(tzid, status); michael@0: if (U_FAILURE(status) || canonicalID == NULL) { michael@0: systemID.setToBogus(); michael@0: return systemID; michael@0: } michael@0: systemID.setTo(TRUE, canonicalID, -1); michael@0: return systemID; michael@0: } michael@0: michael@0: const UChar* U_EXPORT2 michael@0: ZoneMeta::getCanonicalCLDRID(const TimeZone& tz) { michael@0: if (dynamic_cast(&tz) != NULL) { michael@0: // short cut for OlsonTimeZone michael@0: const OlsonTimeZone *otz = (const OlsonTimeZone*)&tz; michael@0: return otz->getCanonicalID(); michael@0: } michael@0: UErrorCode status = U_ZERO_ERROR; michael@0: UnicodeString tzID; michael@0: return getCanonicalCLDRID(tz.getID(tzID), status); michael@0: } michael@0: michael@0: static void U_CALLCONV countryInfoVectorsInit(UErrorCode &status) { michael@0: // Create empty vectors michael@0: // No deleters for these UVectors, it's a reference to a resource bundle string. michael@0: gSingleZoneCountries = new UVector(NULL, uhash_compareUChars, status); michael@0: if (gSingleZoneCountries == NULL) { michael@0: status = U_MEMORY_ALLOCATION_ERROR; michael@0: } michael@0: gMultiZonesCountries = new UVector(NULL, uhash_compareUChars, status); michael@0: if (gMultiZonesCountries == NULL) { michael@0: status = U_MEMORY_ALLOCATION_ERROR; michael@0: } michael@0: michael@0: if (U_FAILURE(status)) { michael@0: delete gSingleZoneCountries; michael@0: delete gMultiZonesCountries; michael@0: gSingleZoneCountries = NULL; michael@0: gMultiZonesCountries = NULL; michael@0: } michael@0: ucln_i18n_registerCleanup(UCLN_I18N_ZONEMETA, zoneMeta_cleanup); michael@0: } michael@0: michael@0: michael@0: UnicodeString& U_EXPORT2 michael@0: ZoneMeta::getCanonicalCountry(const UnicodeString &tzid, UnicodeString &country, UBool *isPrimary /* = NULL */) { michael@0: if (isPrimary != NULL) { michael@0: *isPrimary = FALSE; michael@0: } michael@0: michael@0: const UChar *region = TimeZone::getRegion(tzid); michael@0: if (region != NULL && u_strcmp(gWorld, region) != 0) { michael@0: country.setTo(region, -1); michael@0: } else { michael@0: country.setToBogus(); michael@0: return country; michael@0: } michael@0: michael@0: if (isPrimary != NULL) { michael@0: char regionBuf[] = {0, 0, 0}; michael@0: michael@0: // Checking the cached results michael@0: UErrorCode status = U_ZERO_ERROR; michael@0: umtx_initOnce(gCountryInfoVectorsInitOnce, &countryInfoVectorsInit, status); michael@0: if (U_FAILURE(status)) { michael@0: return country; michael@0: } michael@0: michael@0: // Check if it was already cached michael@0: UBool cached = FALSE; michael@0: UBool singleZone = FALSE; michael@0: umtx_lock(&gZoneMetaLock); michael@0: { michael@0: singleZone = cached = gSingleZoneCountries->contains((void*)region); michael@0: if (!cached) { michael@0: cached = gMultiZonesCountries->contains((void*)region); michael@0: } michael@0: } michael@0: umtx_unlock(&gZoneMetaLock); michael@0: michael@0: if (!cached) { michael@0: // We need to go through all zones associated with the region. michael@0: // This is relatively heavy operation. michael@0: michael@0: U_ASSERT(u_strlen(region) == 2); michael@0: michael@0: u_UCharsToChars(region, regionBuf, 2); michael@0: michael@0: StringEnumeration *ids = TimeZone::createTimeZoneIDEnumeration(UCAL_ZONE_TYPE_CANONICAL_LOCATION, regionBuf, NULL, status); michael@0: int32_t idsLen = ids->count(status); michael@0: if (U_SUCCESS(status) && idsLen == 1) { michael@0: // only the single zone is available for the region michael@0: singleZone = TRUE; michael@0: } michael@0: delete ids; michael@0: michael@0: // Cache the result michael@0: umtx_lock(&gZoneMetaLock); michael@0: { michael@0: UErrorCode ec = U_ZERO_ERROR; michael@0: if (singleZone) { michael@0: if (!gSingleZoneCountries->contains((void*)region)) { michael@0: gSingleZoneCountries->addElement((void*)region, ec); michael@0: } michael@0: } else { michael@0: if (!gMultiZonesCountries->contains((void*)region)) { michael@0: gMultiZonesCountries->addElement((void*)region, ec); michael@0: } michael@0: } michael@0: } michael@0: umtx_unlock(&gZoneMetaLock); michael@0: } michael@0: michael@0: if (singleZone) { michael@0: *isPrimary = TRUE; michael@0: } else { michael@0: // Note: We may cache the primary zone map in future. michael@0: michael@0: // Even a country has multiple zones, one of them might be michael@0: // dominant and treated as a primary zone michael@0: int32_t idLen = 0; michael@0: if (regionBuf[0] == 0) { michael@0: u_UCharsToChars(region, regionBuf, 2); michael@0: } michael@0: michael@0: UResourceBundle *rb = ures_openDirect(NULL, gMetaZones, &status); michael@0: ures_getByKey(rb, gPrimaryZonesTag, rb, &status); michael@0: const UChar *primaryZone = ures_getStringByKey(rb, regionBuf, &idLen, &status); michael@0: if (U_SUCCESS(status)) { michael@0: if (tzid.compare(primaryZone, idLen) == 0) { michael@0: *isPrimary = TRUE; michael@0: } else { michael@0: // The given ID might not be a canonical ID michael@0: UnicodeString canonicalID; michael@0: TimeZone::getCanonicalID(tzid, canonicalID, status); michael@0: if (U_SUCCESS(status) && canonicalID.compare(primaryZone, idLen) == 0) { michael@0: *isPrimary = TRUE; michael@0: } michael@0: } michael@0: } michael@0: ures_close(rb); michael@0: } michael@0: } michael@0: michael@0: return country; michael@0: } michael@0: michael@0: UnicodeString& U_EXPORT2 michael@0: ZoneMeta::getMetazoneID(const UnicodeString &tzid, UDate date, UnicodeString &result) { michael@0: UBool isSet = FALSE; michael@0: const UVector *mappings = getMetazoneMappings(tzid); michael@0: if (mappings != NULL) { michael@0: for (int32_t i = 0; i < mappings->size(); i++) { michael@0: OlsonToMetaMappingEntry *mzm = (OlsonToMetaMappingEntry*)mappings->elementAt(i); michael@0: if (mzm->from <= date && mzm->to > date) { michael@0: result.setTo(mzm->mzid, -1); michael@0: isSet = TRUE; michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: if (!isSet) { michael@0: result.setToBogus(); michael@0: } michael@0: return result; michael@0: } michael@0: michael@0: static void U_CALLCONV olsonToMetaInit(UErrorCode &status) { michael@0: U_ASSERT(gOlsonToMeta == NULL); michael@0: ucln_i18n_registerCleanup(UCLN_I18N_ZONEMETA, zoneMeta_cleanup); michael@0: gOlsonToMeta = uhash_open(uhash_hashUChars, uhash_compareUChars, NULL, &status); michael@0: if (U_FAILURE(status)) { michael@0: gOlsonToMeta = NULL; michael@0: } else { michael@0: uhash_setKeyDeleter(gOlsonToMeta, deleteUCharString); michael@0: uhash_setValueDeleter(gOlsonToMeta, deleteUVector); michael@0: } michael@0: } michael@0: michael@0: michael@0: const UVector* U_EXPORT2 michael@0: ZoneMeta::getMetazoneMappings(const UnicodeString &tzid) { michael@0: UErrorCode status = U_ZERO_ERROR; michael@0: UChar tzidUChars[ZID_KEY_MAX + 1]; michael@0: tzid.extract(tzidUChars, ZID_KEY_MAX + 1, status); michael@0: if (U_FAILURE(status) || status == U_STRING_NOT_TERMINATED_WARNING) { michael@0: return NULL; michael@0: } michael@0: michael@0: umtx_initOnce(gOlsonToMetaInitOnce, &olsonToMetaInit, status); michael@0: if (U_FAILURE(status)) { michael@0: return NULL; michael@0: } michael@0: michael@0: // get the mapping from cache michael@0: const UVector *result = NULL; michael@0: michael@0: umtx_lock(&gZoneMetaLock); michael@0: { michael@0: result = (UVector*) uhash_get(gOlsonToMeta, tzidUChars); michael@0: } michael@0: umtx_unlock(&gZoneMetaLock); michael@0: michael@0: if (result != NULL) { michael@0: return result; michael@0: } michael@0: michael@0: // miss the cache - create new one michael@0: UVector *tmpResult = createMetazoneMappings(tzid); michael@0: if (tmpResult == NULL) { michael@0: // not available michael@0: return NULL; michael@0: } michael@0: michael@0: // put the new one into the cache michael@0: umtx_lock(&gZoneMetaLock); michael@0: { michael@0: // make sure it's already created michael@0: result = (UVector*) uhash_get(gOlsonToMeta, tzidUChars); michael@0: if (result == NULL) { michael@0: // add the one just created michael@0: int32_t tzidLen = tzid.length() + 1; michael@0: UChar *key = (UChar*)uprv_malloc(tzidLen * sizeof(UChar)); michael@0: if (key == NULL) { michael@0: // memory allocation error.. just return NULL michael@0: result = NULL; michael@0: delete tmpResult; michael@0: } else { michael@0: tzid.extract(key, tzidLen, status); michael@0: uhash_put(gOlsonToMeta, key, tmpResult, &status); michael@0: if (U_FAILURE(status)) { michael@0: // delete the mapping michael@0: result = NULL; michael@0: delete tmpResult; michael@0: } else { michael@0: result = tmpResult; michael@0: } michael@0: } michael@0: } else { michael@0: // another thread already put the one michael@0: delete tmpResult; michael@0: } michael@0: } michael@0: umtx_unlock(&gZoneMetaLock); michael@0: michael@0: return result; michael@0: } michael@0: michael@0: UVector* michael@0: ZoneMeta::createMetazoneMappings(const UnicodeString &tzid) { michael@0: UVector *mzMappings = NULL; michael@0: UErrorCode status = U_ZERO_ERROR; michael@0: michael@0: UnicodeString canonicalID; michael@0: UResourceBundle *rb = ures_openDirect(NULL, gMetaZones, &status); michael@0: ures_getByKey(rb, gMetazoneInfo, rb, &status); michael@0: getCanonicalCLDRID(tzid, canonicalID, status); michael@0: michael@0: if (U_SUCCESS(status)) { michael@0: char tzKey[ZID_KEY_MAX + 1]; michael@0: int32_t tzKeyLen = canonicalID.extract(0, canonicalID.length(), tzKey, sizeof(tzKey), US_INV); michael@0: tzKey[tzKeyLen] = 0; michael@0: michael@0: // tzid keys are using ':' as separators michael@0: char *p = tzKey; michael@0: while (*p) { michael@0: if (*p == '/') { michael@0: *p = ':'; michael@0: } michael@0: p++; michael@0: } michael@0: michael@0: ures_getByKey(rb, tzKey, rb, &status); michael@0: michael@0: if (U_SUCCESS(status)) { michael@0: UResourceBundle *mz = NULL; michael@0: while (ures_hasNext(rb)) { michael@0: mz = ures_getNextResource(rb, mz, &status); michael@0: michael@0: const UChar *mz_name = ures_getStringByIndex(mz, 0, NULL, &status); michael@0: const UChar *mz_from = gDefaultFrom; michael@0: const UChar *mz_to = gDefaultTo; michael@0: michael@0: if (ures_getSize(mz) == 3) { michael@0: mz_from = ures_getStringByIndex(mz, 1, NULL, &status); michael@0: mz_to = ures_getStringByIndex(mz, 2, NULL, &status); michael@0: } michael@0: michael@0: if(U_FAILURE(status)){ michael@0: status = U_ZERO_ERROR; michael@0: continue; michael@0: } michael@0: // We do not want to use SimpleDateformat to parse boundary dates, michael@0: // because this code could be triggered by the initialization code michael@0: // used by SimpleDateFormat. michael@0: UDate from = parseDate(mz_from, status); michael@0: UDate to = parseDate(mz_to, status); michael@0: if (U_FAILURE(status)) { michael@0: status = U_ZERO_ERROR; michael@0: continue; michael@0: } michael@0: michael@0: OlsonToMetaMappingEntry *entry = (OlsonToMetaMappingEntry*)uprv_malloc(sizeof(OlsonToMetaMappingEntry)); michael@0: if (entry == NULL) { michael@0: status = U_MEMORY_ALLOCATION_ERROR; michael@0: break; michael@0: } michael@0: entry->mzid = mz_name; michael@0: entry->from = from; michael@0: entry->to = to; michael@0: michael@0: if (mzMappings == NULL) { michael@0: mzMappings = new UVector(deleteOlsonToMetaMappingEntry, NULL, status); michael@0: if (U_FAILURE(status)) { michael@0: delete mzMappings; michael@0: deleteOlsonToMetaMappingEntry(entry); michael@0: uprv_free(entry); michael@0: break; michael@0: } michael@0: } michael@0: michael@0: mzMappings->addElement(entry, status); michael@0: if (U_FAILURE(status)) { michael@0: break; michael@0: } michael@0: } michael@0: ures_close(mz); michael@0: if (U_FAILURE(status)) { michael@0: if (mzMappings != NULL) { michael@0: delete mzMappings; michael@0: mzMappings = NULL; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: ures_close(rb); michael@0: return mzMappings; michael@0: } michael@0: michael@0: UnicodeString& U_EXPORT2 michael@0: ZoneMeta::getZoneIdByMetazone(const UnicodeString &mzid, const UnicodeString ®ion, UnicodeString &result) { michael@0: UErrorCode status = U_ZERO_ERROR; michael@0: const UChar *tzid = NULL; michael@0: int32_t tzidLen = 0; michael@0: char keyBuf[ZID_KEY_MAX + 1]; michael@0: int32_t keyLen = 0; michael@0: michael@0: if (mzid.length() > ZID_KEY_MAX) { michael@0: result.setToBogus(); michael@0: return result; michael@0: } michael@0: michael@0: keyLen = mzid.extract(0, mzid.length(), keyBuf, ZID_KEY_MAX + 1, US_INV); michael@0: keyBuf[keyLen] = 0; michael@0: michael@0: UResourceBundle *rb = ures_openDirect(NULL, gMetaZones, &status); michael@0: ures_getByKey(rb, gMapTimezonesTag, rb, &status); michael@0: ures_getByKey(rb, keyBuf, rb, &status); michael@0: michael@0: if (U_SUCCESS(status)) { michael@0: // check region mapping michael@0: if (region.length() == 2 || region.length() == 3) { michael@0: keyLen = region.extract(0, region.length(), keyBuf, ZID_KEY_MAX + 1, US_INV); michael@0: keyBuf[keyLen] = 0; michael@0: tzid = ures_getStringByKey(rb, keyBuf, &tzidLen, &status); michael@0: if (status == U_MISSING_RESOURCE_ERROR) { michael@0: status = U_ZERO_ERROR; michael@0: } michael@0: } michael@0: if (U_SUCCESS(status) && tzid == NULL) { michael@0: // try "001" michael@0: tzid = ures_getStringByKey(rb, gWorldTag, &tzidLen, &status); michael@0: } michael@0: } michael@0: ures_close(rb); michael@0: michael@0: if (tzid == NULL) { michael@0: result.setToBogus(); michael@0: } else { michael@0: result.setTo(tzid, tzidLen); michael@0: } michael@0: michael@0: return result; michael@0: } michael@0: michael@0: static void U_CALLCONV initAvailableMetaZoneIDs () { michael@0: U_ASSERT(gMetaZoneIDs == NULL); michael@0: U_ASSERT(gMetaZoneIDTable == NULL); michael@0: ucln_i18n_registerCleanup(UCLN_I18N_ZONEMETA, zoneMeta_cleanup); michael@0: michael@0: UErrorCode status = U_ZERO_ERROR; michael@0: gMetaZoneIDTable = uhash_open(uhash_hashUnicodeString, uhash_compareUnicodeString, NULL, &status); michael@0: if (U_FAILURE(status) || gMetaZoneIDTable == NULL) { michael@0: gMetaZoneIDTable = NULL; michael@0: return; michael@0: } michael@0: uhash_setKeyDeleter(gMetaZoneIDTable, uprv_deleteUObject); michael@0: // No valueDeleter, because the vector maintain the value objects michael@0: gMetaZoneIDs = new UVector(NULL, uhash_compareUChars, status); michael@0: if (U_FAILURE(status) || gMetaZoneIDs == NULL) { michael@0: gMetaZoneIDs = NULL; michael@0: uhash_close(gMetaZoneIDTable); michael@0: gMetaZoneIDTable = NULL; michael@0: return; michael@0: } michael@0: gMetaZoneIDs->setDeleter(uprv_free); michael@0: michael@0: UResourceBundle *rb = ures_openDirect(NULL, gMetaZones, &status); michael@0: UResourceBundle *bundle = ures_getByKey(rb, gMapTimezonesTag, NULL, &status); michael@0: UResourceBundle res; michael@0: ures_initStackObject(&res); michael@0: while (U_SUCCESS(status) && ures_hasNext(bundle)) { michael@0: ures_getNextResource(bundle, &res, &status); michael@0: if (U_FAILURE(status)) { michael@0: break; michael@0: } michael@0: const char *mzID = ures_getKey(&res); michael@0: int32_t len = uprv_strlen(mzID); michael@0: UChar *uMzID = (UChar*)uprv_malloc(sizeof(UChar) * (len + 1)); michael@0: if (uMzID == NULL) { michael@0: status = U_MEMORY_ALLOCATION_ERROR; michael@0: break; michael@0: } michael@0: u_charsToUChars(mzID, uMzID, len); michael@0: uMzID[len] = 0; michael@0: UnicodeString *usMzID = new UnicodeString(uMzID); michael@0: if (uhash_get(gMetaZoneIDTable, usMzID) == NULL) { michael@0: gMetaZoneIDs->addElement((void *)uMzID, status); michael@0: uhash_put(gMetaZoneIDTable, (void *)usMzID, (void *)uMzID, &status); michael@0: } else { michael@0: uprv_free(uMzID); michael@0: delete usMzID; michael@0: } michael@0: } michael@0: ures_close(&res); michael@0: ures_close(bundle); michael@0: ures_close(rb); michael@0: michael@0: if (U_FAILURE(status)) { michael@0: uhash_close(gMetaZoneIDTable); michael@0: delete gMetaZoneIDs; michael@0: gMetaZoneIDTable = NULL; michael@0: gMetaZoneIDs = NULL; michael@0: } michael@0: } michael@0: michael@0: const UVector* michael@0: ZoneMeta::getAvailableMetazoneIDs() { michael@0: umtx_initOnce(gMetaZoneIDsInitOnce, &initAvailableMetaZoneIDs); michael@0: return gMetaZoneIDs; michael@0: } michael@0: michael@0: const UChar* michael@0: ZoneMeta::findMetaZoneID(const UnicodeString& mzid) { michael@0: umtx_initOnce(gMetaZoneIDsInitOnce, &initAvailableMetaZoneIDs); michael@0: if (gMetaZoneIDTable == NULL) { michael@0: return NULL; michael@0: } michael@0: return (const UChar*)uhash_get(gMetaZoneIDTable, &mzid); michael@0: } michael@0: michael@0: const UChar* michael@0: ZoneMeta::findTimeZoneID(const UnicodeString& tzid) { michael@0: return TimeZone::findID(tzid); michael@0: } michael@0: michael@0: michael@0: TimeZone* michael@0: ZoneMeta::createCustomTimeZone(int32_t offset) { michael@0: UBool negative = FALSE; michael@0: int32_t tmp = offset; michael@0: if (offset < 0) { michael@0: negative = TRUE; michael@0: tmp = -offset; michael@0: } michael@0: int32_t hour, min, sec; michael@0: michael@0: tmp /= 1000; michael@0: sec = tmp % 60; michael@0: tmp /= 60; michael@0: min = tmp % 60; michael@0: hour = tmp / 60; michael@0: michael@0: UnicodeString zid; michael@0: formatCustomID(hour, min, sec, negative, zid); michael@0: return new SimpleTimeZone(offset, zid); michael@0: } michael@0: michael@0: UnicodeString& michael@0: ZoneMeta::formatCustomID(uint8_t hour, uint8_t min, uint8_t sec, UBool negative, UnicodeString& id) { michael@0: // Create normalized time zone ID - GMT[+|-]HH:mm[:ss] michael@0: id.setTo(gCustomTzPrefix, -1); michael@0: if (hour != 0 || min != 0) { michael@0: if (negative) { michael@0: id.append((UChar)0x2D); // '-' michael@0: } else { michael@0: id.append((UChar)0x2B); // '+' michael@0: } michael@0: // Always use US-ASCII digits michael@0: id.append((UChar)(0x30 + (hour%100)/10)); michael@0: id.append((UChar)(0x30 + (hour%10))); michael@0: id.append((UChar)0x3A); // ':' michael@0: id.append((UChar)(0x30 + (min%100)/10)); michael@0: id.append((UChar)(0x30 + (min%10))); michael@0: if (sec != 0) { michael@0: id.append((UChar)0x3A); // ':' michael@0: id.append((UChar)(0x30 + (sec%100)/10)); michael@0: id.append((UChar)(0x30 + (sec%10))); michael@0: } michael@0: } michael@0: return id; michael@0: } michael@0: michael@0: const UChar* michael@0: ZoneMeta::getShortID(const TimeZone& tz) { michael@0: const UChar* canonicalID = NULL; michael@0: if (dynamic_cast(&tz) != NULL) { michael@0: // short cut for OlsonTimeZone michael@0: const OlsonTimeZone *otz = (const OlsonTimeZone*)&tz; michael@0: canonicalID = otz->getCanonicalID(); michael@0: } michael@0: if (canonicalID == NULL) { michael@0: return NULL; michael@0: } michael@0: return getShortIDFromCanonical(canonicalID); michael@0: } michael@0: michael@0: const UChar* michael@0: ZoneMeta::getShortID(const UnicodeString& id) { michael@0: UErrorCode status = U_ZERO_ERROR; michael@0: const UChar* canonicalID = ZoneMeta::getCanonicalCLDRID(id, status); michael@0: if (U_FAILURE(status) || canonicalID == NULL) { michael@0: return NULL; michael@0: } michael@0: return ZoneMeta::getShortIDFromCanonical(canonicalID); michael@0: } michael@0: michael@0: const UChar* michael@0: ZoneMeta::getShortIDFromCanonical(const UChar* canonicalID) { michael@0: const UChar* shortID = NULL; michael@0: int32_t len = u_strlen(canonicalID); michael@0: char tzidKey[ZID_KEY_MAX + 1]; michael@0: michael@0: u_UCharsToChars(canonicalID, tzidKey, len); michael@0: tzidKey[len] = (char) 0; // Make sure it is null terminated. michael@0: michael@0: // replace '/' with ':' michael@0: char *p = tzidKey; michael@0: while (*p++) { michael@0: if (*p == '/') { michael@0: *p = ':'; michael@0: } michael@0: } michael@0: michael@0: UErrorCode status = U_ZERO_ERROR; michael@0: UResourceBundle *rb = ures_openDirect(NULL, gKeyTypeData, &status); michael@0: ures_getByKey(rb, gTypeMapTag, rb, &status); michael@0: ures_getByKey(rb, gTimezoneTag, rb, &status); michael@0: shortID = ures_getStringByKey(rb, tzidKey, NULL, &status); michael@0: ures_close(rb); michael@0: michael@0: return shortID; michael@0: } michael@0: michael@0: U_NAMESPACE_END michael@0: michael@0: #endif /* #if !UCONFIG_NO_FORMATTING */