]>
Commit | Line | Data |
---|---|---|
f3c0d7a5 A |
1 | // © 2016 and later: Unicode, Inc. and others. |
2 | // License & terms of use: http://www.unicode.org/copyright.html | |
46f4442e A |
3 | /* |
4 | ******************************************************************************* | |
57a6839d A |
5 | * Copyright (C) 2007-2014, International Business Machines Corporation and |
6 | * others. All Rights Reserved. | |
46f4442e A |
7 | ******************************************************************************* |
8 | */ | |
9 | ||
10 | #include "unicode/utypes.h" | |
11 | ||
12 | #if !UCONFIG_NO_FORMATTING | |
13 | ||
14 | #include "zonemeta.h" | |
15 | ||
16 | #include "unicode/timezone.h" | |
17 | #include "unicode/ustring.h" | |
18 | #include "unicode/putil.h" | |
4388f060 | 19 | #include "unicode/simpletz.h" |
f3c0d7a5 | 20 | #include "unicode/strenum.h" |
46f4442e A |
21 | #include "umutex.h" |
22 | #include "uvector.h" | |
23 | #include "cmemory.h" | |
24 | #include "gregoimp.h" | |
25 | #include "cstring.h" | |
26 | #include "ucln_in.h" | |
729e4ab9 | 27 | #include "uassert.h" |
4388f060 A |
28 | #include "uresimp.h" |
29 | #include "uhash.h" | |
30 | #include "olsontz.h" | |
f3c0d7a5 | 31 | #include "uinvchar.h" |
46f4442e | 32 | |
3d1f044b A |
33 | static icu::UMutex *gZoneMetaLock() { |
34 | static icu::UMutex *m = STATIC_NEW(icu::UMutex); | |
35 | return m; | |
36 | } | |
729e4ab9 | 37 | |
4388f060 A |
38 | // CLDR Canonical ID mapping table |
39 | static UHashtable *gCanonicalIDCache = NULL; | |
57a6839d | 40 | static icu::UInitOnce gCanonicalIDCacheInitOnce = U_INITONCE_INITIALIZER; |
4388f060 | 41 | |
729e4ab9 | 42 | // Metazone mapping table |
46f4442e | 43 | static UHashtable *gOlsonToMeta = NULL; |
57a6839d | 44 | static icu::UInitOnce gOlsonToMetaInitOnce = U_INITONCE_INITIALIZER; |
46f4442e | 45 | |
4388f060 A |
46 | // Available metazone IDs vector and table |
47 | static icu::UVector *gMetaZoneIDs = NULL; | |
48 | static UHashtable *gMetaZoneIDTable = NULL; | |
57a6839d | 49 | static icu::UInitOnce gMetaZoneIDsInitOnce = U_INITONCE_INITIALIZER; |
4388f060 | 50 | |
729e4ab9 | 51 | // Country info vectors |
4388f060 A |
52 | static icu::UVector *gSingleZoneCountries = NULL; |
53 | static icu::UVector *gMultiZonesCountries = NULL; | |
57a6839d | 54 | static icu::UInitOnce gCountryInfoVectorsInitOnce = U_INITONCE_INITIALIZER; |
46f4442e | 55 | |
729e4ab9 | 56 | U_CDECL_BEGIN |
46f4442e | 57 | |
46f4442e A |
58 | /** |
59 | * Cleanup callback func | |
60 | */ | |
61 | static UBool U_CALLCONV zoneMeta_cleanup(void) | |
62 | { | |
4388f060 A |
63 | if (gCanonicalIDCache != NULL) { |
64 | uhash_close(gCanonicalIDCache); | |
65 | gCanonicalIDCache = NULL; | |
66 | } | |
57a6839d | 67 | gCanonicalIDCacheInitOnce.reset(); |
46f4442e | 68 | |
46f4442e A |
69 | if (gOlsonToMeta != NULL) { |
70 | uhash_close(gOlsonToMeta); | |
71 | gOlsonToMeta = NULL; | |
72 | } | |
57a6839d | 73 | gOlsonToMetaInitOnce.reset(); |
46f4442e | 74 | |
4388f060 A |
75 | if (gMetaZoneIDTable != NULL) { |
76 | uhash_close(gMetaZoneIDTable); | |
57a6839d | 77 | gMetaZoneIDTable = NULL; |
4388f060 A |
78 | } |
79 | // delete after closing gMetaZoneIDTable, because it holds | |
80 | // value objects held by the hashtable | |
81 | delete gMetaZoneIDs; | |
57a6839d A |
82 | gMetaZoneIDs = NULL; |
83 | gMetaZoneIDsInitOnce.reset(); | |
4388f060 | 84 | |
729e4ab9 | 85 | delete gSingleZoneCountries; |
57a6839d | 86 | gSingleZoneCountries = NULL; |
729e4ab9 | 87 | delete gMultiZonesCountries; |
57a6839d A |
88 | gMultiZonesCountries = NULL; |
89 | gCountryInfoVectorsInitOnce.reset(); | |
46f4442e A |
90 | |
91 | return TRUE; | |
92 | } | |
93 | ||
94 | /** | |
95 | * Deleter for UChar* string | |
96 | */ | |
97 | static void U_CALLCONV | |
98 | deleteUCharString(void *obj) { | |
99 | UChar *entry = (UChar*)obj; | |
100 | uprv_free(entry); | |
101 | } | |
102 | ||
103 | /** | |
104 | * Deleter for UVector | |
105 | */ | |
106 | static void U_CALLCONV | |
107 | deleteUVector(void *obj) { | |
4388f060 | 108 | delete (icu::UVector*) obj; |
46f4442e A |
109 | } |
110 | ||
46f4442e A |
111 | /** |
112 | * Deleter for OlsonToMetaMappingEntry | |
113 | */ | |
114 | static void U_CALLCONV | |
115 | deleteOlsonToMetaMappingEntry(void *obj) { | |
4388f060 | 116 | icu::OlsonToMetaMappingEntry *entry = (icu::OlsonToMetaMappingEntry*)obj; |
46f4442e A |
117 | uprv_free(entry); |
118 | } | |
119 | ||
46f4442e A |
120 | U_CDECL_END |
121 | ||
122 | U_NAMESPACE_BEGIN | |
123 | ||
124 | #define ZID_KEY_MAX 128 | |
46f4442e | 125 | |
729e4ab9 A |
126 | static const char gMetaZones[] = "metaZones"; |
127 | static const char gMetazoneInfo[] = "metazoneInfo"; | |
46f4442e | 128 | static const char gMapTimezonesTag[] = "mapTimezones"; |
46f4442e | 129 | |
51004dcb | 130 | static const char gKeyTypeData[] = "keyTypeData"; |
729e4ab9 A |
131 | static const char gTypeAliasTag[] = "typeAlias"; |
132 | static const char gTypeMapTag[] = "typeMap"; | |
133 | static const char gTimezoneTag[] = "timezone"; | |
46f4442e | 134 | |
51004dcb A |
135 | static const char gPrimaryZonesTag[] = "primaryZones"; |
136 | ||
729e4ab9 | 137 | static const char gWorldTag[] = "001"; |
46f4442e A |
138 | |
139 | static const UChar gWorld[] = {0x30, 0x30, 0x31, 0x00}; // "001" | |
140 | ||
729e4ab9 A |
141 | static const UChar gDefaultFrom[] = {0x31, 0x39, 0x37, 0x30, 0x2D, 0x30, 0x31, 0x2D, 0x30, 0x31, |
142 | 0x20, 0x30, 0x30, 0x3A, 0x30, 0x30, 0x00}; // "1970-01-01 00:00" | |
143 | static const UChar gDefaultTo[] = {0x39, 0x39, 0x39, 0x39, 0x2D, 0x31, 0x32, 0x2D, 0x33, 0x31, | |
144 | 0x20, 0x32, 0x33, 0x3A, 0x35, 0x39, 0x00}; // "9999-12-31 23:59" | |
145 | ||
4388f060 A |
146 | static const UChar gCustomTzPrefix[] = {0x47, 0x4D, 0x54, 0}; // "GMT" |
147 | ||
46f4442e A |
148 | #define ASCII_DIGIT(c) (((c)>=0x30 && (c)<=0x39) ? (c)-0x30 : -1) |
149 | ||
150 | /* | |
151 | * Convert a date string used by metazone mappings to UDate. | |
152 | * The format used by CLDR metazone mapping is "yyyy-MM-dd HH:mm". | |
153 | */ | |
154 | static UDate | |
155 | parseDate (const UChar *text, UErrorCode &status) { | |
156 | if (U_FAILURE(status)) { | |
157 | return 0; | |
158 | } | |
159 | int32_t len = u_strlen(text); | |
160 | if (len != 16 && len != 10) { | |
161 | // It must be yyyy-MM-dd HH:mm (length 16) or yyyy-MM-dd (length 10) | |
162 | status = U_INVALID_FORMAT_ERROR; | |
163 | return 0; | |
164 | } | |
165 | ||
166 | int32_t year = 0, month = 0, day = 0, hour = 0, min = 0, n; | |
167 | int32_t idx; | |
168 | ||
169 | // "yyyy" (0 - 3) | |
170 | for (idx = 0; idx <= 3 && U_SUCCESS(status); idx++) { | |
171 | n = ASCII_DIGIT((int32_t)text[idx]); | |
172 | if (n >= 0) { | |
173 | year = 10*year + n; | |
174 | } else { | |
175 | status = U_INVALID_FORMAT_ERROR; | |
176 | } | |
177 | } | |
178 | // "MM" (5 - 6) | |
179 | for (idx = 5; idx <= 6 && U_SUCCESS(status); idx++) { | |
180 | n = ASCII_DIGIT((int32_t)text[idx]); | |
181 | if (n >= 0) { | |
182 | month = 10*month + n; | |
183 | } else { | |
184 | status = U_INVALID_FORMAT_ERROR; | |
185 | } | |
186 | } | |
187 | // "dd" (8 - 9) | |
188 | for (idx = 8; idx <= 9 && U_SUCCESS(status); idx++) { | |
189 | n = ASCII_DIGIT((int32_t)text[idx]); | |
190 | if (n >= 0) { | |
191 | day = 10*day + n; | |
192 | } else { | |
193 | status = U_INVALID_FORMAT_ERROR; | |
194 | } | |
195 | } | |
196 | if (len == 16) { | |
197 | // "HH" (11 - 12) | |
198 | for (idx = 11; idx <= 12 && U_SUCCESS(status); idx++) { | |
199 | n = ASCII_DIGIT((int32_t)text[idx]); | |
200 | if (n >= 0) { | |
201 | hour = 10*hour + n; | |
202 | } else { | |
203 | status = U_INVALID_FORMAT_ERROR; | |
204 | } | |
205 | } | |
206 | // "mm" (14 - 15) | |
207 | for (idx = 14; idx <= 15 && U_SUCCESS(status); idx++) { | |
208 | n = ASCII_DIGIT((int32_t)text[idx]); | |
209 | if (n >= 0) { | |
210 | min = 10*min + n; | |
211 | } else { | |
212 | status = U_INVALID_FORMAT_ERROR; | |
213 | } | |
214 | } | |
215 | } | |
216 | ||
217 | if (U_SUCCESS(status)) { | |
218 | UDate date = Grego::fieldsToDay(year, month - 1, day) * U_MILLIS_PER_DAY | |
219 | + hour * U_MILLIS_PER_HOUR + min * U_MILLIS_PER_MINUTE; | |
220 | return date; | |
221 | } | |
222 | return 0; | |
223 | } | |
224 | ||
57a6839d A |
225 | static void U_CALLCONV initCanonicalIDCache(UErrorCode &status) { |
226 | gCanonicalIDCache = uhash_open(uhash_hashUChars, uhash_compareUChars, NULL, &status); | |
227 | if (gCanonicalIDCache == NULL) { | |
228 | status = U_MEMORY_ALLOCATION_ERROR; | |
229 | } | |
230 | if (U_FAILURE(status)) { | |
231 | gCanonicalIDCache = NULL; | |
232 | } | |
233 | // No key/value deleters - keys/values are from a resource bundle | |
234 | ucln_i18n_registerCleanup(UCLN_I18N_ZONEMETA, zoneMeta_cleanup); | |
235 | } | |
236 | ||
237 | ||
4388f060 A |
238 | const UChar* U_EXPORT2 |
239 | ZoneMeta::getCanonicalCLDRID(const UnicodeString &tzid, UErrorCode& status) { | |
240 | if (U_FAILURE(status)) { | |
241 | return NULL; | |
242 | } | |
243 | ||
57a6839d | 244 | if (tzid.isBogus() || tzid.length() > ZID_KEY_MAX) { |
729e4ab9 | 245 | status = U_ILLEGAL_ARGUMENT_ERROR; |
4388f060 | 246 | return NULL; |
46f4442e | 247 | } |
46f4442e | 248 | |
4388f060 | 249 | // Checking the cached results |
57a6839d A |
250 | umtx_initOnce(gCanonicalIDCacheInitOnce, &initCanonicalIDCache, status); |
251 | if (U_FAILURE(status)) { | |
252 | return NULL; | |
4388f060 A |
253 | } |
254 | ||
255 | const UChar *canonicalID = NULL; | |
256 | ||
257 | UErrorCode tmpStatus = U_ZERO_ERROR; | |
258 | UChar utzid[ZID_KEY_MAX + 1]; | |
259 | tzid.extract(utzid, ZID_KEY_MAX + 1, tmpStatus); | |
260 | U_ASSERT(tmpStatus == U_ZERO_ERROR); // we checked the length of tzid already | |
261 | ||
f3c0d7a5 A |
262 | if (!uprv_isInvariantUString(utzid, -1)) { |
263 | // All of known tz IDs are only containing ASCII invariant characters. | |
264 | status = U_ILLEGAL_ARGUMENT_ERROR; | |
265 | return NULL; | |
266 | } | |
267 | ||
4388f060 | 268 | // Check if it was already cached |
3d1f044b | 269 | umtx_lock(gZoneMetaLock()); |
4388f060 A |
270 | { |
271 | canonicalID = (const UChar *)uhash_get(gCanonicalIDCache, utzid); | |
272 | } | |
3d1f044b | 273 | umtx_unlock(gZoneMetaLock()); |
4388f060 A |
274 | |
275 | if (canonicalID != NULL) { | |
276 | return canonicalID; | |
277 | } | |
278 | ||
279 | // If not, resolve CLDR canonical ID with resource data | |
280 | UBool isInputCanonical = FALSE; | |
281 | char id[ZID_KEY_MAX + 1]; | |
b331163b | 282 | tzid.extract(0, 0x7fffffff, id, UPRV_LENGTHOF(id), US_INV); |
46f4442e | 283 | |
729e4ab9 A |
284 | // replace '/' with ':' |
285 | char *p = id; | |
286 | while (*p++) { | |
287 | if (*p == '/') { | |
288 | *p = ':'; | |
46f4442e | 289 | } |
729e4ab9 | 290 | } |
46f4442e | 291 | |
51004dcb | 292 | UResourceBundle *top = ures_openDirect(NULL, gKeyTypeData, &tmpStatus); |
729e4ab9 A |
293 | UResourceBundle *rb = ures_getByKey(top, gTypeMapTag, NULL, &tmpStatus); |
294 | ures_getByKey(rb, gTimezoneTag, rb, &tmpStatus); | |
295 | ures_getByKey(rb, id, rb, &tmpStatus); | |
296 | if (U_SUCCESS(tmpStatus)) { | |
4388f060 A |
297 | // type entry (canonical) found |
298 | // the input is the canonical ID. resolve to const UChar* | |
299 | canonicalID = TimeZone::findID(tzid); | |
300 | isInputCanonical = TRUE; | |
729e4ab9 | 301 | } |
46f4442e | 302 | |
4388f060 A |
303 | if (canonicalID == NULL) { |
304 | // If a map element not found, then look for an alias | |
305 | tmpStatus = U_ZERO_ERROR; | |
306 | ures_getByKey(top, gTypeAliasTag, rb, &tmpStatus); | |
307 | ures_getByKey(rb, gTimezoneTag, rb, &tmpStatus); | |
308 | const UChar *canonical = ures_getStringByKey(rb,id,NULL,&tmpStatus); | |
309 | if (U_SUCCESS(tmpStatus)) { | |
310 | // canonical map found | |
311 | canonicalID = canonical; | |
312 | } | |
46f4442e | 313 | |
4388f060 A |
314 | if (canonicalID == NULL) { |
315 | // Dereference the input ID using the tz data | |
316 | const UChar *derefer = TimeZone::dereferOlsonLink(tzid); | |
317 | if (derefer == NULL) { | |
318 | status = U_ILLEGAL_ARGUMENT_ERROR; | |
319 | } else { | |
57a6839d | 320 | int32_t len = u_strlen(derefer); |
4388f060 A |
321 | u_UCharsToChars(derefer,id,len); |
322 | id[len] = (char) 0; // Make sure it is null terminated. | |
323 | ||
324 | // replace '/' with ':' | |
3d1f044b A |
325 | char *q = id; |
326 | while (*q++) { | |
327 | if (*q == '/') { | |
328 | *q = ':'; | |
4388f060 A |
329 | } |
330 | } | |
46f4442e | 331 | |
4388f060 A |
332 | // If a dereference turned something up then look for an alias. |
333 | // rb still points to the alias table, so we don't have to go looking | |
334 | // for it. | |
335 | tmpStatus = U_ZERO_ERROR; | |
336 | canonical = ures_getStringByKey(rb,id,NULL,&tmpStatus); | |
337 | if (U_SUCCESS(tmpStatus)) { | |
338 | // canonical map for the dereferenced ID found | |
339 | canonicalID = canonical; | |
340 | } else { | |
341 | canonicalID = derefer; | |
342 | isInputCanonical = TRUE; | |
343 | } | |
46f4442e A |
344 | } |
345 | } | |
4388f060 A |
346 | } |
347 | ures_close(rb); | |
348 | ures_close(top); | |
46f4442e | 349 | |
4388f060 A |
350 | if (U_SUCCESS(status)) { |
351 | U_ASSERT(canonicalID != NULL); // canocanilD must be non-NULL here | |
352 | ||
353 | // Put the resolved canonical ID to the cache | |
3d1f044b | 354 | umtx_lock(gZoneMetaLock()); |
4388f060 A |
355 | { |
356 | const UChar* idInCache = (const UChar *)uhash_get(gCanonicalIDCache, utzid); | |
357 | if (idInCache == NULL) { | |
358 | const UChar* key = ZoneMeta::findTimeZoneID(tzid); | |
359 | U_ASSERT(key != NULL); | |
360 | if (key != NULL) { | |
361 | idInCache = (const UChar *)uhash_put(gCanonicalIDCache, (void *)key, (void *)canonicalID, &status); | |
362 | U_ASSERT(idInCache == NULL); | |
363 | } | |
364 | } | |
365 | if (U_SUCCESS(status) && isInputCanonical) { | |
366 | // Also put canonical ID itself into the cache if not exist | |
367 | const UChar *canonicalInCache = (const UChar*)uhash_get(gCanonicalIDCache, canonicalID); | |
368 | if (canonicalInCache == NULL) { | |
369 | canonicalInCache = (const UChar *)uhash_put(gCanonicalIDCache, (void *)canonicalID, (void *)canonicalID, &status); | |
370 | U_ASSERT(canonicalInCache == NULL); | |
371 | } | |
372 | } | |
46f4442e | 373 | } |
3d1f044b | 374 | umtx_unlock(gZoneMetaLock()); |
46f4442e A |
375 | } |
376 | ||
4388f060 | 377 | return canonicalID; |
46f4442e A |
378 | } |
379 | ||
4388f060 A |
380 | UnicodeString& U_EXPORT2 |
381 | ZoneMeta::getCanonicalCLDRID(const UnicodeString &tzid, UnicodeString &systemID, UErrorCode& status) { | |
382 | const UChar *canonicalID = getCanonicalCLDRID(tzid, status); | |
383 | if (U_FAILURE(status) || canonicalID == NULL) { | |
384 | systemID.setToBogus(); | |
385 | return systemID; | |
386 | } | |
387 | systemID.setTo(TRUE, canonicalID, -1); | |
388 | return systemID; | |
389 | } | |
390 | ||
391 | const UChar* U_EXPORT2 | |
392 | ZoneMeta::getCanonicalCLDRID(const TimeZone& tz) { | |
393 | if (dynamic_cast<const OlsonTimeZone *>(&tz) != NULL) { | |
394 | // short cut for OlsonTimeZone | |
395 | const OlsonTimeZone *otz = (const OlsonTimeZone*)&tz; | |
396 | return otz->getCanonicalID(); | |
397 | } | |
398 | UErrorCode status = U_ZERO_ERROR; | |
399 | UnicodeString tzID; | |
400 | return getCanonicalCLDRID(tz.getID(tzID), status); | |
401 | } | |
402 | ||
57a6839d A |
403 | static void U_CALLCONV countryInfoVectorsInit(UErrorCode &status) { |
404 | // Create empty vectors | |
405 | // No deleters for these UVectors, it's a reference to a resource bundle string. | |
406 | gSingleZoneCountries = new UVector(NULL, uhash_compareUChars, status); | |
407 | if (gSingleZoneCountries == NULL) { | |
408 | status = U_MEMORY_ALLOCATION_ERROR; | |
409 | } | |
410 | gMultiZonesCountries = new UVector(NULL, uhash_compareUChars, status); | |
411 | if (gMultiZonesCountries == NULL) { | |
412 | status = U_MEMORY_ALLOCATION_ERROR; | |
413 | } | |
414 | ||
415 | if (U_FAILURE(status)) { | |
416 | delete gSingleZoneCountries; | |
417 | delete gMultiZonesCountries; | |
418 | gSingleZoneCountries = NULL; | |
419 | gMultiZonesCountries = NULL; | |
420 | } | |
421 | ucln_i18n_registerCleanup(UCLN_I18N_ZONEMETA, zoneMeta_cleanup); | |
422 | } | |
423 | ||
424 | ||
729e4ab9 | 425 | UnicodeString& U_EXPORT2 |
51004dcb A |
426 | ZoneMeta::getCanonicalCountry(const UnicodeString &tzid, UnicodeString &country, UBool *isPrimary /* = NULL */) { |
427 | if (isPrimary != NULL) { | |
428 | *isPrimary = FALSE; | |
46f4442e | 429 | } |
46f4442e | 430 | |
729e4ab9 | 431 | const UChar *region = TimeZone::getRegion(tzid); |
51004dcb A |
432 | if (region != NULL && u_strcmp(gWorld, region) != 0) { |
433 | country.setTo(region, -1); | |
434 | } else { | |
4388f060 | 435 | country.setToBogus(); |
729e4ab9 | 436 | return country; |
46f4442e A |
437 | } |
438 | ||
51004dcb A |
439 | if (isPrimary != NULL) { |
440 | char regionBuf[] = {0, 0, 0}; | |
441 | ||
442 | // Checking the cached results | |
443 | UErrorCode status = U_ZERO_ERROR; | |
57a6839d A |
444 | umtx_initOnce(gCountryInfoVectorsInitOnce, &countryInfoVectorsInit, status); |
445 | if (U_FAILURE(status)) { | |
446 | return country; | |
46f4442e A |
447 | } |
448 | ||
51004dcb A |
449 | // Check if it was already cached |
450 | UBool cached = FALSE; | |
451 | UBool singleZone = FALSE; | |
3d1f044b | 452 | umtx_lock(gZoneMetaLock()); |
51004dcb A |
453 | { |
454 | singleZone = cached = gSingleZoneCountries->contains((void*)region); | |
455 | if (!cached) { | |
456 | cached = gMultiZonesCountries->contains((void*)region); | |
457 | } | |
729e4ab9 | 458 | } |
3d1f044b | 459 | umtx_unlock(gZoneMetaLock()); |
46f4442e | 460 | |
51004dcb A |
461 | if (!cached) { |
462 | // We need to go through all zones associated with the region. | |
463 | // This is relatively heavy operation. | |
729e4ab9 | 464 | |
51004dcb | 465 | U_ASSERT(u_strlen(region) == 2); |
729e4ab9 | 466 | |
51004dcb | 467 | u_UCharsToChars(region, regionBuf, 2); |
729e4ab9 | 468 | |
51004dcb A |
469 | StringEnumeration *ids = TimeZone::createTimeZoneIDEnumeration(UCAL_ZONE_TYPE_CANONICAL_LOCATION, regionBuf, NULL, status); |
470 | int32_t idsLen = ids->count(status); | |
471 | if (U_SUCCESS(status) && idsLen == 1) { | |
472 | // only the single zone is available for the region | |
473 | singleZone = TRUE; | |
474 | } | |
475 | delete ids; | |
476 | ||
477 | // Cache the result | |
3d1f044b | 478 | umtx_lock(gZoneMetaLock()); |
51004dcb A |
479 | { |
480 | UErrorCode ec = U_ZERO_ERROR; | |
481 | if (singleZone) { | |
482 | if (!gSingleZoneCountries->contains((void*)region)) { | |
483 | gSingleZoneCountries->addElement((void*)region, ec); | |
484 | } | |
485 | } else { | |
486 | if (!gMultiZonesCountries->contains((void*)region)) { | |
487 | gMultiZonesCountries->addElement((void*)region, ec); | |
488 | } | |
489 | } | |
490 | } | |
3d1f044b | 491 | umtx_unlock(gZoneMetaLock()); |
729e4ab9 | 492 | } |
729e4ab9 | 493 | |
51004dcb A |
494 | if (singleZone) { |
495 | *isPrimary = TRUE; | |
496 | } else { | |
497 | // Note: We may cache the primary zone map in future. | |
498 | ||
499 | // Even a country has multiple zones, one of them might be | |
500 | // dominant and treated as a primary zone | |
501 | int32_t idLen = 0; | |
502 | if (regionBuf[0] == 0) { | |
503 | u_UCharsToChars(region, regionBuf, 2); | |
504 | } | |
505 | ||
506 | UResourceBundle *rb = ures_openDirect(NULL, gMetaZones, &status); | |
507 | ures_getByKey(rb, gPrimaryZonesTag, rb, &status); | |
508 | const UChar *primaryZone = ures_getStringByKey(rb, regionBuf, &idLen, &status); | |
509 | if (U_SUCCESS(status)) { | |
510 | if (tzid.compare(primaryZone, idLen) == 0) { | |
511 | *isPrimary = TRUE; | |
512 | } else { | |
513 | // The given ID might not be a canonical ID | |
514 | UnicodeString canonicalID; | |
515 | TimeZone::getCanonicalID(tzid, canonicalID, status); | |
516 | if (U_SUCCESS(status) && canonicalID.compare(primaryZone, idLen) == 0) { | |
517 | *isPrimary = TRUE; | |
518 | } | |
729e4ab9 | 519 | } |
46f4442e | 520 | } |
51004dcb | 521 | ures_close(rb); |
46f4442e A |
522 | } |
523 | } | |
524 | ||
729e4ab9 A |
525 | return country; |
526 | } | |
527 | ||
528 | UnicodeString& U_EXPORT2 | |
529 | ZoneMeta::getMetazoneID(const UnicodeString &tzid, UDate date, UnicodeString &result) { | |
530 | UBool isSet = FALSE; | |
531 | const UVector *mappings = getMetazoneMappings(tzid); | |
532 | if (mappings != NULL) { | |
533 | for (int32_t i = 0; i < mappings->size(); i++) { | |
534 | OlsonToMetaMappingEntry *mzm = (OlsonToMetaMappingEntry*)mappings->elementAt(i); | |
535 | if (mzm->from <= date && mzm->to > date) { | |
536 | result.setTo(mzm->mzid, -1); | |
537 | isSet = TRUE; | |
538 | break; | |
539 | } | |
540 | } | |
46f4442e | 541 | } |
729e4ab9 | 542 | if (!isSet) { |
4388f060 | 543 | result.setToBogus(); |
46f4442e | 544 | } |
729e4ab9 | 545 | return result; |
46f4442e A |
546 | } |
547 | ||
57a6839d A |
548 | static void U_CALLCONV olsonToMetaInit(UErrorCode &status) { |
549 | U_ASSERT(gOlsonToMeta == NULL); | |
550 | ucln_i18n_registerCleanup(UCLN_I18N_ZONEMETA, zoneMeta_cleanup); | |
551 | gOlsonToMeta = uhash_open(uhash_hashUChars, uhash_compareUChars, NULL, &status); | |
552 | if (U_FAILURE(status)) { | |
553 | gOlsonToMeta = NULL; | |
554 | } else { | |
555 | uhash_setKeyDeleter(gOlsonToMeta, deleteUCharString); | |
556 | uhash_setValueDeleter(gOlsonToMeta, deleteUVector); | |
557 | } | |
558 | } | |
559 | ||
560 | ||
729e4ab9 A |
561 | const UVector* U_EXPORT2 |
562 | ZoneMeta::getMetazoneMappings(const UnicodeString &tzid) { | |
46f4442e | 563 | UErrorCode status = U_ZERO_ERROR; |
4388f060 A |
564 | UChar tzidUChars[ZID_KEY_MAX + 1]; |
565 | tzid.extract(tzidUChars, ZID_KEY_MAX + 1, status); | |
729e4ab9 | 566 | if (U_FAILURE(status) || status == U_STRING_NOT_TERMINATED_WARNING) { |
46f4442e A |
567 | return NULL; |
568 | } | |
46f4442e | 569 | |
57a6839d A |
570 | umtx_initOnce(gOlsonToMetaInitOnce, &olsonToMetaInit, status); |
571 | if (U_FAILURE(status)) { | |
572 | return NULL; | |
46f4442e | 573 | } |
46f4442e | 574 | |
729e4ab9 A |
575 | // get the mapping from cache |
576 | const UVector *result = NULL; | |
46f4442e | 577 | |
3d1f044b | 578 | umtx_lock(gZoneMetaLock()); |
729e4ab9 A |
579 | { |
580 | result = (UVector*) uhash_get(gOlsonToMeta, tzidUChars); | |
46f4442e | 581 | } |
3d1f044b | 582 | umtx_unlock(gZoneMetaLock()); |
46f4442e | 583 | |
729e4ab9 A |
584 | if (result != NULL) { |
585 | return result; | |
46f4442e | 586 | } |
46f4442e | 587 | |
729e4ab9 A |
588 | // miss the cache - create new one |
589 | UVector *tmpResult = createMetazoneMappings(tzid); | |
590 | if (tmpResult == NULL) { | |
591 | // not available | |
592 | return NULL; | |
46f4442e | 593 | } |
46f4442e | 594 | |
729e4ab9 | 595 | // put the new one into the cache |
3d1f044b | 596 | umtx_lock(gZoneMetaLock()); |
729e4ab9 A |
597 | { |
598 | // make sure it's already created | |
599 | result = (UVector*) uhash_get(gOlsonToMeta, tzidUChars); | |
600 | if (result == NULL) { | |
601 | // add the one just created | |
602 | int32_t tzidLen = tzid.length() + 1; | |
603 | UChar *key = (UChar*)uprv_malloc(tzidLen * sizeof(UChar)); | |
604 | if (key == NULL) { | |
605 | // memory allocation error.. just return NULL | |
606 | result = NULL; | |
607 | delete tmpResult; | |
608 | } else { | |
609 | tzid.extract(key, tzidLen, status); | |
610 | uhash_put(gOlsonToMeta, key, tmpResult, &status); | |
611 | if (U_FAILURE(status)) { | |
612 | // delete the mapping | |
613 | result = NULL; | |
614 | delete tmpResult; | |
615 | } else { | |
616 | result = tmpResult; | |
617 | } | |
618 | } | |
619 | } else { | |
620 | // another thread already put the one | |
621 | delete tmpResult; | |
622 | } | |
46f4442e | 623 | } |
3d1f044b | 624 | umtx_unlock(gZoneMetaLock()); |
46f4442e | 625 | |
729e4ab9 | 626 | return result; |
46f4442e A |
627 | } |
628 | ||
729e4ab9 A |
629 | UVector* |
630 | ZoneMeta::createMetazoneMappings(const UnicodeString &tzid) { | |
631 | UVector *mzMappings = NULL; | |
632 | UErrorCode status = U_ZERO_ERROR; | |
46f4442e | 633 | |
729e4ab9 A |
634 | UnicodeString canonicalID; |
635 | UResourceBundle *rb = ures_openDirect(NULL, gMetaZones, &status); | |
636 | ures_getByKey(rb, gMetazoneInfo, rb, &status); | |
4388f060 | 637 | getCanonicalCLDRID(tzid, canonicalID, status); |
729e4ab9 A |
638 | |
639 | if (U_SUCCESS(status)) { | |
4388f060 A |
640 | char tzKey[ZID_KEY_MAX + 1]; |
641 | int32_t tzKeyLen = canonicalID.extract(0, canonicalID.length(), tzKey, sizeof(tzKey), US_INV); | |
642 | tzKey[tzKeyLen] = 0; | |
46f4442e | 643 | |
729e4ab9 A |
644 | // tzid keys are using ':' as separators |
645 | char *p = tzKey; | |
646 | while (*p) { | |
647 | if (*p == '/') { | |
648 | *p = ':'; | |
649 | } | |
650 | p++; | |
46f4442e | 651 | } |
46f4442e | 652 | |
729e4ab9 | 653 | ures_getByKey(rb, tzKey, rb, &status); |
46f4442e | 654 | |
729e4ab9 A |
655 | if (U_SUCCESS(status)) { |
656 | UResourceBundle *mz = NULL; | |
657 | while (ures_hasNext(rb)) { | |
658 | mz = ures_getNextResource(rb, mz, &status); | |
46f4442e | 659 | |
729e4ab9 A |
660 | const UChar *mz_name = ures_getStringByIndex(mz, 0, NULL, &status); |
661 | const UChar *mz_from = gDefaultFrom; | |
662 | const UChar *mz_to = gDefaultTo; | |
46f4442e | 663 | |
729e4ab9 A |
664 | if (ures_getSize(mz) == 3) { |
665 | mz_from = ures_getStringByIndex(mz, 1, NULL, &status); | |
666 | mz_to = ures_getStringByIndex(mz, 2, NULL, &status); | |
667 | } | |
668 | ||
669 | if(U_FAILURE(status)){ | |
670 | status = U_ZERO_ERROR; | |
671 | continue; | |
672 | } | |
673 | // We do not want to use SimpleDateformat to parse boundary dates, | |
674 | // because this code could be triggered by the initialization code | |
675 | // used by SimpleDateFormat. | |
676 | UDate from = parseDate(mz_from, status); | |
677 | UDate to = parseDate(mz_to, status); | |
678 | if (U_FAILURE(status)) { | |
679 | status = U_ZERO_ERROR; | |
680 | continue; | |
681 | } | |
682 | ||
683 | OlsonToMetaMappingEntry *entry = (OlsonToMetaMappingEntry*)uprv_malloc(sizeof(OlsonToMetaMappingEntry)); | |
684 | if (entry == NULL) { | |
685 | status = U_MEMORY_ALLOCATION_ERROR; | |
46f4442e A |
686 | break; |
687 | } | |
729e4ab9 A |
688 | entry->mzid = mz_name; |
689 | entry->from = from; | |
690 | entry->to = to; | |
691 | ||
692 | if (mzMappings == NULL) { | |
693 | mzMappings = new UVector(deleteOlsonToMetaMappingEntry, NULL, status); | |
694 | if (U_FAILURE(status)) { | |
695 | delete mzMappings; | |
0f5d89e8 | 696 | mzMappings = NULL; |
729e4ab9 A |
697 | uprv_free(entry); |
698 | break; | |
699 | } | |
700 | } | |
701 | ||
702 | mzMappings->addElement(entry, status); | |
703 | if (U_FAILURE(status)) { | |
704 | break; | |
705 | } | |
706 | } | |
707 | ures_close(mz); | |
708 | if (U_FAILURE(status)) { | |
709 | if (mzMappings != NULL) { | |
710 | delete mzMappings; | |
711 | mzMappings = NULL; | |
712 | } | |
46f4442e A |
713 | } |
714 | } | |
46f4442e | 715 | } |
729e4ab9 A |
716 | ures_close(rb); |
717 | return mzMappings; | |
46f4442e A |
718 | } |
719 | ||
720 | UnicodeString& U_EXPORT2 | |
729e4ab9 A |
721 | ZoneMeta::getZoneIdByMetazone(const UnicodeString &mzid, const UnicodeString ®ion, UnicodeString &result) { |
722 | UErrorCode status = U_ZERO_ERROR; | |
723 | const UChar *tzid = NULL; | |
724 | int32_t tzidLen = 0; | |
725 | char keyBuf[ZID_KEY_MAX + 1]; | |
726 | int32_t keyLen = 0; | |
727 | ||
57a6839d | 728 | if (mzid.isBogus() || mzid.length() > ZID_KEY_MAX) { |
4388f060 | 729 | result.setToBogus(); |
729e4ab9 | 730 | return result; |
46f4442e | 731 | } |
46f4442e | 732 | |
4388f060 A |
733 | keyLen = mzid.extract(0, mzid.length(), keyBuf, ZID_KEY_MAX + 1, US_INV); |
734 | keyBuf[keyLen] = 0; | |
46f4442e | 735 | |
729e4ab9 A |
736 | UResourceBundle *rb = ures_openDirect(NULL, gMetaZones, &status); |
737 | ures_getByKey(rb, gMapTimezonesTag, rb, &status); | |
738 | ures_getByKey(rb, keyBuf, rb, &status); | |
739 | ||
740 | if (U_SUCCESS(status)) { | |
741 | // check region mapping | |
742 | if (region.length() == 2 || region.length() == 3) { | |
4388f060 A |
743 | keyLen = region.extract(0, region.length(), keyBuf, ZID_KEY_MAX + 1, US_INV); |
744 | keyBuf[keyLen] = 0; | |
729e4ab9 A |
745 | tzid = ures_getStringByKey(rb, keyBuf, &tzidLen, &status); |
746 | if (status == U_MISSING_RESOURCE_ERROR) { | |
747 | status = U_ZERO_ERROR; | |
46f4442e A |
748 | } |
749 | } | |
729e4ab9 A |
750 | if (U_SUCCESS(status) && tzid == NULL) { |
751 | // try "001" | |
752 | tzid = ures_getStringByKey(rb, gWorldTag, &tzidLen, &status); | |
753 | } | |
46f4442e | 754 | } |
729e4ab9 A |
755 | ures_close(rb); |
756 | ||
757 | if (tzid == NULL) { | |
4388f060 | 758 | result.setToBogus(); |
729e4ab9 A |
759 | } else { |
760 | result.setTo(tzid, tzidLen); | |
46f4442e | 761 | } |
729e4ab9 | 762 | |
46f4442e A |
763 | return result; |
764 | } | |
765 | ||
57a6839d A |
766 | static void U_CALLCONV initAvailableMetaZoneIDs () { |
767 | U_ASSERT(gMetaZoneIDs == NULL); | |
768 | U_ASSERT(gMetaZoneIDTable == NULL); | |
769 | ucln_i18n_registerCleanup(UCLN_I18N_ZONEMETA, zoneMeta_cleanup); | |
770 | ||
771 | UErrorCode status = U_ZERO_ERROR; | |
772 | gMetaZoneIDTable = uhash_open(uhash_hashUnicodeString, uhash_compareUnicodeString, NULL, &status); | |
773 | if (U_FAILURE(status) || gMetaZoneIDTable == NULL) { | |
774 | gMetaZoneIDTable = NULL; | |
775 | return; | |
776 | } | |
777 | uhash_setKeyDeleter(gMetaZoneIDTable, uprv_deleteUObject); | |
778 | // No valueDeleter, because the vector maintain the value objects | |
779 | gMetaZoneIDs = new UVector(NULL, uhash_compareUChars, status); | |
780 | if (U_FAILURE(status) || gMetaZoneIDs == NULL) { | |
781 | gMetaZoneIDs = NULL; | |
782 | uhash_close(gMetaZoneIDTable); | |
783 | gMetaZoneIDTable = NULL; | |
784 | return; | |
785 | } | |
786 | gMetaZoneIDs->setDeleter(uprv_free); | |
787 | ||
788 | UResourceBundle *rb = ures_openDirect(NULL, gMetaZones, &status); | |
789 | UResourceBundle *bundle = ures_getByKey(rb, gMapTimezonesTag, NULL, &status); | |
3d1f044b | 790 | StackUResourceBundle res; |
57a6839d | 791 | while (U_SUCCESS(status) && ures_hasNext(bundle)) { |
3d1f044b | 792 | ures_getNextResource(bundle, res.getAlias(), &status); |
57a6839d A |
793 | if (U_FAILURE(status)) { |
794 | break; | |
4388f060 | 795 | } |
3d1f044b | 796 | const char *mzID = ures_getKey(res.getAlias()); |
0f5d89e8 | 797 | int32_t len = static_cast<int32_t>(uprv_strlen(mzID)); |
57a6839d A |
798 | UChar *uMzID = (UChar*)uprv_malloc(sizeof(UChar) * (len + 1)); |
799 | if (uMzID == NULL) { | |
800 | status = U_MEMORY_ALLOCATION_ERROR; | |
801 | break; | |
802 | } | |
803 | u_charsToUChars(mzID, uMzID, len); | |
804 | uMzID[len] = 0; | |
805 | UnicodeString *usMzID = new UnicodeString(uMzID); | |
806 | if (uhash_get(gMetaZoneIDTable, usMzID) == NULL) { | |
807 | gMetaZoneIDs->addElement((void *)uMzID, status); | |
808 | uhash_put(gMetaZoneIDTable, (void *)usMzID, (void *)uMzID, &status); | |
809 | } else { | |
810 | uprv_free(uMzID); | |
811 | delete usMzID; | |
812 | } | |
813 | } | |
57a6839d A |
814 | ures_close(bundle); |
815 | ures_close(rb); | |
816 | ||
817 | if (U_FAILURE(status)) { | |
818 | uhash_close(gMetaZoneIDTable); | |
819 | delete gMetaZoneIDs; | |
820 | gMetaZoneIDTable = NULL; | |
821 | gMetaZoneIDs = NULL; | |
4388f060 A |
822 | } |
823 | } | |
824 | ||
825 | const UVector* | |
826 | ZoneMeta::getAvailableMetazoneIDs() { | |
57a6839d | 827 | umtx_initOnce(gMetaZoneIDsInitOnce, &initAvailableMetaZoneIDs); |
4388f060 A |
828 | return gMetaZoneIDs; |
829 | } | |
830 | ||
831 | const UChar* | |
832 | ZoneMeta::findMetaZoneID(const UnicodeString& mzid) { | |
57a6839d A |
833 | umtx_initOnce(gMetaZoneIDsInitOnce, &initAvailableMetaZoneIDs); |
834 | if (gMetaZoneIDTable == NULL) { | |
835 | return NULL; | |
836 | } | |
4388f060 A |
837 | return (const UChar*)uhash_get(gMetaZoneIDTable, &mzid); |
838 | } | |
839 | ||
840 | const UChar* | |
841 | ZoneMeta::findTimeZoneID(const UnicodeString& tzid) { | |
842 | return TimeZone::findID(tzid); | |
843 | } | |
844 | ||
845 | ||
846 | TimeZone* | |
847 | ZoneMeta::createCustomTimeZone(int32_t offset) { | |
848 | UBool negative = FALSE; | |
849 | int32_t tmp = offset; | |
850 | if (offset < 0) { | |
851 | negative = TRUE; | |
852 | tmp = -offset; | |
853 | } | |
3d1f044b | 854 | uint8_t hour, min, sec; |
4388f060 A |
855 | |
856 | tmp /= 1000; | |
3d1f044b | 857 | sec = static_cast<uint8_t>(tmp % 60); |
4388f060 | 858 | tmp /= 60; |
3d1f044b A |
859 | min = static_cast<uint8_t>(tmp % 60); |
860 | hour = static_cast<uint8_t>(tmp / 60); | |
4388f060 A |
861 | |
862 | UnicodeString zid; | |
863 | formatCustomID(hour, min, sec, negative, zid); | |
864 | return new SimpleTimeZone(offset, zid); | |
865 | } | |
866 | ||
867 | UnicodeString& | |
868 | ZoneMeta::formatCustomID(uint8_t hour, uint8_t min, uint8_t sec, UBool negative, UnicodeString& id) { | |
869 | // Create normalized time zone ID - GMT[+|-]HH:mm[:ss] | |
870 | id.setTo(gCustomTzPrefix, -1); | |
871 | if (hour != 0 || min != 0) { | |
872 | if (negative) { | |
51004dcb | 873 | id.append((UChar)0x2D); // '-' |
4388f060 | 874 | } else { |
51004dcb | 875 | id.append((UChar)0x2B); // '+' |
4388f060 A |
876 | } |
877 | // Always use US-ASCII digits | |
51004dcb A |
878 | id.append((UChar)(0x30 + (hour%100)/10)); |
879 | id.append((UChar)(0x30 + (hour%10))); | |
880 | id.append((UChar)0x3A); // ':' | |
881 | id.append((UChar)(0x30 + (min%100)/10)); | |
882 | id.append((UChar)(0x30 + (min%10))); | |
4388f060 | 883 | if (sec != 0) { |
51004dcb A |
884 | id.append((UChar)0x3A); // ':' |
885 | id.append((UChar)(0x30 + (sec%100)/10)); | |
886 | id.append((UChar)(0x30 + (sec%10))); | |
4388f060 A |
887 | } |
888 | } | |
889 | return id; | |
890 | } | |
891 | ||
51004dcb A |
892 | const UChar* |
893 | ZoneMeta::getShortID(const TimeZone& tz) { | |
894 | const UChar* canonicalID = NULL; | |
895 | if (dynamic_cast<const OlsonTimeZone *>(&tz) != NULL) { | |
896 | // short cut for OlsonTimeZone | |
897 | const OlsonTimeZone *otz = (const OlsonTimeZone*)&tz; | |
898 | canonicalID = otz->getCanonicalID(); | |
899 | } | |
900 | if (canonicalID == NULL) { | |
901 | return NULL; | |
902 | } | |
903 | return getShortIDFromCanonical(canonicalID); | |
904 | } | |
905 | ||
906 | const UChar* | |
907 | ZoneMeta::getShortID(const UnicodeString& id) { | |
908 | UErrorCode status = U_ZERO_ERROR; | |
909 | const UChar* canonicalID = ZoneMeta::getCanonicalCLDRID(id, status); | |
910 | if (U_FAILURE(status) || canonicalID == NULL) { | |
911 | return NULL; | |
912 | } | |
913 | return ZoneMeta::getShortIDFromCanonical(canonicalID); | |
914 | } | |
915 | ||
916 | const UChar* | |
917 | ZoneMeta::getShortIDFromCanonical(const UChar* canonicalID) { | |
918 | const UChar* shortID = NULL; | |
919 | int32_t len = u_strlen(canonicalID); | |
920 | char tzidKey[ZID_KEY_MAX + 1]; | |
921 | ||
922 | u_UCharsToChars(canonicalID, tzidKey, len); | |
923 | tzidKey[len] = (char) 0; // Make sure it is null terminated. | |
924 | ||
925 | // replace '/' with ':' | |
926 | char *p = tzidKey; | |
927 | while (*p++) { | |
928 | if (*p == '/') { | |
929 | *p = ':'; | |
930 | } | |
931 | } | |
932 | ||
933 | UErrorCode status = U_ZERO_ERROR; | |
934 | UResourceBundle *rb = ures_openDirect(NULL, gKeyTypeData, &status); | |
935 | ures_getByKey(rb, gTypeMapTag, rb, &status); | |
936 | ures_getByKey(rb, gTimezoneTag, rb, &status); | |
937 | shortID = ures_getStringByKey(rb, tzidKey, NULL, &status); | |
938 | ures_close(rb); | |
939 | ||
940 | return shortID; | |
941 | } | |
4388f060 | 942 | |
46f4442e A |
943 | U_NAMESPACE_END |
944 | ||
945 | #endif /* #if !UCONFIG_NO_FORMATTING */ |