+// © 2016 and later: Unicode, Inc. and others.
+// License & terms of use: http://www.unicode.org/copyright.html
/*
*******************************************************************************
-* Copyright (C) 1997-2006, International Business Machines Corporation and *
-* others. All Rights Reserved. *
+* Copyright (C) 1997-2016, International Business Machines Corporation and
+* others. All Rights Reserved.
*******************************************************************************
*
* File TIMEZONE.CPP
* available IDs code. Misc. cleanup.
*********************************************************************************/
+#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>
#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"
#include "cmemory.h"
#include "unicode/strenum.h"
#include "uassert.h"
+#include "zonemeta.h"
-#define kZONEINFO "zoneinfo"
+#define kZONEINFO "zoneinfo64"
#define kREGIONS "Regions"
#define kZONES "Zones"
#define kRULES "Rules"
#define kNAMES "Names"
-#define kDEFAULT "Default"
+#define kTZVERSION "TZVersion"
+#define kLINKS "links"
+#define kMAX_CUSTOM_HOUR 23
+#define kMAX_CUSTOM_MIN 59
+#define kMAX_CUSTOM_SEC 59
+#define MINUS 0x002D
+#define PLUS 0x002B
+#define ZERO_DIGIT 0x0030
+#define COLON 0x003A
// Static data and constants
+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 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 UChar CUSTOM_ID[] =
-{
- 0x43, 0x75, 0x73, 0x74, 0x6F, 0x6D, 0x00 /* "Custom" */
-};
+static const int32_t UNKNOWN_ZONE_ID_LENGTH = 11;
-static UMTX LOCK;
-static TimeZone* DEFAULT_ZONE = NULL;
-static TimeZone* _GMT = NULL; // cf. TimeZone::GMT
+static icu::TimeZone* DEFAULT_ZONE = NULL;
+static icu::UInitOnce gDefaultZoneInitOnce = U_INITONCE_INITIALIZER;
-#ifdef U_USE_TIMEZONE_OBSOLETE_2_8
-static UnicodeString* OLSON_IDS = 0;
-#endif
+alignas(icu::SimpleTimeZone)
+static char gRawGMT[sizeof(icu::SimpleTimeZone)];
+
+alignas(icu::SimpleTimeZone)
+static char gRawUNKNOWN[sizeof(icu::SimpleTimeZone)];
+
+static icu::UInitOnce gStaticZonesInitOnce = U_INITONCE_INITIALIZER;
+static UBool gStaticZonesInitialized = FALSE; // Whether the static zones are initialized and ready to use.
+
+static char TZDATA_VERSION[16];
+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)
{
-#ifdef U_USE_TIMEZONE_OBSOLETE_2_8
- delete []OLSON_IDS;
- OLSON_IDS = 0;
-#endif
-
+ U_NAMESPACE_USE
delete DEFAULT_ZONE;
DEFAULT_ZONE = NULL;
+ gDefaultZoneInitOnce.reset();
- delete _GMT;
- _GMT = NULL;
-
- if (LOCK) {
- umtx_destroy(&LOCK);
- LOCK = NULL;
+ if (gStaticZonesInitialized) {
+ reinterpret_cast<SimpleTimeZone*>(gRawGMT)->~SimpleTimeZone();
+ reinterpret_cast<SimpleTimeZone*>(gRawUNKNOWN)->~SimpleTimeZone();
+ gStaticZonesInitialized = FALSE;
+ gStaticZonesInitOnce.reset();
}
- return TRUE;
-}
-U_CDECL_END
+ uprv_memset(TZDATA_VERSION, 0, sizeof(TZDATA_VERSION));
+ gTZDataVersionInitOnce.reset();
-U_NAMESPACE_BEGIN
+ LEN_SYSTEM_ZONES = 0;
+ uprv_free(MAP_SYSTEM_ZONES);
+ MAP_SYSTEM_ZONES = 0;
+ gSystemZonesInitOnce.reset();
-/**
- * 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_START = -1; // starting index of zones
-static int32_t OLSON_ZONE_COUNT = 0; // count of zones
+ LEN_CANONICAL_SYSTEM_ZONES = 0;
+ uprv_free(MAP_CANONICAL_SYSTEM_ZONES);
+ MAP_CANONICAL_SYSTEM_ZONES = 0;
+ gCanonicalZonesInitOnce.reset();
-/**
- * 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_START < 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);
- OLSON_ZONE_START = 0;
- U_DEBUG_TZ_MSG(("OZC%d OZS%d\n",OLSON_ZONE_COUNT, OLSON_ZONE_START));
- }
- ures_close(&res);
- }
- return (OLSON_ZONE_START >= 0);
-}
+ LEN_CANONICAL_SYSTEM_LOCATION_ZONES = 0;
+ uprv_free(MAP_CANONICAL_SYSTEM_LOCATION_ZONES);
+ MAP_CANONICAL_SYSTEM_LOCATION_ZONES = 0;
+ gCanonicalLocationZonesInitOnce.reset();
-/**
- * Load up the Olson meta-data. Return TRUE if successful.
- */
-static UBool getOlsonMeta() {
- if (OLSON_ZONE_START < 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_START >= 0);
+ return TRUE;
}
+U_CDECL_END
+
+U_NAMESPACE_BEGIN
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? */
}
/**
- * 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()'
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;
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 {
UResourceBundle& res,
UErrorCode& ec)
{
-#if U_DEBUG_TZ
+#ifdef U_DEBUG_TZ
char buf[128];
id.extract(0, sizeof(buf)-1, buf, sizeof(buf), "");
#endif
// 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_getSize(&res) <= 1 && 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
return top;
}
-#ifdef U_USE_TIMEZONE_OBSOLETE_2_8
-
-/**
- * Load all the ids from the "zoneinfo" resource bundle into a static
- * array that we hang onto. This is _only_ used to implement the
- * deprecated createAvailableIDs() API.
- */
-static UBool loadOlsonIDs() {
- if (OLSON_IDS != 0) {
- return TRUE;
- }
+// -------------------------------------
- UErrorCode ec = U_ZERO_ERROR;
- UnicodeString* ids = 0;
- int32_t count = 0;
- UResourceBundle *top = ures_openDirect(0, kZONEINFO, &ec);
- UResourceBundle *nres = ures_getByKey(top, kNAMES, NULL, &ec); // dereference Names section
- if (U_SUCCESS(ec)) {
- getOlsonMeta(top);
- int32_t start = 0;
- count = ures_getSize(nres);
- ids = new UnicodeString[(count > 0) ? count : 1];
- for (int32_t i=0; i<count; ++i) {
- int32_t idLen = 0;
- const UChar* id = ures_getStringByIndex(nres, i, &idLen, &ec);
- ids[i].fastCopyFrom(UnicodeString(TRUE, id, idLen));
- if (U_FAILURE(ec)) {
- break;
- }
- }
- }
- ures_close(nres);
- ures_close(top);
+namespace {
- if (U_FAILURE(ec)) {
- delete[] ids;
- return FALSE;
- }
+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.
+ ucln_i18n_registerCleanup(UCLN_I18N_TIMEZONE, timeZone_cleanup);
- // Keep mutexed operations as short as possible by doing all
- // computations first, then doing pointer copies within the mutex.
- umtx_lock(&LOCK);
- if (OLSON_IDS == 0) {
- OLSON_IDS = ids;
- ids = 0;
- ucln_i18n_registerCleanup(UCLN_I18N_TIMEZONE, timeZone_cleanup);
- }
- umtx_unlock(&LOCK);
+ // new can't fail below, as we use placement new into staticly allocated space.
+ new(gRawGMT) SimpleTimeZone(0, UnicodeString(TRUE, GMT_ID, GMT_ID_LENGTH));
+ new(gRawUNKNOWN) SimpleTimeZone(0, UnicodeString(TRUE, UNKNOWN_ZONE_ID, UNKNOWN_ZONE_ID_LENGTH));
- // If another thread initialized the statics first, then delete
- // our unused data.
- delete[] ids;
- return TRUE;
+ gStaticZonesInitialized = TRUE;
}
-#endif
+} // anonymous namespace
-// -------------------------------------
+const TimeZone& U_EXPORT2
+TimeZone::getUnknown()
+{
+ umtx_initOnce(gStaticZonesInitOnce, &initStaticTimeZones);
+ return *reinterpret_cast<SimpleTimeZone*>(gRawUNKNOWN);
+}
const TimeZone* U_EXPORT2
TimeZone::getGMT(void)
{
- umtx_init(&LOCK); /* This is here to prevent race conditions. */
- Mutex lock(&LOCK);
- // Initialize _GMT independently of other static data; it should
- // be valid even if we can't load the time zone UDataMemory.
- if (_GMT == 0) {
- _GMT = new SimpleTimeZone(0, UnicodeString(GMT_ID, GMT_ID_LENGTH));
- ucln_i18n_registerCleanup(UCLN_I18N_TIMEZONE, timeZone_cleanup);
- }
- return _GMT;
+ umtx_initOnce(gStaticZonesInitOnce, &initStaticTimeZones);
+ return reinterpret_cast<SimpleTimeZone*>(gRawGMT);
}
// *****************************************************************************
UBool
TimeZone::operator==(const TimeZone& that) const
{
- return getDynamicClassID() == that.getDynamicClassID() &&
+ return typeid(*this) == typeid(that) &&
fID == that.fID;
}
// -------------------------------------
+namespace {
+TimeZone*
+createSystemTimeZone(const UnicodeString& id, UErrorCode& ec) {
+ if (U_FAILURE(ec)) {
+ return NULL;
+ }
+ TimeZone* z = 0;
+ StackUResourceBundle res;
+ U_DEBUG_TZ_MSG(("pre-err=%s\n", u_errorName(ec)));
+ UResourceBundle *top = openOlsonResource(id, res.ref(), ec);
+ U_DEBUG_TZ_MSG(("post-err=%s\n", u_errorName(ec)));
+ if (U_SUCCESS(ec)) {
+ z = new OlsonTimeZone(top, res.getAlias(), id, ec);
+ if (z == NULL) {
+ ec = U_MEMORY_ALLOCATION_ERROR;
+ U_DEBUG_TZ_MSG(("cstz: olson time zone failed to initialize - err %s\n", u_errorName(ec)));
+ }
+ }
+ ures_close(top);
+ if (U_FAILURE(ec)) {
+ U_DEBUG_TZ_MSG(("cstz: failed to create, err %s\n", u_errorName(ec)));
+ delete z;
+ z = NULL;
+ }
+ 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)
{
*/
TimeZone* result = createSystemTimeZone(ID);
- if (result == 0) {
+ if (result == NULL) {
U_DEBUG_TZ_MSG(("failed to load system time zone with id - falling to custom"));
result = createCustomTimeZone(ID);
}
- if (result == 0) {
- U_DEBUG_TZ_MSG(("failed to load time zone with id - falling to GMT"));
- result = getGMT()->clone();
+ if (result == NULL) {
+ U_DEBUG_TZ_MSG(("failed to load time zone with id - falling to Etc/Unknown(GMT)"));
+ const TimeZone& unknown = getUnknown();
+ // Unknown zone uses staticly allocated memory, so creation of it can never fail due to OOM.
+ result = unknown.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.
- * Upon return, DEFAULT_ZONE will not be NULL, unless operator new()
- * returns NULL.
- *
- * Must be called OUTSIDE mutex.
- */
-void
-TimeZone::initDefault()
-{
- // We access system timezone data through TPlatformUtilities,
- // including tzset(), timezone, and tzname[].
+TimeZone* U_EXPORT2
+TimeZone::detectHostTimeZone()
+{
+ // We access system timezone data through uprv_tzset(), uprv_tzname(), and others,
+ // which have platform specific implementations in putil.cpp
int32_t rawOffset = 0;
const char *hostID;
+ UBool hostDetectionSucceeded = TRUE;
// First, try to create a system timezone, based
// on the string ID in tzname[0].
- {
- // NOTE: Global mutex here; TimeZone mutex above
- // mutexed to avoid threading issues in the platform fcns.
- // Some of the locale/timezone OS functions may not be thread safe,
- // so the intent is that any setting from anywhere within ICU
- // happens with the ICU global mutex held.
- Mutex lock;
- 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;
-
- /* Make sure that the string is NULL terminated to prevent BoundsChecker/Purify warnings. */
+
+ uprv_tzset(); // Initialize tz... system data
+
+ uprv_tzname_clear_cache();
+
+ // Get the timezone ID from the host. This function should do
+ // any required host-specific remapping; e.g., on Windows this
+ // function maps the Windows Time Zone name to an ICU timezone ID.
+ hostID = uprv_tzname(0);
+
+ // Invert sign because UNIX semantics are backwards
+ rawOffset = uprv_timezone() * -U_MILLIS_PER_SECOND;
+
+ TimeZone* hostZone = NULL;
+
UnicodeString hostStrID(hostID, -1, US_INV);
- hostStrID.append((UChar)0);
- hostStrID.truncate(hostStrID.length()-1);
- default_zone = createSystemTimeZone(hostStrID);
+
+ if (hostStrID.length() == 0) {
+ // The host time zone detection (or remapping) above has failed and
+ // we have no name at all. Fallback to using the Unknown zone.
+ hostStrID = UnicodeString(TRUE, UNKNOWN_ZONE_ID, UNKNOWN_ZONE_ID_LENGTH);
+ hostDetectionSucceeded = FALSE;
+ }
+
+ hostZone = createSystemTimeZone(hostStrID);
+
+#if U_PLATFORM_USES_ONLY_WIN32_API
+ // hostID points to a heap-allocated location on Windows.
+ uprv_free(const_cast<char *>(hostID));
+#endif
int32_t hostIDLen = hostStrID.length();
- if (default_zone != NULL && rawOffset != default_zone->getRawOffset()
+ if (hostZone != NULL && rawOffset != hostZone->getRawOffset()
&& (3 <= hostIDLen && hostIDLen <= 4))
{
// Uh oh. This probably wasn't a good id.
// It was probably an ambiguous abbreviation
- delete default_zone;
- default_zone = NULL;
- }
-
-#if 0
- // NOTE: As of ICU 2.8, we no longer have an offsets table, since
- // historical zones can change offset over time. If we add
- // build-time heuristics to infer the "most frequent" raw offset
- // of a zone, we can build tables and institute defaults, as done
- // in ICU <= 2.6.
-
- // If we couldn't get the time zone ID from the host, use
- // the default host timezone offset. Further refinements
- // to this include querying the host to determine if DST
- // is in use or not and possibly using the host locale to
- // select from multiple zones at a the same offset. We
- // don't do any of this now, but we could easily add this.
- if (default_zone == NULL) {
- // Use the designated default in the time zone list that has the
- // appropriate GMT offset, if there is one.
-
- const OffsetIndex* index = INDEX_BY_OFFSET;
-
- for (;;) {
- if (index->gmtOffset > rawOffset) {
- // Went past our desired offset; no match found
- break;
- }
- if (index->gmtOffset == rawOffset) {
- // Found our desired offset
- default_zone = createSystemTimeZone(ZONE_IDS[index->defaultZone]);
- break;
- }
- // Compute the position of the next entry. If the delta value
- // in this entry is zero, then there is no next entry.
- uint16_t delta = index->nextEntryDelta;
- if (delta == 0) {
- break;
- }
- index = (const OffsetIndex*)((int8_t*)index + delta);
- }
+ delete hostZone;
+ hostZone = NULL;
}
-#endif
// Construct a fixed standard zone with the host's ID
// and raw offset.
- if (default_zone == NULL) {
- default_zone = new SimpleTimeZone(rawOffset, hostStrID);
+ if (hostZone == NULL && hostDetectionSucceeded) {
+ hostZone = new SimpleTimeZone(rawOffset, hostStrID);
}
- // If we _still_ don't have a time zone, use GMT.
- if (default_zone == NULL) {
- default_zone = getGMT()->clone();
+ // If we _still_ don't have a time zone, use the Unknown zone.
+ //
+ // Note: This is extremely unlikely situation. If
+ // new SimpleTimeZone(...) above fails, the following
+ // code may also fail.
+ if (hostZone == NULL) {
+ // Unknown zone uses static allocated memory, so it must always exist.
+ // However, clone() allocates memory and can fail.
+ hostZone = TimeZone::getUnknown().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);
+ return hostZone;
+}
+
+// -------------------------------------
+
+/**
+ * Initialize DEFAULT_ZONE from the system default time zone.
+ * Upon return, DEFAULT_ZONE will not be NULL, unless operator new()
+ * returns NULL.
+ */
+static void U_CALLCONV initDefault()
+{
+ ucln_i18n_registerCleanup(UCLN_I18N_TIMEZONE, timeZone_cleanup);
- delete default_zone;
+ // If setDefault() has already been called we can skip getting the
+ // default zone information from the system.
+ if (DEFAULT_ZONE != NULL) {
+ return;
+ }
+
+ // 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.
+
+ // The code detecting the host time zone was separated from this
+ // and implemented as TimeZone::detectHostTimeZone()
+
+ TimeZone *default_zone = TimeZone::detectHostTimeZone();
+
+ // 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;
}
// -------------------------------------
TimeZone* U_EXPORT2
TimeZone::createDefault()
{
- umtx_init(&LOCK); /* This is here to prevent race conditions. */
- umtx_lock(&LOCK);
- UBool f = (DEFAULT_ZONE != 0);
- umtx_unlock(&LOCK);
- if (!f) {
- initDefault();
- }
-
- Mutex lock(&LOCK); // In case adoptDefault is called
- return DEFAULT_ZONE->clone();
+ umtx_initOnce(gDefaultZoneInitOnce, initDefault);
+ return (DEFAULT_ZONE != NULL) ? DEFAULT_ZONE->clone() : NULL;
}
// -------------------------------------
{
if (zone != NULL)
{
- TimeZone* old = NULL;
-
- umtx_init(&LOCK); /* This is here to prevent race conditions. */
- 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);
}
//----------------------------------------------------------------------
+
+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
}
rawOffset = getRawOffset();
-
- // Convert to local wall millis if necessary
if (!local) {
date += rawOffset; // now in local standard millis
}
- // When local==FALSE, we might have to recompute. This loop is
- // executed once, unless a recomputation is required; then it is
- // executed twice.
+ // When local == TRUE, date might not be in local standard
+ // millis. getOffset taking 7 parameters used here assume
+ // the given time in day is local standard time.
+ // At STD->DST transition, there is a range of time which
+ // does not exist. When 'date' is in this time range
+ // (and local == TRUE), this method interprets the specified
+ // local time as DST. At DST->STD transition, there is a
+ // range of time which occurs twice. In this case, this
+ // method interprets the specified local time as STD.
+ // To support the behavior above, we need to call getOffset
+ // (with 7 args) twice when local == true and DST is
+ // detected in the initial call.
for (int32_t pass=0; ; ++pass) {
int32_t year, month, dom, dow;
double day = uprv_floor(date / U_MILLIS_PER_DAY);
int32_t millis = (int32_t) (date - day * U_MILLIS_PER_DAY);
-
+
Grego::dayToFields(day, year, month, dom, dow);
-
+
dstOffset = getOffset(GregorianCalendar::AD, year, month, dom,
(uint8_t) dow, millis,
Grego::monthLength(year, month),
ec) - rawOffset;
- // Recompute if local==FALSE, dstOffset!=0, and addition of
- // the dstOffset puts us in a different day.
- if (pass!=0 || local || dstOffset==0) {
- break;
- }
- date += dstOffset;
- if (uprv_floor(date / U_MILLIS_PER_DAY) == day) {
+ // Recompute if local==TRUE, dstOffset!=0.
+ if (pass!=0 || !local || dstOffset == 0) {
break;
}
+ // adjust to local standard millis
+ date -= dstOffset;
}
}
// 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;
- UBool getID(int32_t i) {
- UErrorCode ec = U_ZERO_ERROR;
+ 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) {
int32_t idLen = 0;
const UChar* id = NULL;
UResourceBundle *top = ures_openDirect(0, kZONEINFO, &ec);
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:
+
+#define DEFAULT_FILTERED_MAP_SIZE 8
+#define MAP_INCREMENT_SIZE 8
+
+ static TZEnumeration* create(USystemTimeZoneType type, const char* region, const int32_t* rawOffset, UErrorCode& ec) {
+ if (U_FAILURE(ec)) {
+ return NULL;
}
- // 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;
+ int32_t baseLen;
+ int32_t *baseMap = getMap(type, baseLen, ec);
+
+ if (U_FAILURE(ec)) {
+ return NULL;
}
- 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;
+ // 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;
+ }
+
+ // 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 (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 (tzoffset != *rawOffset) {
+ continue;
+ }
+ }
+
+ 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 {
+ filteredMap = tmp;
+ }
}
- delete z;
+
+ filteredMap[numEntries++] = zidx;
}
- }
- }
- TZEnumeration(const char* country) : map(NULL), len(0), pos(0) {
- if (!getOlsonMeta()) {
- return;
- }
+ if (U_FAILURE(ec)) {
+ uprv_free(filteredMap);
+ filteredMap = NULL;
+ }
- char key[] = {0, 0, 0, 0,0, 0, 0,0, 0, 0,0}; // e.g., "US", or "Default" for no country
- if (country) {
- uprv_strncat(key, country, 2);
- } else {
- uprv_strcpy(key, kDEFAULT);
+ ures_close(res);
}
- UErrorCode ec = U_ZERO_ERROR;
- UResourceBundle *top = ures_openDirect(0, kZONEINFO, &ec);
- top = ures_getByKey(top, kREGIONS, top, &ec); // dereference 'Regions' section
+ TZEnumeration *result = NULL;
if (U_SUCCESS(ec)) {
- UResourceBundle res;
- ures_initStackObject(&res);
- ures_getByKey(top, key, &res, &ec);
- // The list of zones is a list of integers, from 0..n-1,
- // where n is the total number of system zones.
- const int32_t* v = ures_getIntVector(&res, &len, &ec);
- if (U_SUCCESS(ec)) {
- U_ASSERT(len > 0);
- map = (int32_t*)uprv_malloc(sizeof(int32_t) * len);
- if (map != 0) {
- for (uint16_t i=0; i<len; ++i) {
- U_ASSERT(v[i] >= 0 && v[i] < OLSON_ZONE_COUNT);
- map[i] = v[i];
- }
- }
+ // Finally, create a new enumeration instance
+ if (filteredMap == NULL) {
+ result = new TZEnumeration(baseMap, baseLen, FALSE);
} else {
- U_DEBUG_TZ_MSG(("Failed to load tz for region %s: %s\n", country, u_errorName(ec)));
+ result = new TZEnumeration(filteredMap, numEntries, TRUE);
+ filteredMap = NULL;
+ }
+ if (result == NULL) {
+ ec = U_MEMORY_ALLOCATION_ERROR;
}
- ures_close(&res);
}
- ures_close(top);
+
+ if (filteredMap != NULL) {
+ uprv_free(filteredMap);
+ }
+
+ return result;
}
- 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;
- }
- } else {
+ 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);
}
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], status);
++pos;
return &unistr;
}
virtual UClassID getDynamicClassID(void) const;
};
+TZEnumeration::~TZEnumeration() {
+ if (localMap != NULL) {
+ uprv_free(localMap);
+ }
+}
+
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(TZEnumeration)
StringEnumeration* U_EXPORT2
-TimeZone::createEnumeration() {
- return new TZEnumeration();
+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(int32_t rawOffset) {
- return new TZEnumeration(rawOffset);
-}
-
-StringEnumeration* U_EXPORT2
-TimeZone::createEnumeration(const char* country) {
- return new TZEnumeration(country);
-}
-
-// -------------------------------------
-
-#ifdef U_USE_TIMEZONE_OBSOLETE_2_8
-
-const UnicodeString**
-TimeZone::createAvailableIDs(int32_t rawOffset, int32_t& numIDs)
-{
- // We are creating a new array to existing UnicodeString pointers.
- // The caller will delete the array when done, but not the pointers
- // in the array.
-
- numIDs = 0;
- if (!loadOlsonIDs()) {
- return 0;
- }
-
- // Allocate more space than we'll need. The end of the array will
- // be blank.
- const UnicodeString** ids =
- (const UnicodeString** )uprv_malloc(OLSON_ZONE_COUNT * sizeof(UnicodeString *));
- if (ids == 0) {
- return 0;
- }
-
- uprv_memset(ids, 0, sizeof(UnicodeString*) * OLSON_ZONE_COUNT);
-
- UnicodeString s;
- for (int32_t i=0; i<OLSON_ZONE_COUNT; ++i) {
- // This is VERY inefficient.
- TimeZone* z = TimeZone::createTimeZone(OLSON_IDS[i]);
- // Make sure we get back the ID we wanted (if the ID is
- // invalid we get back GMT).
- if (z != 0 && z->getID(s) == OLSON_IDS[i] &&
- z->getRawOffset() == rawOffset) {
- ids[numIDs++] = &OLSON_IDS[i]; // [sic]
- }
- delete z;
- }
-
- return ids;
+TimeZone::createEnumeration() {
+ UErrorCode ec = U_ZERO_ERROR;
+ return TZEnumeration::create(UCAL_ZONE_TYPE_ANY, NULL, NULL, ec);
}
-// -------------------------------------
-
-const UnicodeString**
-TimeZone::createAvailableIDs(const char* country, int32_t& numIDs) {
-
- // We are creating a new array to existing UnicodeString pointers.
- // The caller will delete the array when done, but not the pointers
- // in the array.
-
- numIDs = 0;
- if (!loadOlsonIDs()) {
- return 0;
- }
-
- char key[] = { 0, 0, 0,0, 0, 0,0, 0, 0 }; // e.g., "US", or "Default" for non-country zones
- if (country) {
- uprv_strncat(key, country, 2);
- } else {
- uprv_strcpy(key, kDEFAULT);
- }
-
- const UnicodeString** ids = 0;
-
+StringEnumeration* U_EXPORT2
+TimeZone::createEnumeration(int32_t rawOffset) {
UErrorCode ec = U_ZERO_ERROR;
- UResourceBundle *top = ures_openDirect(0, kZONEINFO, &ec);
- UResourceBundle *ares = ures_getByKey(top, kREGIONS, NULL, &ec); // dereference Regions section
- if (U_SUCCESS(ec)) {
- getOlsonMeta(top);
- UResourceBundle res;
- ures_initStackObject(&res);
- ures_getByKey(ares, key, &res, &ec);
- U_DEBUG_TZ_MSG(("caI: on %s, err %s\n", country, u_errorName(ec)));
- if (U_SUCCESS(ec)) {
- /* The list of zones is a list of integers, from 0..n-1,
- * where n is the total number of system zones. The
- * numbering corresponds exactly to the ordering of
- * OLSON_IDS.
- */
- const int32_t* v = ures_getIntVector(&res, &numIDs, &ec);
- ids = (const UnicodeString**)
- uprv_malloc(numIDs * sizeof(UnicodeString*));
- if (ids == 0) {
- numIDs = 0;
- } else {
- for (int32_t i=0; i<numIDs; ++i) {
- ids[i] = &OLSON_IDS[v[i]]; // [sic]
- }
- }
- }
- ures_close(&res);
- }
- ures_close(ares);
- ures_close(top);
-
- return ids;
+ return TZEnumeration::create(UCAL_ZONE_TYPE_ANY, NULL, &rawOffset, ec);
}
-// -------------------------------------
-
-const UnicodeString**
-TimeZone::createAvailableIDs(int32_t& numIDs)
-{
- // We are creating a new array to existing UnicodeString pointers.
- // The caller will delete the array when done, but not the pointers
- // in the array.
- numIDs = 0;
- if (!loadOlsonIDs()) {
- return 0;
- }
-
- const UnicodeString** ids =
- (const UnicodeString** )uprv_malloc(OLSON_ZONE_COUNT * sizeof(UnicodeString *));
- if (ids != 0) {
- numIDs = OLSON_ZONE_COUNT;
- for (int32_t i=0; i<numIDs; ++i) {
- ids[i] = &OLSON_IDS[i];
- }
- }
-
- return ids;
+StringEnumeration* U_EXPORT2
+TimeZone::createEnumeration(const char* country) {
+ UErrorCode ec = U_ZERO_ERROR;
+ return TZEnumeration::create(UCAL_ZONE_TYPE_ANY, country, NULL, ec);
}
-#endif
-
// ---------------------------------------
int32_t U_EXPORT2
TimeZone::countEquivalentIDs(const UnicodeString& id) {
int32_t result = 0;
UErrorCode ec = U_ZERO_ERROR;
- UResourceBundle res;
- ures_initStackObject(&res);
+ StackUResourceBundle res;
U_DEBUG_TZ_MSG(("countEquivalentIDs..\n"));
- UResourceBundle *top = openOlsonResource(id, res, ec);
+ UResourceBundle *top = openOlsonResource(id, res.ref(), ec);
if (U_SUCCESS(ec)) {
- int32_t size = ures_getSize(&res);
- U_DEBUG_TZ_MSG(("cEI: success (size %d, key %s)..\n", size, ures_getKey(&res)));
- if (size == 4 || size == 6) {
- UResourceBundle r;
- ures_initStackObject(&r);
- ures_getByIndex(&res, size-1, &r, &ec);
- //result = ures_getSize(&r); // doesn't work
- ures_getIntVector(&r, &result, &ec);
- U_DEBUG_TZ_MSG(("ceI: result %d, err %s\n", result, u_errorName(ec)));
- ures_close(&r);
- }
- } else {
- U_DEBUG_TZ_MSG(("cEI: fail, %s\n", u_errorName(ec)));
+ StackUResourceBundle r;
+ ures_getByKey(res.getAlias(), kLINKS, r.getAlias(), &ec);
+ ures_getIntVector(r.getAlias(), &result, &ec);
}
- ures_close(&res);
ures_close(top);
return result;
}
U_DEBUG_TZ_MSG(("gEI(%d)\n", index));
UnicodeString result;
UErrorCode ec = U_ZERO_ERROR;
- UResourceBundle res;
- ures_initStackObject(&res);
- UResourceBundle *top = openOlsonResource(id, res, ec);
+ StackUResourceBundle res;
+ UResourceBundle *top = openOlsonResource(id, res.ref(), ec);
int32_t zone = -1;
if (U_SUCCESS(ec)) {
- int32_t size = ures_getSize(&res);
- if (size == 4 || size == 6) {
- UResourceBundle r;
- ures_initStackObject(&r);
- ures_getByIndex(&res, size-1, &r, &ec);
- const int32_t* v = ures_getIntVector(&r, &size, &ec);
- if (index >= 0 && index < size && getOlsonMeta()) {
+ StackUResourceBundle r;
+ int32_t size;
+ ures_getByKey(res.getAlias(), kLINKS, r.getAlias(), &ec);
+ const int32_t *v = ures_getIntVector(r.getAlias(), &size, &ec);
+ if (U_SUCCESS(ec)) {
+ if (index >= 0 && index < size) {
zone = v[index];
}
- ures_close(&r);
}
}
- ures_close(&res);
if (zone >= 0) {
UResourceBundle *ares = ures_getByKey(top, kNAMES, NULL, &ec); // dereference Zones section
if (U_SUCCESS(ec)) {
int32_t idLen = 0;
- const UChar* id = ures_getStringByIndex(ares, zone, &idLen, &ec);
- result.fastCopyFrom(UnicodeString(TRUE, id, idLen));
+ const UChar* id2 = ures_getStringByIndex(ares, zone, &idLen, &ec);
+ result.fastCopyFrom(UnicodeString(TRUE, id2, idLen));
U_DEBUG_TZ_MSG(("gei(%d) -> %d, len%d, %s\n", index, zone, result.length(), u_errorName(ec)));
}
ures_close(ares);
// ---------------------------------------
+// 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) {
+ 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);
+
+ // open the zone bundle by index
+ ures_getByKey(rb, kZONES, rb, &ec);
+ ures_getByIndex(rb, idx, rb, &ec);
+
+ if (U_SUCCESS(ec)) {
+ if (ures_getType(rb) == URES_INT) {
+ // this is a link - dereference the link
+ int32_t deref = ures_getInt(rb, &ec);
+ const UChar* tmp = ures_getStringByIndex(names, deref, NULL, &ec);
+ if (U_SUCCESS(ec)) {
+ result = tmp;
+ }
+ }
+ }
+
+ ures_close(names);
+ ures_close(rb);
+
+ return result;
+}
+
+const UChar*
+TimeZone::getRegion(const UnicodeString& id) {
+ 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, &status);
+ int32_t idx = findInStringArray(res, id, status);
+
+ // get region mapping
+ ures_getByKey(rb, kREGIONS, res, &status);
+ const UChar *tmp = ures_getStringByIndex(res, idx, NULL, &status);
+ if (U_SUCCESS(status)) {
+ result = tmp;
+ }
+
+ ures_close(res);
+ ures_close(rb);
+
+ 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);
+}
+
+// ---------------------------------------
+
UnicodeString&
TimeZone::getDisplayName(UnicodeString& result) const
}
UnicodeString&
-TimeZone::getDisplayName(UBool daylight, EDisplayType style, UnicodeString& result) const
+TimeZone::getDisplayName(UBool inDaylight, EDisplayType style, UnicodeString& result) const
{
- return getDisplayName(daylight,style, Locale::getDefault(), result);
+ return getDisplayName(inDaylight,style, Locale::getDefault(), result);
}
//--------------------------------------
-int32_t
+int32_t
TimeZone::getDSTSavings()const {
if (useDaylightTime()) {
return 3600000;
}
//---------------------------------------
UnicodeString&
-TimeZone::getDisplayName(UBool daylight, EDisplayType style, const Locale& locale, UnicodeString& result) const
+TimeZone::getDisplayName(UBool inDaylight, 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
- SimpleDateFormat format(style == LONG ? ZZZZ_STR : Z_STR,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();
- }
-
- // Create a new SimpleTimeZone as a stand-in for this zone; the
- // stand-in will have no DST, or all DST, 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;
- SimpleTimeZone *tz = NULL;
- if(daylight && useDaylightTime()){
- // For the pure-DST zone, we use JANUARY and DECEMBER
- int savings = getDSTSavings();
- tz = new SimpleTimeZone(getRawOffset(), getID(tempID),
- UCAL_JANUARY, 1, 0, 0,
- UCAL_FEBRUARY, 1, 0, 0,
- savings, status);
- }else{
- tz = new SimpleTimeZone(getRawOffset(), getID(tempID));
- }
- format.applyPattern(style == LONG ? ZZZZ_STR : Z_STR);
- Calendar *myCalendar = (Calendar*)format.getCalendar();
- myCalendar->setTimeZone(*tz); // copy
-
- delete tz;
+ UDate date = Calendar::getNow();
+ UTimeZoneFormatTimeType timeType = UTZFMT_TIME_TYPE_UNKNOWN;
+ int32_t offset;
- FieldPosition pos(FieldPosition::DONT_CARE);
- return format.format(UDate(864000000L), result, pos); // Must use a valid date here.
-}
+ if (style == GENERIC_LOCATION || style == LONG_GENERIC || style == SHORT_GENERIC) {
+ LocalPointer<TimeZoneFormat> tzfmt(TimeZoneFormat::createInstance(locale, status));
+ if (U_FAILURE(status)) {
+ result.remove();
+ return result;
+ }
+ // 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:
+ UPRV_UNREACHABLE;
+ }
+ // 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 ((inDaylight && timeType == UTZFMT_TIME_TYPE_STANDARD) || (!inDaylight && timeType == UTZFMT_TIME_TYPE_DAYLIGHT)) {
+ offset = inDaylight ? getRawOffset() + getDSTSavings() : getRawOffset();
+ if (style == SHORT_GENERIC) {
+ tzfmt->formatOffsetShortLocalizedGMT(offset, result, status);
+ } else {
+ tzfmt->formatOffsetLocalizedGMT(offset, result, status);
+ }
+ }
+ } else if (style == LONG_GMT || style == SHORT_GMT) {
+ LocalPointer<TimeZoneFormat> tzfmt(TimeZoneFormat::createInstance(locale, status));
+ if (U_FAILURE(status)) {
+ result.remove();
+ return result;
+ }
+ offset = inDaylight && 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:
+ UPRV_UNREACHABLE;
+ }
+ } else {
+ U_ASSERT(style == LONG || style == SHORT || style == SHORT_COMMONLY_USED);
+ UTimeZoneNameType nameType = UTZNM_UNKNOWN;
+ switch (style) {
+ case LONG:
+ nameType = inDaylight ? UTZNM_LONG_DAYLIGHT : UTZNM_LONG_STANDARD;
+ break;
+ case SHORT:
+ case SHORT_COMMONLY_USED:
+ nameType = inDaylight ? UTZNM_SHORT_DAYLIGHT : UTZNM_SHORT_STANDARD;
+ break;
+ default:
+ UPRV_UNREACHABLE;
+ }
+ 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 = inDaylight && useDaylightTime() ? getRawOffset() + getDSTSavings() : getRawOffset();
+ if (style == LONG) {
+ tzfmt->formatOffsetLocalizedGMT(offset, result, status);
+ } else {
+ tzfmt->formatOffsetShortLocalizedGMT(offset, result, status);
+ }
+ }
+ }
+ if (U_FAILURE(status)) {
+ result.remove();
+ }
+ return result;
+}
/**
* Parse a custom time zone identifier and return a corresponding zone.
TimeZone*
TimeZone::createCustomTimeZone(const UnicodeString& id)
{
+ int32_t sign, hour, min, sec;
+ if (parseCustomID(id, sign, hour, min, sec)) {
+ UnicodeString customID;
+ formatCustomID(hour, min, sec, (sign < 0), customID);
+ int32_t offset = sign * ((hour * 60 + min) * 60 + sec) * 1000;
+ return new SimpleTimeZone(offset, customID);
+ }
+ return NULL;
+}
+
+UnicodeString&
+TimeZone::getCustomID(const UnicodeString& id, UnicodeString& normalized, UErrorCode& status) {
+ normalized.remove();
+ if (U_FAILURE(status)) {
+ return normalized;
+ }
+ int32_t sign, hour, min, sec;
+ if (parseCustomID(id, sign, hour, min, sec)) {
+ formatCustomID(hour, min, sec, (sign < 0), normalized);
+ } else {
+ status = U_ILLEGAL_ARGUMENT_ERROR;
+ }
+ return normalized;
+}
+
+UBool
+TimeZone::parseCustomID(const UnicodeString& id, int32_t& sign,
+ int32_t& hour, int32_t& min, int32_t& sec) {
static const int32_t kParseFailed = -99999;
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);
- UBool negative = FALSE;
- int32_t offset;
-
- if (id[pos.getIndex()] == 0x002D /*'-'*/)
- negative = TRUE;
- else if (id[pos.getIndex()] != 0x002B /*'+'*/)
- return 0;
+ sign = 1;
+ hour = 0;
+ min = 0;
+ sec = 0;
+
+ if (id[pos.getIndex()] == MINUS /*'-'*/) {
+ sign = -1;
+ } else if (id[pos.getIndex()] != PLUS /*'+'*/) {
+ return FALSE;
+ }
pos.setIndex(pos.getIndex() + 1);
UErrorCode success = U_ZERO_ERROR;
numberFormat = NumberFormat::createInstance(success);
if(U_FAILURE(success)){
- return NULL;
+ 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();
-
Formattable n(kParseFailed);
-
numberFormat->parse(id, n, pos);
if (pos.getIndex() == start) {
delete numberFormat;
- return 0;
+ return FALSE;
}
- offset = n.getLong();
+ hour = n.getLong();
- if (pos.getIndex() < id.length() &&
- id[pos.getIndex()] == 0x003A /*':'*/)
- {
+ if (pos.getIndex() < id.length()) {
+ if (pos.getIndex() - start > 2
+ || id[pos.getIndex()] != COLON) {
+ delete numberFormat;
+ return FALSE;
+ }
// hh:mm
- offset *= 60;
pos.setIndex(pos.getIndex() + 1);
int32_t oldPos = pos.getIndex();
n.setLong(kParseFailed);
numberFormat->parse(id, n, pos);
- if (pos.getIndex() == oldPos) {
+ if ((pos.getIndex() - oldPos) != 2) {
+ // must be 2 digits
+ delete numberFormat;
+ return FALSE;
+ }
+ min = n.getLong();
+ if (pos.getIndex() < id.length()) {
+ if (id[pos.getIndex()] != COLON) {
+ delete numberFormat;
+ return FALSE;
+ }
+ // [:ss]
+ pos.setIndex(pos.getIndex() + 1);
+ oldPos = pos.getIndex();
+ n.setLong(kParseFailed);
+ numberFormat->parse(id, n, pos);
+ if (pos.getIndex() != id.length()
+ || (pos.getIndex() - oldPos) != 2) {
+ delete numberFormat;
+ return FALSE;
+ }
+ sec = n.getLong();
+ }
+ } else {
+ // Supported formats are below -
+ //
+ // HHmmss
+ // Hmmss
+ // HHmm
+ // Hmm
+ // HH
+ // H
+
+ int32_t length = pos.getIndex() - start;
+ if (length <= 0 || 6 < length) {
+ // invalid length
delete numberFormat;
- return 0;
+ return FALSE;
+ }
+ switch (length) {
+ case 1:
+ case 2:
+ // already set to hour
+ break;
+ case 3:
+ case 4:
+ min = hour % 100;
+ hour /= 100;
+ break;
+ case 5:
+ case 6:
+ sec = hour % 100;
+ min = (hour/100) % 100;
+ hour /= 10000;
+ break;
}
- offset += n.getLong();
}
- else
- {
- // hhmm or hh
-
- // Be strict about interpreting something as hh; it must be
- // an offset < 30, and it must be one or two digits. Thus
- // 0010 is interpreted as 00:10, but 10 is interpreted as
- // 10:00.
- if (offset < 30 && (pos.getIndex() - start) <= 2)
- offset *= 60; // hh, from 00 to 29; 30 is 00:30
- else
- offset = offset % 100 + offset / 100 * 60; // hhmm
+
+ delete numberFormat;
+
+ if (hour > kMAX_CUSTOM_HOUR || min > kMAX_CUSTOM_MIN || sec > kMAX_CUSTOM_SEC) {
+ return FALSE;
+ }
+ return TRUE;
+ }
+ return FALSE;
+}
+
+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, GMT_ID_LENGTH);
+ if (hour | min | sec) {
+ if (negative) {
+ id += (UChar)MINUS;
+ } else {
+ id += (UChar)PLUS;
}
- if(negative)
- offset = -offset;
+ if (hour < 10) {
+ id += (UChar)ZERO_DIGIT;
+ } else {
+ id += (UChar)(ZERO_DIGIT + hour/10);
+ }
+ id += (UChar)(ZERO_DIGIT + hour%10);
+ id += (UChar)COLON;
+ if (min < 10) {
+ id += (UChar)ZERO_DIGIT;
+ } else {
+ id += (UChar)(ZERO_DIGIT + min/10);
+ }
+ id += (UChar)(ZERO_DIGIT + min%10);
- delete numberFormat;
- return new SimpleTimeZone(offset * 60000, CUSTOM_ID);
+ if (sec) {
+ id += (UChar)COLON;
+ if (sec < 10) {
+ id += (UChar)ZERO_DIGIT;
+ } else {
+ id += (UChar)(ZERO_DIGIT + sec/10);
+ }
+ id += (UChar)(ZERO_DIGIT + sec%10);
+ }
}
- return 0;
+ return id;
}
-UBool
+UBool
TimeZone::hasSameRules(const TimeZone& other) const
{
- return (getRawOffset() == other.getRawOffset() &&
+ return (getRawOffset() == other.getRawOffset() &&
useDaylightTime() == other.useDaylightTime());
}
+static void U_CALLCONV initTZDataVersion(UErrorCode &status) {
+ ucln_i18n_registerCleanup(UCLN_I18N_TIMEZONE, timeZone_cleanup);
+ int32_t len = 0;
+ StackUResourceBundle bundle;
+ ures_openDirectFillIn(bundle.getAlias(), NULL, kZONEINFO, &status);
+ const UChar *tzver = ures_getStringByKey(bundle.getAlias(), 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;
+ }
+ u_UCharsToChars(tzver, TZDATA_VERSION, len);
+ }
+}
+
+const char*
+TimeZone::getTZDataVersion(UErrorCode& status)
+{
+ umtx_initOnce(gTZDataVersionInitOnce, &initTZDataVersion, status);
+ return (const char*)TZDATA_VERSION;
+}
+
+UnicodeString&
+TimeZone::getCanonicalID(const UnicodeString& id, UnicodeString& canonicalID, UErrorCode& status)
+{
+ UBool isSystemID = FALSE;
+ return getCanonicalID(id, canonicalID, isSystemID, status);
+}
+
+UnicodeString&
+TimeZone::getCanonicalID(const UnicodeString& id, UnicodeString& canonicalID, UBool& isSystemID,
+ UErrorCode& status)
+{
+ canonicalID.remove();
+ isSystemID = FALSE;
+ if (U_FAILURE(status)) {
+ return canonicalID;
+ }
+ 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 {
+ 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;
+}
+
+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
+ if (status == U_ILLEGAL_ARGUMENT_ERROR) {
+ // getWindowsID() sets an empty string where
+ // getCanonicalID() sets a U_ILLEGAL_ARGUMENT_ERROR.
+ status = U_ZERO_ERROR;
+ }
+ 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, static_cast<int32_t>(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, static_cast<int32_t>(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;
+}
+
+
U_NAMESPACE_END
#endif /* #if !UCONFIG_NO_FORMATTING */