X-Git-Url: https://git.saurik.com/apple/icu.git/blobdiff_plain/374ca955a76ecab1204ca8bfa63ff9238d998416..9953cfe31e2dce81abce5bae29a7d5001cb9c635:/icuSources/i18n/timezone.cpp diff --git a/icuSources/i18n/timezone.cpp b/icuSources/i18n/timezone.cpp index 837f4d0e..82304acc 100644 --- a/icuSources/i18n/timezone.cpp +++ b/icuSources/i18n/timezone.cpp @@ -1,6 +1,6 @@ /* ******************************************************************************* -* Copyright (C) 1997-2004, International Business Machines Corporation and * +* Copyright (C) 1997-2010, International Business Machines Corporation and * * others. All Rights Reserved. * ******************************************************************************* * @@ -35,6 +35,8 @@ * available IDs code. Misc. cleanup. *********************************************************************************/ +#include // for 'typeid' to work + #include "unicode/utypes.h" #include "unicode/ustring.h" @@ -79,51 +81,66 @@ static char gStrBuf[256]; #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 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 int32_t GMT_ID_LENGTH = 3; -static const UChar CUSTOM_ID[] = -{ - 0x43, 0x75, 0x73, 0x74, 0x6F, 0x6D, 0x00 /* "Custom" */ -}; -static UMTX LOCK; -static TimeZone* DEFAULT_ZONE = NULL; -static TimeZone* _GMT = NULL; // cf. TimeZone::GMT +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 -#ifdef U_USE_TIMEZONE_OBSOLETE_2_8 -static UnicodeString* OLSON_IDS = 0; -#endif +static char TZDATA_VERSION[16]; +static UBool TZDataVersionInitialized = FALSE; U_CDECL_BEGIN -static UBool U_CALLCONV timeZone_cleanup() +static UBool U_CALLCONV timeZone_cleanup(void) { -#ifdef U_USE_TIMEZONE_OBSOLETE_2_8 - delete []OLSON_IDS; - OLSON_IDS = 0; -#endif - delete DEFAULT_ZONE; DEFAULT_ZONE = NULL; delete _GMT; _GMT = NULL; + uprv_memset(TZDATA_VERSION, 0, sizeof(TZDATA_VERSION)); + TZDataVersionInitialized = FALSE; + if (LOCK) { umtx_destroy(&LOCK); LOCK = NULL; } + if (TZSET_LOCK) { + umtx_destroy(&TZSET_LOCK); + TZSET_LOCK = NULL; + } return TRUE; } @@ -138,7 +155,6 @@ U_NAMESPACE_BEGIN * 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 /** @@ -146,78 +162,78 @@ static int32_t OLSON_ZONE_COUNT = 0; // count of zones * meta-data. Return TRUE if successful. */ static UBool getOlsonMeta(const UResourceBundle* top) { - if (OLSON_ZONE_START < 0) { + 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); - OLSON_ZONE_START = 0; - U_DEBUG_TZ_MSG(("OZC%d OZS%d\n",OLSON_ZONE_COUNT, OLSON_ZONE_START)); + OLSON_ZONE_COUNT = ures_getSize(&res); + U_DEBUG_TZ_MSG(("OZC%d\n",OLSON_ZONE_COUNT)); } ures_close(&res); } - return (OLSON_ZONE_START >= 0); + return (OLSON_ZONE_COUNT > 0); } /** * Load up the Olson meta-data. Return TRUE if successful. */ static UBool getOlsonMeta() { - if (OLSON_ZONE_START < 0) { + if (OLSON_ZONE_COUNT == 0) { UErrorCode ec = U_ZERO_ERROR; UResourceBundle *top = ures_openDirect(0, kZONEINFO, &ec); if (U_SUCCESS(ec)) { - getOlsonMeta(top); + getOlsonMeta(top); } ures_close(top); } - return (OLSON_ZONE_START >= 0); + return (OLSON_ZONE_COUNT > 0); } static int32_t findInStringArray(UResourceBundle* array, const UnicodeString& id, UErrorCode &status) { UnicodeString copy; - copy.fastCopyFrom(id); - const UChar* buf = copy.getTerminatedBuffer(); - const UChar* u = NULL; - - int32_t count = ures_getSize(array); - int32_t start = 0; - int32_t i; + const UChar *u; int32_t len; - int32_t limit = count; - if(U_FAILURE(status) || (count < 1)) { + + int32_t start = 0; + int32_t limit = ures_getSize(array); + int32_t mid; + int32_t lastMid = INT32_MAX; + 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(buf), start, limit)); - - while(U_SUCCESS(status) && (start 0) ? count : 1]; - for (int32_t i=0; iclone(); + const TimeZone* temptz = getGMT(); + if (temptz == NULL) { + result = NULL; + } else { + result = temptz->clone(); + } } return result; } @@ -491,7 +463,7 @@ TimeZone::createSystemTimeZone(const UnicodeString& id) { */ void TimeZone::initDefault() -{ +{ // We access system timezone data through TPlatformUtilities, // including tzset(), timezone, and tzname[]. int32_t rawOffset = 0; @@ -500,24 +472,38 @@ TimeZone::initDefault() // 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; + // 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; } + UBool initialized; + UMTX_CHECK(&LOCK, (DEFAULT_ZONE != NULL), initialized); + if (initialized) { + /* Hrmph? Either a race condition happened, or tzset initialized ICU. */ + return; + } + TimeZone* default_zone = NULL; /* Make sure that the string is NULL terminated to prevent BoundsChecker/Purify warnings. */ @@ -526,45 +512,20 @@ TimeZone::initDefault() hostStrID.truncate(hostStrID.length()-1); default_zone = createSystemTimeZone(hostStrID); -#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; +#ifdef U_WINDOWS + // hostID points to a heap-allocated location on Windows. + uprv_free(const_cast(hostID)); +#endif - 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); - } + int32_t hostIDLen = hostStrID.length(); + if (default_zone != NULL && rawOffset != default_zone->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; } -#endif // Construct a fixed standard zone with the host's ID // and raw offset. @@ -574,7 +535,12 @@ TimeZone::initDefault() // If we _still_ don't have a time zone, use GMT. if (default_zone == NULL) { - default_zone = getGMT()->clone(); + const TimeZone* temptz = getGMT(); + // If we can't use GMT, get out. + if (temptz == NULL) { + return; + } + default_zone = temptz->clone(); } // If DEFAULT_ZONE is still NULL, set it up. @@ -594,16 +560,15 @@ TimeZone::initDefault() 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) { + /* 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 - return DEFAULT_ZONE->clone(); + return (DEFAULT_ZONE != NULL) ? DEFAULT_ZONE->clone() : NULL; } // ------------------------------------- @@ -615,7 +580,6 @@ TimeZone::adoptDefault(TimeZone* zone) { TimeZone* old = NULL; - umtx_init(&LOCK); /* This is here to prevent race conditions. */ umtx_lock(&LOCK); old = DEFAULT_ZONE; DEFAULT_ZONE = zone; @@ -648,36 +612,40 @@ void TimeZone::getOffset(UDate date, UBool local, int32_t& rawOffset, } 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; } } @@ -686,6 +654,8 @@ void TimeZone::getOffset(UDate date, UBool local, int32_t& rawOffset, // New available IDs API as of ICU 2.4. Uses StringEnumeration API. class TZEnumeration : public StringEnumeration { +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 @@ -694,6 +664,23 @@ class TZEnumeration : public StringEnumeration { int32_t len; int32_t pos; + UBool getID(int32_t i) { + UErrorCode ec = U_ZERO_ERROR; + int32_t idLen = 0; + const UChar* id = NULL; + UResourceBundle *top = ures_openDirect(0, kZONEINFO, &ec); + top = ures_getByKey(top, kNAMES, top, &ec); // dereference Zones section + id = ures_getStringByIndex(top, i, &idLen, &ec); + if(U_FAILURE(ec)) { + unistr.truncate(0); + } + else { + unistr.fastCopyFrom(UnicodeString(TRUE, id, idLen)); + } + ures_close(top); + return U_SUCCESS(ec); + } + public: TZEnumeration() : map(NULL), len(0), pos(0) { if (getOlsonMeta()) { @@ -736,38 +723,56 @@ public: return; } - 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); - } - UErrorCode ec = U_ZERO_ERROR; - UResourceBundle *top = ures_openDirect(0, kZONEINFO, &ec); - top = ures_getByKey(top, kREGIONS, top, &ec); // dereference 'Regions' section - 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= 0 && v[i] < OLSON_ZONE_COUNT); - map[i] = v[i]; + 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); + } + + // 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); + if (U_FAILURE(ec)) { + break; + } + if (u_strcmp(uCountry, region) == 0) { + count++; + } + } + + 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 (U_SUCCESS(ec)) { + len = count; + } else { + uprv_free(map); + map = NULL; + } + } else { + U_DEBUG_TZ_MSG(("Failed to load tz for region %s: %s\n", country, u_errorName(ec))); } - } else { - U_DEBUG_TZ_MSG(("Failed to load tz for region %s: %s\n", country, u_errorName(ec))); } - ures_close(&res); } - ures_close(top); + ures_close(res); } TZEnumeration(const TZEnumeration &other) : StringEnumeration(), map(NULL), len(0), pos(0) { @@ -811,25 +816,6 @@ public: pos = 0; } -private: - - UBool getID(int32_t i) { - UErrorCode ec = U_ZERO_ERROR; - int32_t idLen = 0; - const UChar* id = NULL; - UResourceBundle *top = ures_openDirect(0, kZONEINFO, &ec); - top = ures_getByKey(top, kNAMES, top, &ec); // dereference Zones section - id = ures_getStringByIndex(top, i, &idLen, &ec); - if(U_FAILURE(ec)) { - unistr.truncate(0); - } - else { - unistr.fastCopyFrom(UnicodeString(TRUE, id, idLen)); - } - ures_close(top); - return U_SUCCESS(ec); - } - public: static UClassID U_EXPORT2 getStaticClassID(void); virtual UClassID getDynamicClassID(void) const; @@ -852,132 +838,6 @@ 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; igetID(s) == OLSON_IDS[i] && - z->getRawOffset() == rawOffset) { - ids[numIDs++] = &OLSON_IDS[i]; // [sic] - } - delete z; - } - - return ids; -} - -// ------------------------------------- - -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; - - 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= 0 && index < size && getOlsonMeta()) { zone = v[index]; } - ures_close(&r); } + ures_close(&r); } ures_close(&res); if (zone >= 0) { @@ -1054,6 +906,65 @@ TimeZone::getEquivalentID(const UnicodeString& id, int32_t index) { // --------------------------------------- +// These two methods are used by ZoneMeta class only. + +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) { + const UChar *result = WORLD; + UErrorCode ec = U_ZERO_ERROR; + UResourceBundle *rb = ures_openDirect(NULL, kZONEINFO, &ec); + + // resolve zone index by name + UResourceBundle *res = ures_getByKey(rb, kNAMES, NULL, &ec); + int32_t idx = findInStringArray(res, id, ec); + + // get region mapping + ures_getByKey(rb, kREGIONS, res, &ec); + const UChar *tmp = ures_getStringByIndex(res, idx, NULL, &ec); + if (U_SUCCESS(ec)) { + result = tmp; + } + + ures_close(res); + ures_close(rb); + + return result; +} + +// --------------------------------------- + UnicodeString& TimeZone::getDisplayName(UnicodeString& result) const @@ -1072,7 +983,15 @@ TimeZone::getDisplayName(UBool daylight, EDisplayType style, UnicodeString& resu { return getDisplayName(daylight,style, Locale::getDefault(), result); } - +//-------------------------------------- +int32_t +TimeZone::getDSTSavings()const { + if (useDaylightTime()) { + return 3600000; + } + return 0; +} +//--------------------------------------- UnicodeString& TimeZone::getDisplayName(UBool daylight, EDisplayType style, const Locale& locale, UnicodeString& result) const { @@ -1082,7 +1001,39 @@ TimeZone::getDisplayName(UBool daylight, EDisplayType style, const Locale& local char buf[128]; fID.extract(0, sizeof(buf)-1, buf, sizeof(buf), ""); #endif - SimpleDateFormat format(style == LONG ? ZZZZ_STR : Z_STR,locale,status); + + // 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; + } + + SimpleDateFormat format(pat, locale, status); U_DEBUG_TZ_MSG(("getDisplayName(%s)\n", buf)); if(!U_SUCCESS(status)) { @@ -1094,29 +1045,78 @@ TimeZone::getDisplayName(UBool daylight, EDisplayType style, const Locale& local 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 all DST, but the same ID and offset, + // 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; - SimpleTimeZone *tz = daylight ? - // For the pure-DST zone, we use JANUARY and DECEMBER - - new SimpleTimeZone(getRawOffset(), getID(tempID), - UCAL_JANUARY , 1, 0, 0, - UCAL_DECEMBER , 31, 0, U_MILLIS_PER_DAY, status) : - new SimpleTimeZone(getRawOffset(), getID(tempID)); - - format.applyPattern(style == LONG ? ZZZZ_STR : Z_STR); - Calendar *myCalendar = (Calendar*)format.getCalendar(); - myCalendar->setTimeZone(*tz); // copy - - delete tz; - - FieldPosition pos(FieldPosition::DONT_CARE); - return format.format(UDate(196262345678.), result, pos); // Must use a valid date here. -} + 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 (U_FAILURE(status)) { + return result.remove(); + } + 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(); + } + format.adoptTimeZone(tz); + } else { + format.setTimeZone(*this); + } + } 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; + } + return result.remove(); + } + format.adoptTimeZone(tz); + } + format.format(d, result, status); + return result; +} /** * Parse a custom time zone identifier and return a corresponding zone. @@ -1128,10 +1128,35 @@ TimeZone::getDisplayName(UBool daylight, EDisplayType style, const Locale& local 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); + } + 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(); @@ -1139,78 +1164,223 @@ TimeZone::createCustomTimeZone(const UnicodeString& id) idUppercase.startsWith(GMT_ID)) { 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 FALSE; + } numberFormat->setParseIntegerOnly(TRUE); + numberFormat->setParseStrict(FALSE); // TODO: a wild hack... - // 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 0; + return FALSE; } - offset += n.getLong(); + 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 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; + } + } + + delete numberFormat; + + if (hour > kMAX_CUSTOM_HOUR || min > kMAX_CUSTOM_MIN || sec > kMAX_CUSTOM_SEC) { + return FALSE; } - 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 + 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); + 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()); } +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); + + 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); + } + + ures_close(bundle); + } + if (U_FAILURE(status)) { + return NULL; + } + 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; + } + ZoneMeta::getCanonicalSystemID(id, canonicalID, status); + if (U_SUCCESS(status)) { + isSystemID = TRUE; + } else { + // Not a system ID + status = U_ZERO_ERROR; + getCustomID(id, canonicalID, status); + } + return canonicalID; +} + U_NAMESPACE_END #endif /* #if !UCONFIG_NO_FORMATTING */