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