2 *******************************************************************************
3 * Copyright (C) 2007-2013, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 *******************************************************************************
8 #include "unicode/utypes.h"
10 #if !UCONFIG_NO_FORMATTING
14 #include "unicode/timezone.h"
15 #include "unicode/ustring.h"
16 #include "unicode/putil.h"
17 #include "unicode/simpletz.h"
30 static UMutex gZoneMetaLock = U_MUTEX_INITIALIZER;
32 // CLDR Canonical ID mapping table
33 static UHashtable *gCanonicalIDCache = NULL;
34 static icu::UInitOnce gCanonicalIDCacheInitOnce = U_INITONCE_INITIALIZER;
36 // Metazone mapping table
37 static UHashtable *gOlsonToMeta = NULL;
38 static icu::UInitOnce gOlsonToMetaInitOnce = U_INITONCE_INITIALIZER;
40 // Available metazone IDs vector and table
41 static icu::UVector *gMetaZoneIDs = NULL;
42 static UHashtable *gMetaZoneIDTable = NULL;
43 static icu::UInitOnce gMetaZoneIDsInitOnce = U_INITONCE_INITIALIZER;
45 // Country info vectors
46 static icu::UVector *gSingleZoneCountries = NULL;
47 static icu::UVector *gMultiZonesCountries = NULL;
48 static icu::UInitOnce gCountryInfoVectorsInitOnce = U_INITONCE_INITIALIZER;
53 * Cleanup callback func
55 static UBool U_CALLCONV zoneMeta_cleanup(void)
57 if (gCanonicalIDCache != NULL) {
58 uhash_close(gCanonicalIDCache);
59 gCanonicalIDCache = NULL;
61 gCanonicalIDCacheInitOnce.reset();
63 if (gOlsonToMeta != NULL) {
64 uhash_close(gOlsonToMeta);
67 gOlsonToMetaInitOnce.reset();
69 if (gMetaZoneIDTable != NULL) {
70 uhash_close(gMetaZoneIDTable);
71 gMetaZoneIDTable = NULL;
73 // delete after closing gMetaZoneIDTable, because it holds
74 // value objects held by the hashtable
77 gMetaZoneIDsInitOnce.reset();
79 delete gSingleZoneCountries;
80 gSingleZoneCountries = NULL;
81 delete gMultiZonesCountries;
82 gMultiZonesCountries = NULL;
83 gCountryInfoVectorsInitOnce.reset();
89 * Deleter for UChar* string
91 static void U_CALLCONV
92 deleteUCharString(void *obj) {
93 UChar *entry = (UChar*)obj;
100 static void U_CALLCONV
101 deleteUVector(void *obj) {
102 delete (icu::UVector*) obj;
106 * Deleter for OlsonToMetaMappingEntry
108 static void U_CALLCONV
109 deleteOlsonToMetaMappingEntry(void *obj) {
110 icu::OlsonToMetaMappingEntry *entry = (icu::OlsonToMetaMappingEntry*)obj;
118 #define ZID_KEY_MAX 128
120 static const char gMetaZones[] = "metaZones";
121 static const char gMetazoneInfo[] = "metazoneInfo";
122 static const char gMapTimezonesTag[] = "mapTimezones";
124 static const char gKeyTypeData[] = "keyTypeData";
125 static const char gTypeAliasTag[] = "typeAlias";
126 static const char gTypeMapTag[] = "typeMap";
127 static const char gTimezoneTag[] = "timezone";
129 static const char gPrimaryZonesTag[] = "primaryZones";
131 static const char gWorldTag[] = "001";
133 static const UChar gWorld[] = {0x30, 0x30, 0x31, 0x00}; // "001"
135 static const UChar gDefaultFrom[] = {0x31, 0x39, 0x37, 0x30, 0x2D, 0x30, 0x31, 0x2D, 0x30, 0x31,
136 0x20, 0x30, 0x30, 0x3A, 0x30, 0x30, 0x00}; // "1970-01-01 00:00"
137 static const UChar gDefaultTo[] = {0x39, 0x39, 0x39, 0x39, 0x2D, 0x31, 0x32, 0x2D, 0x33, 0x31,
138 0x20, 0x32, 0x33, 0x3A, 0x35, 0x39, 0x00}; // "9999-12-31 23:59"
140 static const UChar gCustomTzPrefix[] = {0x47, 0x4D, 0x54, 0}; // "GMT"
142 #define ASCII_DIGIT(c) (((c)>=0x30 && (c)<=0x39) ? (c)-0x30 : -1)
145 * Convert a date string used by metazone mappings to UDate.
146 * The format used by CLDR metazone mapping is "yyyy-MM-dd HH:mm".
149 parseDate (const UChar *text, UErrorCode &status) {
150 if (U_FAILURE(status)) {
153 int32_t len = u_strlen(text);
154 if (len != 16 && len != 10) {
155 // It must be yyyy-MM-dd HH:mm (length 16) or yyyy-MM-dd (length 10)
156 status = U_INVALID_FORMAT_ERROR;
160 int32_t year = 0, month = 0, day = 0, hour = 0, min = 0, n;
164 for (idx = 0; idx <= 3 && U_SUCCESS(status); idx++) {
165 n = ASCII_DIGIT((int32_t)text[idx]);
169 status = U_INVALID_FORMAT_ERROR;
173 for (idx = 5; idx <= 6 && U_SUCCESS(status); idx++) {
174 n = ASCII_DIGIT((int32_t)text[idx]);
176 month = 10*month + n;
178 status = U_INVALID_FORMAT_ERROR;
182 for (idx = 8; idx <= 9 && U_SUCCESS(status); idx++) {
183 n = ASCII_DIGIT((int32_t)text[idx]);
187 status = U_INVALID_FORMAT_ERROR;
192 for (idx = 11; idx <= 12 && U_SUCCESS(status); idx++) {
193 n = ASCII_DIGIT((int32_t)text[idx]);
197 status = U_INVALID_FORMAT_ERROR;
201 for (idx = 14; idx <= 15 && U_SUCCESS(status); idx++) {
202 n = ASCII_DIGIT((int32_t)text[idx]);
206 status = U_INVALID_FORMAT_ERROR;
211 if (U_SUCCESS(status)) {
212 UDate date = Grego::fieldsToDay(year, month - 1, day) * U_MILLIS_PER_DAY
213 + hour * U_MILLIS_PER_HOUR + min * U_MILLIS_PER_MINUTE;
219 static void U_CALLCONV initCanonicalIDCache(UErrorCode &status) {
220 gCanonicalIDCache = uhash_open(uhash_hashUChars, uhash_compareUChars, NULL, &status);
221 if (gCanonicalIDCache == NULL) {
222 status = U_MEMORY_ALLOCATION_ERROR;
224 if (U_FAILURE(status)) {
225 gCanonicalIDCache = NULL;
227 // No key/value deleters - keys/values are from a resource bundle
228 ucln_i18n_registerCleanup(UCLN_I18N_ZONEMETA, zoneMeta_cleanup);
232 const UChar* U_EXPORT2
233 ZoneMeta::getCanonicalCLDRID(const UnicodeString &tzid, UErrorCode& status) {
234 if (U_FAILURE(status)) {
238 int32_t len = tzid.length();
239 if (len > ZID_KEY_MAX) {
240 status = U_ILLEGAL_ARGUMENT_ERROR;
244 // Checking the cached results
245 umtx_initOnce(gCanonicalIDCacheInitOnce, &initCanonicalIDCache, status);
246 if (U_FAILURE(status)) {
250 const UChar *canonicalID = NULL;
252 UErrorCode tmpStatus = U_ZERO_ERROR;
253 UChar utzid[ZID_KEY_MAX + 1];
254 tzid.extract(utzid, ZID_KEY_MAX + 1, tmpStatus);
255 U_ASSERT(tmpStatus == U_ZERO_ERROR); // we checked the length of tzid already
257 // Check if it was already cached
258 umtx_lock(&gZoneMetaLock);
260 canonicalID = (const UChar *)uhash_get(gCanonicalIDCache, utzid);
262 umtx_unlock(&gZoneMetaLock);
264 if (canonicalID != NULL) {
268 // If not, resolve CLDR canonical ID with resource data
269 UBool isInputCanonical = FALSE;
270 char id[ZID_KEY_MAX + 1];
271 const UChar* idChars = tzid.getBuffer();
273 u_UCharsToChars(idChars,id,len);
274 id[len] = (char) 0; // Make sure it is null terminated.
276 // replace '/' with ':'
284 UResourceBundle *top = ures_openDirect(NULL, gKeyTypeData, &tmpStatus);
285 UResourceBundle *rb = ures_getByKey(top, gTypeMapTag, NULL, &tmpStatus);
286 ures_getByKey(rb, gTimezoneTag, rb, &tmpStatus);
287 ures_getByKey(rb, id, rb, &tmpStatus);
288 if (U_SUCCESS(tmpStatus)) {
289 // type entry (canonical) found
290 // the input is the canonical ID. resolve to const UChar*
291 canonicalID = TimeZone::findID(tzid);
292 isInputCanonical = TRUE;
295 if (canonicalID == NULL) {
296 // If a map element not found, then look for an alias
297 tmpStatus = U_ZERO_ERROR;
298 ures_getByKey(top, gTypeAliasTag, rb, &tmpStatus);
299 ures_getByKey(rb, gTimezoneTag, rb, &tmpStatus);
300 const UChar *canonical = ures_getStringByKey(rb,id,NULL,&tmpStatus);
301 if (U_SUCCESS(tmpStatus)) {
302 // canonical map found
303 canonicalID = canonical;
306 if (canonicalID == NULL) {
307 // Dereference the input ID using the tz data
308 const UChar *derefer = TimeZone::dereferOlsonLink(tzid);
309 if (derefer == NULL) {
310 status = U_ILLEGAL_ARGUMENT_ERROR;
312 len = u_strlen(derefer);
313 u_UCharsToChars(derefer,id,len);
314 id[len] = (char) 0; // Make sure it is null terminated.
316 // replace '/' with ':'
324 // If a dereference turned something up then look for an alias.
325 // rb still points to the alias table, so we don't have to go looking
327 tmpStatus = U_ZERO_ERROR;
328 canonical = ures_getStringByKey(rb,id,NULL,&tmpStatus);
329 if (U_SUCCESS(tmpStatus)) {
330 // canonical map for the dereferenced ID found
331 canonicalID = canonical;
333 canonicalID = derefer;
334 isInputCanonical = TRUE;
342 if (U_SUCCESS(status)) {
343 U_ASSERT(canonicalID != NULL); // canocanilD must be non-NULL here
345 // Put the resolved canonical ID to the cache
346 umtx_lock(&gZoneMetaLock);
348 const UChar* idInCache = (const UChar *)uhash_get(gCanonicalIDCache, utzid);
349 if (idInCache == NULL) {
350 const UChar* key = ZoneMeta::findTimeZoneID(tzid);
351 U_ASSERT(key != NULL);
353 idInCache = (const UChar *)uhash_put(gCanonicalIDCache, (void *)key, (void *)canonicalID, &status);
354 U_ASSERT(idInCache == NULL);
357 if (U_SUCCESS(status) && isInputCanonical) {
358 // Also put canonical ID itself into the cache if not exist
359 const UChar *canonicalInCache = (const UChar*)uhash_get(gCanonicalIDCache, canonicalID);
360 if (canonicalInCache == NULL) {
361 canonicalInCache = (const UChar *)uhash_put(gCanonicalIDCache, (void *)canonicalID, (void *)canonicalID, &status);
362 U_ASSERT(canonicalInCache == NULL);
366 umtx_unlock(&gZoneMetaLock);
372 UnicodeString& U_EXPORT2
373 ZoneMeta::getCanonicalCLDRID(const UnicodeString &tzid, UnicodeString &systemID, UErrorCode& status) {
374 const UChar *canonicalID = getCanonicalCLDRID(tzid, status);
375 if (U_FAILURE(status) || canonicalID == NULL) {
376 systemID.setToBogus();
379 systemID.setTo(TRUE, canonicalID, -1);
383 const UChar* U_EXPORT2
384 ZoneMeta::getCanonicalCLDRID(const TimeZone& tz) {
385 if (dynamic_cast<const OlsonTimeZone *>(&tz) != NULL) {
386 // short cut for OlsonTimeZone
387 const OlsonTimeZone *otz = (const OlsonTimeZone*)&tz;
388 return otz->getCanonicalID();
390 UErrorCode status = U_ZERO_ERROR;
392 return getCanonicalCLDRID(tz.getID(tzID), status);
395 static void U_CALLCONV countryInfoVectorsInit(UErrorCode &status) {
396 // Create empty vectors
397 // No deleters for these UVectors, it's a reference to a resource bundle string.
398 gSingleZoneCountries = new UVector(NULL, uhash_compareUChars, status);
399 if (gSingleZoneCountries == NULL) {
400 status = U_MEMORY_ALLOCATION_ERROR;
402 gMultiZonesCountries = new UVector(NULL, uhash_compareUChars, status);
403 if (gMultiZonesCountries == NULL) {
404 status = U_MEMORY_ALLOCATION_ERROR;
407 if (U_FAILURE(status)) {
408 delete gSingleZoneCountries;
409 delete gMultiZonesCountries;
410 gSingleZoneCountries = NULL;
411 gMultiZonesCountries = NULL;
413 ucln_i18n_registerCleanup(UCLN_I18N_ZONEMETA, zoneMeta_cleanup);
417 UnicodeString& U_EXPORT2
418 ZoneMeta::getCanonicalCountry(const UnicodeString &tzid, UnicodeString &country, UBool *isPrimary /* = NULL */) {
419 if (isPrimary != NULL) {
423 const UChar *region = TimeZone::getRegion(tzid);
424 if (region != NULL && u_strcmp(gWorld, region) != 0) {
425 country.setTo(region, -1);
427 country.setToBogus();
431 if (isPrimary != NULL) {
432 char regionBuf[] = {0, 0, 0};
434 // Checking the cached results
435 UErrorCode status = U_ZERO_ERROR;
436 umtx_initOnce(gCountryInfoVectorsInitOnce, &countryInfoVectorsInit, status);
437 if (U_FAILURE(status)) {
441 // Check if it was already cached
442 UBool cached = FALSE;
443 UBool singleZone = FALSE;
444 umtx_lock(&gZoneMetaLock);
446 singleZone = cached = gSingleZoneCountries->contains((void*)region);
448 cached = gMultiZonesCountries->contains((void*)region);
451 umtx_unlock(&gZoneMetaLock);
454 // We need to go through all zones associated with the region.
455 // This is relatively heavy operation.
457 U_ASSERT(u_strlen(region) == 2);
459 u_UCharsToChars(region, regionBuf, 2);
461 StringEnumeration *ids = TimeZone::createTimeZoneIDEnumeration(UCAL_ZONE_TYPE_CANONICAL_LOCATION, regionBuf, NULL, status);
462 int32_t idsLen = ids->count(status);
463 if (U_SUCCESS(status) && idsLen == 1) {
464 // only the single zone is available for the region
470 umtx_lock(&gZoneMetaLock);
472 UErrorCode ec = U_ZERO_ERROR;
474 if (!gSingleZoneCountries->contains((void*)region)) {
475 gSingleZoneCountries->addElement((void*)region, ec);
478 if (!gMultiZonesCountries->contains((void*)region)) {
479 gMultiZonesCountries->addElement((void*)region, ec);
483 umtx_unlock(&gZoneMetaLock);
489 // Note: We may cache the primary zone map in future.
491 // Even a country has multiple zones, one of them might be
492 // dominant and treated as a primary zone
494 if (regionBuf[0] == 0) {
495 u_UCharsToChars(region, regionBuf, 2);
498 UResourceBundle *rb = ures_openDirect(NULL, gMetaZones, &status);
499 ures_getByKey(rb, gPrimaryZonesTag, rb, &status);
500 const UChar *primaryZone = ures_getStringByKey(rb, regionBuf, &idLen, &status);
501 if (U_SUCCESS(status)) {
502 if (tzid.compare(primaryZone, idLen) == 0) {
505 // The given ID might not be a canonical ID
506 UnicodeString canonicalID;
507 TimeZone::getCanonicalID(tzid, canonicalID, status);
508 if (U_SUCCESS(status) && canonicalID.compare(primaryZone, idLen) == 0) {
520 UnicodeString& U_EXPORT2
521 ZoneMeta::getMetazoneID(const UnicodeString &tzid, UDate date, UnicodeString &result) {
523 const UVector *mappings = getMetazoneMappings(tzid);
524 if (mappings != NULL) {
525 for (int32_t i = 0; i < mappings->size(); i++) {
526 OlsonToMetaMappingEntry *mzm = (OlsonToMetaMappingEntry*)mappings->elementAt(i);
527 if (mzm->from <= date && mzm->to > date) {
528 result.setTo(mzm->mzid, -1);
540 static void U_CALLCONV olsonToMetaInit(UErrorCode &status) {
541 U_ASSERT(gOlsonToMeta == NULL);
542 ucln_i18n_registerCleanup(UCLN_I18N_ZONEMETA, zoneMeta_cleanup);
543 gOlsonToMeta = uhash_open(uhash_hashUChars, uhash_compareUChars, NULL, &status);
544 if (U_FAILURE(status)) {
547 uhash_setKeyDeleter(gOlsonToMeta, deleteUCharString);
548 uhash_setValueDeleter(gOlsonToMeta, deleteUVector);
553 const UVector* U_EXPORT2
554 ZoneMeta::getMetazoneMappings(const UnicodeString &tzid) {
555 UErrorCode status = U_ZERO_ERROR;
556 UChar tzidUChars[ZID_KEY_MAX + 1];
557 tzid.extract(tzidUChars, ZID_KEY_MAX + 1, status);
558 if (U_FAILURE(status) || status == U_STRING_NOT_TERMINATED_WARNING) {
562 umtx_initOnce(gOlsonToMetaInitOnce, &olsonToMetaInit, status);
563 if (U_FAILURE(status)) {
567 // get the mapping from cache
568 const UVector *result = NULL;
570 umtx_lock(&gZoneMetaLock);
572 result = (UVector*) uhash_get(gOlsonToMeta, tzidUChars);
574 umtx_unlock(&gZoneMetaLock);
576 if (result != NULL) {
580 // miss the cache - create new one
581 UVector *tmpResult = createMetazoneMappings(tzid);
582 if (tmpResult == NULL) {
587 // put the new one into the cache
588 umtx_lock(&gZoneMetaLock);
590 // make sure it's already created
591 result = (UVector*) uhash_get(gOlsonToMeta, tzidUChars);
592 if (result == NULL) {
593 // add the one just created
594 int32_t tzidLen = tzid.length() + 1;
595 UChar *key = (UChar*)uprv_malloc(tzidLen * sizeof(UChar));
597 // memory allocation error.. just return NULL
601 tzid.extract(key, tzidLen, status);
602 uhash_put(gOlsonToMeta, key, tmpResult, &status);
603 if (U_FAILURE(status)) {
604 // delete the mapping
612 // another thread already put the one
616 umtx_unlock(&gZoneMetaLock);
622 ZoneMeta::createMetazoneMappings(const UnicodeString &tzid) {
623 UVector *mzMappings = NULL;
624 UErrorCode status = U_ZERO_ERROR;
626 UnicodeString canonicalID;
627 UResourceBundle *rb = ures_openDirect(NULL, gMetaZones, &status);
628 ures_getByKey(rb, gMetazoneInfo, rb, &status);
629 getCanonicalCLDRID(tzid, canonicalID, status);
631 if (U_SUCCESS(status)) {
632 char tzKey[ZID_KEY_MAX + 1];
633 int32_t tzKeyLen = canonicalID.extract(0, canonicalID.length(), tzKey, sizeof(tzKey), US_INV);
636 // tzid keys are using ':' as separators
645 ures_getByKey(rb, tzKey, rb, &status);
647 if (U_SUCCESS(status)) {
648 UResourceBundle *mz = NULL;
649 while (ures_hasNext(rb)) {
650 mz = ures_getNextResource(rb, mz, &status);
652 const UChar *mz_name = ures_getStringByIndex(mz, 0, NULL, &status);
653 const UChar *mz_from = gDefaultFrom;
654 const UChar *mz_to = gDefaultTo;
656 if (ures_getSize(mz) == 3) {
657 mz_from = ures_getStringByIndex(mz, 1, NULL, &status);
658 mz_to = ures_getStringByIndex(mz, 2, NULL, &status);
661 if(U_FAILURE(status)){
662 status = U_ZERO_ERROR;
665 // We do not want to use SimpleDateformat to parse boundary dates,
666 // because this code could be triggered by the initialization code
667 // used by SimpleDateFormat.
668 UDate from = parseDate(mz_from, status);
669 UDate to = parseDate(mz_to, status);
670 if (U_FAILURE(status)) {
671 status = U_ZERO_ERROR;
675 OlsonToMetaMappingEntry *entry = (OlsonToMetaMappingEntry*)uprv_malloc(sizeof(OlsonToMetaMappingEntry));
677 status = U_MEMORY_ALLOCATION_ERROR;
680 entry->mzid = mz_name;
684 if (mzMappings == NULL) {
685 mzMappings = new UVector(deleteOlsonToMetaMappingEntry, NULL, status);
686 if (U_FAILURE(status)) {
688 deleteOlsonToMetaMappingEntry(entry);
694 mzMappings->addElement(entry, status);
695 if (U_FAILURE(status)) {
700 if (U_FAILURE(status)) {
701 if (mzMappings != NULL) {
712 UnicodeString& U_EXPORT2
713 ZoneMeta::getZoneIdByMetazone(const UnicodeString &mzid, const UnicodeString ®ion, UnicodeString &result) {
714 UErrorCode status = U_ZERO_ERROR;
715 const UChar *tzid = NULL;
717 char keyBuf[ZID_KEY_MAX + 1];
720 if (mzid.length() > ZID_KEY_MAX) {
725 keyLen = mzid.extract(0, mzid.length(), keyBuf, ZID_KEY_MAX + 1, US_INV);
728 UResourceBundle *rb = ures_openDirect(NULL, gMetaZones, &status);
729 ures_getByKey(rb, gMapTimezonesTag, rb, &status);
730 ures_getByKey(rb, keyBuf, rb, &status);
732 if (U_SUCCESS(status)) {
733 // check region mapping
734 if (region.length() == 2 || region.length() == 3) {
735 keyLen = region.extract(0, region.length(), keyBuf, ZID_KEY_MAX + 1, US_INV);
737 tzid = ures_getStringByKey(rb, keyBuf, &tzidLen, &status);
738 if (status == U_MISSING_RESOURCE_ERROR) {
739 status = U_ZERO_ERROR;
742 if (U_SUCCESS(status) && tzid == NULL) {
744 tzid = ures_getStringByKey(rb, gWorldTag, &tzidLen, &status);
752 result.setTo(tzid, tzidLen);
758 static void U_CALLCONV initAvailableMetaZoneIDs () {
759 U_ASSERT(gMetaZoneIDs == NULL);
760 U_ASSERT(gMetaZoneIDTable == NULL);
761 ucln_i18n_registerCleanup(UCLN_I18N_ZONEMETA, zoneMeta_cleanup);
763 UErrorCode status = U_ZERO_ERROR;
764 gMetaZoneIDTable = uhash_open(uhash_hashUnicodeString, uhash_compareUnicodeString, NULL, &status);
765 if (U_FAILURE(status) || gMetaZoneIDTable == NULL) {
766 gMetaZoneIDTable = NULL;
769 uhash_setKeyDeleter(gMetaZoneIDTable, uprv_deleteUObject);
770 // No valueDeleter, because the vector maintain the value objects
771 gMetaZoneIDs = new UVector(NULL, uhash_compareUChars, status);
772 if (U_FAILURE(status) || gMetaZoneIDs == NULL) {
774 uhash_close(gMetaZoneIDTable);
775 gMetaZoneIDTable = NULL;
778 gMetaZoneIDs->setDeleter(uprv_free);
780 UResourceBundle *rb = ures_openDirect(NULL, gMetaZones, &status);
781 UResourceBundle *bundle = ures_getByKey(rb, gMapTimezonesTag, NULL, &status);
783 ures_initStackObject(&res);
784 while (U_SUCCESS(status) && ures_hasNext(bundle)) {
785 ures_getNextResource(bundle, &res, &status);
786 if (U_FAILURE(status)) {
789 const char *mzID = ures_getKey(&res);
790 int32_t len = uprv_strlen(mzID);
791 UChar *uMzID = (UChar*)uprv_malloc(sizeof(UChar) * (len + 1));
793 status = U_MEMORY_ALLOCATION_ERROR;
796 u_charsToUChars(mzID, uMzID, len);
798 UnicodeString *usMzID = new UnicodeString(uMzID);
799 if (uhash_get(gMetaZoneIDTable, usMzID) == NULL) {
800 gMetaZoneIDs->addElement((void *)uMzID, status);
801 uhash_put(gMetaZoneIDTable, (void *)usMzID, (void *)uMzID, &status);
811 if (U_FAILURE(status)) {
812 uhash_close(gMetaZoneIDTable);
814 gMetaZoneIDTable = NULL;
820 ZoneMeta::getAvailableMetazoneIDs() {
821 umtx_initOnce(gMetaZoneIDsInitOnce, &initAvailableMetaZoneIDs);
826 ZoneMeta::findMetaZoneID(const UnicodeString& mzid) {
827 umtx_initOnce(gMetaZoneIDsInitOnce, &initAvailableMetaZoneIDs);
828 if (gMetaZoneIDTable == NULL) {
831 return (const UChar*)uhash_get(gMetaZoneIDTable, &mzid);
835 ZoneMeta::findTimeZoneID(const UnicodeString& tzid) {
836 return TimeZone::findID(tzid);
841 ZoneMeta::createCustomTimeZone(int32_t offset) {
842 UBool negative = FALSE;
843 int32_t tmp = offset;
848 int32_t hour, min, sec;
857 formatCustomID(hour, min, sec, negative, zid);
858 return new SimpleTimeZone(offset, zid);
862 ZoneMeta::formatCustomID(uint8_t hour, uint8_t min, uint8_t sec, UBool negative, UnicodeString& id) {
863 // Create normalized time zone ID - GMT[+|-]HH:mm[:ss]
864 id.setTo(gCustomTzPrefix, -1);
865 if (hour != 0 || min != 0) {
867 id.append((UChar)0x2D); // '-'
869 id.append((UChar)0x2B); // '+'
871 // Always use US-ASCII digits
872 id.append((UChar)(0x30 + (hour%100)/10));
873 id.append((UChar)(0x30 + (hour%10)));
874 id.append((UChar)0x3A); // ':'
875 id.append((UChar)(0x30 + (min%100)/10));
876 id.append((UChar)(0x30 + (min%10)));
878 id.append((UChar)0x3A); // ':'
879 id.append((UChar)(0x30 + (sec%100)/10));
880 id.append((UChar)(0x30 + (sec%10)));
887 ZoneMeta::getShortID(const TimeZone& tz) {
888 const UChar* canonicalID = NULL;
889 if (dynamic_cast<const OlsonTimeZone *>(&tz) != NULL) {
890 // short cut for OlsonTimeZone
891 const OlsonTimeZone *otz = (const OlsonTimeZone*)&tz;
892 canonicalID = otz->getCanonicalID();
894 if (canonicalID == NULL) {
897 return getShortIDFromCanonical(canonicalID);
901 ZoneMeta::getShortID(const UnicodeString& id) {
902 UErrorCode status = U_ZERO_ERROR;
903 const UChar* canonicalID = ZoneMeta::getCanonicalCLDRID(id, status);
904 if (U_FAILURE(status) || canonicalID == NULL) {
907 return ZoneMeta::getShortIDFromCanonical(canonicalID);
911 ZoneMeta::getShortIDFromCanonical(const UChar* canonicalID) {
912 const UChar* shortID = NULL;
913 int32_t len = u_strlen(canonicalID);
914 char tzidKey[ZID_KEY_MAX + 1];
916 u_UCharsToChars(canonicalID, tzidKey, len);
917 tzidKey[len] = (char) 0; // Make sure it is null terminated.
919 // replace '/' with ':'
927 UErrorCode status = U_ZERO_ERROR;
928 UResourceBundle *rb = ures_openDirect(NULL, gKeyTypeData, &status);
929 ures_getByKey(rb, gTypeMapTag, rb, &status);
930 ures_getByKey(rb, gTimezoneTag, rb, &status);
931 shortID = ures_getStringByKey(rb, tzidKey, NULL, &status);
939 #endif /* #if !UCONFIG_NO_FORMATTING */