Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / icu / source / i18n / timezone.cpp
index 607a014..e656a7a 100644 (file)
@@ -1,7 +1,7 @@
 /*
 *******************************************************************************
-* Copyright (C) 1997-2010, International Business Machines Corporation and    *
-* others. All Rights Reserved.                                                *
+* Copyright (C) 1997-2013, International Business Machines Corporation and
+* others. All Rights Reserved.
 *******************************************************************************
 *
 * File TIMEZONE.CPP
 *                           available IDs code.  Misc. cleanup.
 *********************************************************************************/
 
-#include "unicode/utypeinfo.h"  // for 'typeid' to work
+#include "utypeinfo.h"  // for 'typeid' to work
 
 #include "unicode/utypes.h"
 #include "unicode/ustring.h"
+#include "uassert.h"
+#include "ustr_imp.h"
 
 #ifdef U_DEBUG_TZ
 # include <stdio.h>
@@ -67,10 +69,11 @@ static char gStrBuf[256];
 #if !UCONFIG_NO_FORMATTING
 
 #include "unicode/simpletz.h"
-#include "unicode/smpdtfmt.h"
 #include "unicode/calendar.h"
 #include "unicode/gregocal.h"
 #include "unicode/ures.h"
+#include "unicode/tzfmt.h"
+#include "unicode/numfmt.h"
 #include "gregoimp.h"
 #include "uresimp.h" // struct UResourceBundle
 #include "olsontz.h"
@@ -103,44 +106,63 @@ static char gStrBuf[256];
 static const UChar         WORLD[] = {0x30, 0x30, 0x31, 0x00}; /* "001" */
 
 static const UChar         GMT_ID[] = {0x47, 0x4D, 0x54, 0x00}; /* "GMT" */
-static const UChar         Z_STR[] = {0x7A, 0x00}; /* "z" */
-static const UChar         ZZZZ_STR[] = {0x7A, 0x7A, 0x7A, 0x7A, 0x00}; /* "zzzz" */
-static const UChar         Z_UC_STR[] = {0x5A, 0x00}; /* "Z" */
-static const UChar         ZZZZ_UC_STR[] = {0x5A, 0x5A, 0x5A, 0x5A, 0x00}; /* "ZZZZ" */
-static const UChar         V_STR[] = {0x76, 0x00}; /* "v" */
-static const UChar         VVVV_STR[] = {0x76, 0x76, 0x76, 0x76, 0x00}; /* "vvvv" */
-static const UChar         V_UC_STR[] = {0x56, 0x00}; /* "V" */
-static const UChar         VVVV_UC_STR[] = {0x56, 0x56, 0x56, 0x56, 0x00}; /* "VVVV" */
+static const UChar         UNKNOWN_ZONE_ID[] = {0x45, 0x74, 0x63, 0x2F, 0x55, 0x6E, 0x6B, 0x6E, 0x6F, 0x77, 0x6E, 0x00}; /* "Etc/Unknown" */
 static const int32_t       GMT_ID_LENGTH = 3;
+static const int32_t       UNKNOWN_ZONE_ID_LENGTH = 11;
+
+static icu::TimeZone* DEFAULT_ZONE = NULL;
+static icu::UInitOnce gDefaultZoneInitOnce = U_INITONCE_INITIALIZER;
 
-static UMTX                             LOCK;
-static UMTX                             TZSET_LOCK;
-static U_NAMESPACE_QUALIFIER TimeZone*  DEFAULT_ZONE = NULL;
-static U_NAMESPACE_QUALIFIER TimeZone*  _GMT = NULL; // cf. TimeZone::GMT
+static icu::TimeZone* _GMT = NULL;
+static icu::TimeZone* _UNKNOWN_ZONE = NULL;
+static icu::UInitOnce gStaticZonesInitOnce = U_INITONCE_INITIALIZER;
 
 static char TZDATA_VERSION[16];
-static UBool TZDataVersionInitialized = FALSE;
+static icu::UInitOnce gTZDataVersionInitOnce = U_INITONCE_INITIALIZER;
+
+static int32_t* MAP_SYSTEM_ZONES = NULL;
+static int32_t* MAP_CANONICAL_SYSTEM_ZONES = NULL;
+static int32_t* MAP_CANONICAL_SYSTEM_LOCATION_ZONES = NULL;
+
+static int32_t LEN_SYSTEM_ZONES = 0;
+static int32_t LEN_CANONICAL_SYSTEM_ZONES = 0;
+static int32_t LEN_CANONICAL_SYSTEM_LOCATION_ZONES = 0;
+
+static icu::UInitOnce gSystemZonesInitOnce = U_INITONCE_INITIALIZER;
+static icu::UInitOnce gCanonicalZonesInitOnce = U_INITONCE_INITIALIZER;
+static icu::UInitOnce gCanonicalLocationZonesInitOnce = U_INITONCE_INITIALIZER;
 
 U_CDECL_BEGIN
 static UBool U_CALLCONV timeZone_cleanup(void)
 {
+    U_NAMESPACE_USE
     delete DEFAULT_ZONE;
     DEFAULT_ZONE = NULL;
+    gDefaultZoneInitOnce.reset();
 
     delete _GMT;
     _GMT = NULL;
+    delete _UNKNOWN_ZONE;
+    _UNKNOWN_ZONE = NULL;
+    gStaticZonesInitOnce.reset();
 
     uprv_memset(TZDATA_VERSION, 0, sizeof(TZDATA_VERSION));
-    TZDataVersionInitialized = FALSE;
+    gTZDataVersionInitOnce.reset();
 
-    if (LOCK) {
-        umtx_destroy(&LOCK);
-        LOCK = NULL;
-    }
-    if (TZSET_LOCK) {
-        umtx_destroy(&TZSET_LOCK);
-        TZSET_LOCK = NULL;
-    }
+    LEN_SYSTEM_ZONES = 0;
+    uprv_free(MAP_SYSTEM_ZONES);
+    MAP_SYSTEM_ZONES = 0;
+    gSystemZonesInitOnce.reset();
+
+    LEN_CANONICAL_SYSTEM_ZONES = 0;
+    uprv_free(MAP_CANONICAL_SYSTEM_ZONES);
+    MAP_CANONICAL_SYSTEM_ZONES = 0;
+    gCanonicalZonesInitOnce.reset();
+
+    LEN_CANONICAL_SYSTEM_LOCATION_ZONES = 0;
+    uprv_free(MAP_CANONICAL_SYSTEM_LOCATION_ZONES);
+    MAP_CANONICAL_SYSTEM_LOCATION_ZONES = 0;
+    gCanonicalLocationZonesInitOnce.reset();
 
     return TRUE;
 }
@@ -148,64 +170,21 @@ U_CDECL_END
 
 U_NAMESPACE_BEGIN
 
