Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
michael@0 | 1 | /* |
michael@0 | 2 | ******************************************************************************* |
michael@0 | 3 | * Copyright (C) 1997-2013, International Business Machines Corporation and |
michael@0 | 4 | * others. All Rights Reserved. |
michael@0 | 5 | ******************************************************************************* |
michael@0 | 6 | * |
michael@0 | 7 | * File TIMEZONE.CPP |
michael@0 | 8 | * |
michael@0 | 9 | * Modification History: |
michael@0 | 10 | * |
michael@0 | 11 | * Date Name Description |
michael@0 | 12 | * 12/05/96 clhuang Creation. |
michael@0 | 13 | * 04/21/97 aliu General clean-up and bug fixing. |
michael@0 | 14 | * 05/08/97 aliu Fixed Hashtable code per code review. |
michael@0 | 15 | * 07/09/97 helena Changed createInstance to createDefault. |
michael@0 | 16 | * 07/29/97 aliu Updated with all-new list of 96 UNIX-derived |
michael@0 | 17 | * TimeZones. Changed mechanism to load from static |
michael@0 | 18 | * array rather than resource bundle. |
michael@0 | 19 | * 07/07/1998 srl Bugfixes from the Java side: UTC GMT CAT NST |
michael@0 | 20 | * Added getDisplayName API |
michael@0 | 21 | * going to add custom parsing. |
michael@0 | 22 | * |
michael@0 | 23 | * ISSUES: |
michael@0 | 24 | * - should getDisplayName cache something? |
michael@0 | 25 | * - should custom time zones be cached? [probably] |
michael@0 | 26 | * 08/10/98 stephen Brought getDisplayName() API in-line w/ conventions |
michael@0 | 27 | * 08/19/98 stephen Changed createTimeZone() to never return 0 |
michael@0 | 28 | * 09/02/98 stephen Added getOffset(monthLen) and hasSameRules() |
michael@0 | 29 | * 09/15/98 stephen Added getStaticClassID() |
michael@0 | 30 | * 02/22/99 stephen Removed character literals for EBCDIC safety |
michael@0 | 31 | * 05/04/99 stephen Changed initDefault() for Mutex issues |
michael@0 | 32 | * 07/12/99 helena HPUX 11 CC Port. |
michael@0 | 33 | * 12/03/99 aliu Moved data out of static table into icudata.dll. |
michael@0 | 34 | * Substantial rewrite of zone lookup, default zone, and |
michael@0 | 35 | * available IDs code. Misc. cleanup. |
michael@0 | 36 | *********************************************************************************/ |
michael@0 | 37 | |
michael@0 | 38 | #include "utypeinfo.h" // for 'typeid' to work |
michael@0 | 39 | |
michael@0 | 40 | #include "unicode/utypes.h" |
michael@0 | 41 | #include "unicode/ustring.h" |
michael@0 | 42 | #include "uassert.h" |
michael@0 | 43 | #include "ustr_imp.h" |
michael@0 | 44 | |
michael@0 | 45 | #ifdef U_DEBUG_TZ |
michael@0 | 46 | # include <stdio.h> |
michael@0 | 47 | # include "uresimp.h" // for debugging |
michael@0 | 48 | |
michael@0 | 49 | static void debug_tz_loc(const char *f, int32_t l) |
michael@0 | 50 | { |
michael@0 | 51 | fprintf(stderr, "%s:%d: ", f, l); |
michael@0 | 52 | } |
michael@0 | 53 | |
michael@0 | 54 | static void debug_tz_msg(const char *pat, ...) |
michael@0 | 55 | { |
michael@0 | 56 | va_list ap; |
michael@0 | 57 | va_start(ap, pat); |
michael@0 | 58 | vfprintf(stderr, pat, ap); |
michael@0 | 59 | fflush(stderr); |
michael@0 | 60 | } |
michael@0 | 61 | static char gStrBuf[256]; |
michael@0 | 62 | #define U_DEBUG_TZ_STR(x) u_austrncpy(gStrBuf,x,sizeof(gStrBuf)-1) |
michael@0 | 63 | // must use double parens, i.e.: U_DEBUG_TZ_MSG(("four is: %d",4)); |
michael@0 | 64 | #define U_DEBUG_TZ_MSG(x) {debug_tz_loc(__FILE__,__LINE__);debug_tz_msg x;} |
michael@0 | 65 | #else |
michael@0 | 66 | #define U_DEBUG_TZ_MSG(x) |
michael@0 | 67 | #endif |
michael@0 | 68 | |
michael@0 | 69 | #if !UCONFIG_NO_FORMATTING |
michael@0 | 70 | |
michael@0 | 71 | #include "unicode/simpletz.h" |
michael@0 | 72 | #include "unicode/calendar.h" |
michael@0 | 73 | #include "unicode/gregocal.h" |
michael@0 | 74 | #include "unicode/ures.h" |
michael@0 | 75 | #include "unicode/tzfmt.h" |
michael@0 | 76 | #include "unicode/numfmt.h" |
michael@0 | 77 | #include "gregoimp.h" |
michael@0 | 78 | #include "uresimp.h" // struct UResourceBundle |
michael@0 | 79 | #include "olsontz.h" |
michael@0 | 80 | #include "mutex.h" |
michael@0 | 81 | #include "unicode/udata.h" |
michael@0 | 82 | #include "ucln_in.h" |
michael@0 | 83 | #include "cstring.h" |
michael@0 | 84 | #include "cmemory.h" |
michael@0 | 85 | #include "unicode/strenum.h" |
michael@0 | 86 | #include "uassert.h" |
michael@0 | 87 | #include "zonemeta.h" |
michael@0 | 88 | |
michael@0 | 89 | #define kZONEINFO "zoneinfo64" |
michael@0 | 90 | #define kREGIONS "Regions" |
michael@0 | 91 | #define kZONES "Zones" |
michael@0 | 92 | #define kRULES "Rules" |
michael@0 | 93 | #define kNAMES "Names" |
michael@0 | 94 | #define kTZVERSION "TZVersion" |
michael@0 | 95 | #define kLINKS "links" |
michael@0 | 96 | #define kMAX_CUSTOM_HOUR 23 |
michael@0 | 97 | #define kMAX_CUSTOM_MIN 59 |
michael@0 | 98 | #define kMAX_CUSTOM_SEC 59 |
michael@0 | 99 | #define MINUS 0x002D |
michael@0 | 100 | #define PLUS 0x002B |
michael@0 | 101 | #define ZERO_DIGIT 0x0030 |
michael@0 | 102 | #define COLON 0x003A |
michael@0 | 103 | |
michael@0 | 104 | // Static data and constants |
michael@0 | 105 | |
michael@0 | 106 | static const UChar WORLD[] = {0x30, 0x30, 0x31, 0x00}; /* "001" */ |
michael@0 | 107 | |
michael@0 | 108 | static const UChar GMT_ID[] = {0x47, 0x4D, 0x54, 0x00}; /* "GMT" */ |
michael@0 | 109 | static const UChar UNKNOWN_ZONE_ID[] = {0x45, 0x74, 0x63, 0x2F, 0x55, 0x6E, 0x6B, 0x6E, 0x6F, 0x77, 0x6E, 0x00}; /* "Etc/Unknown" */ |
michael@0 | 110 | static const int32_t GMT_ID_LENGTH = 3; |
michael@0 | 111 | static const int32_t UNKNOWN_ZONE_ID_LENGTH = 11; |
michael@0 | 112 | |
michael@0 | 113 | static icu::TimeZone* DEFAULT_ZONE = NULL; |
michael@0 | 114 | static icu::UInitOnce gDefaultZoneInitOnce = U_INITONCE_INITIALIZER; |
michael@0 | 115 | |
michael@0 | 116 | static icu::TimeZone* _GMT = NULL; |
michael@0 | 117 | static icu::TimeZone* _UNKNOWN_ZONE = NULL; |
michael@0 | 118 | static icu::UInitOnce gStaticZonesInitOnce = U_INITONCE_INITIALIZER; |
michael@0 | 119 | |
michael@0 | 120 | static char TZDATA_VERSION[16]; |
michael@0 | 121 | static icu::UInitOnce gTZDataVersionInitOnce = U_INITONCE_INITIALIZER; |
michael@0 | 122 | |
michael@0 | 123 | static int32_t* MAP_SYSTEM_ZONES = NULL; |
michael@0 | 124 | static int32_t* MAP_CANONICAL_SYSTEM_ZONES = NULL; |
michael@0 | 125 | static int32_t* MAP_CANONICAL_SYSTEM_LOCATION_ZONES = NULL; |
michael@0 | 126 | |
michael@0 | 127 | static int32_t LEN_SYSTEM_ZONES = 0; |
michael@0 | 128 | static int32_t LEN_CANONICAL_SYSTEM_ZONES = 0; |
michael@0 | 129 | static int32_t LEN_CANONICAL_SYSTEM_LOCATION_ZONES = 0; |
michael@0 | 130 | |
michael@0 | 131 | static icu::UInitOnce gSystemZonesInitOnce = U_INITONCE_INITIALIZER; |
michael@0 | 132 | static icu::UInitOnce gCanonicalZonesInitOnce = U_INITONCE_INITIALIZER; |
michael@0 | 133 | static icu::UInitOnce gCanonicalLocationZonesInitOnce = U_INITONCE_INITIALIZER; |
michael@0 | 134 | |
michael@0 | 135 | U_CDECL_BEGIN |
michael@0 | 136 | static UBool U_CALLCONV timeZone_cleanup(void) |
michael@0 | 137 | { |
michael@0 | 138 | U_NAMESPACE_USE |
michael@0 | 139 | delete DEFAULT_ZONE; |
michael@0 | 140 | DEFAULT_ZONE = NULL; |
michael@0 | 141 | gDefaultZoneInitOnce.reset(); |
michael@0 | 142 | |
michael@0 | 143 | delete _GMT; |
michael@0 | 144 | _GMT = NULL; |
michael@0 | 145 | delete _UNKNOWN_ZONE; |
michael@0 | 146 | _UNKNOWN_ZONE = NULL; |
michael@0 | 147 | gStaticZonesInitOnce.reset(); |
michael@0 | 148 | |
michael@0 | 149 | uprv_memset(TZDATA_VERSION, 0, sizeof(TZDATA_VERSION)); |
michael@0 | 150 | gTZDataVersionInitOnce.reset(); |
michael@0 | 151 | |
michael@0 | 152 | LEN_SYSTEM_ZONES = 0; |
michael@0 | 153 | uprv_free(MAP_SYSTEM_ZONES); |
michael@0 | 154 | MAP_SYSTEM_ZONES = 0; |
michael@0 | 155 | gSystemZonesInitOnce.reset(); |
michael@0 | 156 | |
michael@0 | 157 | LEN_CANONICAL_SYSTEM_ZONES = 0; |
michael@0 | 158 | uprv_free(MAP_CANONICAL_SYSTEM_ZONES); |
michael@0 | 159 | MAP_CANONICAL_SYSTEM_ZONES = 0; |
michael@0 | 160 | gCanonicalZonesInitOnce.reset(); |
michael@0 | 161 | |
michael@0 | 162 | LEN_CANONICAL_SYSTEM_LOCATION_ZONES = 0; |
michael@0 | 163 | uprv_free(MAP_CANONICAL_SYSTEM_LOCATION_ZONES); |
michael@0 | 164 | MAP_CANONICAL_SYSTEM_LOCATION_ZONES = 0; |
michael@0 | 165 | gCanonicalLocationZonesInitOnce.reset(); |
michael@0 | 166 | |
michael@0 | 167 | return TRUE; |
michael@0 | 168 | } |
michael@0 | 169 | U_CDECL_END |
michael@0 | 170 | |
michael@0 | 171 | U_NAMESPACE_BEGIN |
michael@0 | 172 | |
michael@0 | 173 | static int32_t findInStringArray(UResourceBundle* array, const UnicodeString& id, UErrorCode &status) |
michael@0 | 174 | { |
michael@0 | 175 | UnicodeString copy; |
michael@0 | 176 | const UChar *u; |
michael@0 | 177 | int32_t len; |
michael@0 | 178 | |
michael@0 | 179 | int32_t start = 0; |
michael@0 | 180 | int32_t limit = ures_getSize(array); |
michael@0 | 181 | int32_t mid; |
michael@0 | 182 | int32_t lastMid = INT32_MAX; |
michael@0 | 183 | if(U_FAILURE(status) || (limit < 1)) { |
michael@0 | 184 | return -1; |
michael@0 | 185 | } |
michael@0 | 186 | U_DEBUG_TZ_MSG(("fisa: Looking for %s, between %d and %d\n", U_DEBUG_TZ_STR(UnicodeString(id).getTerminatedBuffer()), start, limit)); |
michael@0 | 187 | |
michael@0 | 188 | for (;;) { |
michael@0 | 189 | mid = (int32_t)((start + limit) / 2); |
michael@0 | 190 | if (lastMid == mid) { /* Have we moved? */ |
michael@0 | 191 | break; /* We haven't moved, and it wasn't found. */ |
michael@0 | 192 | } |
michael@0 | 193 | lastMid = mid; |
michael@0 | 194 | u = ures_getStringByIndex(array, mid, &len, &status); |
michael@0 | 195 | if (U_FAILURE(status)) { |
michael@0 | 196 | break; |
michael@0 | 197 | } |
michael@0 | 198 | U_DEBUG_TZ_MSG(("tz: compare to %s, %d .. [%d] .. %d\n", U_DEBUG_TZ_STR(u), start, mid, limit)); |
michael@0 | 199 | copy.setTo(TRUE, u, len); |
michael@0 | 200 | int r = id.compare(copy); |
michael@0 | 201 | if(r==0) { |
michael@0 | 202 | U_DEBUG_TZ_MSG(("fisa: found at %d\n", mid)); |
michael@0 | 203 | return mid; |
michael@0 | 204 | } else if(r<0) { |
michael@0 | 205 | limit = mid; |
michael@0 | 206 | } else { |
michael@0 | 207 | start = mid; |
michael@0 | 208 | } |
michael@0 | 209 | } |
michael@0 | 210 | U_DEBUG_TZ_MSG(("fisa: not found\n")); |
michael@0 | 211 | return -1; |
michael@0 | 212 | } |
michael@0 | 213 | |
michael@0 | 214 | /** |
michael@0 | 215 | * Fetch a specific zone by name. Replaces the getByKey call. |
michael@0 | 216 | * @param top Top timezone resource |
michael@0 | 217 | * @param id Time zone ID |
michael@0 | 218 | * @param oldbundle Bundle for reuse (or NULL). see 'ures_open()' |
michael@0 | 219 | * @return the zone's bundle if found, or undefined if error. Reuses oldbundle. |
michael@0 | 220 | */ |
michael@0 | 221 | static UResourceBundle* getZoneByName(const UResourceBundle* top, const UnicodeString& id, UResourceBundle *oldbundle, UErrorCode& status) { |
michael@0 | 222 | // load the Rules object |
michael@0 | 223 | UResourceBundle *tmp = ures_getByKey(top, kNAMES, NULL, &status); |
michael@0 | 224 | |
michael@0 | 225 | // search for the string |
michael@0 | 226 | int32_t idx = findInStringArray(tmp, id, status); |
michael@0 | 227 | |
michael@0 | 228 | if((idx == -1) && U_SUCCESS(status)) { |
michael@0 | 229 | // not found |
michael@0 | 230 | status = U_MISSING_RESOURCE_ERROR; |
michael@0 | 231 | //ures_close(oldbundle); |
michael@0 | 232 | //oldbundle = NULL; |
michael@0 | 233 | } else { |
michael@0 | 234 | U_DEBUG_TZ_MSG(("gzbn: oldbundle= size %d, type %d, %s\n", ures_getSize(tmp), ures_getType(tmp), u_errorName(status))); |
michael@0 | 235 | tmp = ures_getByKey(top, kZONES, tmp, &status); // get Zones object from top |
michael@0 | 236 | U_DEBUG_TZ_MSG(("gzbn: loaded ZONES, size %d, type %d, path %s %s\n", ures_getSize(tmp), ures_getType(tmp), ures_getPath(tmp), u_errorName(status))); |
michael@0 | 237 | oldbundle = ures_getByIndex(tmp, idx, oldbundle, &status); // get nth Zone object |
michael@0 | 238 | 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))); |
michael@0 | 239 | } |
michael@0 | 240 | ures_close(tmp); |
michael@0 | 241 | if(U_FAILURE(status)) { |
michael@0 | 242 | //ures_close(oldbundle); |
michael@0 | 243 | return NULL; |
michael@0 | 244 | } else { |
michael@0 | 245 | return oldbundle; |
michael@0 | 246 | } |
michael@0 | 247 | } |
michael@0 | 248 | |
michael@0 | 249 | |
michael@0 | 250 | UResourceBundle* TimeZone::loadRule(const UResourceBundle* top, const UnicodeString& ruleid, UResourceBundle* oldbundle, UErrorCode& status) { |
michael@0 | 251 | char key[64]; |
michael@0 | 252 | ruleid.extract(0, sizeof(key)-1, key, (int32_t)sizeof(key)-1, US_INV); |
michael@0 | 253 | U_DEBUG_TZ_MSG(("loadRule(%s)\n", key)); |
michael@0 | 254 | UResourceBundle *r = ures_getByKey(top, kRULES, oldbundle, &status); |
michael@0 | 255 | U_DEBUG_TZ_MSG(("loadRule(%s) -> kRULES [%s]\n", key, u_errorName(status))); |
michael@0 | 256 | r = ures_getByKey(r, key, r, &status); |
michael@0 | 257 | U_DEBUG_TZ_MSG(("loadRule(%s) -> item [%s]\n", key, u_errorName(status))); |
michael@0 | 258 | return r; |
michael@0 | 259 | } |
michael@0 | 260 | |
michael@0 | 261 | /** |
michael@0 | 262 | * Given an ID, open the appropriate resource for the given time zone. |
michael@0 | 263 | * Dereference aliases if necessary. |
michael@0 | 264 | * @param id zone id |
michael@0 | 265 | * @param res resource, which must be ready for use (initialized but not open) |
michael@0 | 266 | * @param ec input-output error code |
michael@0 | 267 | * @return top-level resource bundle |
michael@0 | 268 | */ |
michael@0 | 269 | static UResourceBundle* openOlsonResource(const UnicodeString& id, |
michael@0 | 270 | UResourceBundle& res, |
michael@0 | 271 | UErrorCode& ec) |
michael@0 | 272 | { |
michael@0 | 273 | #if U_DEBUG_TZ |
michael@0 | 274 | char buf[128]; |
michael@0 | 275 | id.extract(0, sizeof(buf)-1, buf, sizeof(buf), ""); |
michael@0 | 276 | #endif |
michael@0 | 277 | UResourceBundle *top = ures_openDirect(0, kZONEINFO, &ec); |
michael@0 | 278 | U_DEBUG_TZ_MSG(("pre: res sz=%d\n", ures_getSize(&res))); |
michael@0 | 279 | /* &res = */ getZoneByName(top, id, &res, ec); |
michael@0 | 280 | // Dereference if this is an alias. Docs say result should be 1 |
michael@0 | 281 | // but it is 0 in 2.8 (?). |
michael@0 | 282 | U_DEBUG_TZ_MSG(("Loading zone '%s' (%s, size %d) - %s\n", buf, ures_getKey((UResourceBundle*)&res), ures_getSize(&res), u_errorName(ec))); |
michael@0 | 283 | if (ures_getType(&res) == URES_INT) { |
michael@0 | 284 | int32_t deref = ures_getInt(&res, &ec) + 0; |
michael@0 | 285 | U_DEBUG_TZ_MSG(("getInt: %s - type is %d\n", u_errorName(ec), ures_getType(&res))); |
michael@0 | 286 | UResourceBundle *ares = ures_getByKey(top, kZONES, NULL, &ec); // dereference Zones section |
michael@0 | 287 | ures_getByIndex(ares, deref, &res, &ec); |
michael@0 | 288 | ures_close(ares); |
michael@0 | 289 | U_DEBUG_TZ_MSG(("alias to #%d (%s) - %s\n", deref, "??", u_errorName(ec))); |
michael@0 | 290 | } else { |
michael@0 | 291 | U_DEBUG_TZ_MSG(("not an alias - size %d\n", ures_getSize(&res))); |
michael@0 | 292 | } |
michael@0 | 293 | U_DEBUG_TZ_MSG(("%s - final status is %s\n", buf, u_errorName(ec))); |
michael@0 | 294 | return top; |
michael@0 | 295 | } |
michael@0 | 296 | |
michael@0 | 297 | // ------------------------------------- |
michael@0 | 298 | |
michael@0 | 299 | namespace { |
michael@0 | 300 | |
michael@0 | 301 | void U_CALLCONV initStaticTimeZones() { |
michael@0 | 302 | // Initialize _GMT independently of other static data; it should |
michael@0 | 303 | // be valid even if we can't load the time zone UDataMemory. |
michael@0 | 304 | ucln_i18n_registerCleanup(UCLN_I18N_TIMEZONE, timeZone_cleanup); |
michael@0 | 305 | _UNKNOWN_ZONE = new SimpleTimeZone(0, UnicodeString(TRUE, UNKNOWN_ZONE_ID, UNKNOWN_ZONE_ID_LENGTH)); |
michael@0 | 306 | _GMT = new SimpleTimeZone(0, UnicodeString(TRUE, GMT_ID, GMT_ID_LENGTH)); |
michael@0 | 307 | } |
michael@0 | 308 | |
michael@0 | 309 | } // anonymous namespace |
michael@0 | 310 | |
michael@0 | 311 | const TimeZone& U_EXPORT2 |
michael@0 | 312 | TimeZone::getUnknown() |
michael@0 | 313 | { |
michael@0 | 314 | umtx_initOnce(gStaticZonesInitOnce, &initStaticTimeZones); |
michael@0 | 315 | return *_UNKNOWN_ZONE; |
michael@0 | 316 | } |
michael@0 | 317 | |
michael@0 | 318 | const TimeZone* U_EXPORT2 |
michael@0 | 319 | TimeZone::getGMT(void) |
michael@0 | 320 | { |
michael@0 | 321 | umtx_initOnce(gStaticZonesInitOnce, &initStaticTimeZones); |
michael@0 | 322 | return _GMT; |
michael@0 | 323 | } |
michael@0 | 324 | |
michael@0 | 325 | // ***************************************************************************** |
michael@0 | 326 | // class TimeZone |
michael@0 | 327 | // ***************************************************************************** |
michael@0 | 328 | |
michael@0 | 329 | UOBJECT_DEFINE_ABSTRACT_RTTI_IMPLEMENTATION(TimeZone) |
michael@0 | 330 | |
michael@0 | 331 | TimeZone::TimeZone() |
michael@0 | 332 | : UObject(), fID() |
michael@0 | 333 | { |
michael@0 | 334 | } |
michael@0 | 335 | |
michael@0 | 336 | // ------------------------------------- |
michael@0 | 337 | |
michael@0 | 338 | TimeZone::TimeZone(const UnicodeString &id) |
michael@0 | 339 | : UObject(), fID(id) |
michael@0 | 340 | { |
michael@0 | 341 | } |
michael@0 | 342 | |
michael@0 | 343 | // ------------------------------------- |
michael@0 | 344 | |
michael@0 | 345 | TimeZone::~TimeZone() |
michael@0 | 346 | { |
michael@0 | 347 | } |
michael@0 | 348 | |
michael@0 | 349 | // ------------------------------------- |
michael@0 | 350 | |
michael@0 | 351 | TimeZone::TimeZone(const TimeZone &source) |
michael@0 | 352 | : UObject(source), fID(source.fID) |
michael@0 | 353 | { |
michael@0 | 354 | } |
michael@0 | 355 | |
michael@0 | 356 | // ------------------------------------- |
michael@0 | 357 | |
michael@0 | 358 | TimeZone & |
michael@0 | 359 | TimeZone::operator=(const TimeZone &right) |
michael@0 | 360 | { |
michael@0 | 361 | if (this != &right) fID = right.fID; |
michael@0 | 362 | return *this; |
michael@0 | 363 | } |
michael@0 | 364 | |
michael@0 | 365 | // ------------------------------------- |
michael@0 | 366 | |
michael@0 | 367 | UBool |
michael@0 | 368 | TimeZone::operator==(const TimeZone& that) const |
michael@0 | 369 | { |
michael@0 | 370 | return typeid(*this) == typeid(that) && |
michael@0 | 371 | fID == that.fID; |
michael@0 | 372 | } |
michael@0 | 373 | |
michael@0 | 374 | // ------------------------------------- |
michael@0 | 375 | |
michael@0 | 376 | namespace { |
michael@0 | 377 | TimeZone* |
michael@0 | 378 | createSystemTimeZone(const UnicodeString& id, UErrorCode& ec) { |
michael@0 | 379 | if (U_FAILURE(ec)) { |
michael@0 | 380 | return NULL; |
michael@0 | 381 | } |
michael@0 | 382 | TimeZone* z = 0; |
michael@0 | 383 | UResourceBundle res; |
michael@0 | 384 | ures_initStackObject(&res); |
michael@0 | 385 | U_DEBUG_TZ_MSG(("pre-err=%s\n", u_errorName(ec))); |
michael@0 | 386 | UResourceBundle *top = openOlsonResource(id, res, ec); |
michael@0 | 387 | U_DEBUG_TZ_MSG(("post-err=%s\n", u_errorName(ec))); |
michael@0 | 388 | if (U_SUCCESS(ec)) { |
michael@0 | 389 | z = new OlsonTimeZone(top, &res, id, ec); |
michael@0 | 390 | if (z == NULL) { |
michael@0 | 391 | U_DEBUG_TZ_MSG(("cstz: olson time zone failed to initialize - err %s\n", u_errorName(ec))); |
michael@0 | 392 | } |
michael@0 | 393 | } |
michael@0 | 394 | ures_close(&res); |
michael@0 | 395 | ures_close(top); |
michael@0 | 396 | if (U_FAILURE(ec)) { |
michael@0 | 397 | U_DEBUG_TZ_MSG(("cstz: failed to create, err %s\n", u_errorName(ec))); |
michael@0 | 398 | delete z; |
michael@0 | 399 | z = 0; |
michael@0 | 400 | } |
michael@0 | 401 | return z; |
michael@0 | 402 | } |
michael@0 | 403 | |
michael@0 | 404 | /** |
michael@0 | 405 | * Lookup the given name in our system zone table. If found, |
michael@0 | 406 | * instantiate a new zone of that name and return it. If not |
michael@0 | 407 | * found, return 0. |
michael@0 | 408 | */ |
michael@0 | 409 | TimeZone* |
michael@0 | 410 | createSystemTimeZone(const UnicodeString& id) { |
michael@0 | 411 | UErrorCode ec = U_ZERO_ERROR; |
michael@0 | 412 | return createSystemTimeZone(id, ec); |
michael@0 | 413 | } |
michael@0 | 414 | |
michael@0 | 415 | } |
michael@0 | 416 | |
michael@0 | 417 | TimeZone* U_EXPORT2 |
michael@0 | 418 | TimeZone::createTimeZone(const UnicodeString& ID) |
michael@0 | 419 | { |
michael@0 | 420 | /* We first try to lookup the zone ID in our system list. If this |
michael@0 | 421 | * fails, we try to parse it as a custom string GMT[+-]hh:mm. If |
michael@0 | 422 | * all else fails, we return GMT, which is probably not what the |
michael@0 | 423 | * user wants, but at least is a functioning TimeZone object. |
michael@0 | 424 | * |
michael@0 | 425 | * We cannot return NULL, because that would break compatibility |
michael@0 | 426 | * with the JDK. |
michael@0 | 427 | */ |
michael@0 | 428 | TimeZone* result = createSystemTimeZone(ID); |
michael@0 | 429 | |
michael@0 | 430 | if (result == 0) { |
michael@0 | 431 | U_DEBUG_TZ_MSG(("failed to load system time zone with id - falling to custom")); |
michael@0 | 432 | result = createCustomTimeZone(ID); |
michael@0 | 433 | } |
michael@0 | 434 | if (result == 0) { |
michael@0 | 435 | U_DEBUG_TZ_MSG(("failed to load time zone with id - falling to Etc/Unknown(GMT)")); |
michael@0 | 436 | result = getUnknown().clone(); |
michael@0 | 437 | } |
michael@0 | 438 | return result; |
michael@0 | 439 | } |
michael@0 | 440 | |
michael@0 | 441 | // ------------------------------------- |
michael@0 | 442 | |
michael@0 | 443 | /** |
michael@0 | 444 | * Initialize DEFAULT_ZONE from the system default time zone. |
michael@0 | 445 | * Upon return, DEFAULT_ZONE will not be NULL, unless operator new() |
michael@0 | 446 | * returns NULL. |
michael@0 | 447 | */ |
michael@0 | 448 | static void U_CALLCONV initDefault() |
michael@0 | 449 | { |
michael@0 | 450 | ucln_i18n_registerCleanup(UCLN_I18N_TIMEZONE, timeZone_cleanup); |
michael@0 | 451 | |
michael@0 | 452 | // If setDefault() has already been called we can skip getting the |
michael@0 | 453 | // default zone information from the system. |
michael@0 | 454 | if (DEFAULT_ZONE != NULL) { |
michael@0 | 455 | return; |
michael@0 | 456 | } |
michael@0 | 457 | |
michael@0 | 458 | // We access system timezone data through TPlatformUtilities, |
michael@0 | 459 | // including tzset(), timezone, and tzname[]. |
michael@0 | 460 | int32_t rawOffset = 0; |
michael@0 | 461 | const char *hostID; |
michael@0 | 462 | |
michael@0 | 463 | // First, try to create a system timezone, based |
michael@0 | 464 | // on the string ID in tzname[0]. |
michael@0 | 465 | |
michael@0 | 466 | // NOTE: this code is safely single threaded, being only |
michael@0 | 467 | // run via umtx_initOnce(). |
michael@0 | 468 | // |
michael@0 | 469 | // Some of the locale/timezone OS functions may not be thread safe, |
michael@0 | 470 | // |
michael@0 | 471 | // The operating system might actually use ICU to implement timezones. |
michael@0 | 472 | // So we may have ICU calling ICU here, like on AIX. |
michael@0 | 473 | // There shouldn't be a problem with this; initOnce does not hold a mutex |
michael@0 | 474 | // while the init function is being run. |
michael@0 | 475 | |
michael@0 | 476 | uprv_tzset(); // Initialize tz... system data |
michael@0 | 477 | |
michael@0 | 478 | // Get the timezone ID from the host. This function should do |
michael@0 | 479 | // any required host-specific remapping; e.g., on Windows this |
michael@0 | 480 | // function maps the Date and Time control panel setting to an |
michael@0 | 481 | // ICU timezone ID. |
michael@0 | 482 | hostID = uprv_tzname(0); |
michael@0 | 483 | |
michael@0 | 484 | // Invert sign because UNIX semantics are backwards |
michael@0 | 485 | rawOffset = uprv_timezone() * -U_MILLIS_PER_SECOND; |
michael@0 | 486 | |
michael@0 | 487 | TimeZone* default_zone = NULL; |
michael@0 | 488 | |
michael@0 | 489 | /* Make sure that the string is NULL terminated to prevent BoundsChecker/Purify warnings. */ |
michael@0 | 490 | UnicodeString hostStrID(hostID, -1, US_INV); |
michael@0 | 491 | hostStrID.append((UChar)0); |
michael@0 | 492 | hostStrID.truncate(hostStrID.length()-1); |
michael@0 | 493 | default_zone = createSystemTimeZone(hostStrID); |
michael@0 | 494 | |
michael@0 | 495 | #if U_PLATFORM_USES_ONLY_WIN32_API |
michael@0 | 496 | // hostID points to a heap-allocated location on Windows. |
michael@0 | 497 | uprv_free(const_cast<char *>(hostID)); |
michael@0 | 498 | #endif |
michael@0 | 499 | |
michael@0 | 500 | int32_t hostIDLen = hostStrID.length(); |
michael@0 | 501 | if (default_zone != NULL && rawOffset != default_zone->getRawOffset() |
michael@0 | 502 | && (3 <= hostIDLen && hostIDLen <= 4)) |
michael@0 | 503 | { |
michael@0 | 504 | // Uh oh. This probably wasn't a good id. |
michael@0 | 505 | // It was probably an ambiguous abbreviation |
michael@0 | 506 | delete default_zone; |
michael@0 | 507 | default_zone = NULL; |
michael@0 | 508 | } |
michael@0 | 509 | |
michael@0 | 510 | // Construct a fixed standard zone with the host's ID |
michael@0 | 511 | // and raw offset. |
michael@0 | 512 | if (default_zone == NULL) { |
michael@0 | 513 | default_zone = new SimpleTimeZone(rawOffset, hostStrID); |
michael@0 | 514 | } |
michael@0 | 515 | |
michael@0 | 516 | // If we _still_ don't have a time zone, use GMT. |
michael@0 | 517 | if (default_zone == NULL) { |
michael@0 | 518 | const TimeZone* temptz = TimeZone::getGMT(); |
michael@0 | 519 | // If we can't use GMT, get out. |
michael@0 | 520 | if (temptz == NULL) { |
michael@0 | 521 | return; |
michael@0 | 522 | } |
michael@0 | 523 | default_zone = temptz->clone(); |
michael@0 | 524 | } |
michael@0 | 525 | |
michael@0 | 526 | // The only way for DEFAULT_ZONE to be non-null at this point is if the user |
michael@0 | 527 | // made a thread-unsafe call to setDefault() or adoptDefault() in another |
michael@0 | 528 | // thread while this thread was doing something that required getting the default. |
michael@0 | 529 | U_ASSERT(DEFAULT_ZONE == NULL); |
michael@0 | 530 | |
michael@0 | 531 | DEFAULT_ZONE = default_zone; |
michael@0 | 532 | } |
michael@0 | 533 | |
michael@0 | 534 | // ------------------------------------- |
michael@0 | 535 | |
michael@0 | 536 | TimeZone* U_EXPORT2 |
michael@0 | 537 | TimeZone::createDefault() |
michael@0 | 538 | { |
michael@0 | 539 | umtx_initOnce(gDefaultZoneInitOnce, initDefault); |
michael@0 | 540 | return (DEFAULT_ZONE != NULL) ? DEFAULT_ZONE->clone() : NULL; |
michael@0 | 541 | } |
michael@0 | 542 | |
michael@0 | 543 | // ------------------------------------- |
michael@0 | 544 | |
michael@0 | 545 | void U_EXPORT2 |
michael@0 | 546 | TimeZone::adoptDefault(TimeZone* zone) |
michael@0 | 547 | { |
michael@0 | 548 | if (zone != NULL) |
michael@0 | 549 | { |
michael@0 | 550 | TimeZone *old = DEFAULT_ZONE; |
michael@0 | 551 | DEFAULT_ZONE = zone; |
michael@0 | 552 | delete old; |
michael@0 | 553 | ucln_i18n_registerCleanup(UCLN_I18N_TIMEZONE, timeZone_cleanup); |
michael@0 | 554 | } |
michael@0 | 555 | } |
michael@0 | 556 | // ------------------------------------- |
michael@0 | 557 | |
michael@0 | 558 | void U_EXPORT2 |
michael@0 | 559 | TimeZone::setDefault(const TimeZone& zone) |
michael@0 | 560 | { |
michael@0 | 561 | adoptDefault(zone.clone()); |
michael@0 | 562 | } |
michael@0 | 563 | |
michael@0 | 564 | //---------------------------------------------------------------------- |
michael@0 | 565 | |
michael@0 | 566 | |
michael@0 | 567 | static void U_CALLCONV initMap(USystemTimeZoneType type, UErrorCode& ec) { |
michael@0 | 568 | ucln_i18n_registerCleanup(UCLN_I18N_TIMEZONE, timeZone_cleanup); |
michael@0 | 569 | |
michael@0 | 570 | UResourceBundle *res = ures_openDirect(0, kZONEINFO, &ec); |
michael@0 | 571 | res = ures_getByKey(res, kNAMES, res, &ec); // dereference Zones section |
michael@0 | 572 | if (U_SUCCESS(ec)) { |
michael@0 | 573 | int32_t size = ures_getSize(res); |
michael@0 | 574 | int32_t *m = (int32_t *)uprv_malloc(size * sizeof(int32_t)); |
michael@0 | 575 | if (m == NULL) { |
michael@0 | 576 | ec = U_MEMORY_ALLOCATION_ERROR; |
michael@0 | 577 | } else { |
michael@0 | 578 | int32_t numEntries = 0; |
michael@0 | 579 | for (int32_t i = 0; i < size; i++) { |
michael@0 | 580 | UnicodeString id = ures_getUnicodeStringByIndex(res, i, &ec); |
michael@0 | 581 | if (U_FAILURE(ec)) { |
michael@0 | 582 | break; |
michael@0 | 583 | } |
michael@0 | 584 | if (0 == id.compare(UNKNOWN_ZONE_ID, UNKNOWN_ZONE_ID_LENGTH)) { |
michael@0 | 585 | // exclude Etc/Unknown |
michael@0 | 586 | continue; |
michael@0 | 587 | } |
michael@0 | 588 | if (type == UCAL_ZONE_TYPE_CANONICAL || type == UCAL_ZONE_TYPE_CANONICAL_LOCATION) { |
michael@0 | 589 | UnicodeString canonicalID; |
michael@0 | 590 | ZoneMeta::getCanonicalCLDRID(id, canonicalID, ec); |
michael@0 | 591 | if (U_FAILURE(ec)) { |
michael@0 | 592 | break; |
michael@0 | 593 | } |
michael@0 | 594 | if (canonicalID != id) { |
michael@0 | 595 | // exclude aliases |
michael@0 | 596 | continue; |
michael@0 | 597 | } |
michael@0 | 598 | } |
michael@0 | 599 | if (type == UCAL_ZONE_TYPE_CANONICAL_LOCATION) { |
michael@0 | 600 | const UChar *region = TimeZone::getRegion(id, ec); |
michael@0 | 601 | if (U_FAILURE(ec)) { |
michael@0 | 602 | break; |
michael@0 | 603 | } |
michael@0 | 604 | if (u_strcmp(region, WORLD) == 0) { |
michael@0 | 605 | // exclude non-location ("001") |
michael@0 | 606 | continue; |
michael@0 | 607 | } |
michael@0 | 608 | } |
michael@0 | 609 | m[numEntries++] = i; |
michael@0 | 610 | } |
michael@0 | 611 | if (U_SUCCESS(ec)) { |
michael@0 | 612 | int32_t *tmp = m; |
michael@0 | 613 | m = (int32_t *)uprv_realloc(tmp, numEntries * sizeof(int32_t)); |
michael@0 | 614 | if (m == NULL) { |
michael@0 | 615 | // realloc failed.. use the original one even it has unused |
michael@0 | 616 | // area at the end |
michael@0 | 617 | m = tmp; |
michael@0 | 618 | } |
michael@0 | 619 | |
michael@0 | 620 | switch(type) { |
michael@0 | 621 | case UCAL_ZONE_TYPE_ANY: |
michael@0 | 622 | U_ASSERT(MAP_SYSTEM_ZONES == NULL); |
michael@0 | 623 | MAP_SYSTEM_ZONES = m; |
michael@0 | 624 | LEN_SYSTEM_ZONES = numEntries; |
michael@0 | 625 | break; |
michael@0 | 626 | case UCAL_ZONE_TYPE_CANONICAL: |
michael@0 | 627 | U_ASSERT(MAP_CANONICAL_SYSTEM_ZONES == NULL); |
michael@0 | 628 | MAP_CANONICAL_SYSTEM_ZONES = m; |
michael@0 | 629 | LEN_CANONICAL_SYSTEM_ZONES = numEntries; |
michael@0 | 630 | break; |
michael@0 | 631 | case UCAL_ZONE_TYPE_CANONICAL_LOCATION: |
michael@0 | 632 | U_ASSERT(MAP_CANONICAL_SYSTEM_LOCATION_ZONES == NULL); |
michael@0 | 633 | MAP_CANONICAL_SYSTEM_LOCATION_ZONES = m; |
michael@0 | 634 | LEN_CANONICAL_SYSTEM_LOCATION_ZONES = numEntries; |
michael@0 | 635 | break; |
michael@0 | 636 | } |
michael@0 | 637 | } |
michael@0 | 638 | } |
michael@0 | 639 | } |
michael@0 | 640 | ures_close(res); |
michael@0 | 641 | } |
michael@0 | 642 | |
michael@0 | 643 | |
michael@0 | 644 | /** |
michael@0 | 645 | * This is the default implementation for subclasses that do not |
michael@0 | 646 | * override this method. This implementation calls through to the |
michael@0 | 647 | * 8-argument getOffset() method after suitable computations, and |
michael@0 | 648 | * correctly adjusts GMT millis to local millis when necessary. |
michael@0 | 649 | */ |
michael@0 | 650 | void TimeZone::getOffset(UDate date, UBool local, int32_t& rawOffset, |
michael@0 | 651 | int32_t& dstOffset, UErrorCode& ec) const { |
michael@0 | 652 | if (U_FAILURE(ec)) { |
michael@0 | 653 | return; |
michael@0 | 654 | } |
michael@0 | 655 | |
michael@0 | 656 | rawOffset = getRawOffset(); |
michael@0 | 657 | if (!local) { |
michael@0 | 658 | date += rawOffset; // now in local standard millis |
michael@0 | 659 | } |
michael@0 | 660 | |
michael@0 | 661 | // When local == TRUE, date might not be in local standard |
michael@0 | 662 | // millis. getOffset taking 7 parameters used here assume |
michael@0 | 663 | // the given time in day is local standard time. |
michael@0 | 664 | // At STD->DST transition, there is a range of time which |
michael@0 | 665 | // does not exist. When 'date' is in this time range |
michael@0 | 666 | // (and local == TRUE), this method interprets the specified |
michael@0 | 667 | // local time as DST. At DST->STD transition, there is a |
michael@0 | 668 | // range of time which occurs twice. In this case, this |
michael@0 | 669 | // method interprets the specified local time as STD. |
michael@0 | 670 | // To support the behavior above, we need to call getOffset |
michael@0 | 671 | // (with 7 args) twice when local == true and DST is |
michael@0 | 672 | // detected in the initial call. |
michael@0 | 673 | for (int32_t pass=0; ; ++pass) { |
michael@0 | 674 | int32_t year, month, dom, dow; |
michael@0 | 675 | double day = uprv_floor(date / U_MILLIS_PER_DAY); |
michael@0 | 676 | int32_t millis = (int32_t) (date - day * U_MILLIS_PER_DAY); |
michael@0 | 677 | |
michael@0 | 678 | Grego::dayToFields(day, year, month, dom, dow); |
michael@0 | 679 | |
michael@0 | 680 | dstOffset = getOffset(GregorianCalendar::AD, year, month, dom, |
michael@0 | 681 | (uint8_t) dow, millis, |
michael@0 | 682 | Grego::monthLength(year, month), |
michael@0 | 683 | ec) - rawOffset; |
michael@0 | 684 | |
michael@0 | 685 | // Recompute if local==TRUE, dstOffset!=0. |
michael@0 | 686 | if (pass!=0 || !local || dstOffset == 0) { |
michael@0 | 687 | break; |
michael@0 | 688 | } |
michael@0 | 689 | // adjust to local standard millis |
michael@0 | 690 | date -= dstOffset; |
michael@0 | 691 | } |
michael@0 | 692 | } |
michael@0 | 693 | |
michael@0 | 694 | // ------------------------------------- |
michael@0 | 695 | |
michael@0 | 696 | // New available IDs API as of ICU 2.4. Uses StringEnumeration API. |
michael@0 | 697 | |
michael@0 | 698 | class TZEnumeration : public StringEnumeration { |
michael@0 | 699 | private: |
michael@0 | 700 | |
michael@0 | 701 | // Map into to zones. Our results are zone[map[i]] for |
michael@0 | 702 | // i=0..len-1, where zone[i] is the i-th Olson zone. If map==NULL |
michael@0 | 703 | // then our results are zone[i] for i=0..len-1. Len will be zero |
michael@0 | 704 | // if the zone data could not be loaded. |
michael@0 | 705 | int32_t* map; |
michael@0 | 706 | int32_t* localMap; |
michael@0 | 707 | int32_t len; |
michael@0 | 708 | int32_t pos; |
michael@0 | 709 | |
michael@0 | 710 | TZEnumeration(int32_t* mapData, int32_t mapLen, UBool adoptMapData) : pos(0) { |
michael@0 | 711 | map = mapData; |
michael@0 | 712 | localMap = adoptMapData ? mapData : NULL; |
michael@0 | 713 | len = mapLen; |
michael@0 | 714 | } |
michael@0 | 715 | |
michael@0 | 716 | UBool getID(int32_t i) { |
michael@0 | 717 | UErrorCode ec = U_ZERO_ERROR; |
michael@0 | 718 | int32_t idLen = 0; |
michael@0 | 719 | const UChar* id = NULL; |
michael@0 | 720 | UResourceBundle *top = ures_openDirect(0, kZONEINFO, &ec); |
michael@0 | 721 | top = ures_getByKey(top, kNAMES, top, &ec); // dereference Zones section |
michael@0 | 722 | id = ures_getStringByIndex(top, i, &idLen, &ec); |
michael@0 | 723 | if(U_FAILURE(ec)) { |
michael@0 | 724 | unistr.truncate(0); |
michael@0 | 725 | } |
michael@0 | 726 | else { |
michael@0 | 727 | unistr.fastCopyFrom(UnicodeString(TRUE, id, idLen)); |
michael@0 | 728 | } |
michael@0 | 729 | ures_close(top); |
michael@0 | 730 | return U_SUCCESS(ec); |
michael@0 | 731 | } |
michael@0 | 732 | |
michael@0 | 733 | static int32_t* getMap(USystemTimeZoneType type, int32_t& len, UErrorCode& ec) { |
michael@0 | 734 | len = 0; |
michael@0 | 735 | if (U_FAILURE(ec)) { |
michael@0 | 736 | return NULL; |
michael@0 | 737 | } |
michael@0 | 738 | int32_t* m = NULL; |
michael@0 | 739 | switch (type) { |
michael@0 | 740 | case UCAL_ZONE_TYPE_ANY: |
michael@0 | 741 | umtx_initOnce(gSystemZonesInitOnce, &initMap, type, ec); |
michael@0 | 742 | m = MAP_SYSTEM_ZONES; |
michael@0 | 743 | len = LEN_SYSTEM_ZONES; |
michael@0 | 744 | break; |
michael@0 | 745 | case UCAL_ZONE_TYPE_CANONICAL: |
michael@0 | 746 | umtx_initOnce(gCanonicalZonesInitOnce, &initMap, type, ec); |
michael@0 | 747 | m = MAP_CANONICAL_SYSTEM_ZONES; |
michael@0 | 748 | len = LEN_CANONICAL_SYSTEM_ZONES; |
michael@0 | 749 | break; |
michael@0 | 750 | case UCAL_ZONE_TYPE_CANONICAL_LOCATION: |
michael@0 | 751 | umtx_initOnce(gCanonicalLocationZonesInitOnce, &initMap, type, ec); |
michael@0 | 752 | m = MAP_CANONICAL_SYSTEM_LOCATION_ZONES; |
michael@0 | 753 | len = LEN_CANONICAL_SYSTEM_LOCATION_ZONES; |
michael@0 | 754 | break; |
michael@0 | 755 | default: |
michael@0 | 756 | ec = U_ILLEGAL_ARGUMENT_ERROR; |
michael@0 | 757 | m = NULL; |
michael@0 | 758 | len = 0; |
michael@0 | 759 | break; |
michael@0 | 760 | } |
michael@0 | 761 | return m; |
michael@0 | 762 | } |
michael@0 | 763 | |
michael@0 | 764 | public: |
michael@0 | 765 | |
michael@0 | 766 | #define DEFAULT_FILTERED_MAP_SIZE 8 |
michael@0 | 767 | #define MAP_INCREMENT_SIZE 8 |
michael@0 | 768 | |
michael@0 | 769 | static TZEnumeration* create(USystemTimeZoneType type, const char* region, const int32_t* rawOffset, UErrorCode& ec) { |
michael@0 | 770 | if (U_FAILURE(ec)) { |
michael@0 | 771 | return NULL; |
michael@0 | 772 | } |
michael@0 | 773 | |
michael@0 | 774 | int32_t baseLen; |
michael@0 | 775 | int32_t *baseMap = getMap(type, baseLen, ec); |
michael@0 | 776 | |
michael@0 | 777 | if (U_FAILURE(ec)) { |
michael@0 | 778 | return NULL; |
michael@0 | 779 | } |
michael@0 | 780 | |
michael@0 | 781 | // If any additional conditions are available, |
michael@0 | 782 | // create instance local map filtered by the conditions. |
michael@0 | 783 | |
michael@0 | 784 | int32_t *filteredMap = NULL; |
michael@0 | 785 | int32_t numEntries = 0; |
michael@0 | 786 | |
michael@0 | 787 | if (region != NULL || rawOffset != NULL) { |
michael@0 | 788 | int32_t filteredMapSize = DEFAULT_FILTERED_MAP_SIZE; |
michael@0 | 789 | filteredMap = (int32_t *)uprv_malloc(filteredMapSize * sizeof(int32_t)); |
michael@0 | 790 | if (filteredMap == NULL) { |
michael@0 | 791 | ec = U_MEMORY_ALLOCATION_ERROR; |
michael@0 | 792 | return NULL; |
michael@0 | 793 | } |
michael@0 | 794 | |
michael@0 | 795 | // Walk through the base map |
michael@0 | 796 | UResourceBundle *res = ures_openDirect(0, kZONEINFO, &ec); |
michael@0 | 797 | res = ures_getByKey(res, kNAMES, res, &ec); // dereference Zones section |
michael@0 | 798 | for (int32_t i = 0; i < baseLen; i++) { |
michael@0 | 799 | int32_t zidx = baseMap[i]; |
michael@0 | 800 | UnicodeString id = ures_getUnicodeStringByIndex(res, zidx, &ec); |
michael@0 | 801 | if (U_FAILURE(ec)) { |
michael@0 | 802 | break; |
michael@0 | 803 | } |
michael@0 | 804 | if (region != NULL) { |
michael@0 | 805 | // Filter by region |
michael@0 | 806 | char tzregion[4]; // max 3 letters + null term |
michael@0 | 807 | TimeZone::getRegion(id, tzregion, sizeof(tzregion), ec); |
michael@0 | 808 | if (U_FAILURE(ec)) { |
michael@0 | 809 | break; |
michael@0 | 810 | } |
michael@0 | 811 | if (uprv_stricmp(tzregion, region) != 0) { |
michael@0 | 812 | // region does not match |
michael@0 | 813 | continue; |
michael@0 | 814 | } |
michael@0 | 815 | } |
michael@0 | 816 | if (rawOffset != NULL) { |
michael@0 | 817 | // Filter by raw offset |
michael@0 | 818 | // Note: This is VERY inefficient |
michael@0 | 819 | TimeZone *z = createSystemTimeZone(id, ec); |
michael@0 | 820 | if (U_FAILURE(ec)) { |
michael@0 | 821 | break; |
michael@0 | 822 | } |
michael@0 | 823 | int32_t tzoffset = z->getRawOffset(); |
michael@0 | 824 | delete z; |
michael@0 | 825 | |
michael@0 | 826 | if (tzoffset != *rawOffset) { |
michael@0 | 827 | continue; |
michael@0 | 828 | } |
michael@0 | 829 | } |
michael@0 | 830 | |
michael@0 | 831 | if (filteredMapSize <= numEntries) { |
michael@0 | 832 | filteredMapSize += MAP_INCREMENT_SIZE; |
michael@0 | 833 | int32_t *tmp = (int32_t *)uprv_realloc(filteredMap, filteredMapSize * sizeof(int32_t)); |
michael@0 | 834 | if (tmp == NULL) { |
michael@0 | 835 | ec = U_MEMORY_ALLOCATION_ERROR; |
michael@0 | 836 | break; |
michael@0 | 837 | } else { |
michael@0 | 838 | filteredMap = tmp; |
michael@0 | 839 | } |
michael@0 | 840 | } |
michael@0 | 841 | |
michael@0 | 842 | filteredMap[numEntries++] = zidx; |
michael@0 | 843 | } |
michael@0 | 844 | |
michael@0 | 845 | if (U_FAILURE(ec)) { |
michael@0 | 846 | uprv_free(filteredMap); |
michael@0 | 847 | filteredMap = NULL; |
michael@0 | 848 | } |
michael@0 | 849 | |
michael@0 | 850 | ures_close(res); |
michael@0 | 851 | } |
michael@0 | 852 | |
michael@0 | 853 | TZEnumeration *result = NULL; |
michael@0 | 854 | if (U_SUCCESS(ec)) { |
michael@0 | 855 | // Finally, create a new enumeration instance |
michael@0 | 856 | if (filteredMap == NULL) { |
michael@0 | 857 | result = new TZEnumeration(baseMap, baseLen, FALSE); |
michael@0 | 858 | } else { |
michael@0 | 859 | result = new TZEnumeration(filteredMap, numEntries, TRUE); |
michael@0 | 860 | filteredMap = NULL; |
michael@0 | 861 | } |
michael@0 | 862 | if (result == NULL) { |
michael@0 | 863 | ec = U_MEMORY_ALLOCATION_ERROR; |
michael@0 | 864 | } |
michael@0 | 865 | } |
michael@0 | 866 | |
michael@0 | 867 | if (filteredMap != NULL) { |
michael@0 | 868 | uprv_free(filteredMap); |
michael@0 | 869 | } |
michael@0 | 870 | |
michael@0 | 871 | return result; |
michael@0 | 872 | } |
michael@0 | 873 | |
michael@0 | 874 | TZEnumeration(const TZEnumeration &other) : StringEnumeration(), map(NULL), localMap(NULL), len(0), pos(0) { |
michael@0 | 875 | if (other.localMap != NULL) { |
michael@0 | 876 | localMap = (int32_t *)uprv_malloc(other.len * sizeof(int32_t)); |
michael@0 | 877 | if (localMap != NULL) { |
michael@0 | 878 | len = other.len; |
michael@0 | 879 | uprv_memcpy(localMap, other.localMap, len * sizeof(int32_t)); |
michael@0 | 880 | pos = other.pos; |
michael@0 | 881 | map = localMap; |
michael@0 | 882 | } else { |
michael@0 | 883 | len = 0; |
michael@0 | 884 | pos = 0; |
michael@0 | 885 | map = NULL; |
michael@0 | 886 | } |
michael@0 | 887 | } else { |
michael@0 | 888 | map = other.map; |
michael@0 | 889 | localMap = NULL; |
michael@0 | 890 | len = other.len; |
michael@0 | 891 | pos = other.pos; |
michael@0 | 892 | } |
michael@0 | 893 | } |
michael@0 | 894 | |
michael@0 | 895 | virtual ~TZEnumeration(); |
michael@0 | 896 | |
michael@0 | 897 | virtual StringEnumeration *clone() const { |
michael@0 | 898 | return new TZEnumeration(*this); |
michael@0 | 899 | } |
michael@0 | 900 | |
michael@0 | 901 | virtual int32_t count(UErrorCode& status) const { |
michael@0 | 902 | return U_FAILURE(status) ? 0 : len; |
michael@0 | 903 | } |
michael@0 | 904 | |
michael@0 | 905 | virtual const UnicodeString* snext(UErrorCode& status) { |
michael@0 | 906 | if (U_SUCCESS(status) && map != NULL && pos < len) { |
michael@0 | 907 | getID(map[pos]); |
michael@0 | 908 | ++pos; |
michael@0 | 909 | return &unistr; |
michael@0 | 910 | } |
michael@0 | 911 | return 0; |
michael@0 | 912 | } |
michael@0 | 913 | |
michael@0 | 914 | virtual void reset(UErrorCode& /*status*/) { |
michael@0 | 915 | pos = 0; |
michael@0 | 916 | } |
michael@0 | 917 | |
michael@0 | 918 | public: |
michael@0 | 919 | static UClassID U_EXPORT2 getStaticClassID(void); |
michael@0 | 920 | virtual UClassID getDynamicClassID(void) const; |
michael@0 | 921 | }; |
michael@0 | 922 | |
michael@0 | 923 | TZEnumeration::~TZEnumeration() { |
michael@0 | 924 | if (localMap != NULL) { |
michael@0 | 925 | uprv_free(localMap); |
michael@0 | 926 | } |
michael@0 | 927 | } |
michael@0 | 928 | |
michael@0 | 929 | UOBJECT_DEFINE_RTTI_IMPLEMENTATION(TZEnumeration) |
michael@0 | 930 | |
michael@0 | 931 | StringEnumeration* U_EXPORT2 |
michael@0 | 932 | TimeZone::createTimeZoneIDEnumeration( |
michael@0 | 933 | USystemTimeZoneType zoneType, |
michael@0 | 934 | const char* region, |
michael@0 | 935 | const int32_t* rawOffset, |
michael@0 | 936 | UErrorCode& ec) { |
michael@0 | 937 | return TZEnumeration::create(zoneType, region, rawOffset, ec); |
michael@0 | 938 | } |
michael@0 | 939 | |
michael@0 | 940 | StringEnumeration* U_EXPORT2 |
michael@0 | 941 | TimeZone::createEnumeration() { |
michael@0 | 942 | UErrorCode ec = U_ZERO_ERROR; |
michael@0 | 943 | return TZEnumeration::create(UCAL_ZONE_TYPE_ANY, NULL, NULL, ec); |
michael@0 | 944 | } |
michael@0 | 945 | |
michael@0 | 946 | StringEnumeration* U_EXPORT2 |
michael@0 | 947 | TimeZone::createEnumeration(int32_t rawOffset) { |
michael@0 | 948 | UErrorCode ec = U_ZERO_ERROR; |
michael@0 | 949 | return TZEnumeration::create(UCAL_ZONE_TYPE_ANY, NULL, &rawOffset, ec); |
michael@0 | 950 | } |
michael@0 | 951 | |
michael@0 | 952 | StringEnumeration* U_EXPORT2 |
michael@0 | 953 | TimeZone::createEnumeration(const char* country) { |
michael@0 | 954 | UErrorCode ec = U_ZERO_ERROR; |
michael@0 | 955 | return TZEnumeration::create(UCAL_ZONE_TYPE_ANY, country, NULL, ec); |
michael@0 | 956 | } |
michael@0 | 957 | |
michael@0 | 958 | // --------------------------------------- |
michael@0 | 959 | |
michael@0 | 960 | int32_t U_EXPORT2 |
michael@0 | 961 | TimeZone::countEquivalentIDs(const UnicodeString& id) { |
michael@0 | 962 | int32_t result = 0; |
michael@0 | 963 | UErrorCode ec = U_ZERO_ERROR; |
michael@0 | 964 | UResourceBundle res; |
michael@0 | 965 | ures_initStackObject(&res); |
michael@0 | 966 | U_DEBUG_TZ_MSG(("countEquivalentIDs..\n")); |
michael@0 | 967 | UResourceBundle *top = openOlsonResource(id, res, ec); |
michael@0 | 968 | if (U_SUCCESS(ec)) { |
michael@0 | 969 | UResourceBundle r; |
michael@0 | 970 | ures_initStackObject(&r); |
michael@0 | 971 | ures_getByKey(&res, kLINKS, &r, &ec); |
michael@0 | 972 | ures_getIntVector(&r, &result, &ec); |
michael@0 | 973 | ures_close(&r); |
michael@0 | 974 | } |
michael@0 | 975 | ures_close(&res); |
michael@0 | 976 | ures_close(top); |
michael@0 | 977 | return result; |
michael@0 | 978 | } |
michael@0 | 979 | |
michael@0 | 980 | // --------------------------------------- |
michael@0 | 981 | |
michael@0 | 982 | const UnicodeString U_EXPORT2 |
michael@0 | 983 | TimeZone::getEquivalentID(const UnicodeString& id, int32_t index) { |
michael@0 | 984 | U_DEBUG_TZ_MSG(("gEI(%d)\n", index)); |
michael@0 | 985 | UnicodeString result; |
michael@0 | 986 | UErrorCode ec = U_ZERO_ERROR; |
michael@0 | 987 | UResourceBundle res; |
michael@0 | 988 | ures_initStackObject(&res); |
michael@0 | 989 | UResourceBundle *top = openOlsonResource(id, res, ec); |
michael@0 | 990 | int32_t zone = -1; |
michael@0 | 991 | if (U_SUCCESS(ec)) { |
michael@0 | 992 | UResourceBundle r; |
michael@0 | 993 | ures_initStackObject(&r); |
michael@0 | 994 | int32_t size; |
michael@0 | 995 | ures_getByKey(&res, kLINKS, &r, &ec); |
michael@0 | 996 | const int32_t* v = ures_getIntVector(&r, &size, &ec); |
michael@0 | 997 | if (U_SUCCESS(ec)) { |
michael@0 | 998 | if (index >= 0 && index < size) { |
michael@0 | 999 | zone = v[index]; |
michael@0 | 1000 | } |
michael@0 | 1001 | } |
michael@0 | 1002 | ures_close(&r); |
michael@0 | 1003 | } |
michael@0 | 1004 | ures_close(&res); |
michael@0 | 1005 | if (zone >= 0) { |
michael@0 | 1006 | UResourceBundle *ares = ures_getByKey(top, kNAMES, NULL, &ec); // dereference Zones section |
michael@0 | 1007 | if (U_SUCCESS(ec)) { |
michael@0 | 1008 | int32_t idLen = 0; |
michael@0 | 1009 | const UChar* id = ures_getStringByIndex(ares, zone, &idLen, &ec); |
michael@0 | 1010 | result.fastCopyFrom(UnicodeString(TRUE, id, idLen)); |
michael@0 | 1011 | U_DEBUG_TZ_MSG(("gei(%d) -> %d, len%d, %s\n", index, zone, result.length(), u_errorName(ec))); |
michael@0 | 1012 | } |
michael@0 | 1013 | ures_close(ares); |
michael@0 | 1014 | } |
michael@0 | 1015 | ures_close(top); |
michael@0 | 1016 | #if defined(U_DEBUG_TZ) |
michael@0 | 1017 | if(result.length() ==0) { |
michael@0 | 1018 | U_DEBUG_TZ_MSG(("equiv [__, #%d] -> 0 (%s)\n", index, u_errorName(ec))); |
michael@0 | 1019 | } |
michael@0 | 1020 | #endif |
michael@0 | 1021 | return result; |
michael@0 | 1022 | } |
michael@0 | 1023 | |
michael@0 | 1024 | // --------------------------------------- |
michael@0 | 1025 | |
michael@0 | 1026 | // These methods are used by ZoneMeta class only. |
michael@0 | 1027 | |
michael@0 | 1028 | const UChar* |
michael@0 | 1029 | TimeZone::findID(const UnicodeString& id) { |
michael@0 | 1030 | const UChar *result = NULL; |
michael@0 | 1031 | UErrorCode ec = U_ZERO_ERROR; |
michael@0 | 1032 | UResourceBundle *rb = ures_openDirect(NULL, kZONEINFO, &ec); |
michael@0 | 1033 | |
michael@0 | 1034 | // resolve zone index by name |
michael@0 | 1035 | UResourceBundle *names = ures_getByKey(rb, kNAMES, NULL, &ec); |
michael@0 | 1036 | int32_t idx = findInStringArray(names, id, ec); |
michael@0 | 1037 | result = ures_getStringByIndex(names, idx, NULL, &ec); |
michael@0 | 1038 | if (U_FAILURE(ec)) { |
michael@0 | 1039 | result = NULL; |
michael@0 | 1040 | } |
michael@0 | 1041 | ures_close(names); |
michael@0 | 1042 | ures_close(rb); |
michael@0 | 1043 | return result; |
michael@0 | 1044 | } |
michael@0 | 1045 | |
michael@0 | 1046 | |
michael@0 | 1047 | const UChar* |
michael@0 | 1048 | TimeZone::dereferOlsonLink(const UnicodeString& id) { |
michael@0 | 1049 | const UChar *result = NULL; |
michael@0 | 1050 | UErrorCode ec = U_ZERO_ERROR; |
michael@0 | 1051 | UResourceBundle *rb = ures_openDirect(NULL, kZONEINFO, &ec); |
michael@0 | 1052 | |
michael@0 | 1053 | // resolve zone index by name |
michael@0 | 1054 | UResourceBundle *names = ures_getByKey(rb, kNAMES, NULL, &ec); |
michael@0 | 1055 | int32_t idx = findInStringArray(names, id, ec); |
michael@0 | 1056 | result = ures_getStringByIndex(names, idx, NULL, &ec); |
michael@0 | 1057 | |
michael@0 | 1058 | // open the zone bundle by index |
michael@0 | 1059 | ures_getByKey(rb, kZONES, rb, &ec); |
michael@0 | 1060 | ures_getByIndex(rb, idx, rb, &ec); |
michael@0 | 1061 | |
michael@0 | 1062 | if (U_SUCCESS(ec)) { |
michael@0 | 1063 | if (ures_getType(rb) == URES_INT) { |
michael@0 | 1064 | // this is a link - dereference the link |
michael@0 | 1065 | int32_t deref = ures_getInt(rb, &ec); |
michael@0 | 1066 | const UChar* tmp = ures_getStringByIndex(names, deref, NULL, &ec); |
michael@0 | 1067 | if (U_SUCCESS(ec)) { |
michael@0 | 1068 | result = tmp; |
michael@0 | 1069 | } |
michael@0 | 1070 | } |
michael@0 | 1071 | } |
michael@0 | 1072 | |
michael@0 | 1073 | ures_close(names); |
michael@0 | 1074 | ures_close(rb); |
michael@0 | 1075 | |
michael@0 | 1076 | return result; |
michael@0 | 1077 | } |
michael@0 | 1078 | |
michael@0 | 1079 | const UChar* |
michael@0 | 1080 | TimeZone::getRegion(const UnicodeString& id) { |
michael@0 | 1081 | UErrorCode status = U_ZERO_ERROR; |
michael@0 | 1082 | return getRegion(id, status); |
michael@0 | 1083 | } |
michael@0 | 1084 | |
michael@0 | 1085 | const UChar* |
michael@0 | 1086 | TimeZone::getRegion(const UnicodeString& id, UErrorCode& status) { |
michael@0 | 1087 | if (U_FAILURE(status)) { |
michael@0 | 1088 | return NULL; |
michael@0 | 1089 | } |
michael@0 | 1090 | const UChar *result = NULL; |
michael@0 | 1091 | UResourceBundle *rb = ures_openDirect(NULL, kZONEINFO, &status); |
michael@0 | 1092 | |
michael@0 | 1093 | // resolve zone index by name |
michael@0 | 1094 | UResourceBundle *res = ures_getByKey(rb, kNAMES, NULL, &status); |
michael@0 | 1095 | int32_t idx = findInStringArray(res, id, status); |
michael@0 | 1096 | |
michael@0 | 1097 | // get region mapping |
michael@0 | 1098 | ures_getByKey(rb, kREGIONS, res, &status); |
michael@0 | 1099 | const UChar *tmp = ures_getStringByIndex(res, idx, NULL, &status); |
michael@0 | 1100 | if (U_SUCCESS(status)) { |
michael@0 | 1101 | result = tmp; |
michael@0 | 1102 | } |
michael@0 | 1103 | |
michael@0 | 1104 | ures_close(res); |
michael@0 | 1105 | ures_close(rb); |
michael@0 | 1106 | |
michael@0 | 1107 | return result; |
michael@0 | 1108 | } |
michael@0 | 1109 | |
michael@0 | 1110 | |
michael@0 | 1111 | // --------------------------------------- |
michael@0 | 1112 | int32_t |
michael@0 | 1113 | TimeZone::getRegion(const UnicodeString& id, char *region, int32_t capacity, UErrorCode& status) |
michael@0 | 1114 | { |
michael@0 | 1115 | int32_t resultLen = 0; |
michael@0 | 1116 | *region = 0; |
michael@0 | 1117 | if (U_FAILURE(status)) { |
michael@0 | 1118 | return 0; |
michael@0 | 1119 | } |
michael@0 | 1120 | |
michael@0 | 1121 | const UChar *uregion = NULL; |
michael@0 | 1122 | // "Etc/Unknown" is not a system zone ID, |
michael@0 | 1123 | // but in the zone data |
michael@0 | 1124 | if (id.compare(UNKNOWN_ZONE_ID, UNKNOWN_ZONE_ID_LENGTH) != 0) { |
michael@0 | 1125 | uregion = getRegion(id); |
michael@0 | 1126 | } |
michael@0 | 1127 | if (uregion == NULL) { |
michael@0 | 1128 | status = U_ILLEGAL_ARGUMENT_ERROR; |
michael@0 | 1129 | return 0; |
michael@0 | 1130 | } |
michael@0 | 1131 | resultLen = u_strlen(uregion); |
michael@0 | 1132 | // A region code is represented by invariant characters |
michael@0 | 1133 | u_UCharsToChars(uregion, region, uprv_min(resultLen, capacity)); |
michael@0 | 1134 | |
michael@0 | 1135 | if (capacity < resultLen) { |
michael@0 | 1136 | status = U_BUFFER_OVERFLOW_ERROR; |
michael@0 | 1137 | return resultLen; |
michael@0 | 1138 | } |
michael@0 | 1139 | |
michael@0 | 1140 | return u_terminateChars(region, capacity, resultLen, &status); |
michael@0 | 1141 | } |
michael@0 | 1142 | |
michael@0 | 1143 | // --------------------------------------- |
michael@0 | 1144 | |
michael@0 | 1145 | |
michael@0 | 1146 | UnicodeString& |
michael@0 | 1147 | TimeZone::getDisplayName(UnicodeString& result) const |
michael@0 | 1148 | { |
michael@0 | 1149 | return getDisplayName(FALSE,LONG,Locale::getDefault(), result); |
michael@0 | 1150 | } |
michael@0 | 1151 | |
michael@0 | 1152 | UnicodeString& |
michael@0 | 1153 | TimeZone::getDisplayName(const Locale& locale, UnicodeString& result) const |
michael@0 | 1154 | { |
michael@0 | 1155 | return getDisplayName(FALSE, LONG, locale, result); |
michael@0 | 1156 | } |
michael@0 | 1157 | |
michael@0 | 1158 | UnicodeString& |
michael@0 | 1159 | TimeZone::getDisplayName(UBool daylight, EDisplayType style, UnicodeString& result) const |
michael@0 | 1160 | { |
michael@0 | 1161 | return getDisplayName(daylight,style, Locale::getDefault(), result); |
michael@0 | 1162 | } |
michael@0 | 1163 | //-------------------------------------- |
michael@0 | 1164 | int32_t |
michael@0 | 1165 | TimeZone::getDSTSavings()const { |
michael@0 | 1166 | if (useDaylightTime()) { |
michael@0 | 1167 | return 3600000; |
michael@0 | 1168 | } |
michael@0 | 1169 | return 0; |
michael@0 | 1170 | } |
michael@0 | 1171 | //--------------------------------------- |
michael@0 | 1172 | UnicodeString& |
michael@0 | 1173 | TimeZone::getDisplayName(UBool daylight, EDisplayType style, const Locale& locale, UnicodeString& result) const |
michael@0 | 1174 | { |
michael@0 | 1175 | UErrorCode status = U_ZERO_ERROR; |
michael@0 | 1176 | UDate date = Calendar::getNow(); |
michael@0 | 1177 | UTimeZoneFormatTimeType timeType; |
michael@0 | 1178 | int32_t offset; |
michael@0 | 1179 | |
michael@0 | 1180 | if (style == GENERIC_LOCATION || style == LONG_GENERIC || style == SHORT_GENERIC) { |
michael@0 | 1181 | LocalPointer<TimeZoneFormat> tzfmt(TimeZoneFormat::createInstance(locale, status)); |
michael@0 | 1182 | if (U_FAILURE(status)) { |
michael@0 | 1183 | result.remove(); |
michael@0 | 1184 | return result; |
michael@0 | 1185 | } |
michael@0 | 1186 | // Generic format |
michael@0 | 1187 | switch (style) { |
michael@0 | 1188 | case GENERIC_LOCATION: |
michael@0 | 1189 | tzfmt->format(UTZFMT_STYLE_GENERIC_LOCATION, *this, date, result, &timeType); |
michael@0 | 1190 | break; |
michael@0 | 1191 | case LONG_GENERIC: |
michael@0 | 1192 | tzfmt->format(UTZFMT_STYLE_GENERIC_LONG, *this, date, result, &timeType); |
michael@0 | 1193 | break; |
michael@0 | 1194 | case SHORT_GENERIC: |
michael@0 | 1195 | tzfmt->format(UTZFMT_STYLE_GENERIC_SHORT, *this, date, result, &timeType); |
michael@0 | 1196 | break; |
michael@0 | 1197 | default: |
michael@0 | 1198 | U_ASSERT(FALSE); |
michael@0 | 1199 | } |
michael@0 | 1200 | // Generic format many use Localized GMT as the final fallback. |
michael@0 | 1201 | // When Localized GMT format is used, the result might not be |
michael@0 | 1202 | // appropriate for the requested daylight value. |
michael@0 | 1203 | if ((daylight && timeType == UTZFMT_TIME_TYPE_STANDARD) || (!daylight && timeType == UTZFMT_TIME_TYPE_DAYLIGHT)) { |
michael@0 | 1204 | offset = daylight ? getRawOffset() + getDSTSavings() : getRawOffset(); |
michael@0 | 1205 | if (style == SHORT_GENERIC) { |
michael@0 | 1206 | tzfmt->formatOffsetShortLocalizedGMT(offset, result, status); |
michael@0 | 1207 | } else { |
michael@0 | 1208 | tzfmt->formatOffsetLocalizedGMT(offset, result, status); |
michael@0 | 1209 | } |
michael@0 | 1210 | } |
michael@0 | 1211 | } else if (style == LONG_GMT || style == SHORT_GMT) { |
michael@0 | 1212 | LocalPointer<TimeZoneFormat> tzfmt(TimeZoneFormat::createInstance(locale, status)); |
michael@0 | 1213 | if (U_FAILURE(status)) { |
michael@0 | 1214 | result.remove(); |
michael@0 | 1215 | return result; |
michael@0 | 1216 | } |
michael@0 | 1217 | offset = daylight && useDaylightTime() ? getRawOffset() + getDSTSavings() : getRawOffset(); |
michael@0 | 1218 | switch (style) { |
michael@0 | 1219 | case LONG_GMT: |
michael@0 | 1220 | tzfmt->formatOffsetLocalizedGMT(offset, result, status); |
michael@0 | 1221 | break; |
michael@0 | 1222 | case SHORT_GMT: |
michael@0 | 1223 | tzfmt->formatOffsetISO8601Basic(offset, FALSE, FALSE, FALSE, result, status); |
michael@0 | 1224 | break; |
michael@0 | 1225 | default: |
michael@0 | 1226 | U_ASSERT(FALSE); |
michael@0 | 1227 | } |
michael@0 | 1228 | |
michael@0 | 1229 | } else { |
michael@0 | 1230 | U_ASSERT(style == LONG || style == SHORT || style == SHORT_COMMONLY_USED); |
michael@0 | 1231 | UTimeZoneNameType nameType = UTZNM_UNKNOWN; |
michael@0 | 1232 | switch (style) { |
michael@0 | 1233 | case LONG: |
michael@0 | 1234 | nameType = daylight ? UTZNM_LONG_DAYLIGHT : UTZNM_LONG_STANDARD; |
michael@0 | 1235 | break; |
michael@0 | 1236 | case SHORT: |
michael@0 | 1237 | case SHORT_COMMONLY_USED: |
michael@0 | 1238 | nameType = daylight ? UTZNM_SHORT_DAYLIGHT : UTZNM_SHORT_STANDARD; |
michael@0 | 1239 | break; |
michael@0 | 1240 | default: |
michael@0 | 1241 | U_ASSERT(FALSE); |
michael@0 | 1242 | } |
michael@0 | 1243 | LocalPointer<TimeZoneNames> tznames(TimeZoneNames::createInstance(locale, status)); |
michael@0 | 1244 | if (U_FAILURE(status)) { |
michael@0 | 1245 | result.remove(); |
michael@0 | 1246 | return result; |
michael@0 | 1247 | } |
michael@0 | 1248 | UnicodeString canonicalID(ZoneMeta::getCanonicalCLDRID(*this)); |
michael@0 | 1249 | tznames->getDisplayName(canonicalID, nameType, date, result); |
michael@0 | 1250 | if (result.isEmpty()) { |
michael@0 | 1251 | // Fallback to localized GMT |
michael@0 | 1252 | LocalPointer<TimeZoneFormat> tzfmt(TimeZoneFormat::createInstance(locale, status)); |
michael@0 | 1253 | offset = daylight && useDaylightTime() ? getRawOffset() + getDSTSavings() : getRawOffset(); |
michael@0 | 1254 | if (style == LONG) { |
michael@0 | 1255 | tzfmt->formatOffsetLocalizedGMT(offset, result, status); |
michael@0 | 1256 | } else { |
michael@0 | 1257 | tzfmt->formatOffsetShortLocalizedGMT(offset, result, status); |
michael@0 | 1258 | } |
michael@0 | 1259 | } |
michael@0 | 1260 | } |
michael@0 | 1261 | if (U_FAILURE(status)) { |
michael@0 | 1262 | result.remove(); |
michael@0 | 1263 | } |
michael@0 | 1264 | return result; |
michael@0 | 1265 | } |
michael@0 | 1266 | |
michael@0 | 1267 | /** |
michael@0 | 1268 | * Parse a custom time zone identifier and return a corresponding zone. |
michael@0 | 1269 | * @param id a string of the form GMT[+-]hh:mm, GMT[+-]hhmm, or |
michael@0 | 1270 | * GMT[+-]hh. |
michael@0 | 1271 | * @return a newly created SimpleTimeZone with the given offset and |
michael@0 | 1272 | * no Daylight Savings Time, or null if the id cannot be parsed. |
michael@0 | 1273 | */ |
michael@0 | 1274 | TimeZone* |
michael@0 | 1275 | TimeZone::createCustomTimeZone(const UnicodeString& id) |
michael@0 | 1276 | { |
michael@0 | 1277 | int32_t sign, hour, min, sec; |
michael@0 | 1278 | if (parseCustomID(id, sign, hour, min, sec)) { |
michael@0 | 1279 | UnicodeString customID; |
michael@0 | 1280 | formatCustomID(hour, min, sec, (sign < 0), customID); |
michael@0 | 1281 | int32_t offset = sign * ((hour * 60 + min) * 60 + sec) * 1000; |
michael@0 | 1282 | return new SimpleTimeZone(offset, customID); |
michael@0 | 1283 | } |
michael@0 | 1284 | return NULL; |
michael@0 | 1285 | } |
michael@0 | 1286 | |
michael@0 | 1287 | UnicodeString& |
michael@0 | 1288 | TimeZone::getCustomID(const UnicodeString& id, UnicodeString& normalized, UErrorCode& status) { |
michael@0 | 1289 | normalized.remove(); |
michael@0 | 1290 | if (U_FAILURE(status)) { |
michael@0 | 1291 | return normalized; |
michael@0 | 1292 | } |
michael@0 | 1293 | int32_t sign, hour, min, sec; |
michael@0 | 1294 | if (parseCustomID(id, sign, hour, min, sec)) { |
michael@0 | 1295 | formatCustomID(hour, min, sec, (sign < 0), normalized); |
michael@0 | 1296 | } |
michael@0 | 1297 | return normalized; |
michael@0 | 1298 | } |
michael@0 | 1299 | |
michael@0 | 1300 | UBool |
michael@0 | 1301 | TimeZone::parseCustomID(const UnicodeString& id, int32_t& sign, |
michael@0 | 1302 | int32_t& hour, int32_t& min, int32_t& sec) { |
michael@0 | 1303 | static const int32_t kParseFailed = -99999; |
michael@0 | 1304 | |
michael@0 | 1305 | NumberFormat* numberFormat = 0; |
michael@0 | 1306 | UnicodeString idUppercase = id; |
michael@0 | 1307 | idUppercase.toUpper(""); |
michael@0 | 1308 | |
michael@0 | 1309 | if (id.length() > GMT_ID_LENGTH && |
michael@0 | 1310 | idUppercase.startsWith(GMT_ID, GMT_ID_LENGTH)) |
michael@0 | 1311 | { |
michael@0 | 1312 | ParsePosition pos(GMT_ID_LENGTH); |
michael@0 | 1313 | sign = 1; |
michael@0 | 1314 | hour = 0; |
michael@0 | 1315 | min = 0; |
michael@0 | 1316 | sec = 0; |
michael@0 | 1317 | |
michael@0 | 1318 | if (id[pos.getIndex()] == MINUS /*'-'*/) { |
michael@0 | 1319 | sign = -1; |
michael@0 | 1320 | } else if (id[pos.getIndex()] != PLUS /*'+'*/) { |
michael@0 | 1321 | return FALSE; |
michael@0 | 1322 | } |
michael@0 | 1323 | pos.setIndex(pos.getIndex() + 1); |
michael@0 | 1324 | |
michael@0 | 1325 | UErrorCode success = U_ZERO_ERROR; |
michael@0 | 1326 | numberFormat = NumberFormat::createInstance(success); |
michael@0 | 1327 | if(U_FAILURE(success)){ |
michael@0 | 1328 | return FALSE; |
michael@0 | 1329 | } |
michael@0 | 1330 | numberFormat->setParseIntegerOnly(TRUE); |
michael@0 | 1331 | //numberFormat->setLenient(TRUE); // TODO: May need to set this, depends on latest timezone parsing |
michael@0 | 1332 | |
michael@0 | 1333 | // Look for either hh:mm, hhmm, or hh |
michael@0 | 1334 | int32_t start = pos.getIndex(); |
michael@0 | 1335 | Formattable n(kParseFailed); |
michael@0 | 1336 | numberFormat->parse(id, n, pos); |
michael@0 | 1337 | if (pos.getIndex() == start) { |
michael@0 | 1338 | delete numberFormat; |
michael@0 | 1339 | return FALSE; |
michael@0 | 1340 | } |
michael@0 | 1341 | hour = n.getLong(); |
michael@0 | 1342 | |
michael@0 | 1343 | if (pos.getIndex() < id.length()) { |
michael@0 | 1344 | if (pos.getIndex() - start > 2 |
michael@0 | 1345 | || id[pos.getIndex()] != COLON) { |
michael@0 | 1346 | delete numberFormat; |
michael@0 | 1347 | return FALSE; |
michael@0 | 1348 | } |
michael@0 | 1349 | // hh:mm |
michael@0 | 1350 | pos.setIndex(pos.getIndex() + 1); |
michael@0 | 1351 | int32_t oldPos = pos.getIndex(); |
michael@0 | 1352 | n.setLong(kParseFailed); |
michael@0 | 1353 | numberFormat->parse(id, n, pos); |
michael@0 | 1354 | if ((pos.getIndex() - oldPos) != 2) { |
michael@0 | 1355 | // must be 2 digits |
michael@0 | 1356 | delete numberFormat; |
michael@0 | 1357 | return FALSE; |
michael@0 | 1358 | } |
michael@0 | 1359 | min = n.getLong(); |
michael@0 | 1360 | if (pos.getIndex() < id.length()) { |
michael@0 | 1361 | if (id[pos.getIndex()] != COLON) { |
michael@0 | 1362 | delete numberFormat; |
michael@0 | 1363 | return FALSE; |
michael@0 | 1364 | } |
michael@0 | 1365 | // [:ss] |
michael@0 | 1366 | pos.setIndex(pos.getIndex() + 1); |
michael@0 | 1367 | oldPos = pos.getIndex(); |
michael@0 | 1368 | n.setLong(kParseFailed); |
michael@0 | 1369 | numberFormat->parse(id, n, pos); |
michael@0 | 1370 | if (pos.getIndex() != id.length() |
michael@0 | 1371 | || (pos.getIndex() - oldPos) != 2) { |
michael@0 | 1372 | delete numberFormat; |
michael@0 | 1373 | return FALSE; |
michael@0 | 1374 | } |
michael@0 | 1375 | sec = n.getLong(); |
michael@0 | 1376 | } |
michael@0 | 1377 | } else { |
michael@0 | 1378 | // Supported formats are below - |
michael@0 | 1379 | // |
michael@0 | 1380 | // HHmmss |
michael@0 | 1381 | // Hmmss |
michael@0 | 1382 | // HHmm |
michael@0 | 1383 | // Hmm |
michael@0 | 1384 | // HH |
michael@0 | 1385 | // H |
michael@0 | 1386 | |
michael@0 | 1387 | int32_t length = pos.getIndex() - start; |
michael@0 | 1388 | if (length <= 0 || 6 < length) { |
michael@0 | 1389 | // invalid length |
michael@0 | 1390 | delete numberFormat; |
michael@0 | 1391 | return FALSE; |
michael@0 | 1392 | } |
michael@0 | 1393 | switch (length) { |
michael@0 | 1394 | case 1: |
michael@0 | 1395 | case 2: |
michael@0 | 1396 | // already set to hour |
michael@0 | 1397 | break; |
michael@0 | 1398 | case 3: |
michael@0 | 1399 | case 4: |
michael@0 | 1400 | min = hour % 100; |
michael@0 | 1401 | hour /= 100; |
michael@0 | 1402 | break; |
michael@0 | 1403 | case 5: |
michael@0 | 1404 | case 6: |
michael@0 | 1405 | sec = hour % 100; |
michael@0 | 1406 | min = (hour/100) % 100; |
michael@0 | 1407 | hour /= 10000; |
michael@0 | 1408 | break; |
michael@0 | 1409 | } |
michael@0 | 1410 | } |
michael@0 | 1411 | |
michael@0 | 1412 | delete numberFormat; |
michael@0 | 1413 | |
michael@0 | 1414 | if (hour > kMAX_CUSTOM_HOUR || min > kMAX_CUSTOM_MIN || sec > kMAX_CUSTOM_SEC) { |
michael@0 | 1415 | return FALSE; |
michael@0 | 1416 | } |
michael@0 | 1417 | return TRUE; |
michael@0 | 1418 | } |
michael@0 | 1419 | return FALSE; |
michael@0 | 1420 | } |
michael@0 | 1421 | |
michael@0 | 1422 | UnicodeString& |
michael@0 | 1423 | TimeZone::formatCustomID(int32_t hour, int32_t min, int32_t sec, |
michael@0 | 1424 | UBool negative, UnicodeString& id) { |
michael@0 | 1425 | // Create time zone ID - GMT[+|-]hhmm[ss] |
michael@0 | 1426 | id.setTo(GMT_ID, GMT_ID_LENGTH); |
michael@0 | 1427 | if (hour | min | sec) { |
michael@0 | 1428 | if (negative) { |
michael@0 | 1429 | id += (UChar)MINUS; |
michael@0 | 1430 | } else { |
michael@0 | 1431 | id += (UChar)PLUS; |
michael@0 | 1432 | } |
michael@0 | 1433 | |
michael@0 | 1434 | if (hour < 10) { |
michael@0 | 1435 | id += (UChar)ZERO_DIGIT; |
michael@0 | 1436 | } else { |
michael@0 | 1437 | id += (UChar)(ZERO_DIGIT + hour/10); |
michael@0 | 1438 | } |
michael@0 | 1439 | id += (UChar)(ZERO_DIGIT + hour%10); |
michael@0 | 1440 | id += (UChar)COLON; |
michael@0 | 1441 | if (min < 10) { |
michael@0 | 1442 | id += (UChar)ZERO_DIGIT; |
michael@0 | 1443 | } else { |
michael@0 | 1444 | id += (UChar)(ZERO_DIGIT + min/10); |
michael@0 | 1445 | } |
michael@0 | 1446 | id += (UChar)(ZERO_DIGIT + min%10); |
michael@0 | 1447 | |
michael@0 | 1448 | if (sec) { |
michael@0 | 1449 | id += (UChar)COLON; |
michael@0 | 1450 | if (sec < 10) { |
michael@0 | 1451 | id += (UChar)ZERO_DIGIT; |
michael@0 | 1452 | } else { |
michael@0 | 1453 | id += (UChar)(ZERO_DIGIT + sec/10); |
michael@0 | 1454 | } |
michael@0 | 1455 | id += (UChar)(ZERO_DIGIT + sec%10); |
michael@0 | 1456 | } |
michael@0 | 1457 | } |
michael@0 | 1458 | return id; |
michael@0 | 1459 | } |
michael@0 | 1460 | |
michael@0 | 1461 | |
michael@0 | 1462 | UBool |
michael@0 | 1463 | TimeZone::hasSameRules(const TimeZone& other) const |
michael@0 | 1464 | { |
michael@0 | 1465 | return (getRawOffset() == other.getRawOffset() && |
michael@0 | 1466 | useDaylightTime() == other.useDaylightTime()); |
michael@0 | 1467 | } |
michael@0 | 1468 | |
michael@0 | 1469 | static void U_CALLCONV initTZDataVersion(UErrorCode &status) { |
michael@0 | 1470 | ucln_i18n_registerCleanup(UCLN_I18N_TIMEZONE, timeZone_cleanup); |
michael@0 | 1471 | int32_t len = 0; |
michael@0 | 1472 | UResourceBundle *bundle = ures_openDirect(NULL, kZONEINFO, &status); |
michael@0 | 1473 | const UChar *tzver = ures_getStringByKey(bundle, kTZVERSION, &len, &status); |
michael@0 | 1474 | |
michael@0 | 1475 | if (U_SUCCESS(status)) { |
michael@0 | 1476 | if (len >= (int32_t)sizeof(TZDATA_VERSION)) { |
michael@0 | 1477 | // Ensure that there is always space for a trailing nul in TZDATA_VERSION |
michael@0 | 1478 | len = sizeof(TZDATA_VERSION) - 1; |
michael@0 | 1479 | } |
michael@0 | 1480 | u_UCharsToChars(tzver, TZDATA_VERSION, len); |
michael@0 | 1481 | } |
michael@0 | 1482 | ures_close(bundle); |
michael@0 | 1483 | |
michael@0 | 1484 | } |
michael@0 | 1485 | |
michael@0 | 1486 | const char* |
michael@0 | 1487 | TimeZone::getTZDataVersion(UErrorCode& status) |
michael@0 | 1488 | { |
michael@0 | 1489 | umtx_initOnce(gTZDataVersionInitOnce, &initTZDataVersion, status); |
michael@0 | 1490 | return (const char*)TZDATA_VERSION; |
michael@0 | 1491 | } |
michael@0 | 1492 | |
michael@0 | 1493 | UnicodeString& |
michael@0 | 1494 | TimeZone::getCanonicalID(const UnicodeString& id, UnicodeString& canonicalID, UErrorCode& status) |
michael@0 | 1495 | { |
michael@0 | 1496 | UBool isSystemID = FALSE; |
michael@0 | 1497 | return getCanonicalID(id, canonicalID, isSystemID, status); |
michael@0 | 1498 | } |
michael@0 | 1499 | |
michael@0 | 1500 | UnicodeString& |
michael@0 | 1501 | TimeZone::getCanonicalID(const UnicodeString& id, UnicodeString& canonicalID, UBool& isSystemID, |
michael@0 | 1502 | UErrorCode& status) |
michael@0 | 1503 | { |
michael@0 | 1504 | canonicalID.remove(); |
michael@0 | 1505 | isSystemID = FALSE; |
michael@0 | 1506 | if (U_FAILURE(status)) { |
michael@0 | 1507 | return canonicalID; |
michael@0 | 1508 | } |
michael@0 | 1509 | if (id.compare(UNKNOWN_ZONE_ID, UNKNOWN_ZONE_ID_LENGTH) == 0) { |
michael@0 | 1510 | // special case - Etc/Unknown is a canonical ID, but not system ID |
michael@0 | 1511 | canonicalID.fastCopyFrom(id); |
michael@0 | 1512 | isSystemID = FALSE; |
michael@0 | 1513 | } else { |
michael@0 | 1514 | ZoneMeta::getCanonicalCLDRID(id, canonicalID, status); |
michael@0 | 1515 | if (U_SUCCESS(status)) { |
michael@0 | 1516 | isSystemID = TRUE; |
michael@0 | 1517 | } else { |
michael@0 | 1518 | // Not a system ID |
michael@0 | 1519 | status = U_ZERO_ERROR; |
michael@0 | 1520 | getCustomID(id, canonicalID, status); |
michael@0 | 1521 | } |
michael@0 | 1522 | } |
michael@0 | 1523 | return canonicalID; |
michael@0 | 1524 | } |
michael@0 | 1525 | |
michael@0 | 1526 | #ifndef U_HIDE_DRAFT_API |
michael@0 | 1527 | UnicodeString& |
michael@0 | 1528 | TimeZone::getWindowsID(const UnicodeString& id, UnicodeString& winid, UErrorCode& status) { |
michael@0 | 1529 | winid.remove(); |
michael@0 | 1530 | if (U_FAILURE(status)) { |
michael@0 | 1531 | return winid; |
michael@0 | 1532 | } |
michael@0 | 1533 | |
michael@0 | 1534 | // canonicalize the input ID |
michael@0 | 1535 | UnicodeString canonicalID; |
michael@0 | 1536 | UBool isSystemID = FALSE; |
michael@0 | 1537 | |
michael@0 | 1538 | getCanonicalID(id, canonicalID, isSystemID, status); |
michael@0 | 1539 | if (U_FAILURE(status) || !isSystemID) { |
michael@0 | 1540 | // mapping data is only applicable to tz database IDs |
michael@0 | 1541 | return winid; |
michael@0 | 1542 | } |
michael@0 | 1543 | |
michael@0 | 1544 | UResourceBundle *mapTimezones = ures_openDirect(NULL, "windowsZones", &status); |
michael@0 | 1545 | ures_getByKey(mapTimezones, "mapTimezones", mapTimezones, &status); |
michael@0 | 1546 | |
michael@0 | 1547 | if (U_FAILURE(status)) { |
michael@0 | 1548 | return winid; |
michael@0 | 1549 | } |
michael@0 | 1550 | |
michael@0 | 1551 | UResourceBundle *winzone = NULL; |
michael@0 | 1552 | UBool found = FALSE; |
michael@0 | 1553 | while (ures_hasNext(mapTimezones) && !found) { |
michael@0 | 1554 | winzone = ures_getNextResource(mapTimezones, winzone, &status); |
michael@0 | 1555 | if (U_FAILURE(status)) { |
michael@0 | 1556 | break; |
michael@0 | 1557 | } |
michael@0 | 1558 | if (ures_getType(winzone) != URES_TABLE) { |
michael@0 | 1559 | continue; |
michael@0 | 1560 | } |
michael@0 | 1561 | UResourceBundle *regionalData = NULL; |
michael@0 | 1562 | while (ures_hasNext(winzone) && !found) { |
michael@0 | 1563 | regionalData = ures_getNextResource(winzone, regionalData, &status); |
michael@0 | 1564 | if (U_FAILURE(status)) { |
michael@0 | 1565 | break; |
michael@0 | 1566 | } |
michael@0 | 1567 | if (ures_getType(regionalData) != URES_STRING) { |
michael@0 | 1568 | continue; |
michael@0 | 1569 | } |
michael@0 | 1570 | int32_t len; |
michael@0 | 1571 | const UChar *tzids = ures_getString(regionalData, &len, &status); |
michael@0 | 1572 | if (U_FAILURE(status)) { |
michael@0 | 1573 | break; |
michael@0 | 1574 | } |
michael@0 | 1575 | |
michael@0 | 1576 | const UChar *start = tzids; |
michael@0 | 1577 | UBool hasNext = TRUE; |
michael@0 | 1578 | while (hasNext) { |
michael@0 | 1579 | const UChar *end = u_strchr(start, (UChar)0x20); |
michael@0 | 1580 | if (end == NULL) { |
michael@0 | 1581 | end = tzids + len; |
michael@0 | 1582 | hasNext = FALSE; |
michael@0 | 1583 | } |
michael@0 | 1584 | if (canonicalID.compare(start, end - start) == 0) { |
michael@0 | 1585 | winid = UnicodeString(ures_getKey(winzone), -1 , US_INV); |
michael@0 | 1586 | found = TRUE; |
michael@0 | 1587 | break; |
michael@0 | 1588 | } |
michael@0 | 1589 | start = end + 1; |
michael@0 | 1590 | } |
michael@0 | 1591 | } |
michael@0 | 1592 | ures_close(regionalData); |
michael@0 | 1593 | } |
michael@0 | 1594 | ures_close(winzone); |
michael@0 | 1595 | ures_close(mapTimezones); |
michael@0 | 1596 | |
michael@0 | 1597 | return winid; |
michael@0 | 1598 | } |
michael@0 | 1599 | |
michael@0 | 1600 | #define MAX_WINDOWS_ID_SIZE 128 |
michael@0 | 1601 | |
michael@0 | 1602 | UnicodeString& |
michael@0 | 1603 | TimeZone::getIDForWindowsID(const UnicodeString& winid, const char* region, UnicodeString& id, UErrorCode& status) { |
michael@0 | 1604 | id.remove(); |
michael@0 | 1605 | if (U_FAILURE(status)) { |
michael@0 | 1606 | return id; |
michael@0 | 1607 | } |
michael@0 | 1608 | |
michael@0 | 1609 | UResourceBundle *zones = ures_openDirect(NULL, "windowsZones", &status); |
michael@0 | 1610 | ures_getByKey(zones, "mapTimezones", zones, &status); |
michael@0 | 1611 | if (U_FAILURE(status)) { |
michael@0 | 1612 | ures_close(zones); |
michael@0 | 1613 | return id; |
michael@0 | 1614 | } |
michael@0 | 1615 | |
michael@0 | 1616 | UErrorCode tmperr = U_ZERO_ERROR; |
michael@0 | 1617 | char winidKey[MAX_WINDOWS_ID_SIZE]; |
michael@0 | 1618 | int32_t winKeyLen = winid.extract(0, winid.length(), winidKey, sizeof(winidKey) - 1, US_INV); |
michael@0 | 1619 | |
michael@0 | 1620 | if (winKeyLen == 0 || winKeyLen >= (int32_t)sizeof(winidKey)) { |
michael@0 | 1621 | ures_close(zones); |
michael@0 | 1622 | return id; |
michael@0 | 1623 | } |
michael@0 | 1624 | winidKey[winKeyLen] = 0; |
michael@0 | 1625 | |
michael@0 | 1626 | ures_getByKey(zones, winidKey, zones, &tmperr); // use tmperr, because windows mapping might not |
michael@0 | 1627 | // be avaiable by design |
michael@0 | 1628 | if (U_FAILURE(tmperr)) { |
michael@0 | 1629 | ures_close(zones); |
michael@0 | 1630 | return id; |
michael@0 | 1631 | } |
michael@0 | 1632 | |
michael@0 | 1633 | const UChar *tzid = NULL; |
michael@0 | 1634 | int32_t len = 0; |
michael@0 | 1635 | UBool gotID = FALSE; |
michael@0 | 1636 | if (region) { |
michael@0 | 1637 | const UChar *tzids = ures_getStringByKey(zones, region, &len, &tmperr); // use tmperr, because |
michael@0 | 1638 | // regional mapping is optional |
michael@0 | 1639 | if (U_SUCCESS(tmperr)) { |
michael@0 | 1640 | // first ID delimited by space is the defasult one |
michael@0 | 1641 | const UChar *end = u_strchr(tzids, (UChar)0x20); |
michael@0 | 1642 | if (end == NULL) { |
michael@0 | 1643 | id.setTo(tzids, -1); |
michael@0 | 1644 | } else { |
michael@0 | 1645 | id.setTo(tzids, end - tzids); |
michael@0 | 1646 | } |
michael@0 | 1647 | gotID = TRUE; |
michael@0 | 1648 | } |
michael@0 | 1649 | } |
michael@0 | 1650 | |
michael@0 | 1651 | if (!gotID) { |
michael@0 | 1652 | tzid = ures_getStringByKey(zones, "001", &len, &status); // using status, because "001" must be |
michael@0 | 1653 | // available at this point |
michael@0 | 1654 | if (U_SUCCESS(status)) { |
michael@0 | 1655 | id.setTo(tzid, len); |
michael@0 | 1656 | } |
michael@0 | 1657 | } |
michael@0 | 1658 | |
michael@0 | 1659 | ures_close(zones); |
michael@0 | 1660 | return id; |
michael@0 | 1661 | } |
michael@0 | 1662 | #endif /* U_HIDE_DRAFT_API */ |
michael@0 | 1663 | |
michael@0 | 1664 | |
michael@0 | 1665 | U_NAMESPACE_END |
michael@0 | 1666 | |
michael@0 | 1667 | #endif /* #if !UCONFIG_NO_FORMATTING */ |
michael@0 | 1668 | |
michael@0 | 1669 | //eof |