Bug Summary

File:out/../deps/icu-small/source/i18n/zonemeta.cpp
Warning:line 358, column 21
Value stored to 'canonicalInCache' is never read

Annotated Source Code

Press '?' to see keyboard shortcuts

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