-/**
- * The Olson data is stored the "zoneinfo" resource bundle.
- * Sub-resources are organized into three ranges of data: Zones, final
- * rules, and country tables.  There is also a meta-data resource
- * which has 3 integers: The number of zones, rules, and countries,
- * respectively.  The country count includes the non-country 'Default'.
- */
-static int32_t OLSON_ZONE_COUNT = 0;  // count of zones
-
-/**
- * Given a pointer to an open "zoneinfo" resource, load up the Olson
- * meta-data. Return TRUE if successful.
- */
-static UBool getOlsonMeta(const UResourceBundle* top) {
-    if (OLSON_ZONE_COUNT == 0) {
-        UErrorCode ec = U_ZERO_ERROR;
-        UResourceBundle res;
-        ures_initStackObject(&res);
-        ures_getByKey(top, kZONES, &res, &ec);
-        if(U_SUCCESS(ec)) {
-            OLSON_ZONE_COUNT = ures_getSize(&res);
-            U_DEBUG_TZ_MSG(("OZC%d\n",OLSON_ZONE_COUNT));
-        }
-        ures_close(&res);
-    }
-    return (OLSON_ZONE_COUNT > 0);
-}
-
-/**
- * Load up the Olson meta-data. Return TRUE if successful.
- */
-static UBool getOlsonMeta() {
-    if (OLSON_ZONE_COUNT == 0) {
-        UErrorCode ec = U_ZERO_ERROR;
-        UResourceBundle *top = ures_openDirect(0, kZONEINFO, &ec);
-        if (U_SUCCESS(ec)) {
-            getOlsonMeta(top);
-        }
-        ures_close(top);
-    }
-    return (OLSON_ZONE_COUNT > 0);
-}
-
 static int32_t findInStringArray(UResourceBundle* array, const UnicodeString& id, UErrorCode &status)
 {
     UnicodeString copy;
     const UChar *u;
     int32_t len;
-    
+
     int32_t start = 0;
     int32_t limit = ures_getSize(array);
     int32_t mid;
     int32_t lastMid = INT32_MAX;
-    if(U_FAILURE(status) || (limit < 1)) { 
+    if(U_FAILURE(status) || (limit < 1)) {
         return -1;
     }
     U_DEBUG_TZ_MSG(("fisa: Looking for %s, between %d and %d\n", U_DEBUG_TZ_STR(UnicodeString(id).getTerminatedBuffer()), start, limit));
-    
+
     for (;;) {
         mid = (int32_t)((start + limit) / 2);
         if (lastMid == mid) {   /* Have we moved? */
@@ -233,7 +212,7 @@ static int32_t findInStringArray(UResourceBundle* array, const UnicodeString& id
 }
 
 /**
- * Fetch a specific zone by name.  Replaces the getByKey call. 
+ * Fetch a specific zone by name.  Replaces the getByKey call.
  * @param top Top timezone resource
  * @param id Time zone ID
  * @param oldbundle Bundle for reuse (or NULL).   see 'ures_open()'
@@ -242,12 +221,12 @@ static int32_t findInStringArray(UResourceBundle* array, const UnicodeString& id
 static UResourceBundle* getZoneByName(const UResourceBundle* top, const UnicodeString& id, UResourceBundle *oldbundle, UErrorCode& status) {
     // load the Rules object
     UResourceBundle *tmp = ures_getByKey(top, kNAMES, NULL, &status);
-    
+
     // search for the string
     int32_t idx = findInStringArray(tmp, id, status);
-    
+
     if((idx == -1) && U_SUCCESS(status)) {
-        // not found 
+        // not found
         status = U_MISSING_RESOURCE_ERROR;
         //ures_close(oldbundle);
         //oldbundle = NULL;
@@ -259,7 +238,7 @@ static UResourceBundle* getZoneByName(const UResourceBundle* top, const UnicodeS
         U_DEBUG_TZ_MSG(("gzbn: loaded z#%d, size %d, type %d, path %s, %s\n", idx, ures_getSize(oldbundle), ures_getType(oldbundle), ures_getPath(oldbundle),  u_errorName(status)));
     }
     ures_close(tmp);
-    if(U_FAILURE(status)) { 
+    if(U_FAILURE(status)) {
         //ures_close(oldbundle);
         return NULL;
     } else {
@@ -301,7 +280,7 @@ static UResourceBundle* openOlsonResource(const UnicodeString& id,
     // Dereference if this is an alias.  Docs say result should be 1
     // but it is 0 in 2.8 (?).
     U_DEBUG_TZ_MSG(("Loading zone '%s' (%s, size %d) - %s\n", buf, ures_getKey((UResourceBundle*)&res), ures_getSize(&res), u_errorName(ec)));
-    if (ures_getType(&res) == URES_INT && getOlsonMeta(top)) {
+    if (ures_getType(&res) == URES_INT) {
         int32_t deref = ures_getInt(&res, &ec) + 0;
         U_DEBUG_TZ_MSG(("getInt: %s - type is %d\n", u_errorName(ec), ures_getType(&res)));
         UResourceBundle *ares = ures_getByKey(top, kZONES, NULL, &ec); // dereference Zones section
@@ -317,25 +296,29 @@ static UResourceBundle* openOlsonResource(const UnicodeString& id,
 
 // -------------------------------------
 
-const TimeZone* U_EXPORT2
-TimeZone::getGMT(void)
-{
-    UBool needsInit;
-    UMTX_CHECK(&LOCK, (_GMT == NULL), needsInit);   /* This is here to prevent race conditions. */
+namespace {
 
+void U_CALLCONV initStaticTimeZones() {
     // Initialize _GMT independently of other static data; it should
     // be valid even if we can't load the time zone UDataMemory.
-    if (needsInit) {
-        SimpleTimeZone *tmpGMT = new SimpleTimeZone(0, UnicodeString(TRUE, GMT_ID, GMT_ID_LENGTH));
-        umtx_lock(&LOCK);
-        if (_GMT == 0) {
-            _GMT = tmpGMT;
-            tmpGMT = NULL;
-        }
-        umtx_unlock(&LOCK);
-        ucln_i18n_registerCleanup(UCLN_I18N_TIMEZONE, timeZone_cleanup);
-        delete tmpGMT;
-    }
+    ucln_i18n_registerCleanup(UCLN_I18N_TIMEZONE, timeZone_cleanup);
+    _UNKNOWN_ZONE = new SimpleTimeZone(0, UnicodeString(TRUE, UNKNOWN_ZONE_ID, UNKNOWN_ZONE_ID_LENGTH));
+    _GMT = new SimpleTimeZone(0, UnicodeString(TRUE, GMT_ID, GMT_ID_LENGTH));
+}
+
+}  // anonymous namespace
+
+const TimeZone& U_EXPORT2
+TimeZone::getUnknown()
+{
+    umtx_initOnce(gStaticZonesInitOnce, &initStaticTimeZones);
+    return *_UNKNOWN_ZONE;
+}
+
+const TimeZone* U_EXPORT2
+TimeZone::getGMT(void)
+{
+    umtx_initOnce(gStaticZonesInitOnce, &initStaticTimeZones);
     return _GMT;
 }
 
@@ -390,6 +373,47 @@ TimeZone::operator==(const TimeZone& that) const
 
 // -------------------------------------
 
+namespace {
+TimeZone*
+createSystemTimeZone(const UnicodeString& id, UErrorCode& ec) {
+    if (U_FAILURE(ec)) {
+        return NULL;
+    }
+    TimeZone* z = 0;
+    UResourceBundle res;
+    ures_initStackObject(&res);
+    U_DEBUG_TZ_MSG(("pre-err=%s\n", u_errorName(ec)));
+    UResourceBundle *top = openOlsonResource(id, res, ec);
+    U_DEBUG_TZ_MSG(("post-err=%s\n", u_errorName(ec)));
+    if (U_SUCCESS(ec)) {
+        z = new OlsonTimeZone(top, &res, id, ec);
+        if (z == NULL) {
+          U_DEBUG_TZ_MSG(("cstz: olson time zone failed to initialize - err %s\n", u_errorName(ec)));
+        }
+    }
+    ures_close(&res);
+    ures_close(top);
+    if (U_FAILURE(ec)) {
+        U_DEBUG_TZ_MSG(("cstz: failed to create, err %s\n", u_errorName(ec)));
+        delete z;
+        z = 0;
+    }
+    return z;
+}
+
+/**
+ * Lookup the given name in our system zone table.  If found,
+ * instantiate a new zone of that name and return it.  If not
+ * found, return 0.
+ */
+TimeZone*
+createSystemTimeZone(const UnicodeString& id) {
+    UErrorCode ec = U_ZERO_ERROR;
+    return createSystemTimeZone(id, ec);
+}
+
+}
+
 TimeZone* U_EXPORT2
 TimeZone::createTimeZone(const UnicodeString& ID)
 {
@@ -408,62 +432,29 @@ TimeZone::createTimeZone(const UnicodeString& ID)
         result = createCustomTimeZone(ID);
     }
     if (result == 0) {
-        U_DEBUG_TZ_MSG(("failed to load time zone with id - falling to GMT"));
-        const TimeZone* temptz = getGMT();
-        if (temptz == NULL) {
-            result = NULL;
-        } else {
-            result = temptz->clone();
-        }
+        U_DEBUG_TZ_MSG(("failed to load time zone with id - falling to Etc/Unknown(GMT)"));
+        result = getUnknown().clone();
     }
     return result;
 }
 
-/**
- * Lookup the given name in our system zone table.  If found,
- * instantiate a new zone of that name and return it.  If not
- * found, return 0.
- */
-TimeZone*
-TimeZone::createSystemTimeZone(const UnicodeString& id) {
-    TimeZone* z = 0;
-    UErrorCode ec = U_ZERO_ERROR;
-    UResourceBundle res;
-    ures_initStackObject(&res);
-    U_DEBUG_TZ_MSG(("pre-err=%s\n", u_errorName(ec)));
-    UResourceBundle *top = openOlsonResource(id, res, ec);
-    U_DEBUG_TZ_MSG(("post-err=%s\n", u_errorName(ec)));
-    if (U_SUCCESS(ec)) {
-        z = new OlsonTimeZone(top, &res, ec);
-        if (z) {
-          z->setID(id);
-        } else {
-          U_DEBUG_TZ_MSG(("cstz: olson time zone failed to initialize - err %s\n", u_errorName(ec)));
-        }
-    }
-    ures_close(&res);
-    ures_close(top);
-    if (U_FAILURE(ec)) {
-        U_DEBUG_TZ_MSG(("cstz: failed to create, err %s\n", u_errorName(ec)));
-        delete z;
-        z = 0;
-    }
-    return z;
-}
-
 // -------------------------------------
 
 /**
- * Initialize DEFAULT_ZONE from the system default time zone.  The
- * caller should confirm that DEFAULT_ZONE is NULL before calling.
+ * Initialize DEFAULT_ZONE from the system default time zone.  
  * Upon return, DEFAULT_ZONE will not be NULL, unless operator new()
  * returns NULL.
- *
- * Must be called OUTSIDE mutex.
  */
-void
-TimeZone::initDefault()
-{ 
+static void U_CALLCONV initDefault()
+{
+    ucln_i18n_registerCleanup(UCLN_I18N_TIMEZONE, timeZone_cleanup);
+
+    // If setDefault() has already been called we can skip getting the
+    // default zone information from the system.
+    if (DEFAULT_ZONE != NULL) {
+        return;
+    }
+    
     // We access system timezone data through TPlatformUtilities,
     // including tzset(), timezone, and tzname[].
     int32_t rawOffset = 0;
@@ -471,38 +462,27 @@ TimeZone::initDefault()
 
     // First, try to create a system timezone, based
     // on the string ID in tzname[0].
-    {
-        // NOTE: Local mutex here. TimeZone mutex below
-        // mutexed to avoid threading issues in the platform functions.
-        // Some of the locale/timezone OS functions may not be thread safe,
-        // so the intent is that any setting from anywhere within ICU
-        // happens while the ICU mutex is held.
-        // The operating system might actually use ICU to implement timezones.
-        // So we may have ICU calling ICU here, like on AIX.
-        // In order to prevent a double lock of a non-reentrant mutex in a
-        // different part of ICU, we use TZSET_LOCK to allow only one instance
-        // of ICU to query these thread unsafe OS functions at any given time.
-        Mutex lock(&TZSET_LOCK);
 
-        ucln_i18n_registerCleanup(UCLN_I18N_TIMEZONE, timeZone_cleanup);
-        uprv_tzset(); // Initialize tz... system data
-        
-        // Get the timezone ID from the host.  This function should do
-        // any required host-specific remapping; e.g., on Windows this
-        // function maps the Date and Time control panel setting to an
-        // ICU timezone ID.
-        hostID = uprv_tzname(0);
-        
-        // Invert sign because UNIX semantics are backwards
-        rawOffset = uprv_timezone() * -U_MILLIS_PER_SECOND;
-    }
+    // NOTE:  this code is safely single threaded, being only
+    // run via umtx_initOnce().
+    //
+    // Some of the locale/timezone OS functions may not be thread safe,
+    //
+    // The operating system might actually use ICU to implement timezones.
+    // So we may have ICU calling ICU here, like on AIX.
+    // There shouldn't be a problem with this; initOnce does not hold a mutex
+    // while the init function is being run.
 
-    UBool initialized;
-    UMTX_CHECK(&LOCK, (DEFAULT_ZONE != NULL), initialized);
-    if (initialized) {
-        /* Hrmph? Either a race condition happened, or tzset initialized ICU. */
-        return;
-    }
+    uprv_tzset(); // Initialize tz... system data
+
+    // Get the timezone ID from the host.  This function should do
+    // any required host-specific remapping; e.g., on Windows this
+    // function maps the Date and Time control panel setting to an
+    // ICU timezone ID.
+    hostID = uprv_tzname(0);
+
+    // Invert sign because UNIX semantics are backwards
+    rawOffset = uprv_timezone() * -U_MILLIS_PER_SECOND;
 
     TimeZone* default_zone = NULL;
 
@@ -512,7 +492,7 @@ TimeZone::initDefault()
     hostStrID.truncate(hostStrID.length()-1);
     default_zone = createSystemTimeZone(hostStrID);
 
-#ifdef U_WINDOWS
+#if U_PLATFORM_USES_ONLY_WIN32_API
     // hostID points to a heap-allocated location on Windows.
     uprv_free(const_cast<char *>(hostID));
 #endif
@@ -535,7 +515,7 @@ TimeZone::initDefault()
 
     // If we _still_ don't have a time zone, use GMT.
     if (default_zone == NULL) {
-        const TimeZone* temptz = getGMT();
+        const TimeZone* temptz = TimeZone::getGMT();
         // If we can't use GMT, get out.
         if (temptz == NULL) {
             return;
@@ -543,16 +523,12 @@ TimeZone::initDefault()
         default_zone = temptz->clone();
     }
 
-    // If DEFAULT_ZONE is still NULL, set it up.
-    umtx_lock(&LOCK);
-    if (DEFAULT_ZONE == NULL) {
-        DEFAULT_ZONE = default_zone;
-        default_zone = NULL;
-        ucln_i18n_registerCleanup(UCLN_I18N_TIMEZONE, timeZone_cleanup);
-    }
-    umtx_unlock(&LOCK);
-    
-    delete default_zone;
+    // The only way for DEFAULT_ZONE to be non-null at this point is if the user
+    // made a thread-unsafe call to setDefault() or adoptDefault() in another
+    // thread while this thread was doing something that required getting the default.
+    U_ASSERT(DEFAULT_ZONE == NULL);
+
+    DEFAULT_ZONE = default_zone;
 }
 
 // -------------------------------------
@@ -560,14 +536,7 @@ TimeZone::initDefault()
 TimeZone* U_EXPORT2
 TimeZone::createDefault()
 {
-    /* This is here to prevent race conditions. */
-    UBool needsInit;
-    UMTX_CHECK(&LOCK, (DEFAULT_ZONE == NULL), needsInit);
-    if (needsInit) {
-        initDefault();
-    }
-
-    Mutex lock(&LOCK); // In case adoptDefault is called
+    umtx_initOnce(gDefaultZoneInitOnce, initDefault);
     return (DEFAULT_ZONE != NULL) ? DEFAULT_ZONE->clone() : NULL;
 }
 
@@ -578,13 +547,8 @@ TimeZone::adoptDefault(TimeZone* zone)
 {
     if (zone != NULL)
     {
-        TimeZone* old = NULL;
-
-        umtx_lock(&LOCK);
-        old = DEFAULT_ZONE;
+        TimeZone *old = DEFAULT_ZONE;
         DEFAULT_ZONE = zone;
-        umtx_unlock(&LOCK);
-
         delete old;
         ucln_i18n_registerCleanup(UCLN_I18N_TIMEZONE, timeZone_cleanup);
     }
@@ -599,6 +563,84 @@ TimeZone::setDefault(const TimeZone& zone)
 
 //----------------------------------------------------------------------
 
+
+static void U_CALLCONV initMap(USystemTimeZoneType type, UErrorCode& ec) {
+    ucln_i18n_registerCleanup(UCLN_I18N_TIMEZONE, timeZone_cleanup);
+
+    UResourceBundle *res = ures_openDirect(0, kZONEINFO, &ec);
+    res = ures_getByKey(res, kNAMES, res, &ec); // dereference Zones section
+    if (U_SUCCESS(ec)) {
+        int32_t size = ures_getSize(res);
+        int32_t *m = (int32_t *)uprv_malloc(size * sizeof(int32_t));
+        if (m == NULL) {
+            ec = U_MEMORY_ALLOCATION_ERROR;
+        } else {
+            int32_t numEntries = 0;
+            for (int32_t i = 0; i < size; i++) {
+                UnicodeString id = ures_getUnicodeStringByIndex(res, i, &ec);
+                if (U_FAILURE(ec)) {
+                    break;
+                }
+                if (0 == id.compare(UNKNOWN_ZONE_ID, UNKNOWN_ZONE_ID_LENGTH)) {
+                    // exclude Etc/Unknown
+                    continue;
+                }
+                if (type == UCAL_ZONE_TYPE_CANONICAL || type == UCAL_ZONE_TYPE_CANONICAL_LOCATION) {
+                    UnicodeString canonicalID;
+                    ZoneMeta::getCanonicalCLDRID(id, canonicalID, ec);
+                    if (U_FAILURE(ec)) {
+                        break;
+                    }
+                    if (canonicalID != id) {
+                        // exclude aliases
+                        continue;
+                    }
+                }
+                if (type == UCAL_ZONE_TYPE_CANONICAL_LOCATION) {
+                    const UChar *region = TimeZone::getRegion(id, ec);
+                    if (U_FAILURE(ec)) {
+                        break;
+                    }
+                    if (u_strcmp(region, WORLD) == 0) {
+                       // exclude non-location ("001")
+                        continue;
+                    }
+                }
+                m[numEntries++] = i;
+            }
+            if (U_SUCCESS(ec)) {
+                int32_t *tmp = m;
+                m = (int32_t *)uprv_realloc(tmp, numEntries * sizeof(int32_t));
+                if (m == NULL) {
+                    // realloc failed.. use the original one even it has unused
+                    // area at the end
+                    m = tmp;
+                }
+
+                switch(type) {
+                case UCAL_ZONE_TYPE_ANY:
+                    U_ASSERT(MAP_SYSTEM_ZONES == NULL);
+                    MAP_SYSTEM_ZONES = m;
+                    LEN_SYSTEM_ZONES = numEntries;
+                    break;
+                case UCAL_ZONE_TYPE_CANONICAL:
+                    U_ASSERT(MAP_CANONICAL_SYSTEM_ZONES == NULL);
+                    MAP_CANONICAL_SYSTEM_ZONES = m;
+                    LEN_CANONICAL_SYSTEM_ZONES = numEntries;
+                    break;
+                case UCAL_ZONE_TYPE_CANONICAL_LOCATION:
+                    U_ASSERT(MAP_CANONICAL_SYSTEM_LOCATION_ZONES == NULL);
+                    MAP_CANONICAL_SYSTEM_LOCATION_ZONES = m;
+                    LEN_CANONICAL_SYSTEM_LOCATION_ZONES = numEntries;
+                    break;
+                }
+            }
+        }
+    }
+    ures_close(res);
+}
+
+
 /**
  * This is the default implementation for subclasses that do not
  * override this method.  This implementation calls through to the
@@ -659,11 +701,18 @@ private:
     // Map into to zones.  Our results are zone[map[i]] for
     // i=0..len-1, where zone[i] is the i-th Olson zone.  If map==NULL
     // then our results are zone[i] for i=0..len-1.  Len will be zero
-    // iff the zone data could not be loaded.
+    // if the zone data could not be loaded.
     int32_t* map;
+    int32_t* localMap;
     int32_t  len;
     int32_t  pos;
 
+    TZEnumeration(int32_t* mapData, int32_t mapLen, UBool adoptMapData) : pos(0) {
+        map = mapData;
+        localMap = adoptMapData ? mapData : NULL;
+        len = mapLen;
+    }
+
     UBool getID(int32_t i) {
         UErrorCode ec = U_ZERO_ERROR;
         int32_t idLen = 0;
@@ -681,119 +730,169 @@ private:
         return U_SUCCESS(ec);
     }
 
-public:
-    TZEnumeration() : map(NULL), len(0), pos(0) {
-        if (getOlsonMeta()) {
-            len = OLSON_ZONE_COUNT;
+    static int32_t* getMap(USystemTimeZoneType type, int32_t& len, UErrorCode& ec) {
+        len = 0;
+        if (U_FAILURE(ec)) {
+            return NULL;
+        }
+        int32_t* m = NULL;
+        switch (type) {
+        case UCAL_ZONE_TYPE_ANY:
+            umtx_initOnce(gSystemZonesInitOnce, &initMap, type, ec);
+            m = MAP_SYSTEM_ZONES;
+            len = LEN_SYSTEM_ZONES;
+            break;
+        case UCAL_ZONE_TYPE_CANONICAL:
+            umtx_initOnce(gCanonicalZonesInitOnce, &initMap, type, ec);
+            m = MAP_CANONICAL_SYSTEM_ZONES;
+            len = LEN_CANONICAL_SYSTEM_ZONES;
+            break;
+        case UCAL_ZONE_TYPE_CANONICAL_LOCATION:
+            umtx_initOnce(gCanonicalLocationZonesInitOnce, &initMap, type, ec);
+            m = MAP_CANONICAL_SYSTEM_LOCATION_ZONES;
+            len = LEN_CANONICAL_SYSTEM_LOCATION_ZONES;
+            break;
+        default:
+            ec = U_ILLEGAL_ARGUMENT_ERROR;
+            m = NULL;
+            len = 0;
+            break;
         }
+        return m;
     }
 
-    TZEnumeration(int32_t rawOffset) : map(NULL), len(0), pos(0) {
-        if (!getOlsonMeta()) {
-            return;
-        }
+public:
 
-        // Allocate more space than we'll need.  The end of the array will
-        // be blank.
-        map = (int32_t*)uprv_malloc(OLSON_ZONE_COUNT * sizeof(int32_t));
-        if (map == 0) {
-            return;
-        }
+#define DEFAULT_FILTERED_MAP_SIZE 8
+#define MAP_INCREMENT_SIZE 8
 
-        uprv_memset(map, 0, sizeof(int32_t) * OLSON_ZONE_COUNT);
-
-        UnicodeString s;
-        for (int32_t i=0; i<OLSON_ZONE_COUNT; ++i) {
-            if (getID(i)) {
-                // This is VERY inefficient.
-                TimeZone* z = TimeZone::createTimeZone(unistr);
-                // Make sure we get back the ID we wanted (if the ID is
-                // invalid we get back GMT).
-                if (z != 0 && z->getID(s) == unistr &&
-                    z->getRawOffset() == rawOffset) {
-                    map[len++] = i;
-                }
-                delete z;
-            }
+    static TZEnumeration* create(USystemTimeZoneType type, const char* region, const int32_t* rawOffset, UErrorCode& ec) {
+        if (U_FAILURE(ec)) {
+            return NULL;
         }
-    }
 
-    TZEnumeration(const char* country) : map(NULL), len(0), pos(0) {
-        if (!getOlsonMeta()) {
-            return;
+        int32_t baseLen;
+        int32_t *baseMap = getMap(type, baseLen, ec);
+
+        if (U_FAILURE(ec)) {
+            return NULL;
         }
 
-        UErrorCode ec = U_ZERO_ERROR;
-        UResourceBundle *res = ures_openDirect(0, kZONEINFO, &ec);
-        ures_getByKey(res, kREGIONS, res, &ec);
-        if (U_SUCCESS(ec) && ures_getType(res) == URES_ARRAY) {
-            UChar uCountry[] = {0, 0, 0, 0};
-            if (country) {
-                u_charsToUChars(country, uCountry, 2);
-            } else {
-                u_strcpy(uCountry, WORLD);
+        // If any additional conditions are available,
+        // create instance local map filtered by the conditions.
+
+        int32_t *filteredMap = NULL;
+        int32_t numEntries = 0;
+
+        if (region != NULL || rawOffset != NULL) {
+            int32_t filteredMapSize = DEFAULT_FILTERED_MAP_SIZE;
+            filteredMap = (int32_t *)uprv_malloc(filteredMapSize * sizeof(int32_t));
+            if (filteredMap == NULL) {
+                ec = U_MEMORY_ALLOCATION_ERROR;
+                return NULL;
             }
 
-            // count matches
-            int32_t count = 0;
-            int32_t i;
-            const UChar *region;
-            for (i = 0; i < ures_getSize(res); i++) {
-                region = ures_getStringByIndex(res, i, NULL, &ec);
+            // Walk through the base map
+            UResourceBundle *res = ures_openDirect(0, kZONEINFO, &ec);
+            res = ures_getByKey(res, kNAMES, res, &ec); // dereference Zones section
+            for (int32_t i = 0; i < baseLen; i++) {
+                int32_t zidx = baseMap[i];
+                UnicodeString id = ures_getUnicodeStringByIndex(res, zidx, &ec);
                 if (U_FAILURE(ec)) {
                     break;
                 }
-                if (u_strcmp(uCountry, region) == 0) {
-                    count++;
+                if (region != NULL) {
+                    // Filter by region
+                    char tzregion[4]; // max 3 letters + null term
+                    TimeZone::getRegion(id, tzregion, sizeof(tzregion), ec);
+                    if (U_FAILURE(ec)) {
+                        break;
+                    }
+                    if (uprv_stricmp(tzregion, region) != 0) {
+                        // region does not match
+                        continue;
+                    }
                 }
-            }
+                if (rawOffset != NULL) {
+                    // Filter by raw offset
+                    // Note: This is VERY inefficient
+                    TimeZone *z = createSystemTimeZone(id, ec);
+                    if (U_FAILURE(ec)) {
+                        break;
+                    }
+                    int32_t tzoffset = z->getRawOffset();
+                    delete z;
 
-            if (count > 0) {
-                map = (int32_t*)uprv_malloc(sizeof(int32_t) * count);
-                if (map != NULL) {
-                    int32_t idx = 0;
-                    for (i = 0; i < ures_getSize(res); i++) {
-                        region = ures_getStringByIndex(res, i, NULL, &ec);
-                        if (U_FAILURE(ec)) {
-                            break;
-                        }
-                        if (u_strcmp(uCountry, region) == 0) {
-                            map[idx++] = i;
-                        }
+                    if (tzoffset != *rawOffset) {
+                        continue;
                     }
-                    if (U_SUCCESS(ec)) {
-                        len = count;
+                }
+
+                if (filteredMapSize <= numEntries) {
+                    filteredMapSize += MAP_INCREMENT_SIZE;
+                    int32_t *tmp = (int32_t *)uprv_realloc(filteredMap, filteredMapSize * sizeof(int32_t));
+                    if (tmp == NULL) {
+                        ec = U_MEMORY_ALLOCATION_ERROR;
+                        break;
                     } else {
-                        uprv_free(map);
-                        map = NULL;
+                        filteredMap = tmp;
                     }
-                } else {
-                    U_DEBUG_TZ_MSG(("Failed to load tz for region %s: %s\n", country, u_errorName(ec)));
                 }
+
+                filteredMap[numEntries++] = zidx;
             }
+
+            if (U_FAILURE(ec)) {
+                uprv_free(filteredMap);
+                filteredMap = NULL;
+            }
+
+            ures_close(res);
         }
-        ures_close(res);
-    }
 
-  TZEnumeration(const TZEnumeration &other) : StringEnumeration(), map(NULL), len(0), pos(0) {
-        if(other.len > 0) {
-            if(other.map != NULL) {
-                map = (int32_t *)uprv_malloc(other.len * sizeof(int32_t));
-                if(map != NULL) {
-                    len = other.len;
-                    uprv_memcpy(map, other.map, len * sizeof(int32_t));
-                    pos = other.pos;
-                }
+        TZEnumeration *result = NULL;
+        if (U_SUCCESS(ec)) {
+            // Finally, create a new enumeration instance
+            if (filteredMap == NULL) {
+                result = new TZEnumeration(baseMap, baseLen, FALSE);
             } else {
+                result = new TZEnumeration(filteredMap, numEntries, TRUE);
+                filteredMap = NULL;
+            }
+            if (result == NULL) {
+                ec = U_MEMORY_ALLOCATION_ERROR;
+            }
+        }
+
+        if (filteredMap != NULL) {
+            uprv_free(filteredMap);
+        }
+
+        return result;
+    }
+
+    TZEnumeration(const TZEnumeration &other) : StringEnumeration(), map(NULL), localMap(NULL), len(0), pos(0) {
+        if (other.localMap != NULL) {
+            localMap = (int32_t *)uprv_malloc(other.len * sizeof(int32_t));
+            if (localMap != NULL) {
                 len = other.len;
+                uprv_memcpy(localMap, other.localMap, len * sizeof(int32_t));
                 pos = other.pos;
+                map = localMap;
+            } else {
+                len = 0;
+                pos = 0;
+                map = NULL;
             }
+        } else {
+            map = other.map;
+            localMap = NULL;
+            len = other.len;
+            pos = other.pos;
         }
     }
 
-    virtual ~TZEnumeration() {
-        uprv_free(map);
-    }
+    virtual ~TZEnumeration();
 
     virtual StringEnumeration *clone() const {
         return new TZEnumeration(*this);
@@ -804,8 +903,8 @@ public:
     }
 
     virtual const UnicodeString* snext(UErrorCode& status) {
-        if (U_SUCCESS(status) && pos < len) {
-            getID((map == 0) ? pos : map[pos]);
+        if (U_SUCCESS(status) && map != NULL && pos < len) {
+            getID(map[pos]);
             ++pos;
             return &unistr;
         }
@@ -821,21 +920,39 @@ public:
     virtual UClassID getDynamicClassID(void) const;
 };
 
+TZEnumeration::~TZEnumeration() {
+    if (localMap != NULL) {
+        uprv_free(localMap);
+    }
+}
+
 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(TZEnumeration)
 
 StringEnumeration* U_EXPORT2
+TimeZone::createTimeZoneIDEnumeration(
+            USystemTimeZoneType zoneType,
+            const char* region,
+            const int32_t* rawOffset,
+            UErrorCode& ec) {
+    return TZEnumeration::create(zoneType, region, rawOffset, ec);
+}
+
+StringEnumeration* U_EXPORT2
 TimeZone::createEnumeration() {
-    return new TZEnumeration();
+    UErrorCode ec = U_ZERO_ERROR;
+    return TZEnumeration::create(UCAL_ZONE_TYPE_ANY, NULL, NULL, ec);
 }
 
 StringEnumeration* U_EXPORT2
 TimeZone::createEnumeration(int32_t rawOffset) {
-    return new TZEnumeration(rawOffset);
+    UErrorCode ec = U_ZERO_ERROR;
+    return TZEnumeration::create(UCAL_ZONE_TYPE_ANY, NULL, &rawOffset, ec);
 }
 
 StringEnumeration* U_EXPORT2
 TimeZone::createEnumeration(const char* country) {
-    return new TZEnumeration(country);
+    UErrorCode ec = U_ZERO_ERROR;
+    return TZEnumeration::create(UCAL_ZONE_TYPE_ANY, country, NULL, ec);
 }
 
 // ---------------------------------------
@@ -878,7 +995,7 @@ TimeZone::getEquivalentID(const UnicodeString& id, int32_t index) {
         ures_getByKey(&res, kLINKS, &r, &ec);
         const int32_t* v = ures_getIntVector(&r, &size, &ec);
         if (U_SUCCESS(ec)) {
-            if (index >= 0 && index < size && getOlsonMeta()) {
+            if (index >= 0 && index < size) {
                 zone = v[index];
             }
         }
@@ -906,7 +1023,26 @@ TimeZone::getEquivalentID(const UnicodeString& id, int32_t index) {
 
 // ---------------------------------------
 
-// These two methods are used by ZoneMeta class only.
+// These methods are used by ZoneMeta class only.
+
+const UChar*
+TimeZone::findID(const UnicodeString& id) {
+    const UChar *result = NULL;
+    UErrorCode ec = U_ZERO_ERROR;
+    UResourceBundle *rb = ures_openDirect(NULL, kZONEINFO, &ec);
+
+    // resolve zone index by name
+    UResourceBundle *names = ures_getByKey(rb, kNAMES, NULL, &ec);
+    int32_t idx = findInStringArray(names, id, ec);
+    result = ures_getStringByIndex(names, idx, NULL, &ec);
+    if (U_FAILURE(ec)) {
+        result = NULL;
+    }
+    ures_close(names);
+    ures_close(rb);
+    return result;
+}
+
 
 const UChar*
 TimeZone::dereferOlsonLink(const UnicodeString& id) {
@@ -942,18 +1078,26 @@ TimeZone::dereferOlsonLink(const UnicodeString& id) {
 
 const UChar*
 TimeZone::getRegion(const UnicodeString& id) {
-    const UChar *result = WORLD;
-    UErrorCode ec = U_ZERO_ERROR;
-    UResourceBundle *rb = ures_openDirect(NULL, kZONEINFO, &ec);
+    UErrorCode status = U_ZERO_ERROR;
+    return getRegion(id, status);
+}
+
+const UChar*
+TimeZone::getRegion(const UnicodeString& id, UErrorCode& status) {
+    if (U_FAILURE(status)) {
+        return NULL;
+    }
+    const UChar *result = NULL;
+    UResourceBundle *rb = ures_openDirect(NULL, kZONEINFO, &status);
 
     // resolve zone index by name
-    UResourceBundle *res = ures_getByKey(rb, kNAMES, NULL, &ec);
-    int32_t idx = findInStringArray(res, id, ec);
+    UResourceBundle *res = ures_getByKey(rb, kNAMES, NULL, &status);
+    int32_t idx = findInStringArray(res, id, status);
 
     // get region mapping
-    ures_getByKey(rb, kREGIONS, res, &ec);
-    const UChar *tmp = ures_getStringByIndex(res, idx, NULL, &ec);
-    if (U_SUCCESS(ec)) {
+    ures_getByKey(rb, kREGIONS, res, &status);
+    const UChar *tmp = ures_getStringByIndex(res, idx, NULL, &status);
+    if (U_SUCCESS(status)) {
         result = tmp;
     }
 
@@ -963,6 +1107,39 @@ TimeZone::getRegion(const UnicodeString& id) {
     return result;
 }
 
+
+// ---------------------------------------
+int32_t
+TimeZone::getRegion(const UnicodeString& id, char *region, int32_t capacity, UErrorCode& status)
+{
+    int32_t resultLen = 0;
+    *region = 0;
+    if (U_FAILURE(status)) {
+        return 0;
+    }
+
+    const UChar *uregion = NULL;
+    // "Etc/Unknown" is not a system zone ID,
+    // but in the zone data
+    if (id.compare(UNKNOWN_ZONE_ID, UNKNOWN_ZONE_ID_LENGTH) != 0) {
+        uregion = getRegion(id);
+    }
+    if (uregion == NULL) {
+        status = U_ILLEGAL_ARGUMENT_ERROR;
+        return 0;
+    }
+    resultLen = u_strlen(uregion);
+    // A region code is represented by invariant characters
+    u_UCharsToChars(uregion, region, uprv_min(resultLen, capacity));
+
+    if (capacity < resultLen) {
+        status = U_BUFFER_OVERFLOW_ERROR;
+        return resultLen;
+    }
+
+    return u_terminateChars(region, capacity, resultLen, &status);
+}
+
 // ---------------------------------------
 
 
@@ -984,7 +1161,7 @@ TimeZone::getDisplayName(UBool daylight, EDisplayType style, UnicodeString& resu
     return getDisplayName(daylight,style, Locale::getDefault(), result);
 }
 //--------------------------------------
-int32_t 
+int32_t
 TimeZone::getDSTSavings()const {
     if (useDaylightTime()) {
         return 3600000;
@@ -995,126 +1172,95 @@ TimeZone::getDSTSavings()const {
 UnicodeString&
 TimeZone::getDisplayName(UBool daylight, EDisplayType style, const Locale& locale, UnicodeString& result) const
 {
-    // SRL TODO: cache the SDF, just like java.
     UErrorCode status = U_ZERO_ERROR;
-#ifdef U_DEBUG_TZ
-    char buf[128];
-    fID.extract(0, sizeof(buf)-1, buf, sizeof(buf), "");
-#endif
-
-    // select the proper format string
-    UnicodeString pat;
-    switch(style){
-    case LONG:
-        pat = ZZZZ_STR;
-        break;
-    case SHORT_GENERIC:
-        pat = V_STR;
-        break;
-    case LONG_GENERIC:
-        pat = VVVV_STR;
-        break;
-    case SHORT_GMT:
-        pat = Z_UC_STR;
-        break;
-    case LONG_GMT:
-        pat = ZZZZ_UC_STR;
-        break;
-    case SHORT_COMMONLY_USED:
-        //pat = V_UC_STR;
-        pat = Z_STR;
-        break;
-    case GENERIC_LOCATION:
-        pat = VVVV_UC_STR;
-        break;
-    default: // SHORT
-        //pat = Z_STR;
-        pat = V_UC_STR;
-        break;
-    }
+    UDate date = Calendar::getNow();
+    UTimeZoneFormatTimeType timeType;
+    int32_t offset;
 
-    SimpleDateFormat format(pat, locale, status);
-    U_DEBUG_TZ_MSG(("getDisplayName(%s)\n", buf));
-    if(!U_SUCCESS(status))
-    {
-#ifdef U_DEBUG_TZ
-      char buf2[128];
-      result.extract(0, sizeof(buf2)-1, buf2, sizeof(buf2), "");
-      U_DEBUG_TZ_MSG(("getDisplayName(%s) -> %s\n", buf, buf2));
-#endif
-      return result.remove();
-    }
-
-    UDate d = Calendar::getNow();
-    int32_t  rawOffset;
-    int32_t  dstOffset;
-    this->getOffset(d, FALSE, rawOffset, dstOffset, status);
-    if (U_FAILURE(status)) {
-        return result.remove();
-    }
-    
-    if ((daylight && dstOffset != 0) || 
-        (!daylight && dstOffset == 0) ||
-        (style == SHORT_GENERIC) ||
-        (style == LONG_GENERIC)
-       ) {
-        // Current time and the request (daylight / not daylight) agree.
-        format.setTimeZone(*this);
-        return format.format(d, result);
-    }
-    
-    // Create a new SimpleTimeZone as a stand-in for this zone; the
-    // stand-in will have no DST, or DST during July, but the same ID and offset,
-    // and hence the same display name.
-    // We don't cache these because they're small and cheap to create.
-    UnicodeString tempID;
-    getID(tempID);
-    SimpleTimeZone *tz = NULL;
-    if(daylight && useDaylightTime()){
-        // The display name for daylight saving time was requested, but currently not in DST
-        // Set a fixed date (July 1) in this Gregorian year
-        GregorianCalendar cal(*this, status);
+    if (style == GENERIC_LOCATION || style == LONG_GENERIC || style == SHORT_GENERIC) {
+        LocalPointer<TimeZoneFormat> tzfmt(TimeZoneFormat::createInstance(locale, status));
         if (U_FAILURE(status)) {
-            return result.remove();
+            result.remove();
+            return result;
         }
-        cal.set(UCAL_MONTH, UCAL_JULY);
-        cal.set(UCAL_DATE, 1);
-        
-        // Get July 1 date
-        d = cal.getTime(status);
-        
-        // Check if it is in DST
-        if (cal.get(UCAL_DST_OFFSET, status) == 0) {
-            // We need to create a fake time zone
-            tz = new SimpleTimeZone(rawOffset, tempID,
-                                UCAL_JUNE, 1, 0, 0,
-                                UCAL_AUGUST, 1, 0, 0,
-                                getDSTSavings(), status);
-            if (U_FAILURE(status) || tz == NULL) {
-                if (U_SUCCESS(status)) {
-                    status = U_MEMORY_ALLOCATION_ERROR;
-                }
-                return result.remove();
+        // Generic format
+        switch (style) {
+        case GENERIC_LOCATION:
+            tzfmt->format(UTZFMT_STYLE_GENERIC_LOCATION, *this, date, result, &timeType);
+            break;
+        case LONG_GENERIC:
+            tzfmt->format(UTZFMT_STYLE_GENERIC_LONG, *this, date, result, &timeType);
+            break;
+        case SHORT_GENERIC:
+            tzfmt->format(UTZFMT_STYLE_GENERIC_SHORT, *this, date, result, &timeType);
+            break;
+        default:
+            U_ASSERT(FALSE);
+        }
+        // Generic format many use Localized GMT as the final fallback.
+        // When Localized GMT format is used, the result might not be
+        // appropriate for the requested daylight value.
+        if ((daylight && timeType == UTZFMT_TIME_TYPE_STANDARD) || (!daylight && timeType == UTZFMT_TIME_TYPE_DAYLIGHT)) {
+            offset = daylight ? getRawOffset() + getDSTSavings() : getRawOffset();
+            if (style == SHORT_GENERIC) {
+                tzfmt->formatOffsetShortLocalizedGMT(offset, result, status);
+            } else {
+                tzfmt->formatOffsetLocalizedGMT(offset, result, status);
             }
-            format.adoptTimeZone(tz);
-        } else {
-            format.setTimeZone(*this);
         }
+    } else if (style == LONG_GMT || style == SHORT_GMT) {
+        LocalPointer<TimeZoneFormat> tzfmt(TimeZoneFormat::createInstance(locale, status));
+        if (U_FAILURE(status)) {
+            result.remove();
+            return result;
+        }
+        offset = daylight && useDaylightTime() ? getRawOffset() + getDSTSavings() : getRawOffset();
+        switch (style) {
+        case LONG_GMT:
+            tzfmt->formatOffsetLocalizedGMT(offset, result, status);
+            break;
+        case SHORT_GMT:
+            tzfmt->formatOffsetISO8601Basic(offset, FALSE, FALSE, FALSE, result, status);
+            break;
+        default:
+            U_ASSERT(FALSE);
+        }
+
     } else {
-        // The display name for standard time was requested, but currently in DST
-        // or display name for daylight saving time was requested, but this zone no longer
-        // observes DST.
-        tz = new SimpleTimeZone(rawOffset, tempID);
-        if (U_FAILURE(status) || tz == NULL) {
-            if (U_SUCCESS(status)) {
-                status = U_MEMORY_ALLOCATION_ERROR;
+        U_ASSERT(style == LONG || style == SHORT || style == SHORT_COMMONLY_USED);
+        UTimeZoneNameType nameType = UTZNM_UNKNOWN;
+        switch (style) {
+        case LONG:
+            nameType = daylight ? UTZNM_LONG_DAYLIGHT : UTZNM_LONG_STANDARD;
+            break;
+        case SHORT:
+        case SHORT_COMMONLY_USED:
+            nameType = daylight ? UTZNM_SHORT_DAYLIGHT : UTZNM_SHORT_STANDARD;
+            break;
+        default:
+            U_ASSERT(FALSE);
+        }
+        LocalPointer<TimeZoneNames> tznames(TimeZoneNames::createInstance(locale, status));
+        if (U_FAILURE(status)) {
+            result.remove();
+            return result;
+        }
+        UnicodeString canonicalID(ZoneMeta::getCanonicalCLDRID(*this));
+        tznames->getDisplayName(canonicalID, nameType, date, result);
+        if (result.isEmpty()) {
+            // Fallback to localized GMT
+            LocalPointer<TimeZoneFormat> tzfmt(TimeZoneFormat::createInstance(locale, status));
+            offset = daylight && useDaylightTime() ? getRawOffset() + getDSTSavings() : getRawOffset();
+            if (style == LONG) {
+                tzfmt->formatOffsetLocalizedGMT(offset, result, status);
+            } else {
+                tzfmt->formatOffsetShortLocalizedGMT(offset, result, status);
             }
-            return result.remove();
         }
-        format.adoptTimeZone(tz);
     }
-
-    format.format(d, result, status);
+    if (U_FAILURE(status)) {
+        result.remove();
+    }
     return  result;
 }
 
@@ -1158,10 +1304,10 @@ TimeZone::parseCustomID(const UnicodeString& id, int32_t& sign,
 
     NumberFormat* numberFormat = 0;
     UnicodeString idUppercase = id;
-    idUppercase.toUpper();
+    idUppercase.toUpper("");
 
     if (id.length() > GMT_ID_LENGTH &&
-        idUppercase.startsWith(GMT_ID))
+        idUppercase.startsWith(GMT_ID, GMT_ID_LENGTH))
     {
         ParsePosition pos(GMT_ID_LENGTH);
         sign = 1;
@@ -1182,6 +1328,7 @@ TimeZone::parseCustomID(const UnicodeString& id, int32_t& sign,
             return FALSE;
         }
         numberFormat->setParseIntegerOnly(TRUE);
+        //numberFormat->setLenient(TRUE); // TODO: May need to set this, depends on latest timezone parsing
 
         // Look for either hh:mm, hhmm, or hh
         int32_t start = pos.getIndex();
@@ -1276,7 +1423,7 @@ UnicodeString&
 TimeZone::formatCustomID(int32_t hour, int32_t min, int32_t sec,
                          UBool negative, UnicodeString& id) {
     // Create time zone ID - GMT[+|-]hhmm[ss]
-    id.setTo(GMT_ID);
+    id.setTo(GMT_ID, GMT_ID_LENGTH);
     if (hour | min | sec) {
         if (negative) {
             id += (UChar)MINUS;
@@ -1312,44 +1459,34 @@ TimeZone::formatCustomID(int32_t hour, int32_t min, int32_t sec,
 }
 
 
-UBool 
+UBool
 TimeZone::hasSameRules(const TimeZone& other) const
 {
-    return (getRawOffset() == other.getRawOffset() && 
+    return (getRawOffset() == other.getRawOffset() &&
             useDaylightTime() == other.useDaylightTime());
 }
 
-const char*
-TimeZone::getTZDataVersion(UErrorCode& status)
-{
-    /* This is here to prevent race conditions. */
-    UBool needsInit;
-    UMTX_CHECK(&LOCK, !TZDataVersionInitialized, needsInit);
-    if (needsInit) {
-        int32_t len = 0;
-        UResourceBundle *bundle = ures_openDirect(NULL, kZONEINFO, &status);
-        const UChar *tzver = ures_getStringByKey(bundle, kTZVERSION,
-            &len, &status);
+static void U_CALLCONV initTZDataVersion(UErrorCode &status) {
+    ucln_i18n_registerCleanup(UCLN_I18N_TIMEZONE, timeZone_cleanup);
+    int32_t len = 0;
+    UResourceBundle *bundle = ures_openDirect(NULL, kZONEINFO, &status);
+    const UChar *tzver = ures_getStringByKey(bundle, kTZVERSION, &len, &status);
 
-        if (U_SUCCESS(status)) {
-            if (len >= (int32_t)sizeof(TZDATA_VERSION)) {
-                // Ensure that there is always space for a trailing nul in TZDATA_VERSION
-                len = sizeof(TZDATA_VERSION) - 1;
-            }
-            umtx_lock(&LOCK);
-            if (!TZDataVersionInitialized) {
-                u_UCharsToChars(tzver, TZDATA_VERSION, len);
-                TZDataVersionInitialized = TRUE;
-            }
-            umtx_unlock(&LOCK);
-            ucln_i18n_registerCleanup(UCLN_I18N_TIMEZONE, timeZone_cleanup);
+    if (U_SUCCESS(status)) {
+        if (len >= (int32_t)sizeof(TZDATA_VERSION)) {
+            // Ensure that there is always space for a trailing nul in TZDATA_VERSION
+            len = sizeof(TZDATA_VERSION) - 1;
         }
-
-        ures_close(bundle);
-    }
-    if (U_FAILURE(status)) {
-        return NULL;
+        u_UCharsToChars(tzver, TZDATA_VERSION, len);
     }
+    ures_close(bundle);
+
+}
+
+const char*
+TimeZone::getTZDataVersion(UErrorCode& status)
+{
+    umtx_initOnce(gTZDataVersionInitOnce, &initTZDataVersion, status);
     return (const char*)TZDATA_VERSION;
 }
 
@@ -1369,17 +1506,162 @@ TimeZone::getCanonicalID(const UnicodeString& id, UnicodeString& canonicalID, UB
     if (U_FAILURE(status)) {
         return canonicalID;
     }
-    ZoneMeta::getCanonicalSystemID(id, canonicalID, status);
-    if (U_SUCCESS(status)) {
-        isSystemID = TRUE;
+    if (id.compare(UNKNOWN_ZONE_ID, UNKNOWN_ZONE_ID_LENGTH) == 0) {
+        // special case - Etc/Unknown is a canonical ID, but not system ID
+        canonicalID.fastCopyFrom(id);
+        isSystemID = FALSE;
     } else {
-        // Not a system ID
-        status = U_ZERO_ERROR;
-        getCustomID(id, canonicalID, status);
+        ZoneMeta::getCanonicalCLDRID(id, canonicalID, status);
+        if (U_SUCCESS(status)) {
+            isSystemID = TRUE;
+        } else {
+            // Not a system ID
+            status = U_ZERO_ERROR;
+            getCustomID(id, canonicalID, status);
+        }
     }
     return canonicalID;
 }
 
+#ifndef U_HIDE_DRAFT_API
+UnicodeString&
+TimeZone::getWindowsID(const UnicodeString& id, UnicodeString& winid, UErrorCode& status) {
+    winid.remove();
+    if (U_FAILURE(status)) {
+        return winid;
+    }
+
+    // canonicalize the input ID
+    UnicodeString canonicalID;
+    UBool isSystemID = FALSE;
+
+    getCanonicalID(id, canonicalID, isSystemID, status);
+    if (U_FAILURE(status) || !isSystemID) {
+        // mapping data is only applicable to tz database IDs
+        return winid;
+    }
+
+    UResourceBundle *mapTimezones = ures_openDirect(NULL, "windowsZones", &status);
+    ures_getByKey(mapTimezones, "mapTimezones", mapTimezones, &status);
+
+    if (U_FAILURE(status)) {
+        return winid;
+    }
+
+    UResourceBundle *winzone = NULL;
+    UBool found = FALSE;
+    while (ures_hasNext(mapTimezones) && !found) {
+        winzone = ures_getNextResource(mapTimezones, winzone, &status);
+        if (U_FAILURE(status)) {
+            break;
+        }
+        if (ures_getType(winzone) != URES_TABLE) {
+            continue;
+        }
+        UResourceBundle *regionalData = NULL;
+        while (ures_hasNext(winzone) && !found) {
+            regionalData = ures_getNextResource(winzone, regionalData, &status);
+            if (U_FAILURE(status)) {
+                break;
+            }
+            if (ures_getType(regionalData) != URES_STRING) {
+                continue;
+            }
+            int32_t len;
+            const UChar *tzids = ures_getString(regionalData, &len, &status);
+            if (U_FAILURE(status)) {
+                break;
+            }
+
+            const UChar *start = tzids;
+            UBool hasNext = TRUE;
+            while (hasNext) {
+                const UChar *end = u_strchr(start, (UChar)0x20);
+                if (end == NULL) {
+                    end = tzids + len;
+                    hasNext = FALSE;
+                }
+                if (canonicalID.compare(start, end - start) == 0) {
+                    winid = UnicodeString(ures_getKey(winzone), -1 , US_INV);
+                    found = TRUE;
+                    break;
+                }
+                start = end + 1;
+            }
+        }
+        ures_close(regionalData);
+    }
+    ures_close(winzone);
+    ures_close(mapTimezones);
+
+    return winid;
+}
+
+#define MAX_WINDOWS_ID_SIZE 128
+
+UnicodeString&
+TimeZone::getIDForWindowsID(const UnicodeString& winid, const char* region, UnicodeString& id, UErrorCode& status) {
+    id.remove();
+    if (U_FAILURE(status)) {
+        return id;
+    }
+
+    UResourceBundle *zones = ures_openDirect(NULL, "windowsZones", &status);
+    ures_getByKey(zones, "mapTimezones", zones, &status);
+    if (U_FAILURE(status)) {
+        ures_close(zones);
+        return id;
+    }
+
+    UErrorCode tmperr = U_ZERO_ERROR;
+    char winidKey[MAX_WINDOWS_ID_SIZE];
+    int32_t winKeyLen = winid.extract(0, winid.length(), winidKey, sizeof(winidKey) - 1, US_INV);
+
+    if (winKeyLen == 0 || winKeyLen >= (int32_t)sizeof(winidKey)) {
+        ures_close(zones);
+        return id;
+    }
+    winidKey[winKeyLen] = 0;
+
+    ures_getByKey(zones, winidKey, zones, &tmperr); // use tmperr, because windows mapping might not
+                                                    // be avaiable by design
+    if (U_FAILURE(tmperr)) {
+        ures_close(zones);
+        return id;
+    }
+
+    const UChar *tzid = NULL;
+    int32_t len = 0;
+    UBool gotID = FALSE;
+    if (region) {
+        const UChar *tzids = ures_getStringByKey(zones, region, &len, &tmperr); // use tmperr, because
+                                                                                // regional mapping is optional
+        if (U_SUCCESS(tmperr)) {
+            // first ID delimited by space is the defasult one
+            const UChar *end = u_strchr(tzids, (UChar)0x20);
+            if (end == NULL) {
+                id.setTo(tzids, -1);
+            } else {
+                id.setTo(tzids, end - tzids);
+            }
+            gotID = TRUE;
+        }
+    }
+
+    if (!gotID) {
+        tzid = ures_getStringByKey(zones, "001", &len, &status);    // using status, because "001" must be
+                                                                // available at this point
+        if (U_SUCCESS(status)) {
+            id.setTo(tzid, len);
+        }
+    }
+
+    ures_close(zones);
+    return id;
+}
+#endif /* U_HIDE_DRAFT_API */
+
+
 U_NAMESPACE_END
 
 #endif /* #if !UCONFIG_NO_FORMATTING */