]>
Commit | Line | Data |
---|---|---|
f3c0d7a5 A |
1 | // © 2016 and later: Unicode, Inc. and others. |
2 | // License & terms of use: http://www.unicode.org/copyright.html | |
4388f060 A |
3 | /* |
4 | ******************************************************************************* | |
b331163b | 5 | * Copyright (C) 2011-2015, International Business Machines Corporation and |
57a6839d | 6 | * others. All Rights Reserved. |
4388f060 A |
7 | ******************************************************************************* |
8 | */ | |
9 | ||
10 | #include "unicode/utypes.h" | |
11 | ||
12 | #if !UCONFIG_NO_FORMATTING | |
13 | ||
14 | #include "unicode/calendar.h" | |
15 | #include "unicode/tzfmt.h" | |
16 | #include "unicode/numsys.h" | |
f3c0d7a5 | 17 | #include "unicode/strenum.h" |
4388f060 A |
18 | #include "unicode/uchar.h" |
19 | #include "unicode/udat.h" | |
f3c0d7a5 | 20 | #include "unicode/ustring.h" |
4388f060 A |
21 | #include "tzgnames.h" |
22 | #include "cmemory.h" | |
23 | #include "cstring.h" | |
24 | #include "putilimp.h" | |
25 | #include "uassert.h" | |
26 | #include "ucln_in.h" | |
27 | #include "umutex.h" | |
28 | #include "uresimp.h" | |
29 | #include "ureslocs.h" | |
30 | #include "uvector.h" | |
31 | #include "zonemeta.h" | |
51004dcb | 32 | #include "tznames_impl.h" // TextTrieMap |
4388f060 A |
33 | |
34 | U_NAMESPACE_BEGIN | |
35 | ||
51004dcb A |
36 | // Bit flags used by the parse method. |
37 | // The order must match UTimeZoneFormatStyle enum. | |
38 | #define ISO_Z_STYLE_FLAG 0x0080 | |
39 | #define ISO_LOCAL_STYLE_FLAG 0x0100 | |
40 | static const int16_t STYLE_PARSE_FLAGS[] = { | |
41 | 0x0001, // UTZFMT_STYLE_GENERIC_LOCATION, | |
42 | 0x0002, // UTZFMT_STYLE_GENERIC_LONG, | |
43 | 0x0004, // UTZFMT_STYLE_GENERIC_SHORT, | |
44 | 0x0008, // UTZFMT_STYLE_SPECIFIC_LONG, | |
45 | 0x0010, // UTZFMT_STYLE_SPECIFIC_SHORT, | |
46 | 0x0020, // UTZFMT_STYLE_LOCALIZED_GMT, | |
47 | 0x0040, // UTZFMT_STYLE_LOCALIZED_GMT_SHORT, | |
48 | ISO_Z_STYLE_FLAG, // UTZFMT_STYLE_ISO_BASIC_SHORT, | |
49 | ISO_LOCAL_STYLE_FLAG, // UTZFMT_STYLE_ISO_BASIC_LOCAL_SHORT, | |
50 | ISO_Z_STYLE_FLAG, // UTZFMT_STYLE_ISO_BASIC_FIXED, | |
51 | ISO_LOCAL_STYLE_FLAG, // UTZFMT_STYLE_ISO_BASIC_LOCAL_FIXED, | |
52 | ISO_Z_STYLE_FLAG, // UTZFMT_STYLE_ISO_BASIC_FULL, | |
53 | ISO_LOCAL_STYLE_FLAG, // UTZFMT_STYLE_ISO_BASIC_LOCAL_FULL, | |
54 | ISO_Z_STYLE_FLAG, // UTZFMT_STYLE_ISO_EXTENDED_FIXED, | |
55 | ISO_LOCAL_STYLE_FLAG, // UTZFMT_STYLE_ISO_EXTENDED_LOCAL_FIXED, | |
56 | ISO_Z_STYLE_FLAG, // UTZFMT_STYLE_ISO_EXTENDED_FULL, | |
57 | ISO_LOCAL_STYLE_FLAG, // UTZFMT_STYLE_ISO_EXTENDED_LOCAL_FULL, | |
58 | 0x0200, // UTZFMT_STYLE_ZONE_ID, | |
59 | 0x0400, // UTZFMT_STYLE_ZONE_ID_SHORT, | |
60 | 0x0800 // UTZFMT_STYLE_EXEMPLAR_LOCATION | |
61 | }; | |
62 | ||
4388f060 A |
63 | static const char gZoneStringsTag[] = "zoneStrings"; |
64 | static const char gGmtFormatTag[]= "gmtFormat"; | |
65 | static const char gGmtZeroFormatTag[] = "gmtZeroFormat"; | |
66 | static const char gHourFormatTag[]= "hourFormat"; | |
67 | ||
68 | static const UChar TZID_GMT[] = {0x0045, 0x0074, 0x0063, 0x002F, 0x0047, 0x004D, 0x0054, 0}; // Etc/GMT | |
51004dcb A |
69 | static const UChar UNKNOWN_ZONE_ID[] = { |
70 | 0x0045, 0x0074, 0x0063, 0x002F, 0x0055, 0x006E, 0x006B, 0x006E, 0x006F, 0x0077, 0x006E, 0}; // Etc/Unknown | |
71 | static const UChar UNKNOWN_SHORT_ZONE_ID[] = {0x0075, 0x006E, 0x006B, 0}; // unk | |
72 | static const UChar UNKNOWN_LOCATION[] = {0x0055, 0x006E, 0x006B, 0x006E, 0x006F, 0x0077, 0x006E, 0}; // Unknown | |
4388f060 A |
73 | |
74 | static const UChar DEFAULT_GMT_PATTERN[] = {0x0047, 0x004D, 0x0054, 0x007B, 0x0030, 0x007D, 0}; // GMT{0} | |
51004dcb A |
75 | //static const UChar DEFAULT_GMT_ZERO[] = {0x0047, 0x004D, 0x0054, 0}; // GMT |
76 | static const UChar DEFAULT_GMT_POSITIVE_HM[] = {0x002B, 0x0048, 0x003A, 0x006D, 0x006D, 0}; // +H:mm | |
77 | static const UChar DEFAULT_GMT_POSITIVE_HMS[] = {0x002B, 0x0048, 0x003A, 0x006D, 0x006D, 0x003A, 0x0073, 0x0073, 0}; // +H:mm:ss | |
78 | static const UChar DEFAULT_GMT_NEGATIVE_HM[] = {0x002D, 0x0048, 0x003A, 0x006D, 0x006D, 0}; // -H:mm | |
79 | static const UChar DEFAULT_GMT_NEGATIVE_HMS[] = {0x002D, 0x0048, 0x003A, 0x006D, 0x006D, 0x003A, 0x0073, 0x0073, 0}; // -H:mm:ss | |
80 | static const UChar DEFAULT_GMT_POSITIVE_H[] = {0x002B, 0x0048, 0}; // +H | |
81 | static const UChar DEFAULT_GMT_NEGATIVE_H[] = {0x002D, 0x0048, 0}; // -H | |
4388f060 A |
82 | |
83 | static const UChar32 DEFAULT_GMT_DIGITS[] = { | |
84 | 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, | |
85 | 0x0035, 0x0036, 0x0037, 0x0038, 0x0039 | |
86 | }; | |
87 | ||
88 | static const UChar DEFAULT_GMT_OFFSET_SEP = 0x003A; // ':' | |
89 | ||
90 | static const UChar ARG0[] = {0x007B, 0x0030, 0x007D}; // "{0}" | |
51004dcb | 91 | static const int32_t ARG0_LEN = 3; |
4388f060 A |
92 | |
93 | static const UChar DEFAULT_GMT_OFFSET_MINUTE_PATTERN[] = {0x006D, 0x006D, 0}; // "mm" | |
94 | static const UChar DEFAULT_GMT_OFFSET_SECOND_PATTERN[] = {0x0073, 0x0073, 0}; // "ss" | |
95 | ||
96 | static const UChar ALT_GMT_STRINGS[][4] = { | |
97 | {0x0047, 0x004D, 0x0054, 0}, // GMT | |
98 | {0x0055, 0x0054, 0x0043, 0}, // UTC | |
99 | {0x0055, 0x0054, 0, 0}, // UT | |
100 | {0, 0, 0, 0} | |
101 | }; | |
102 | ||
103 | // Order of GMT offset pattern parsing, *_HMS must be evaluated first | |
104 | // because *_HM is most likely a substring of *_HMS | |
105 | static const int32_t PARSE_GMT_OFFSET_TYPES[] = { | |
106 | UTZFMT_PAT_POSITIVE_HMS, | |
107 | UTZFMT_PAT_NEGATIVE_HMS, | |
108 | UTZFMT_PAT_POSITIVE_HM, | |
109 | UTZFMT_PAT_NEGATIVE_HM, | |
51004dcb A |
110 | UTZFMT_PAT_POSITIVE_H, |
111 | UTZFMT_PAT_NEGATIVE_H, | |
4388f060 A |
112 | -1 |
113 | }; | |
114 | ||
115 | static const UChar SINGLEQUOTE = 0x0027; | |
116 | static const UChar PLUS = 0x002B; | |
117 | static const UChar MINUS = 0x002D; | |
118 | static const UChar ISO8601_UTC = 0x005A; // 'Z' | |
119 | static const UChar ISO8601_SEP = 0x003A; // ':' | |
120 | ||
121 | static const int32_t MILLIS_PER_HOUR = 60 * 60 * 1000; | |
122 | static const int32_t MILLIS_PER_MINUTE = 60 * 1000; | |
123 | static const int32_t MILLIS_PER_SECOND = 1000; | |
124 | ||
125 | // Maximum offset (exclusive) in millisecond supported by offset formats | |
126 | static int32_t MAX_OFFSET = 24 * MILLIS_PER_HOUR; | |
127 | ||
128 | // Maximum values for GMT offset fields | |
129 | static const int32_t MAX_OFFSET_HOUR = 23; | |
130 | static const int32_t MAX_OFFSET_MINUTE = 59; | |
131 | static const int32_t MAX_OFFSET_SECOND = 59; | |
132 | ||
133 | static const int32_t UNKNOWN_OFFSET = 0x7FFFFFFF; | |
134 | ||
51004dcb | 135 | static const int32_t ALL_SIMPLE_NAME_TYPES = UTZNM_LONG_STANDARD | UTZNM_LONG_DAYLIGHT | UTZNM_SHORT_STANDARD | UTZNM_SHORT_DAYLIGHT | UTZNM_EXEMPLAR_LOCATION; |
4388f060 A |
136 | static const int32_t ALL_GENERIC_NAME_TYPES = UTZGNM_LOCATION | UTZGNM_LONG | UTZGNM_SHORT; |
137 | ||
4388f060 A |
138 | #define DIGIT_VAL(c) (0x0030 <= (c) && (c) <= 0x0039 ? (c) - 0x0030 : -1) |
139 | #define MAX_OFFSET_DIGITS 6 | |
140 | ||
51004dcb A |
141 | // Time Zone ID/Short ID trie |
142 | static TextTrieMap *gZoneIdTrie = NULL; | |
57a6839d | 143 | static icu::UInitOnce gZoneIdTrieInitOnce = U_INITONCE_INITIALIZER; |
51004dcb A |
144 | |
145 | static TextTrieMap *gShortZoneIdTrie = NULL; | |
57a6839d | 146 | static icu::UInitOnce gShortZoneIdTrieInitOnce = U_INITONCE_INITIALIZER; |
51004dcb A |
147 | |
148 | static UMutex gLock = U_MUTEX_INITIALIZER; | |
149 | ||
150 | U_CDECL_BEGIN | |
151 | /** | |
152 | * Cleanup callback func | |
153 | */ | |
154 | static UBool U_CALLCONV tzfmt_cleanup(void) | |
155 | { | |
156 | if (gZoneIdTrie != NULL) { | |
157 | delete gZoneIdTrie; | |
158 | } | |
159 | gZoneIdTrie = NULL; | |
57a6839d | 160 | gZoneIdTrieInitOnce.reset(); |
51004dcb A |
161 | |
162 | if (gShortZoneIdTrie != NULL) { | |
163 | delete gShortZoneIdTrie; | |
164 | } | |
165 | gShortZoneIdTrie = NULL; | |
57a6839d | 166 | gShortZoneIdTrieInitOnce.reset(); |
51004dcb A |
167 | |
168 | return TRUE; | |
169 | } | |
170 | U_CDECL_END | |
4388f060 A |
171 | |
172 | // ------------------------------------------------------------------ | |
173 | // GMTOffsetField | |
174 | // | |
175 | // This class represents a localized GMT offset pattern | |
176 | // item and used by TimeZoneFormat | |
177 | // ------------------------------------------------------------------ | |
178 | class GMTOffsetField : public UMemory { | |
179 | public: | |
180 | enum FieldType { | |
181 | TEXT = 0, | |
182 | HOUR = 1, | |
183 | MINUTE = 2, | |
184 | SECOND = 4 | |
185 | }; | |
186 | ||
187 | virtual ~GMTOffsetField(); | |
188 | ||
189 | static GMTOffsetField* createText(const UnicodeString& text, UErrorCode& status); | |
190 | static GMTOffsetField* createTimeField(FieldType type, uint8_t width, UErrorCode& status); | |
191 | static UBool isValid(FieldType type, int32_t width); | |
192 | static FieldType getTypeByLetter(UChar ch); | |
193 | ||
194 | FieldType getType() const; | |
195 | uint8_t getWidth() const; | |
196 | const UChar* getPatternText(void) const; | |
197 | ||
198 | private: | |
199 | UChar* fText; | |
200 | FieldType fType; | |
201 | uint8_t fWidth; | |
202 | ||
203 | GMTOffsetField(); | |
204 | }; | |
205 | ||
206 | GMTOffsetField::GMTOffsetField() | |
207 | : fText(NULL), fType(TEXT), fWidth(0) { | |
208 | } | |
209 | ||
210 | GMTOffsetField::~GMTOffsetField() { | |
211 | if (fText) { | |
212 | uprv_free(fText); | |
213 | } | |
214 | } | |
215 | ||
216 | GMTOffsetField* | |
217 | GMTOffsetField::createText(const UnicodeString& text, UErrorCode& status) { | |
218 | if (U_FAILURE(status)) { | |
219 | return NULL; | |
220 | } | |
221 | GMTOffsetField* result = new GMTOffsetField(); | |
222 | if (result == NULL) { | |
223 | status = U_MEMORY_ALLOCATION_ERROR; | |
224 | return NULL; | |
225 | } | |
226 | ||
227 | int32_t len = text.length(); | |
228 | result->fText = (UChar*)uprv_malloc((len + 1) * sizeof(UChar)); | |
229 | if (result->fText == NULL) { | |
230 | status = U_MEMORY_ALLOCATION_ERROR; | |
231 | delete result; | |
232 | return NULL; | |
233 | } | |
234 | u_strncpy(result->fText, text.getBuffer(), len); | |
235 | result->fText[len] = 0; | |
236 | result->fType = TEXT; | |
237 | ||
238 | return result; | |
239 | } | |
240 | ||
241 | GMTOffsetField* | |
242 | GMTOffsetField::createTimeField(FieldType type, uint8_t width, UErrorCode& status) { | |
243 | U_ASSERT(type != TEXT); | |
244 | if (U_FAILURE(status)) { | |
245 | return NULL; | |
246 | } | |
247 | GMTOffsetField* result = new GMTOffsetField(); | |
248 | if (result == NULL) { | |
249 | status = U_MEMORY_ALLOCATION_ERROR; | |
250 | return NULL; | |
251 | } | |
252 | ||
253 | result->fType = type; | |
254 | result->fWidth = width; | |
255 | ||
256 | return result; | |
257 | } | |
258 | ||
259 | UBool | |
260 | GMTOffsetField::isValid(FieldType type, int32_t width) { | |
261 | switch (type) { | |
262 | case HOUR: | |
263 | return (width == 1 || width == 2); | |
264 | case MINUTE: | |
265 | case SECOND: | |
266 | return (width == 2); | |
267 | default: | |
268 | U_ASSERT(FALSE); | |
269 | } | |
270 | return (width > 0); | |
271 | } | |
272 | ||
273 | GMTOffsetField::FieldType | |
274 | GMTOffsetField::getTypeByLetter(UChar ch) { | |
275 | if (ch == 0x0048 /* H */) { | |
276 | return HOUR; | |
277 | } else if (ch == 0x006D /* m */) { | |
278 | return MINUTE; | |
279 | } else if (ch == 0x0073 /* s */) { | |
280 | return SECOND; | |
281 | } | |
282 | return TEXT; | |
283 | } | |
284 | ||
285 | inline GMTOffsetField::FieldType | |
286 | GMTOffsetField::getType() const { | |
287 | return fType; | |
288 | } | |
289 | ||
290 | inline uint8_t | |
291 | GMTOffsetField::getWidth() const { | |
292 | return fWidth; | |
293 | } | |
294 | ||
295 | inline const UChar* | |
296 | GMTOffsetField::getPatternText(void) const { | |
297 | return fText; | |
298 | } | |
299 | ||
300 | ||
301 | U_CDECL_BEGIN | |
302 | static void U_CALLCONV | |
303 | deleteGMTOffsetField(void *obj) { | |
304 | delete static_cast<GMTOffsetField *>(obj); | |
305 | } | |
306 | U_CDECL_END | |
307 | ||
308 | ||
309 | // ------------------------------------------------------------------ | |
310 | // TimeZoneFormat | |
311 | // ------------------------------------------------------------------ | |
312 | UOBJECT_DEFINE_RTTI_IMPLEMENTATION(TimeZoneFormat) | |
313 | ||
314 | TimeZoneFormat::TimeZoneFormat(const Locale& locale, UErrorCode& status) | |
b331163b A |
315 | : fLocale(locale), fTimeZoneNames(NULL), fTimeZoneGenericNames(NULL), |
316 | fDefParseOptionFlags(0), fTZDBTimeZoneNames(NULL) { | |
4388f060 | 317 | |
51004dcb | 318 | for (int32_t i = 0; i < UTZFMT_PAT_COUNT; i++) { |
4388f060 A |
319 | fGMTOffsetPatternItems[i] = NULL; |
320 | } | |
321 | ||
322 | const char* region = fLocale.getCountry(); | |
323 | int32_t regionLen = uprv_strlen(region); | |
324 | if (regionLen == 0) { | |
325 | char loc[ULOC_FULLNAME_CAPACITY]; | |
326 | uloc_addLikelySubtags(fLocale.getName(), loc, sizeof(loc), &status); | |
327 | ||
328 | regionLen = uloc_getCountry(loc, fTargetRegion, sizeof(fTargetRegion), &status); | |
329 | if (U_SUCCESS(status)) { | |
330 | fTargetRegion[regionLen] = 0; | |
331 | } else { | |
332 | return; | |
333 | } | |
334 | } else if (regionLen < (int32_t)sizeof(fTargetRegion)) { | |
335 | uprv_strcpy(fTargetRegion, region); | |
336 | } else { | |
337 | fTargetRegion[0] = 0; | |
338 | } | |
339 | ||
340 | fTimeZoneNames = TimeZoneNames::createInstance(locale, status); | |
341 | // fTimeZoneGenericNames is lazily instantiated | |
51004dcb A |
342 | if (U_FAILURE(status)) { |
343 | return; | |
344 | } | |
4388f060 A |
345 | |
346 | const UChar* gmtPattern = NULL; | |
347 | const UChar* hourFormats = NULL; | |
348 | ||
349 | UResourceBundle *zoneBundle = ures_open(U_ICUDATA_ZONE, locale.getName(), &status); | |
350 | UResourceBundle *zoneStringsArray = ures_getByKeyWithFallback(zoneBundle, gZoneStringsTag, NULL, &status); | |
351 | if (U_SUCCESS(status)) { | |
352 | const UChar* resStr; | |
353 | int32_t len; | |
354 | resStr = ures_getStringByKeyWithFallback(zoneStringsArray, gGmtFormatTag, &len, &status); | |
355 | if (len > 0) { | |
356 | gmtPattern = resStr; | |
357 | } | |
358 | resStr = ures_getStringByKeyWithFallback(zoneStringsArray, gGmtZeroFormatTag, &len, &status); | |
359 | if (len > 0) { | |
360 | fGMTZeroFormat.setTo(TRUE, resStr, len); | |
361 | } | |
362 | resStr = ures_getStringByKeyWithFallback(zoneStringsArray, gHourFormatTag, &len, &status); | |
363 | if (len > 0) { | |
364 | hourFormats = resStr; | |
365 | } | |
366 | ures_close(zoneStringsArray); | |
367 | ures_close(zoneBundle); | |
368 | } | |
369 | ||
370 | if (gmtPattern == NULL) { | |
371 | gmtPattern = DEFAULT_GMT_PATTERN; | |
372 | } | |
b331163b | 373 | initGMTPattern(UnicodeString(TRUE, gmtPattern, -1), status); |
4388f060 | 374 | |
51004dcb | 375 | UBool useDefaultOffsetPatterns = TRUE; |
4388f060 A |
376 | if (hourFormats) { |
377 | UChar *sep = u_strchr(hourFormats, (UChar)0x003B /* ';' */); | |
378 | if (sep != NULL) { | |
51004dcb | 379 | UErrorCode tmpStatus = U_ZERO_ERROR; |
4388f060 A |
380 | fGMTOffsetPatterns[UTZFMT_PAT_POSITIVE_HM].setTo(FALSE, hourFormats, (int32_t)(sep - hourFormats)); |
381 | fGMTOffsetPatterns[UTZFMT_PAT_NEGATIVE_HM].setTo(TRUE, sep + 1, -1); | |
51004dcb A |
382 | expandOffsetPattern(fGMTOffsetPatterns[UTZFMT_PAT_POSITIVE_HM], fGMTOffsetPatterns[UTZFMT_PAT_POSITIVE_HMS], tmpStatus); |
383 | expandOffsetPattern(fGMTOffsetPatterns[UTZFMT_PAT_NEGATIVE_HM], fGMTOffsetPatterns[UTZFMT_PAT_NEGATIVE_HMS], tmpStatus); | |
384 | truncateOffsetPattern(fGMTOffsetPatterns[UTZFMT_PAT_POSITIVE_HM], fGMTOffsetPatterns[UTZFMT_PAT_POSITIVE_H], tmpStatus); | |
385 | truncateOffsetPattern(fGMTOffsetPatterns[UTZFMT_PAT_NEGATIVE_HM], fGMTOffsetPatterns[UTZFMT_PAT_NEGATIVE_H], tmpStatus); | |
386 | if (U_SUCCESS(tmpStatus)) { | |
387 | useDefaultOffsetPatterns = FALSE; | |
388 | } | |
4388f060 A |
389 | } |
390 | } | |
51004dcb A |
391 | if (useDefaultOffsetPatterns) { |
392 | fGMTOffsetPatterns[UTZFMT_PAT_POSITIVE_H].setTo(TRUE, DEFAULT_GMT_POSITIVE_H, -1); | |
4388f060 A |
393 | fGMTOffsetPatterns[UTZFMT_PAT_POSITIVE_HM].setTo(TRUE, DEFAULT_GMT_POSITIVE_HM, -1); |
394 | fGMTOffsetPatterns[UTZFMT_PAT_POSITIVE_HMS].setTo(TRUE, DEFAULT_GMT_POSITIVE_HMS, -1); | |
51004dcb | 395 | fGMTOffsetPatterns[UTZFMT_PAT_NEGATIVE_H].setTo(TRUE, DEFAULT_GMT_NEGATIVE_H, -1); |
4388f060 A |
396 | fGMTOffsetPatterns[UTZFMT_PAT_NEGATIVE_HM].setTo(TRUE, DEFAULT_GMT_NEGATIVE_HM, -1); |
397 | fGMTOffsetPatterns[UTZFMT_PAT_NEGATIVE_HMS].setTo(TRUE, DEFAULT_GMT_NEGATIVE_HMS, -1); | |
398 | } | |
399 | initGMTOffsetPatterns(status); | |
400 | ||
401 | NumberingSystem* ns = NumberingSystem::createInstance(locale, status); | |
402 | UBool useDefDigits = TRUE; | |
403 | if (ns && !ns->isAlgorithmic()) { | |
404 | UnicodeString digits = ns->getDescription(); | |
405 | useDefDigits = !toCodePoints(digits, fGMTOffsetDigits, 10); | |
406 | } | |
407 | if (useDefDigits) { | |
408 | uprv_memcpy(fGMTOffsetDigits, DEFAULT_GMT_DIGITS, sizeof(UChar32) * 10); | |
409 | } | |
410 | delete ns; | |
411 | } | |
412 | ||
413 | TimeZoneFormat::TimeZoneFormat(const TimeZoneFormat& other) | |
b331163b A |
414 | : Format(other), fTimeZoneNames(NULL), fTimeZoneGenericNames(NULL), |
415 | fTZDBTimeZoneNames(NULL) { | |
4388f060 | 416 | |
51004dcb | 417 | for (int32_t i = 0; i < UTZFMT_PAT_COUNT; i++) { |
4388f060 A |
418 | fGMTOffsetPatternItems[i] = NULL; |
419 | } | |
420 | *this = other; | |
421 | } | |
422 | ||
423 | ||
424 | TimeZoneFormat::~TimeZoneFormat() { | |
425 | delete fTimeZoneNames; | |
426 | delete fTimeZoneGenericNames; | |
b331163b | 427 | delete fTZDBTimeZoneNames; |
51004dcb | 428 | for (int32_t i = 0; i < UTZFMT_PAT_COUNT; i++) { |
4388f060 A |
429 | delete fGMTOffsetPatternItems[i]; |
430 | } | |
4388f060 A |
431 | } |
432 | ||
433 | TimeZoneFormat& | |
434 | TimeZoneFormat::operator=(const TimeZoneFormat& other) { | |
435 | if (this == &other) { | |
436 | return *this; | |
437 | } | |
438 | ||
439 | delete fTimeZoneNames; | |
440 | delete fTimeZoneGenericNames; | |
441 | fTimeZoneGenericNames = NULL; | |
b331163b A |
442 | delete fTZDBTimeZoneNames; |
443 | fTZDBTimeZoneNames = NULL; | |
4388f060 A |
444 | |
445 | fLocale = other.fLocale; | |
446 | uprv_memcpy(fTargetRegion, other.fTargetRegion, sizeof(fTargetRegion)); | |
447 | ||
448 | fTimeZoneNames = other.fTimeZoneNames->clone(); | |
449 | if (other.fTimeZoneGenericNames) { | |
57a6839d | 450 | // TODO: this test has dubious thread safety. |
4388f060 A |
451 | fTimeZoneGenericNames = other.fTimeZoneGenericNames->clone(); |
452 | } | |
453 | ||
454 | fGMTPattern = other.fGMTPattern; | |
455 | fGMTPatternPrefix = other.fGMTPatternPrefix; | |
456 | fGMTPatternSuffix = other.fGMTPatternSuffix; | |
457 | ||
458 | UErrorCode status = U_ZERO_ERROR; | |
51004dcb | 459 | for (int32_t i = 0; i < UTZFMT_PAT_COUNT; i++) { |
4388f060 A |
460 | fGMTOffsetPatterns[i] = other.fGMTOffsetPatterns[i]; |
461 | delete fGMTOffsetPatternItems[i]; | |
b331163b | 462 | fGMTOffsetPatternItems[i] = NULL; |
4388f060 A |
463 | } |
464 | initGMTOffsetPatterns(status); | |
465 | U_ASSERT(U_SUCCESS(status)); | |
466 | ||
467 | fGMTZeroFormat = other.fGMTZeroFormat; | |
468 | ||
469 | uprv_memcpy(fGMTOffsetDigits, other.fGMTOffsetDigits, sizeof(fGMTOffsetDigits)); | |
470 | ||
471 | fDefParseOptionFlags = other.fDefParseOptionFlags; | |
472 | ||
473 | return *this; | |
474 | } | |
475 | ||
476 | ||
477 | UBool | |
478 | TimeZoneFormat::operator==(const Format& other) const { | |
479 | TimeZoneFormat* tzfmt = (TimeZoneFormat*)&other; | |
480 | ||
481 | UBool isEqual = | |
482 | fLocale == tzfmt->fLocale | |
483 | && fGMTPattern == tzfmt->fGMTPattern | |
484 | && fGMTZeroFormat == tzfmt->fGMTZeroFormat | |
485 | && *fTimeZoneNames == *tzfmt->fTimeZoneNames; | |
486 | ||
51004dcb | 487 | for (int32_t i = 0; i < UTZFMT_PAT_COUNT && isEqual; i++) { |
4388f060 A |
488 | isEqual = fGMTOffsetPatterns[i] == tzfmt->fGMTOffsetPatterns[i]; |
489 | } | |
490 | for (int32_t i = 0; i < 10 && isEqual; i++) { | |
491 | isEqual = fGMTOffsetDigits[i] == tzfmt->fGMTOffsetDigits[i]; | |
492 | } | |
493 | // TODO | |
494 | // Check fTimeZoneGenericNames. For now, | |
495 | // if fTimeZoneNames is same, fTimeZoneGenericNames should | |
496 | // be also equivalent. | |
497 | return isEqual; | |
498 | } | |
499 | ||
500 | Format* | |
501 | TimeZoneFormat::clone() const { | |
502 | return new TimeZoneFormat(*this); | |
503 | } | |
504 | ||
505 | TimeZoneFormat* U_EXPORT2 | |
506 | TimeZoneFormat::createInstance(const Locale& locale, UErrorCode& status) { | |
507 | TimeZoneFormat* tzfmt = new TimeZoneFormat(locale, status); | |
508 | if (U_SUCCESS(status)) { | |
509 | return tzfmt; | |
510 | } | |
511 | delete tzfmt; | |
512 | return NULL; | |
513 | } | |
514 | ||
515 | // ------------------------------------------------------------------ | |
516 | // Setter and Getter | |
517 | ||
518 | const TimeZoneNames* | |
519 | TimeZoneFormat::getTimeZoneNames() const { | |
520 | return (const TimeZoneNames*)fTimeZoneNames; | |
521 | } | |
522 | ||
523 | void | |
524 | TimeZoneFormat::adoptTimeZoneNames(TimeZoneNames *tznames) { | |
525 | delete fTimeZoneNames; | |
526 | fTimeZoneNames = tznames; | |
527 | ||
528 | // TODO - We should also update fTimeZoneGenericNames | |
529 | } | |
530 | ||
531 | void | |
532 | TimeZoneFormat::setTimeZoneNames(const TimeZoneNames &tznames) { | |
533 | delete fTimeZoneNames; | |
534 | fTimeZoneNames = tznames.clone(); | |
535 | ||
536 | // TODO - We should also update fTimeZoneGenericNames | |
537 | } | |
538 | ||
539 | void | |
51004dcb | 540 | TimeZoneFormat::setDefaultParseOptions(uint32_t flags) { |
4388f060 A |
541 | fDefParseOptionFlags = flags; |
542 | } | |
543 | ||
51004dcb | 544 | uint32_t |
4388f060 A |
545 | TimeZoneFormat::getDefaultParseOptions(void) const { |
546 | return fDefParseOptionFlags; | |
547 | } | |
548 | ||
549 | ||
550 | UnicodeString& | |
551 | TimeZoneFormat::getGMTPattern(UnicodeString& pattern) const { | |
552 | return pattern.setTo(fGMTPattern); | |
553 | } | |
554 | ||
555 | void | |
556 | TimeZoneFormat::setGMTPattern(const UnicodeString& pattern, UErrorCode& status) { | |
557 | initGMTPattern(pattern, status); | |
558 | } | |
559 | ||
560 | UnicodeString& | |
561 | TimeZoneFormat::getGMTOffsetPattern(UTimeZoneFormatGMTOffsetPatternType type, UnicodeString& pattern) const { | |
562 | return pattern.setTo(fGMTOffsetPatterns[type]); | |
563 | } | |
564 | ||
565 | void | |
566 | TimeZoneFormat::setGMTOffsetPattern(UTimeZoneFormatGMTOffsetPatternType type, const UnicodeString& pattern, UErrorCode& status) { | |
567 | if (U_FAILURE(status)) { | |
568 | return; | |
569 | } | |
570 | if (pattern == fGMTOffsetPatterns[type]) { | |
571 | // No need to reset | |
572 | return; | |
573 | } | |
574 | ||
51004dcb A |
575 | OffsetFields required = FIELDS_HM; |
576 | switch (type) { | |
577 | case UTZFMT_PAT_POSITIVE_H: | |
578 | case UTZFMT_PAT_NEGATIVE_H: | |
579 | required = FIELDS_H; | |
580 | break; | |
581 | case UTZFMT_PAT_POSITIVE_HM: | |
582 | case UTZFMT_PAT_NEGATIVE_HM: | |
583 | required = FIELDS_HM; | |
584 | break; | |
585 | case UTZFMT_PAT_POSITIVE_HMS: | |
586 | case UTZFMT_PAT_NEGATIVE_HMS: | |
587 | required = FIELDS_HMS; | |
588 | break; | |
589 | default: | |
590 | U_ASSERT(FALSE); | |
591 | break; | |
592 | } | |
4388f060 A |
593 | |
594 | UVector* patternItems = parseOffsetPattern(pattern, required, status); | |
595 | if (patternItems == NULL) { | |
596 | return; | |
597 | } | |
598 | ||
599 | fGMTOffsetPatterns[type].setTo(pattern); | |
600 | delete fGMTOffsetPatternItems[type]; | |
601 | fGMTOffsetPatternItems[type] = patternItems; | |
51004dcb | 602 | checkAbuttingHoursAndMinutes(); |
4388f060 A |
603 | } |
604 | ||
605 | UnicodeString& | |
606 | TimeZoneFormat::getGMTOffsetDigits(UnicodeString& digits) const { | |
607 | digits.remove(); | |
608 | for (int32_t i = 0; i < 10; i++) { | |
609 | digits.append(fGMTOffsetDigits[i]); | |
610 | } | |
611 | return digits; | |
612 | } | |
613 | ||
614 | void | |
615 | TimeZoneFormat::setGMTOffsetDigits(const UnicodeString& digits, UErrorCode& status) { | |
616 | if (U_FAILURE(status)) { | |
617 | return; | |
618 | } | |
619 | UChar32 digitArray[10]; | |
620 | if (!toCodePoints(digits, digitArray, 10)) { | |
621 | status = U_ILLEGAL_ARGUMENT_ERROR; | |
622 | return; | |
623 | } | |
624 | uprv_memcpy(fGMTOffsetDigits, digitArray, sizeof(UChar32)*10); | |
625 | } | |
626 | ||
627 | UnicodeString& | |
628 | TimeZoneFormat::getGMTZeroFormat(UnicodeString& gmtZeroFormat) const { | |
629 | return gmtZeroFormat.setTo(fGMTZeroFormat); | |
630 | } | |
631 | ||
632 | void | |
633 | TimeZoneFormat::setGMTZeroFormat(const UnicodeString& gmtZeroFormat, UErrorCode& status) { | |
634 | if (U_SUCCESS(status)) { | |
635 | if (gmtZeroFormat.isEmpty()) { | |
636 | status = U_ILLEGAL_ARGUMENT_ERROR; | |
637 | } else if (gmtZeroFormat != fGMTZeroFormat) { | |
638 | fGMTZeroFormat.setTo(gmtZeroFormat); | |
639 | } | |
640 | } | |
641 | } | |
642 | ||
643 | // ------------------------------------------------------------------ | |
644 | // Format and Parse | |
645 | ||
646 | UnicodeString& | |
647 | TimeZoneFormat::format(UTimeZoneFormatStyle style, const TimeZone& tz, UDate date, | |
648 | UnicodeString& name, UTimeZoneFormatTimeType* timeType /* = NULL */) const { | |
649 | if (timeType) { | |
650 | *timeType = UTZFMT_TIME_TYPE_UNKNOWN; | |
651 | } | |
57a6839d A |
652 | |
653 | UBool noOffsetFormatFallback = FALSE; | |
654 | ||
4388f060 A |
655 | switch (style) { |
656 | case UTZFMT_STYLE_GENERIC_LOCATION: | |
657 | formatGeneric(tz, UTZGNM_LOCATION, date, name); | |
658 | break; | |
659 | case UTZFMT_STYLE_GENERIC_LONG: | |
660 | formatGeneric(tz, UTZGNM_LONG, date, name); | |
661 | break; | |
662 | case UTZFMT_STYLE_GENERIC_SHORT: | |
663 | formatGeneric(tz, UTZGNM_SHORT, date, name); | |
664 | break; | |
665 | case UTZFMT_STYLE_SPECIFIC_LONG: | |
666 | formatSpecific(tz, UTZNM_LONG_STANDARD, UTZNM_LONG_DAYLIGHT, date, name, timeType); | |
667 | break; | |
668 | case UTZFMT_STYLE_SPECIFIC_SHORT: | |
669 | formatSpecific(tz, UTZNM_SHORT_STANDARD, UTZNM_SHORT_DAYLIGHT, date, name, timeType); | |
670 | break; | |
57a6839d A |
671 | |
672 | case UTZFMT_STYLE_ZONE_ID: | |
673 | tz.getID(name); | |
674 | noOffsetFormatFallback = TRUE; | |
675 | break; | |
676 | case UTZFMT_STYLE_ZONE_ID_SHORT: | |
677 | { | |
678 | const UChar* shortID = ZoneMeta::getShortID(tz); | |
679 | if (shortID == NULL) { | |
680 | shortID = UNKNOWN_SHORT_ZONE_ID; | |
681 | } | |
682 | name.setTo(shortID, -1); | |
683 | } | |
684 | noOffsetFormatFallback = TRUE; | |
685 | break; | |
686 | ||
687 | case UTZFMT_STYLE_EXEMPLAR_LOCATION: | |
688 | formatExemplarLocation(tz, name); | |
689 | noOffsetFormatFallback = TRUE; | |
690 | break; | |
691 | ||
51004dcb | 692 | default: |
4388f060 A |
693 | // will be handled below |
694 | break; | |
695 | } | |
696 | ||
57a6839d | 697 | if (name.isEmpty() && !noOffsetFormatFallback) { |
4388f060 A |
698 | UErrorCode status = U_ZERO_ERROR; |
699 | int32_t rawOffset, dstOffset; | |
700 | tz.getOffset(date, FALSE, rawOffset, dstOffset, status); | |
51004dcb | 701 | int32_t offset = rawOffset + dstOffset; |
4388f060 A |
702 | if (U_SUCCESS(status)) { |
703 | switch (style) { | |
51004dcb A |
704 | case UTZFMT_STYLE_GENERIC_LOCATION: |
705 | case UTZFMT_STYLE_GENERIC_LONG: | |
706 | case UTZFMT_STYLE_SPECIFIC_LONG: | |
707 | case UTZFMT_STYLE_LOCALIZED_GMT: | |
708 | formatOffsetLocalizedGMT(offset, name, status); | |
709 | break; | |
710 | ||
711 | case UTZFMT_STYLE_GENERIC_SHORT: | |
712 | case UTZFMT_STYLE_SPECIFIC_SHORT: | |
713 | case UTZFMT_STYLE_LOCALIZED_GMT_SHORT: | |
714 | formatOffsetShortLocalizedGMT(offset, name, status); | |
4388f060 | 715 | break; |
51004dcb A |
716 | |
717 | case UTZFMT_STYLE_ISO_BASIC_SHORT: | |
718 | formatOffsetISO8601Basic(offset, TRUE, TRUE, TRUE, name, status); | |
4388f060 | 719 | break; |
51004dcb A |
720 | |
721 | case UTZFMT_STYLE_ISO_BASIC_LOCAL_SHORT: | |
722 | formatOffsetISO8601Basic(offset, FALSE, TRUE, TRUE, name, status); | |
723 | break; | |
724 | ||
725 | case UTZFMT_STYLE_ISO_BASIC_FIXED: | |
726 | formatOffsetISO8601Basic(offset, TRUE, FALSE, TRUE, name, status); | |
727 | break; | |
728 | ||
729 | case UTZFMT_STYLE_ISO_BASIC_LOCAL_FIXED: | |
730 | formatOffsetISO8601Basic(offset, FALSE, FALSE, TRUE, name, status); | |
731 | break; | |
732 | ||
733 | case UTZFMT_STYLE_ISO_EXTENDED_FIXED: | |
734 | formatOffsetISO8601Extended(offset, TRUE, FALSE, TRUE, name, status); | |
735 | break; | |
736 | ||
737 | case UTZFMT_STYLE_ISO_EXTENDED_LOCAL_FIXED: | |
738 | formatOffsetISO8601Extended(offset, FALSE, FALSE, TRUE, name, status); | |
739 | break; | |
740 | ||
741 | case UTZFMT_STYLE_ISO_BASIC_FULL: | |
742 | formatOffsetISO8601Basic(offset, TRUE, FALSE, FALSE, name, status); | |
743 | break; | |
744 | ||
745 | case UTZFMT_STYLE_ISO_BASIC_LOCAL_FULL: | |
746 | formatOffsetISO8601Basic(offset, FALSE, FALSE, FALSE, name, status); | |
747 | break; | |
748 | ||
749 | case UTZFMT_STYLE_ISO_EXTENDED_FULL: | |
750 | formatOffsetISO8601Extended(offset, TRUE, FALSE, FALSE, name, status); | |
751 | break; | |
752 | ||
753 | case UTZFMT_STYLE_ISO_EXTENDED_LOCAL_FULL: | |
754 | formatOffsetISO8601Extended(offset, FALSE, FALSE, FALSE, name, status); | |
755 | break; | |
756 | ||
57a6839d A |
757 | default: |
758 | // UTZFMT_STYLE_ZONE_ID, UTZFMT_STYLE_ZONE_ID_SHORT, UTZFMT_STYLE_EXEMPLAR_LOCATION | |
759 | break; | |
4388f060 | 760 | } |
57a6839d | 761 | |
4388f060 A |
762 | if (timeType) { |
763 | *timeType = (dstOffset != 0) ? UTZFMT_TIME_TYPE_DAYLIGHT : UTZFMT_TIME_TYPE_STANDARD; | |
764 | } | |
765 | } | |
4388f060 A |
766 | } |
767 | ||
768 | return name; | |
769 | } | |
770 | ||
771 | UnicodeString& | |
772 | TimeZoneFormat::format(const Formattable& obj, UnicodeString& appendTo, | |
773 | FieldPosition& pos, UErrorCode& status) const { | |
774 | if (U_FAILURE(status)) { | |
775 | return appendTo; | |
776 | } | |
777 | UDate date = Calendar::getNow(); | |
778 | if (obj.getType() == Formattable::kObject) { | |
779 | const UObject* formatObj = obj.getObject(); | |
780 | const TimeZone* tz = dynamic_cast<const TimeZone*>(formatObj); | |
781 | if (tz == NULL) { | |
782 | const Calendar* cal = dynamic_cast<const Calendar*>(formatObj); | |
783 | if (cal != NULL) { | |
784 | tz = &cal->getTimeZone(); | |
785 | date = cal->getTime(status); | |
786 | } | |
787 | } | |
788 | if (tz != NULL) { | |
789 | int32_t rawOffset, dstOffset; | |
790 | tz->getOffset(date, FALSE, rawOffset, dstOffset, status); | |
b331163b A |
791 | UChar buf[32]; |
792 | UnicodeString result(buf, 0, UPRV_LENGTHOF(buf)); | |
4388f060 A |
793 | formatOffsetLocalizedGMT(rawOffset + dstOffset, result, status); |
794 | if (U_SUCCESS(status)) { | |
795 | appendTo.append(result); | |
796 | if (pos.getField() == UDAT_TIMEZONE_FIELD) { | |
797 | pos.setBeginIndex(0); | |
798 | pos.setEndIndex(result.length()); | |
799 | } | |
800 | } | |
801 | } | |
802 | } | |
803 | return appendTo; | |
804 | } | |
805 | ||
806 | TimeZone* | |
807 | TimeZoneFormat::parse(UTimeZoneFormatStyle style, const UnicodeString& text, ParsePosition& pos, | |
808 | UTimeZoneFormatTimeType* timeType /*= NULL*/) const { | |
809 | return parse(style, text, pos, getDefaultParseOptions(), timeType); | |
810 | } | |
811 | ||
812 | TimeZone* | |
813 | TimeZoneFormat::parse(UTimeZoneFormatStyle style, const UnicodeString& text, ParsePosition& pos, | |
814 | int32_t parseOptions, UTimeZoneFormatTimeType* timeType /* = NULL */) const { | |
815 | if (timeType) { | |
816 | *timeType = UTZFMT_TIME_TYPE_UNKNOWN; | |
817 | } | |
818 | ||
819 | int32_t startIdx = pos.getIndex(); | |
820 | int32_t maxPos = text.length(); | |
821 | int32_t offset; | |
822 | ||
51004dcb A |
823 | // Styles using localized GMT format as fallback |
824 | UBool fallbackLocalizedGMT = | |
825 | (style == UTZFMT_STYLE_SPECIFIC_LONG || style == UTZFMT_STYLE_GENERIC_LONG || style == UTZFMT_STYLE_GENERIC_LOCATION); | |
826 | UBool fallbackShortLocalizedGMT = | |
827 | (style == UTZFMT_STYLE_SPECIFIC_SHORT || style == UTZFMT_STYLE_GENERIC_SHORT); | |
4388f060 | 828 | |
51004dcb | 829 | int32_t evaluated = 0; // bit flags representing already evaluated styles |
4388f060 A |
830 | ParsePosition tmpPos(startIdx); |
831 | ||
832 | int32_t parsedOffset = UNKNOWN_OFFSET; // stores successfully parsed offset for later use | |
833 | int32_t parsedPos = -1; // stores successfully parsed offset position for later use | |
834 | ||
835 | // Try localized GMT format first if necessary | |
51004dcb | 836 | if (fallbackLocalizedGMT || fallbackShortLocalizedGMT) { |
4388f060 | 837 | UBool hasDigitOffset = FALSE; |
51004dcb | 838 | offset = parseOffsetLocalizedGMT(text, tmpPos, fallbackShortLocalizedGMT, &hasDigitOffset); |
4388f060 A |
839 | if (tmpPos.getErrorIndex() == -1) { |
840 | // Even when the input text was successfully parsed as a localized GMT format text, | |
841 | // we may still need to evaluate the specified style if - | |
842 | // 1) GMT zero format was used, and | |
843 | // 2) The input text was not completely processed | |
844 | if (tmpPos.getIndex() == maxPos || hasDigitOffset) { | |
845 | pos.setIndex(tmpPos.getIndex()); | |
846 | return createTimeZoneForOffset(offset); | |
847 | } | |
848 | parsedOffset = offset; | |
849 | parsedPos = tmpPos.getIndex(); | |
850 | } | |
51004dcb A |
851 | // Note: For now, no distinction between long/short localized GMT format in the parser. |
852 | // This might be changed in future. | |
853 | // evaluated |= (fallbackLocalizedGMT ? STYLE_PARSE_FLAGS[UTZFMT_STYLE_LOCALIZED_GMT] : STYLE_PARSE_FLAGS[UTZFMT_STYLE_LOCALIZED_GMT_SHORT]); | |
854 | evaluated |= STYLE_PARSE_FLAGS[UTZFMT_STYLE_LOCALIZED_GMT] | STYLE_PARSE_FLAGS[UTZFMT_STYLE_LOCALIZED_GMT_SHORT]; | |
4388f060 A |
855 | } |
856 | ||
857 | UErrorCode status = U_ZERO_ERROR; | |
b331163b A |
858 | UChar tzIDBuf[32]; |
859 | UnicodeString tzID(tzIDBuf, 0, UPRV_LENGTHOF(tzIDBuf)); | |
860 | ||
861 | UBool parseTZDBAbbrev = ((parseOptions & UTZFMT_PARSE_OPTION_TZ_DATABASE_ABBREVIATIONS) != 0); | |
4388f060 A |
862 | |
863 | // Try the specified style | |
864 | switch (style) { | |
51004dcb | 865 | case UTZFMT_STYLE_LOCALIZED_GMT: |
4388f060 | 866 | { |
51004dcb A |
867 | tmpPos.setIndex(startIdx); |
868 | tmpPos.setErrorIndex(-1); | |
869 | ||
870 | offset = parseOffsetLocalizedGMT(text, tmpPos); | |
4388f060 A |
871 | if (tmpPos.getErrorIndex() == -1) { |
872 | pos.setIndex(tmpPos.getIndex()); | |
873 | return createTimeZoneForOffset(offset); | |
874 | } | |
4388f060 | 875 | |
51004dcb A |
876 | // Note: For now, no distinction between long/short localized GMT format in the parser. |
877 | // This might be changed in future. | |
878 | evaluated |= STYLE_PARSE_FLAGS[UTZFMT_STYLE_LOCALIZED_GMT_SHORT]; | |
879 | ||
880 | break; | |
881 | } | |
882 | case UTZFMT_STYLE_LOCALIZED_GMT_SHORT: | |
4388f060 | 883 | { |
51004dcb A |
884 | tmpPos.setIndex(startIdx); |
885 | tmpPos.setErrorIndex(-1); | |
886 | ||
887 | offset = parseOffsetShortLocalizedGMT(text, tmpPos); | |
4388f060 A |
888 | if (tmpPos.getErrorIndex() == -1) { |
889 | pos.setIndex(tmpPos.getIndex()); | |
890 | return createTimeZoneForOffset(offset); | |
891 | } | |
4388f060 | 892 | |
51004dcb A |
893 | // Note: For now, no distinction between long/short localized GMT format in the parser. |
894 | // This might be changed in future. | |
895 | evaluated |= STYLE_PARSE_FLAGS[UTZFMT_STYLE_LOCALIZED_GMT]; | |
896 | ||
897 | break; | |
898 | } | |
899 | case UTZFMT_STYLE_ISO_BASIC_SHORT: | |
900 | case UTZFMT_STYLE_ISO_BASIC_FIXED: | |
901 | case UTZFMT_STYLE_ISO_BASIC_FULL: | |
902 | case UTZFMT_STYLE_ISO_EXTENDED_FIXED: | |
903 | case UTZFMT_STYLE_ISO_EXTENDED_FULL: | |
4388f060 | 904 | { |
51004dcb A |
905 | tmpPos.setIndex(startIdx); |
906 | tmpPos.setErrorIndex(-1); | |
907 | ||
4388f060 A |
908 | offset = parseOffsetISO8601(text, tmpPos); |
909 | if (tmpPos.getErrorIndex() == -1) { | |
910 | pos.setIndex(tmpPos.getIndex()); | |
911 | return createTimeZoneForOffset(offset); | |
912 | } | |
51004dcb A |
913 | |
914 | break; | |
915 | } | |
916 | ||
917 | case UTZFMT_STYLE_ISO_BASIC_LOCAL_SHORT: | |
918 | case UTZFMT_STYLE_ISO_BASIC_LOCAL_FIXED: | |
919 | case UTZFMT_STYLE_ISO_BASIC_LOCAL_FULL: | |
920 | case UTZFMT_STYLE_ISO_EXTENDED_LOCAL_FIXED: | |
921 | case UTZFMT_STYLE_ISO_EXTENDED_LOCAL_FULL: | |
922 | { | |
923 | tmpPos.setIndex(startIdx); | |
924 | tmpPos.setErrorIndex(-1); | |
925 | ||
926 | // Exclude the case of UTC Indicator "Z" here | |
927 | UBool hasDigitOffset = FALSE; | |
928 | offset = parseOffsetISO8601(text, tmpPos, FALSE, &hasDigitOffset); | |
929 | if (tmpPos.getErrorIndex() == -1 && hasDigitOffset) { | |
930 | pos.setIndex(tmpPos.getIndex()); | |
931 | return createTimeZoneForOffset(offset); | |
932 | } | |
933 | ||
934 | break; | |
4388f060 | 935 | } |
4388f060 A |
936 | |
937 | case UTZFMT_STYLE_SPECIFIC_LONG: | |
938 | case UTZFMT_STYLE_SPECIFIC_SHORT: | |
939 | { | |
940 | // Specific styles | |
941 | int32_t nameTypes = 0; | |
942 | if (style == UTZFMT_STYLE_SPECIFIC_LONG) { | |
943 | nameTypes = (UTZNM_LONG_STANDARD | UTZNM_LONG_DAYLIGHT); | |
944 | } else { | |
945 | U_ASSERT(style == UTZFMT_STYLE_SPECIFIC_SHORT); | |
946 | nameTypes = (UTZNM_SHORT_STANDARD | UTZNM_SHORT_DAYLIGHT); | |
947 | } | |
948 | LocalPointer<TimeZoneNames::MatchInfoCollection> specificMatches(fTimeZoneNames->find(text, startIdx, nameTypes, status)); | |
949 | if (U_FAILURE(status)) { | |
950 | pos.setErrorIndex(startIdx); | |
951 | return NULL; | |
952 | } | |
953 | if (!specificMatches.isNull()) { | |
954 | int32_t matchIdx = -1; | |
955 | int32_t matchPos = -1; | |
956 | for (int32_t i = 0; i < specificMatches->size(); i++) { | |
957 | matchPos = startIdx + specificMatches->getMatchLengthAt(i); | |
958 | if (matchPos > parsedPos) { | |
959 | matchIdx = i; | |
960 | parsedPos = matchPos; | |
961 | } | |
962 | } | |
963 | if (matchIdx >= 0) { | |
964 | if (timeType) { | |
965 | *timeType = getTimeType(specificMatches->getNameTypeAt(matchIdx)); | |
966 | } | |
967 | pos.setIndex(matchPos); | |
968 | getTimeZoneID(specificMatches.getAlias(), matchIdx, tzID); | |
969 | U_ASSERT(!tzID.isEmpty()); | |
970 | return TimeZone::createTimeZone(tzID); | |
971 | } | |
972 | } | |
b331163b A |
973 | |
974 | if (parseTZDBAbbrev && style == UTZFMT_STYLE_SPECIFIC_SHORT) { | |
975 | U_ASSERT((nameTypes & UTZNM_SHORT_STANDARD) != 0); | |
976 | U_ASSERT((nameTypes & UTZNM_SHORT_DAYLIGHT) != 0); | |
977 | ||
978 | const TZDBTimeZoneNames *tzdbTimeZoneNames = getTZDBTimeZoneNames(status); | |
979 | if (U_SUCCESS(status)) { | |
980 | LocalPointer<TimeZoneNames::MatchInfoCollection> tzdbNameMatches( | |
981 | tzdbTimeZoneNames->find(text, startIdx, nameTypes, status)); | |
982 | if (U_FAILURE(status)) { | |
983 | pos.setErrorIndex(startIdx); | |
984 | return NULL; | |
985 | } | |
986 | if (!tzdbNameMatches.isNull()) { | |
987 | int32_t matchIdx = -1; | |
988 | int32_t matchPos = -1; | |
989 | for (int32_t i = 0; i < tzdbNameMatches->size(); i++) { | |
990 | matchPos = startIdx + tzdbNameMatches->getMatchLengthAt(i); | |
991 | if (matchPos > parsedPos) { | |
992 | matchIdx = i; | |
993 | parsedPos = matchPos; | |
994 | } | |
995 | } | |
996 | if (matchIdx >= 0) { | |
997 | if (timeType) { | |
998 | *timeType = getTimeType(tzdbNameMatches->getNameTypeAt(matchIdx)); | |
999 | } | |
1000 | pos.setIndex(matchPos); | |
1001 | getTimeZoneID(tzdbNameMatches.getAlias(), matchIdx, tzID); | |
1002 | U_ASSERT(!tzID.isEmpty()); | |
1003 | return TimeZone::createTimeZone(tzID); | |
1004 | } | |
1005 | } | |
1006 | } | |
1007 | } | |
51004dcb | 1008 | break; |
4388f060 | 1009 | } |
4388f060 A |
1010 | case UTZFMT_STYLE_GENERIC_LONG: |
1011 | case UTZFMT_STYLE_GENERIC_SHORT: | |
1012 | case UTZFMT_STYLE_GENERIC_LOCATION: | |
1013 | { | |
1014 | int32_t genericNameTypes = 0; | |
1015 | switch (style) { | |
1016 | case UTZFMT_STYLE_GENERIC_LOCATION: | |
1017 | genericNameTypes = UTZGNM_LOCATION; | |
1018 | break; | |
1019 | ||
1020 | case UTZFMT_STYLE_GENERIC_LONG: | |
1021 | genericNameTypes = UTZGNM_LONG | UTZGNM_LOCATION; | |
1022 | break; | |
1023 | ||
1024 | case UTZFMT_STYLE_GENERIC_SHORT: | |
1025 | genericNameTypes = UTZGNM_SHORT | UTZGNM_LOCATION; | |
1026 | break; | |
1027 | ||
1028 | default: | |
1029 | U_ASSERT(FALSE); | |
1030 | } | |
1031 | ||
1032 | int32_t len = 0; | |
51004dcb | 1033 | UTimeZoneFormatTimeType tt = UTZFMT_TIME_TYPE_UNKNOWN; |
4388f060 A |
1034 | const TimeZoneGenericNames *gnames = getTimeZoneGenericNames(status); |
1035 | if (U_SUCCESS(status)) { | |
51004dcb | 1036 | len = gnames->findBestMatch(text, startIdx, genericNameTypes, tzID, tt, status); |
4388f060 A |
1037 | } |
1038 | if (U_FAILURE(status)) { | |
1039 | pos.setErrorIndex(startIdx); | |
1040 | return NULL; | |
1041 | } | |
1042 | if (len > 0) { | |
1043 | // Found a match | |
1044 | if (timeType) { | |
51004dcb | 1045 | *timeType = tt; |
4388f060 A |
1046 | } |
1047 | pos.setIndex(startIdx + len); | |
1048 | U_ASSERT(!tzID.isEmpty()); | |
1049 | return TimeZone::createTimeZone(tzID); | |
1050 | } | |
51004dcb A |
1051 | |
1052 | break; | |
1053 | } | |
1054 | case UTZFMT_STYLE_ZONE_ID: | |
1055 | { | |
1056 | tmpPos.setIndex(startIdx); | |
1057 | tmpPos.setErrorIndex(-1); | |
1058 | ||
1059 | parseZoneID(text, tmpPos, tzID); | |
1060 | if (tmpPos.getErrorIndex() == -1) { | |
1061 | pos.setIndex(tmpPos.getIndex()); | |
1062 | return TimeZone::createTimeZone(tzID); | |
1063 | } | |
1064 | break; | |
1065 | } | |
1066 | case UTZFMT_STYLE_ZONE_ID_SHORT: | |
1067 | { | |
1068 | tmpPos.setIndex(startIdx); | |
1069 | tmpPos.setErrorIndex(-1); | |
1070 | ||
1071 | parseShortZoneID(text, tmpPos, tzID); | |
1072 | if (tmpPos.getErrorIndex() == -1) { | |
1073 | pos.setIndex(tmpPos.getIndex()); | |
1074 | return TimeZone::createTimeZone(tzID); | |
1075 | } | |
1076 | break; | |
1077 | } | |
1078 | case UTZFMT_STYLE_EXEMPLAR_LOCATION: | |
1079 | { | |
1080 | tmpPos.setIndex(startIdx); | |
1081 | tmpPos.setErrorIndex(-1); | |
1082 | ||
1083 | parseExemplarLocation(text, tmpPos, tzID); | |
1084 | if (tmpPos.getErrorIndex() == -1) { | |
1085 | pos.setIndex(tmpPos.getIndex()); | |
1086 | return TimeZone::createTimeZone(tzID); | |
1087 | } | |
1088 | break; | |
4388f060 | 1089 | } |
4388f060 | 1090 | } |
51004dcb | 1091 | evaluated |= STYLE_PARSE_FLAGS[style]; |
4388f060 A |
1092 | |
1093 | ||
1094 | if (parsedPos > startIdx) { | |
1095 | // When the specified style is one of SPECIFIC_XXX or GENERIC_XXX, we tried to parse the input | |
1096 | // as localized GMT format earlier. If parsedOffset is positive, it means it was successfully | |
1097 | // parsed as localized GMT format, but offset digits were not detected (more specifically, GMT | |
1098 | // zero format). Then, it tried to find a match within the set of display names, but could not | |
1099 | // find a match. At this point, we can safely assume the input text contains the localized | |
1100 | // GMT format. | |
1101 | U_ASSERT(parsedOffset != UNKNOWN_OFFSET); | |
1102 | pos.setIndex(parsedPos); | |
1103 | return createTimeZoneForOffset(parsedOffset); | |
1104 | } | |
1105 | ||
1106 | // Failed to parse the input text as the time zone format in the specified style. | |
1107 | // Check the longest match among other styles below. | |
b331163b A |
1108 | UChar parsedIDBuf[32]; |
1109 | UnicodeString parsedID(parsedIDBuf, 0, UPRV_LENGTHOF(parsedIDBuf)); | |
51004dcb A |
1110 | UTimeZoneFormatTimeType parsedTimeType = UTZFMT_TIME_TYPE_UNKNOWN; |
1111 | ||
4388f060 A |
1112 | U_ASSERT(parsedPos < 0); |
1113 | U_ASSERT(parsedOffset == UNKNOWN_OFFSET); | |
4388f060 A |
1114 | |
1115 | // ISO 8601 | |
51004dcb A |
1116 | if (parsedPos < maxPos && |
1117 | ((evaluated & ISO_Z_STYLE_FLAG) == 0 || (evaluated & ISO_LOCAL_STYLE_FLAG) == 0)) { | |
1118 | tmpPos.setIndex(startIdx); | |
1119 | tmpPos.setErrorIndex(-1); | |
1120 | ||
4388f060 A |
1121 | UBool hasDigitOffset = FALSE; |
1122 | offset = parseOffsetISO8601(text, tmpPos, FALSE, &hasDigitOffset); | |
1123 | if (tmpPos.getErrorIndex() == -1) { | |
1124 | if (tmpPos.getIndex() == maxPos || hasDigitOffset) { | |
1125 | pos.setIndex(tmpPos.getIndex()); | |
1126 | return createTimeZoneForOffset(offset); | |
1127 | } | |
1128 | // Note: When ISO 8601 format contains offset digits, it should not | |
51004dcb A |
1129 | // collide with other formats. However, ISO 8601 UTC format "Z" (single letter) |
1130 | // may collide with other names. In this case, we need to evaluate other names. | |
1131 | if (parsedPos < tmpPos.getIndex()) { | |
1132 | parsedOffset = offset; | |
1133 | parsedID.setToBogus(); | |
1134 | parsedTimeType = UTZFMT_TIME_TYPE_UNKNOWN; | |
1135 | parsedPos = tmpPos.getIndex(); | |
1136 | U_ASSERT(parsedPos == startIdx + 1); // only when "Z" is used | |
1137 | } | |
4388f060 | 1138 | } |
51004dcb A |
1139 | } |
1140 | ||
1141 | // Localized GMT format | |
1142 | if (parsedPos < maxPos && | |
1143 | (evaluated & STYLE_PARSE_FLAGS[UTZFMT_STYLE_LOCALIZED_GMT]) == 0) { | |
4388f060 A |
1144 | tmpPos.setIndex(startIdx); |
1145 | tmpPos.setErrorIndex(-1); | |
4388f060 | 1146 | |
51004dcb A |
1147 | UBool hasDigitOffset = FALSE; |
1148 | offset = parseOffsetLocalizedGMT(text, tmpPos, FALSE, &hasDigitOffset); | |
1149 | if (tmpPos.getErrorIndex() == -1) { | |
1150 | if (tmpPos.getIndex() == maxPos || hasDigitOffset) { | |
1151 | pos.setIndex(tmpPos.getIndex()); | |
1152 | return createTimeZoneForOffset(offset); | |
1153 | } | |
1154 | // Evaluate other names - see the comment earlier in this method. | |
1155 | if (parsedPos < tmpPos.getIndex()) { | |
1156 | parsedOffset = offset; | |
1157 | parsedID.setToBogus(); | |
1158 | parsedTimeType = UTZFMT_TIME_TYPE_UNKNOWN; | |
1159 | parsedPos = tmpPos.getIndex(); | |
1160 | } | |
1161 | } | |
1162 | } | |
4388f060 | 1163 | |
51004dcb A |
1164 | if (parsedPos < maxPos && |
1165 | (evaluated & STYLE_PARSE_FLAGS[UTZFMT_STYLE_LOCALIZED_GMT_SHORT]) == 0) { | |
1166 | tmpPos.setIndex(startIdx); | |
1167 | tmpPos.setErrorIndex(-1); | |
4388f060 | 1168 | |
4388f060 | 1169 | UBool hasDigitOffset = FALSE; |
51004dcb | 1170 | offset = parseOffsetLocalizedGMT(text, tmpPos, TRUE, &hasDigitOffset); |
4388f060 A |
1171 | if (tmpPos.getErrorIndex() == -1) { |
1172 | if (tmpPos.getIndex() == maxPos || hasDigitOffset) { | |
1173 | pos.setIndex(tmpPos.getIndex()); | |
1174 | return createTimeZoneForOffset(offset); | |
1175 | } | |
1176 | // Evaluate other names - see the comment earlier in this method. | |
51004dcb A |
1177 | if (parsedPos < tmpPos.getIndex()) { |
1178 | parsedOffset = offset; | |
1179 | parsedID.setToBogus(); | |
1180 | parsedTimeType = UTZFMT_TIME_TYPE_UNKNOWN; | |
1181 | parsedPos = tmpPos.getIndex(); | |
1182 | } | |
4388f060 A |
1183 | } |
1184 | } | |
1185 | ||
51004dcb | 1186 | // When ParseOption.ALL_STYLES is available, we also try to look all possible display names and IDs. |
4388f060 A |
1187 | // For example, when style is GENERIC_LONG, "EST" (SPECIFIC_SHORT) is never |
1188 | // used for America/New_York. With parseAllStyles true, this code parses "EST" | |
1189 | // as America/New_York. | |
1190 | ||
1191 | // Note: Adding all possible names into the trie used by the implementation is quite heavy operation, | |
1192 | // which we want to avoid normally (note that we cache the trie, so this is applicable to the | |
1193 | // first time only as long as the cache does not expire). | |
51004dcb | 1194 | |
4388f060 | 1195 | if (parseOptions & UTZFMT_PARSE_OPTION_ALL_STYLES) { |
51004dcb A |
1196 | // Try all specific names and exemplar location names |
1197 | if (parsedPos < maxPos) { | |
1198 | LocalPointer<TimeZoneNames::MatchInfoCollection> specificMatches(fTimeZoneNames->find(text, startIdx, ALL_SIMPLE_NAME_TYPES, status)); | |
1199 | if (U_FAILURE(status)) { | |
1200 | pos.setErrorIndex(startIdx); | |
1201 | return NULL; | |
1202 | } | |
1203 | int32_t specificMatchIdx = -1; | |
4388f060 | 1204 | int32_t matchPos = -1; |
51004dcb A |
1205 | if (!specificMatches.isNull()) { |
1206 | for (int32_t i = 0; i < specificMatches->size(); i++) { | |
1207 | if (startIdx + specificMatches->getMatchLengthAt(i) > matchPos) { | |
1208 | specificMatchIdx = i; | |
1209 | matchPos = startIdx + specificMatches->getMatchLengthAt(i); | |
1210 | } | |
4388f060 A |
1211 | } |
1212 | } | |
51004dcb A |
1213 | if (parsedPos < matchPos) { |
1214 | U_ASSERT(specificMatchIdx >= 0); | |
1215 | parsedPos = matchPos; | |
1216 | getTimeZoneID(specificMatches.getAlias(), specificMatchIdx, parsedID); | |
1217 | parsedTimeType = getTimeType(specificMatches->getNameTypeAt(specificMatchIdx)); | |
1218 | parsedOffset = UNKNOWN_OFFSET; | |
1219 | } | |
4388f060 | 1220 | } |
b331163b A |
1221 | if (parseTZDBAbbrev && parsedPos < maxPos && (evaluated & STYLE_PARSE_FLAGS[UTZFMT_STYLE_SPECIFIC_SHORT]) == 0) { |
1222 | const TZDBTimeZoneNames *tzdbTimeZoneNames = getTZDBTimeZoneNames(status); | |
1223 | if (U_SUCCESS(status)) { | |
1224 | LocalPointer<TimeZoneNames::MatchInfoCollection> tzdbNameMatches( | |
1225 | tzdbTimeZoneNames->find(text, startIdx, ALL_SIMPLE_NAME_TYPES, status)); | |
1226 | if (U_FAILURE(status)) { | |
1227 | pos.setErrorIndex(startIdx); | |
1228 | return NULL; | |
1229 | } | |
1230 | int32_t tzdbNameMatchIdx = -1; | |
1231 | int32_t matchPos = -1; | |
1232 | if (!tzdbNameMatches.isNull()) { | |
1233 | for (int32_t i = 0; i < tzdbNameMatches->size(); i++) { | |
1234 | if (startIdx + tzdbNameMatches->getMatchLengthAt(i) > matchPos) { | |
1235 | tzdbNameMatchIdx = i; | |
1236 | matchPos = startIdx + tzdbNameMatches->getMatchLengthAt(i); | |
1237 | } | |
1238 | } | |
1239 | } | |
1240 | if (parsedPos < matchPos) { | |
1241 | U_ASSERT(tzdbNameMatchIdx >= 0); | |
1242 | parsedPos = matchPos; | |
1243 | getTimeZoneID(tzdbNameMatches.getAlias(), tzdbNameMatchIdx, parsedID); | |
1244 | parsedTimeType = getTimeType(tzdbNameMatches->getNameTypeAt(tzdbNameMatchIdx)); | |
1245 | parsedOffset = UNKNOWN_OFFSET; | |
1246 | } | |
1247 | } | |
1248 | } | |
51004dcb | 1249 | // Try generic names |
4388f060 | 1250 | if (parsedPos < maxPos) { |
51004dcb A |
1251 | int32_t genMatchLen = -1; |
1252 | UTimeZoneFormatTimeType tt = UTZFMT_TIME_TYPE_UNKNOWN; | |
1253 | ||
4388f060 A |
1254 | const TimeZoneGenericNames *gnames = getTimeZoneGenericNames(status); |
1255 | if (U_SUCCESS(status)) { | |
51004dcb | 1256 | genMatchLen = gnames->findBestMatch(text, startIdx, ALL_GENERIC_NAME_TYPES, tzID, tt, status); |
4388f060 A |
1257 | } |
1258 | if (U_FAILURE(status)) { | |
1259 | pos.setErrorIndex(startIdx); | |
1260 | return NULL; | |
1261 | } | |
51004dcb | 1262 | |
b331163b | 1263 | if (genMatchLen > 0 && parsedPos < startIdx + genMatchLen) { |
51004dcb A |
1264 | parsedPos = startIdx + genMatchLen; |
1265 | parsedID.setTo(tzID); | |
1266 | parsedTimeType = tt; | |
1267 | parsedOffset = UNKNOWN_OFFSET; | |
1268 | } | |
4388f060 | 1269 | } |
51004dcb A |
1270 | |
1271 | // Try time zone ID | |
1272 | if (parsedPos < maxPos && (evaluated & STYLE_PARSE_FLAGS[UTZFMT_STYLE_ZONE_ID]) == 0) { | |
1273 | tmpPos.setIndex(startIdx); | |
1274 | tmpPos.setErrorIndex(-1); | |
1275 | ||
1276 | parseZoneID(text, tmpPos, tzID); | |
1277 | if (tmpPos.getErrorIndex() == -1 && parsedPos < tmpPos.getIndex()) { | |
1278 | parsedPos = tmpPos.getIndex(); | |
1279 | parsedID.setTo(tzID); | |
1280 | parsedTimeType = UTZFMT_TIME_TYPE_UNKNOWN; | |
1281 | parsedOffset = UNKNOWN_OFFSET; | |
4388f060 | 1282 | } |
51004dcb A |
1283 | } |
1284 | // Try short time zone ID | |
1285 | if (parsedPos < maxPos && (evaluated & STYLE_PARSE_FLAGS[UTZFMT_STYLE_ZONE_ID]) == 0) { | |
1286 | tmpPos.setIndex(startIdx); | |
1287 | tmpPos.setErrorIndex(-1); | |
1288 | ||
1289 | parseShortZoneID(text, tmpPos, tzID); | |
1290 | if (tmpPos.getErrorIndex() == -1 && parsedPos < tmpPos.getIndex()) { | |
1291 | parsedPos = tmpPos.getIndex(); | |
1292 | parsedID.setTo(tzID); | |
1293 | parsedTimeType = UTZFMT_TIME_TYPE_UNKNOWN; | |
1294 | parsedOffset = UNKNOWN_OFFSET; | |
4388f060 | 1295 | } |
4388f060 A |
1296 | } |
1297 | } | |
1298 | ||
1299 | if (parsedPos > startIdx) { | |
51004dcb A |
1300 | // Parsed successfully |
1301 | TimeZone* parsedTZ; | |
1302 | if (parsedID.length() > 0) { | |
1303 | parsedTZ = TimeZone::createTimeZone(parsedID); | |
1304 | } else { | |
1305 | U_ASSERT(parsedOffset != UNKNOWN_OFFSET); | |
1306 | parsedTZ = createTimeZoneForOffset(parsedOffset); | |
1307 | } | |
1308 | if (timeType) { | |
1309 | *timeType = parsedTimeType; | |
1310 | } | |
4388f060 | 1311 | pos.setIndex(parsedPos); |
51004dcb | 1312 | return parsedTZ; |
4388f060 A |
1313 | } |
1314 | ||
1315 | pos.setErrorIndex(startIdx); | |
1316 | return NULL; | |
1317 | } | |
1318 | ||
1319 | void | |
1320 | TimeZoneFormat::parseObject(const UnicodeString& source, Formattable& result, | |
1321 | ParsePosition& parse_pos) const { | |
1322 | result.adoptObject(parse(UTZFMT_STYLE_GENERIC_LOCATION, source, parse_pos, UTZFMT_PARSE_OPTION_ALL_STYLES)); | |
1323 | } | |
1324 | ||
1325 | ||
1326 | // ------------------------------------------------------------------ | |
1327 | // Private zone name format/parse implementation | |
1328 | ||
1329 | UnicodeString& | |
1330 | TimeZoneFormat::formatGeneric(const TimeZone& tz, int32_t genType, UDate date, UnicodeString& name) const { | |
1331 | UErrorCode status = U_ZERO_ERROR; | |
1332 | const TimeZoneGenericNames* gnames = getTimeZoneGenericNames(status); | |
1333 | if (U_FAILURE(status)) { | |
1334 | name.setToBogus(); | |
1335 | return name; | |
1336 | } | |
1337 | ||
1338 | if (genType == UTZGNM_LOCATION) { | |
1339 | const UChar* canonicalID = ZoneMeta::getCanonicalCLDRID(tz); | |
1340 | if (canonicalID == NULL) { | |
1341 | name.setToBogus(); | |
1342 | return name; | |
1343 | } | |
b331163b | 1344 | return gnames->getGenericLocationName(UnicodeString(TRUE, canonicalID, -1), name); |
4388f060 A |
1345 | } |
1346 | return gnames->getDisplayName(tz, (UTimeZoneGenericNameType)genType, date, name); | |
1347 | } | |
1348 | ||
1349 | UnicodeString& | |
1350 | TimeZoneFormat::formatSpecific(const TimeZone& tz, UTimeZoneNameType stdType, UTimeZoneNameType dstType, | |
1351 | UDate date, UnicodeString& name, UTimeZoneFormatTimeType *timeType) const { | |
1352 | if (fTimeZoneNames == NULL) { | |
1353 | name.setToBogus(); | |
1354 | return name; | |
1355 | } | |
1356 | ||
1357 | UErrorCode status = U_ZERO_ERROR; | |
1358 | UBool isDaylight = tz.inDaylightTime(date, status); | |
1359 | const UChar* canonicalID = ZoneMeta::getCanonicalCLDRID(tz); | |
1360 | ||
1361 | if (U_FAILURE(status) || canonicalID == NULL) { | |
1362 | name.setToBogus(); | |
1363 | return name; | |
1364 | } | |
1365 | ||
1366 | if (isDaylight) { | |
b331163b | 1367 | fTimeZoneNames->getDisplayName(UnicodeString(TRUE, canonicalID, -1), dstType, date, name); |
4388f060 | 1368 | } else { |
b331163b | 1369 | fTimeZoneNames->getDisplayName(UnicodeString(TRUE, canonicalID, -1), stdType, date, name); |
4388f060 A |
1370 | } |
1371 | ||
1372 | if (timeType && !name.isEmpty()) { | |
1373 | *timeType = isDaylight ? UTZFMT_TIME_TYPE_DAYLIGHT : UTZFMT_TIME_TYPE_STANDARD; | |
1374 | } | |
1375 | return name; | |
1376 | } | |
1377 | ||
1378 | const TimeZoneGenericNames* | |
1379 | TimeZoneFormat::getTimeZoneGenericNames(UErrorCode& status) const { | |
1380 | if (U_FAILURE(status)) { | |
1381 | return NULL; | |
1382 | } | |
1383 | ||
57a6839d A |
1384 | umtx_lock(&gLock); |
1385 | if (fTimeZoneGenericNames == NULL) { | |
4388f060 | 1386 | TimeZoneFormat *nonConstThis = const_cast<TimeZoneFormat *>(this); |
57a6839d | 1387 | nonConstThis->fTimeZoneGenericNames = TimeZoneGenericNames::createInstance(fLocale, status); |
4388f060 | 1388 | } |
57a6839d | 1389 | umtx_unlock(&gLock); |
4388f060 A |
1390 | |
1391 | return fTimeZoneGenericNames; | |
1392 | } | |
1393 | ||
b331163b A |
1394 | const TZDBTimeZoneNames* |
1395 | TimeZoneFormat::getTZDBTimeZoneNames(UErrorCode& status) const { | |
1396 | if (U_FAILURE(status)) { | |
1397 | return NULL; | |
1398 | } | |
1399 | ||
1400 | umtx_lock(&gLock); | |
1401 | if (fTZDBTimeZoneNames == NULL) { | |
1402 | TZDBTimeZoneNames *tzdbNames = new TZDBTimeZoneNames(fLocale); | |
1403 | if (tzdbNames == NULL) { | |
1404 | status = U_MEMORY_ALLOCATION_ERROR; | |
1405 | } else { | |
1406 | TimeZoneFormat *nonConstThis = const_cast<TimeZoneFormat *>(this); | |
1407 | nonConstThis->fTZDBTimeZoneNames = tzdbNames; | |
1408 | } | |
1409 | } | |
1410 | umtx_unlock(&gLock); | |
1411 | ||
1412 | return fTZDBTimeZoneNames; | |
1413 | } | |
1414 | ||
51004dcb A |
1415 | UnicodeString& |
1416 | TimeZoneFormat::formatExemplarLocation(const TimeZone& tz, UnicodeString& name) const { | |
b331163b A |
1417 | UChar locationBuf[64]; |
1418 | UnicodeString location(locationBuf, 0, UPRV_LENGTHOF(locationBuf)); | |
51004dcb A |
1419 | const UChar* canonicalID = ZoneMeta::getCanonicalCLDRID(tz); |
1420 | ||
1421 | if (canonicalID) { | |
b331163b | 1422 | fTimeZoneNames->getExemplarLocationName(UnicodeString(TRUE, canonicalID, -1), location); |
51004dcb A |
1423 | } |
1424 | if (location.length() > 0) { | |
1425 | name.setTo(location); | |
1426 | } else { | |
1427 | // Use "unknown" location | |
b331163b | 1428 | fTimeZoneNames->getExemplarLocationName(UnicodeString(TRUE, UNKNOWN_ZONE_ID, -1), location); |
51004dcb A |
1429 | if (location.length() > 0) { |
1430 | name.setTo(location); | |
1431 | } else { | |
1432 | // last resort | |
1433 | name.setTo(UNKNOWN_LOCATION, -1); | |
1434 | } | |
1435 | } | |
1436 | return name; | |
1437 | } | |
1438 | ||
1439 | ||
4388f060 A |
1440 | // ------------------------------------------------------------------ |
1441 | // Zone offset format and parse | |
1442 | ||
1443 | UnicodeString& | |
51004dcb A |
1444 | TimeZoneFormat::formatOffsetISO8601Basic(int32_t offset, UBool useUtcIndicator, UBool isShort, UBool ignoreSeconds, |
1445 | UnicodeString& result, UErrorCode& status) const { | |
1446 | return formatOffsetISO8601(offset, TRUE, useUtcIndicator, isShort, ignoreSeconds, result, status); | |
1447 | } | |
1448 | ||
1449 | UnicodeString& | |
1450 | TimeZoneFormat::formatOffsetISO8601Extended(int32_t offset, UBool useUtcIndicator, UBool isShort, UBool ignoreSeconds, | |
1451 | UnicodeString& result, UErrorCode& status) const { | |
1452 | return formatOffsetISO8601(offset, FALSE, useUtcIndicator, isShort, ignoreSeconds, result, status); | |
1453 | } | |
1454 | ||
1455 | UnicodeString& | |
1456 | TimeZoneFormat::formatOffsetLocalizedGMT(int32_t offset, UnicodeString& result, UErrorCode& status) const { | |
1457 | return formatOffsetLocalizedGMT(offset, FALSE, result, status); | |
1458 | } | |
1459 | ||
1460 | UnicodeString& | |
1461 | TimeZoneFormat::formatOffsetShortLocalizedGMT(int32_t offset, UnicodeString& result, UErrorCode& status) const { | |
1462 | return formatOffsetLocalizedGMT(offset, TRUE, result, status); | |
1463 | } | |
1464 | ||
1465 | int32_t | |
1466 | TimeZoneFormat::parseOffsetISO8601(const UnicodeString& text, ParsePosition& pos) const { | |
1467 | return parseOffsetISO8601(text, pos, FALSE); | |
1468 | } | |
1469 | ||
1470 | int32_t | |
1471 | TimeZoneFormat::parseOffsetLocalizedGMT(const UnicodeString& text, ParsePosition& pos) const { | |
1472 | return parseOffsetLocalizedGMT(text, pos, FALSE, NULL); | |
1473 | } | |
1474 | ||
1475 | int32_t | |
1476 | TimeZoneFormat::parseOffsetShortLocalizedGMT(const UnicodeString& text, ParsePosition& pos) const { | |
1477 | return parseOffsetLocalizedGMT(text, pos, TRUE, NULL); | |
1478 | } | |
1479 | ||
1480 | // ------------------------------------------------------------------ | |
1481 | // Private zone offset format/parse implementation | |
1482 | ||
1483 | UnicodeString& | |
1484 | TimeZoneFormat::formatOffsetISO8601(int32_t offset, UBool isBasic, UBool useUtcIndicator, | |
1485 | UBool isShort, UBool ignoreSeconds, UnicodeString& result, UErrorCode& status) const { | |
4388f060 A |
1486 | if (U_FAILURE(status)) { |
1487 | result.setToBogus(); | |
1488 | return result; | |
1489 | } | |
51004dcb A |
1490 | int32_t absOffset = offset < 0 ? -offset : offset; |
1491 | if (useUtcIndicator && (absOffset < MILLIS_PER_SECOND || (ignoreSeconds && absOffset < MILLIS_PER_MINUTE))) { | |
1492 | result.setTo(ISO8601_UTC); | |
1493 | return result; | |
1494 | } | |
1495 | ||
1496 | OffsetFields minFields = isShort ? FIELDS_H : FIELDS_HM; | |
1497 | OffsetFields maxFields = ignoreSeconds ? FIELDS_HM : FIELDS_HMS; | |
1498 | UChar sep = isBasic ? 0 : ISO8601_SEP; | |
1499 | ||
1500 | // Note: FIELDS_HMS as maxFields is a CLDR/ICU extension. ISO 8601 specification does | |
1501 | // not support seconds field. | |
1502 | ||
1503 | if (absOffset >= MAX_OFFSET) { | |
4388f060 A |
1504 | result.setToBogus(); |
1505 | status = U_ILLEGAL_ARGUMENT_ERROR; | |
1506 | return result; | |
1507 | } | |
1508 | ||
51004dcb A |
1509 | int fields[3]; |
1510 | fields[0] = absOffset / MILLIS_PER_HOUR; | |
1511 | absOffset = absOffset % MILLIS_PER_HOUR; | |
1512 | fields[1] = absOffset / MILLIS_PER_MINUTE; | |
1513 | absOffset = absOffset % MILLIS_PER_MINUTE; | |
1514 | fields[2] = absOffset / MILLIS_PER_SECOND; | |
1515 | ||
1516 | U_ASSERT(fields[0] >= 0 && fields[0] <= MAX_OFFSET_HOUR); | |
1517 | U_ASSERT(fields[1] >= 0 && fields[1] <= MAX_OFFSET_MINUTE); | |
1518 | U_ASSERT(fields[2] >= 0 && fields[2] <= MAX_OFFSET_SECOND); | |
1519 | ||
1520 | int32_t lastIdx = maxFields; | |
1521 | while (lastIdx > minFields) { | |
1522 | if (fields[lastIdx] != 0) { | |
1523 | break; | |
1524 | } | |
1525 | lastIdx--; | |
1526 | } | |
1527 | ||
1528 | UChar sign = PLUS; | |
1529 | if (offset < 0) { | |
1530 | // if all output fields are 0s, do not use negative sign | |
1531 | for (int32_t idx = 0; idx <= lastIdx; idx++) { | |
1532 | if (fields[idx] != 0) { | |
1533 | sign = MINUS; | |
1534 | break; | |
1535 | } | |
1536 | } | |
1537 | } | |
1538 | result.setTo(sign); | |
1539 | ||
1540 | for (int32_t idx = 0; idx <= lastIdx; idx++) { | |
1541 | if (sep && idx != 0) { | |
1542 | result.append(sep); | |
1543 | } | |
1544 | result.append((UChar)(0x0030 + fields[idx]/10)); | |
1545 | result.append((UChar)(0x0030 + fields[idx]%10)); | |
1546 | } | |
1547 | ||
1548 | return result; | |
4388f060 A |
1549 | } |
1550 | ||
1551 | UnicodeString& | |
51004dcb | 1552 | TimeZoneFormat::formatOffsetLocalizedGMT(int32_t offset, UBool isShort, UnicodeString& result, UErrorCode& status) const { |
4388f060 A |
1553 | if (U_FAILURE(status)) { |
1554 | result.setToBogus(); | |
1555 | return result; | |
1556 | } | |
1557 | if (offset <= -MAX_OFFSET || offset >= MAX_OFFSET) { | |
1558 | result.setToBogus(); | |
1559 | status = U_ILLEGAL_ARGUMENT_ERROR; | |
1560 | return result; | |
1561 | } | |
1562 | ||
1563 | if (offset == 0) { | |
1564 | result.setTo(fGMTZeroFormat); | |
1565 | return result; | |
1566 | } | |
1567 | ||
1568 | UBool positive = TRUE; | |
1569 | if (offset < 0) { | |
1570 | offset = -offset; | |
1571 | positive = FALSE; | |
1572 | } | |
1573 | ||
1574 | int32_t offsetH = offset / MILLIS_PER_HOUR; | |
1575 | offset = offset % MILLIS_PER_HOUR; | |
1576 | int32_t offsetM = offset / MILLIS_PER_MINUTE; | |
1577 | offset = offset % MILLIS_PER_MINUTE; | |
1578 | int32_t offsetS = offset / MILLIS_PER_SECOND; | |
1579 | ||
1580 | U_ASSERT(offsetH <= MAX_OFFSET_HOUR && offsetM <= MAX_OFFSET_MINUTE && offsetS <= MAX_OFFSET_SECOND); | |
1581 | ||
1582 | const UVector* offsetPatternItems = NULL; | |
1583 | if (positive) { | |
51004dcb A |
1584 | if (offsetS != 0) { |
1585 | offsetPatternItems = fGMTOffsetPatternItems[UTZFMT_PAT_POSITIVE_HMS]; | |
1586 | } else if (offsetM != 0 || !isShort) { | |
1587 | offsetPatternItems = fGMTOffsetPatternItems[UTZFMT_PAT_POSITIVE_HM]; | |
1588 | } else { | |
1589 | offsetPatternItems = fGMTOffsetPatternItems[UTZFMT_PAT_POSITIVE_H]; | |
1590 | } | |
4388f060 | 1591 | } else { |
51004dcb A |
1592 | if (offsetS != 0) { |
1593 | offsetPatternItems = fGMTOffsetPatternItems[UTZFMT_PAT_NEGATIVE_HMS]; | |
1594 | } else if (offsetM != 0 || !isShort) { | |
1595 | offsetPatternItems = fGMTOffsetPatternItems[UTZFMT_PAT_NEGATIVE_HM]; | |
1596 | } else { | |
1597 | offsetPatternItems = fGMTOffsetPatternItems[UTZFMT_PAT_NEGATIVE_H]; | |
1598 | } | |
4388f060 A |
1599 | } |
1600 | ||
1601 | U_ASSERT(offsetPatternItems != NULL); | |
1602 | ||
1603 | // Building the GMT format string | |
1604 | result.setTo(fGMTPatternPrefix); | |
1605 | ||
1606 | for (int32_t i = 0; i < offsetPatternItems->size(); i++) { | |
1607 | const GMTOffsetField* item = (GMTOffsetField*)offsetPatternItems->elementAt(i); | |
1608 | GMTOffsetField::FieldType type = item->getType(); | |
1609 | ||
1610 | switch (type) { | |
1611 | case GMTOffsetField::TEXT: | |
1612 | result.append(item->getPatternText(), -1); | |
1613 | break; | |
1614 | ||
1615 | case GMTOffsetField::HOUR: | |
51004dcb | 1616 | appendOffsetDigits(result, offsetH, (isShort ? 1 : 2)); |
4388f060 A |
1617 | break; |
1618 | ||
1619 | case GMTOffsetField::MINUTE: | |
51004dcb | 1620 | appendOffsetDigits(result, offsetM, 2); |
4388f060 A |
1621 | break; |
1622 | ||
1623 | case GMTOffsetField::SECOND: | |
51004dcb | 1624 | appendOffsetDigits(result, offsetS, 2); |
4388f060 A |
1625 | break; |
1626 | } | |
1627 | } | |
1628 | ||
1629 | result.append(fGMTPatternSuffix); | |
1630 | return result; | |
1631 | } | |
1632 | ||
4388f060 A |
1633 | int32_t |
1634 | TimeZoneFormat::parseOffsetISO8601(const UnicodeString& text, ParsePosition& pos, UBool extendedOnly, UBool* hasDigitOffset /* = NULL */) const { | |
1635 | if (hasDigitOffset) { | |
1636 | *hasDigitOffset = FALSE; | |
1637 | } | |
1638 | int32_t start = pos.getIndex(); | |
1639 | if (start >= text.length()) { | |
1640 | pos.setErrorIndex(start); | |
1641 | return 0; | |
1642 | } | |
1643 | ||
1644 | UChar firstChar = text.charAt(start); | |
1645 | if (firstChar == ISO8601_UTC || firstChar == (UChar)(ISO8601_UTC + 0x20)) { | |
1646 | // "Z" (or "z") - indicates UTC | |
1647 | pos.setIndex(start + 1); | |
1648 | return 0; | |
1649 | } | |
1650 | ||
1651 | int32_t sign = 1; | |
1652 | if (firstChar == PLUS) { | |
1653 | sign = 1; | |
1654 | } else if (firstChar == MINUS) { | |
1655 | sign = -1; | |
1656 | } else { | |
1657 | // Not an ISO 8601 offset string | |
1658 | pos.setErrorIndex(start); | |
1659 | return 0; | |
1660 | } | |
1661 | ParsePosition posOffset(start + 1); | |
51004dcb | 1662 | int32_t offset = parseAsciiOffsetFields(text, posOffset, ISO8601_SEP, FIELDS_H, FIELDS_HMS); |
4388f060 A |
1663 | if (posOffset.getErrorIndex() == -1 && !extendedOnly && (posOffset.getIndex() - start <= 3)) { |
1664 | // If the text is successfully parsed as extended format with the options above, it can be also parsed | |
1665 | // as basic format. For example, "0230" can be parsed as offset 2:00 (only first digits are valid for | |
1666 | // extended format), but it can be parsed as offset 2:30 with basic format. We use longer result. | |
1667 | ParsePosition posBasic(start + 1); | |
1668 | int32_t tmpOffset = parseAbuttingAsciiOffsetFields(text, posBasic, FIELDS_H, FIELDS_HMS, FALSE); | |
1669 | if (posBasic.getErrorIndex() == -1 && posBasic.getIndex() > posOffset.getIndex()) { | |
1670 | offset = tmpOffset; | |
1671 | posOffset.setIndex(posBasic.getIndex()); | |
1672 | } | |
1673 | } | |
1674 | ||
1675 | if (posOffset.getErrorIndex() != -1) { | |
1676 | pos.setErrorIndex(start); | |
1677 | return 0; | |
1678 | } | |
1679 | ||
1680 | pos.setIndex(posOffset.getIndex()); | |
1681 | if (hasDigitOffset) { | |
1682 | *hasDigitOffset = TRUE; | |
1683 | } | |
1684 | return sign * offset; | |
1685 | } | |
1686 | ||
1687 | int32_t | |
51004dcb | 1688 | TimeZoneFormat::parseOffsetLocalizedGMT(const UnicodeString& text, ParsePosition& pos, UBool isShort, UBool* hasDigitOffset) const { |
4388f060 | 1689 | int32_t start = pos.getIndex(); |
4388f060 | 1690 | int32_t offset = 0; |
51004dcb | 1691 | int32_t parsedLength = 0; |
4388f060 A |
1692 | |
1693 | if (hasDigitOffset) { | |
1694 | *hasDigitOffset = FALSE; | |
1695 | } | |
1696 | ||
51004dcb | 1697 | offset = parseOffsetLocalizedGMTPattern(text, start, isShort, parsedLength); |
4388f060 | 1698 | |
51004dcb A |
1699 | // For now, parseOffsetLocalizedGMTPattern handles both long and short |
1700 | // formats, no matter isShort is true or false. This might be changed in future | |
1701 | // when strict parsing is necessary, or different set of patterns are used for | |
1702 | // short/long formats. | |
1703 | #if 0 | |
1704 | if (parsedLength == 0) { | |
1705 | offset = parseOffsetLocalizedGMTPattern(text, start, !isShort, parsedLength); | |
1706 | } | |
1707 | #endif | |
4388f060 | 1708 | |
51004dcb | 1709 | if (parsedLength > 0) { |
4388f060 A |
1710 | if (hasDigitOffset) { |
1711 | *hasDigitOffset = TRUE; | |
1712 | } | |
51004dcb | 1713 | pos.setIndex(start + parsedLength); |
4388f060 A |
1714 | return offset; |
1715 | } | |
1716 | ||
1717 | // Try the default patterns | |
4388f060 A |
1718 | offset = parseOffsetDefaultLocalizedGMT(text, start, parsedLength); |
1719 | if (parsedLength > 0) { | |
1720 | if (hasDigitOffset) { | |
1721 | *hasDigitOffset = TRUE; | |
1722 | } | |
1723 | pos.setIndex(start + parsedLength); | |
1724 | return offset; | |
1725 | } | |
1726 | ||
1727 | // Check if this is a GMT zero format | |
1728 | if (text.caseCompare(start, fGMTZeroFormat.length(), fGMTZeroFormat, 0) == 0) { | |
1729 | pos.setIndex(start + fGMTZeroFormat.length()); | |
1730 | return 0; | |
1731 | } | |
1732 | ||
1733 | // Check if this is a default GMT zero format | |
1734 | for (int32_t i = 0; ALT_GMT_STRINGS[i][0] != 0; i++) { | |
1735 | const UChar* defGMTZero = ALT_GMT_STRINGS[i]; | |
1736 | int32_t defGMTZeroLen = u_strlen(defGMTZero); | |
1737 | if (text.caseCompare(start, defGMTZeroLen, defGMTZero, 0) == 0) { | |
1738 | pos.setIndex(start + defGMTZeroLen); | |
1739 | return 0; | |
1740 | } | |
1741 | } | |
1742 | ||
1743 | // Nothing matched | |
1744 | pos.setErrorIndex(start); | |
1745 | return 0; | |
1746 | } | |
1747 | ||
1748 | int32_t | |
51004dcb A |
1749 | TimeZoneFormat::parseOffsetLocalizedGMTPattern(const UnicodeString& text, int32_t start, UBool /*isShort*/, int32_t& parsedLen) const { |
1750 | int32_t idx = start; | |
4388f060 | 1751 | int32_t offset = 0; |
51004dcb A |
1752 | UBool parsed = FALSE; |
1753 | ||
1754 | do { | |
1755 | // Prefix part | |
1756 | int32_t len = fGMTPatternPrefix.length(); | |
1757 | if (len > 0 && text.caseCompare(idx, len, fGMTPatternPrefix, 0) != 0) { | |
1758 | // prefix match failed | |
1759 | break; | |
1760 | } | |
1761 | idx += len; | |
1762 | ||
1763 | // Offset part | |
1764 | offset = parseOffsetFields(text, idx, FALSE, len); | |
1765 | if (len == 0) { | |
1766 | // offset field match failed | |
1767 | break; | |
1768 | } | |
1769 | idx += len; | |
1770 | ||
1771 | len = fGMTPatternSuffix.length(); | |
1772 | if (len > 0 && text.caseCompare(idx, len, fGMTPatternSuffix, 0) != 0) { | |
1773 | // no suffix match | |
1774 | break; | |
1775 | } | |
1776 | idx += len; | |
1777 | parsed = TRUE; | |
1778 | } while (FALSE); | |
1779 | ||
1780 | parsedLen = parsed ? idx - start : 0; | |
1781 | return offset; | |
1782 | } | |
1783 | ||
1784 | int32_t | |
1785 | TimeZoneFormat::parseOffsetFields(const UnicodeString& text, int32_t start, UBool /*isShort*/, int32_t& parsedLen) const { | |
1786 | int32_t outLen = 0; | |
1787 | int32_t offset = 0; | |
1788 | int32_t sign = 1; | |
4388f060 A |
1789 | |
1790 | parsedLen = 0; | |
1791 | ||
51004dcb A |
1792 | int32_t offsetH, offsetM, offsetS; |
1793 | offsetH = offsetM = offsetS = 0; | |
1794 | ||
4388f060 A |
1795 | for (int32_t patidx = 0; PARSE_GMT_OFFSET_TYPES[patidx] >= 0; patidx++) { |
1796 | int32_t gmtPatType = PARSE_GMT_OFFSET_TYPES[patidx]; | |
4388f060 A |
1797 | UVector* items = fGMTOffsetPatternItems[gmtPatType]; |
1798 | U_ASSERT(items != NULL); | |
4388f060 | 1799 | |
51004dcb A |
1800 | outLen = parseOffsetFieldsWithPattern(text, start, items, FALSE, offsetH, offsetM, offsetS); |
1801 | if (outLen > 0) { | |
1802 | sign = (gmtPatType == UTZFMT_PAT_POSITIVE_H || gmtPatType == UTZFMT_PAT_POSITIVE_HM || gmtPatType == UTZFMT_PAT_POSITIVE_HMS) ? | |
1803 | 1 : -1; | |
4388f060 A |
1804 | break; |
1805 | } | |
1806 | } | |
1807 | ||
51004dcb A |
1808 | if (outLen > 0 && fAbuttingOffsetHoursAndMinutes) { |
1809 | // When hours field is sabutting minutes field, | |
1810 | // the parse result above may not be appropriate. | |
1811 | // For example, "01020" is parsed as 01:02: above, | |
1812 | // but it should be parsed as 00:10:20. | |
1813 | int32_t tmpLen = 0; | |
1814 | int32_t tmpSign = 1; | |
1815 | int32_t tmpH, tmpM, tmpS; | |
1816 | ||
1817 | for (int32_t patidx = 0; PARSE_GMT_OFFSET_TYPES[patidx] >= 0; patidx++) { | |
1818 | int32_t gmtPatType = PARSE_GMT_OFFSET_TYPES[patidx]; | |
1819 | UVector* items = fGMTOffsetPatternItems[gmtPatType]; | |
1820 | U_ASSERT(items != NULL); | |
4388f060 | 1821 | |
51004dcb A |
1822 | // forcing parse to use single hour digit |
1823 | tmpLen = parseOffsetFieldsWithPattern(text, start, items, TRUE, tmpH, tmpM, tmpS); | |
1824 | if (tmpLen > 0) { | |
1825 | tmpSign = (gmtPatType == UTZFMT_PAT_POSITIVE_H || gmtPatType == UTZFMT_PAT_POSITIVE_HM || gmtPatType == UTZFMT_PAT_POSITIVE_HMS) ? | |
1826 | 1 : -1; | |
1827 | break; | |
1828 | } | |
1829 | } | |
1830 | if (tmpLen > outLen) { | |
1831 | // Better parse result with single hour digit | |
1832 | outLen = tmpLen; | |
1833 | sign = tmpSign; | |
1834 | offsetH = tmpH; | |
1835 | offsetM = tmpM; | |
1836 | offsetS = tmpS; | |
1837 | } | |
1838 | } | |
1839 | ||
1840 | if (outLen > 0) { | |
1841 | offset = ((((offsetH * 60) + offsetM) * 60) + offsetS) * 1000 * sign; | |
1842 | parsedLen = outLen; | |
4388f060 A |
1843 | } |
1844 | ||
1845 | return offset; | |
1846 | } | |
1847 | ||
51004dcb A |
1848 | int32_t |
1849 | TimeZoneFormat::parseOffsetFieldsWithPattern(const UnicodeString& text, int32_t start, | |
1850 | UVector* patternItems, UBool forceSingleHourDigit, int32_t& hour, int32_t& min, int32_t& sec) const { | |
1851 | UBool failed = FALSE; | |
1852 | int32_t offsetH, offsetM, offsetS; | |
1853 | offsetH = offsetM = offsetS = 0; | |
1854 | int32_t idx = start; | |
1855 | ||
1856 | for (int32_t i = 0; i < patternItems->size(); i++) { | |
57a6839d | 1857 | int32_t len = 0; |
51004dcb A |
1858 | const GMTOffsetField* field = (const GMTOffsetField*)patternItems->elementAt(i); |
1859 | GMTOffsetField::FieldType fieldType = field->getType(); | |
1860 | if (fieldType == GMTOffsetField::TEXT) { | |
1861 | const UChar* patStr = field->getPatternText(); | |
1862 | len = u_strlen(patStr); | |
1863 | if (text.caseCompare(idx, len, patStr, 0) != 0) { | |
1864 | failed = TRUE; | |
1865 | break; | |
1866 | } | |
1867 | idx += len; | |
1868 | } else { | |
1869 | if (fieldType == GMTOffsetField::HOUR) { | |
1870 | uint8_t maxDigits = forceSingleHourDigit ? 1 : 2; | |
1871 | offsetH = parseOffsetFieldWithLocalizedDigits(text, idx, 1, maxDigits, 0, MAX_OFFSET_HOUR, len); | |
1872 | } else if (fieldType == GMTOffsetField::MINUTE) { | |
1873 | offsetM = parseOffsetFieldWithLocalizedDigits(text, idx, 2, 2, 0, MAX_OFFSET_MINUTE, len); | |
1874 | } else if (fieldType == GMTOffsetField::SECOND) { | |
1875 | offsetS = parseOffsetFieldWithLocalizedDigits(text, idx, 2, 2, 0, MAX_OFFSET_SECOND, len); | |
1876 | } | |
1877 | ||
1878 | if (len == 0) { | |
1879 | failed = TRUE; | |
1880 | break; | |
1881 | } | |
1882 | idx += len; | |
1883 | } | |
1884 | } | |
1885 | ||
1886 | if (failed) { | |
1887 | hour = min = sec = 0; | |
1888 | return 0; | |
1889 | } | |
1890 | ||
1891 | hour = offsetH; | |
1892 | min = offsetM; | |
1893 | sec = offsetS; | |
1894 | ||
1895 | return idx - start; | |
1896 | } | |
1897 | ||
4388f060 A |
1898 | int32_t |
1899 | TimeZoneFormat::parseAbuttingOffsetFields(const UnicodeString& text, int32_t start, int32_t& parsedLen) const { | |
1900 | int32_t digits[MAX_OFFSET_DIGITS]; | |
1901 | int32_t parsed[MAX_OFFSET_DIGITS]; // accumulative offsets | |
1902 | ||
1903 | // Parse digits into int[] | |
1904 | int32_t idx = start; | |
1905 | int32_t len = 0; | |
1906 | int32_t numDigits = 0; | |
1907 | for (int32_t i = 0; i < MAX_OFFSET_DIGITS; i++) { | |
1908 | digits[i] = parseSingleLocalizedDigit(text, idx, len); | |
1909 | if (digits[i] < 0) { | |
1910 | break; | |
1911 | } | |
1912 | idx += len; | |
1913 | parsed[i] = idx - start; | |
1914 | numDigits++; | |
1915 | } | |
1916 | ||
1917 | if (numDigits == 0) { | |
1918 | parsedLen = 0; | |
1919 | return 0; | |
1920 | } | |
1921 | ||
1922 | int32_t offset = 0; | |
1923 | while (numDigits > 0) { | |
1924 | int32_t hour = 0; | |
1925 | int32_t min = 0; | |
1926 | int32_t sec = 0; | |
1927 | ||
1928 | U_ASSERT(numDigits > 0 && numDigits <= MAX_OFFSET_DIGITS); | |
1929 | switch (numDigits) { | |
1930 | case 1: // H | |
1931 | hour = digits[0]; | |
1932 | break; | |
1933 | case 2: // HH | |
1934 | hour = digits[0] * 10 + digits[1]; | |
1935 | break; | |
1936 | case 3: // Hmm | |
1937 | hour = digits[0]; | |
1938 | min = digits[1] * 10 + digits[2]; | |
1939 | break; | |
1940 | case 4: // HHmm | |
1941 | hour = digits[0] * 10 + digits[1]; | |
1942 | min = digits[2] * 10 + digits[3]; | |
1943 | break; | |
1944 | case 5: // Hmmss | |
1945 | hour = digits[0]; | |
1946 | min = digits[1] * 10 + digits[2]; | |
1947 | sec = digits[3] * 10 + digits[4]; | |
1948 | break; | |
1949 | case 6: // HHmmss | |
1950 | hour = digits[0] * 10 + digits[1]; | |
1951 | min = digits[2] * 10 + digits[3]; | |
1952 | sec = digits[4] * 10 + digits[5]; | |
1953 | break; | |
1954 | } | |
1955 | if (hour <= MAX_OFFSET_HOUR && min <= MAX_OFFSET_MINUTE && sec <= MAX_OFFSET_SECOND) { | |
1956 | // found a valid combination | |
1957 | offset = hour * MILLIS_PER_HOUR + min * MILLIS_PER_MINUTE + sec * MILLIS_PER_SECOND; | |
1958 | parsedLen = parsed[numDigits - 1]; | |
1959 | break; | |
1960 | } | |
1961 | numDigits--; | |
1962 | } | |
1963 | return offset; | |
1964 | } | |
1965 | ||
1966 | int32_t | |
1967 | TimeZoneFormat::parseOffsetDefaultLocalizedGMT(const UnicodeString& text, int start, int32_t& parsedLen) const { | |
1968 | int32_t idx = start; | |
1969 | int32_t offset = 0; | |
1970 | int32_t parsed = 0; | |
1971 | ||
1972 | do { | |
1973 | // check global default GMT alternatives | |
1974 | int32_t gmtLen = 0; | |
1975 | ||
1976 | for (int32_t i = 0; ALT_GMT_STRINGS[i][0] != 0; i++) { | |
1977 | const UChar* gmt = ALT_GMT_STRINGS[i]; | |
1978 | int32_t len = u_strlen(gmt); | |
1979 | if (text.caseCompare(start, len, gmt, 0) == 0) { | |
1980 | gmtLen = len; | |
1981 | break; | |
1982 | } | |
1983 | } | |
1984 | if (gmtLen == 0) { | |
1985 | break; | |
1986 | } | |
1987 | idx += gmtLen; | |
1988 | ||
1989 | // offset needs a sign char and a digit at minimum | |
1990 | if (idx + 1 >= text.length()) { | |
1991 | break; | |
1992 | } | |
1993 | ||
1994 | // parse sign | |
1995 | int32_t sign = 1; | |
1996 | UChar c = text.charAt(idx); | |
1997 | if (c == PLUS) { | |
1998 | sign = 1; | |
1999 | } else if (c == MINUS) { | |
2000 | sign = -1; | |
2001 | } else { | |
2002 | break; | |
2003 | } | |
2004 | idx++; | |
2005 | ||
2006 | // offset part | |
2007 | // try the default pattern with the separator first | |
2008 | int32_t lenWithSep = 0; | |
2009 | int32_t offsetWithSep = parseDefaultOffsetFields(text, idx, DEFAULT_GMT_OFFSET_SEP, lenWithSep); | |
2010 | if (lenWithSep == text.length() - idx) { | |
2011 | // maximum match | |
2012 | offset = offsetWithSep * sign; | |
2013 | idx += lenWithSep; | |
2014 | } else { | |
2015 | // try abutting field pattern | |
2016 | int32_t lenAbut = 0; | |
2017 | int32_t offsetAbut = parseAbuttingOffsetFields(text, idx, lenAbut); | |
2018 | ||
2019 | if (lenWithSep > lenAbut) { | |
2020 | offset = offsetWithSep * sign; | |
2021 | idx += lenWithSep; | |
2022 | } else { | |
2023 | offset = offsetAbut * sign; | |
2024 | idx += lenAbut; | |
2025 | } | |
2026 | } | |
2027 | parsed = idx - start; | |
2028 | } while (false); | |
2029 | ||
2030 | parsedLen = parsed; | |
2031 | return offset; | |
2032 | } | |
2033 | ||
2034 | int32_t | |
2035 | TimeZoneFormat::parseDefaultOffsetFields(const UnicodeString& text, int32_t start, UChar separator, int32_t& parsedLen) const { | |
2036 | int32_t max = text.length(); | |
2037 | int32_t idx = start; | |
2038 | int32_t len = 0; | |
2039 | int32_t hour = 0, min = 0, sec = 0; | |
2040 | ||
2041 | parsedLen = 0; | |
2042 | ||
2043 | do { | |
2044 | hour = parseOffsetFieldWithLocalizedDigits(text, idx, 1, 2, 0, MAX_OFFSET_HOUR, len); | |
2045 | if (len == 0) { | |
2046 | break; | |
2047 | } | |
2048 | idx += len; | |
2049 | ||
2050 | if (idx + 1 < max && text.charAt(idx) == separator) { | |
2051 | min = parseOffsetFieldWithLocalizedDigits(text, idx + 1, 2, 2, 0, MAX_OFFSET_MINUTE, len); | |
2052 | if (len == 0) { | |
2053 | break; | |
2054 | } | |
2055 | idx += (1 + len); | |
2056 | ||
2057 | if (idx + 1 < max && text.charAt(idx) == separator) { | |
2058 | sec = parseOffsetFieldWithLocalizedDigits(text, idx + 1, 2, 2, 0, MAX_OFFSET_SECOND, len); | |
2059 | if (len == 0) { | |
2060 | break; | |
2061 | } | |
2062 | idx += (1 + len); | |
2063 | } | |
2064 | } | |
2065 | } while (FALSE); | |
2066 | ||
2067 | if (idx == start) { | |
2068 | return 0; | |
2069 | } | |
2070 | ||
2071 | parsedLen = idx - start; | |
2072 | return hour * MILLIS_PER_HOUR + min * MILLIS_PER_MINUTE + sec * MILLIS_PER_SECOND; | |
2073 | } | |
2074 | ||
2075 | int32_t | |
2076 | TimeZoneFormat::parseOffsetFieldWithLocalizedDigits(const UnicodeString& text, int32_t start, uint8_t minDigits, uint8_t maxDigits, uint16_t minVal, uint16_t maxVal, int32_t& parsedLen) const { | |
2077 | parsedLen = 0; | |
2078 | ||
2079 | int32_t decVal = 0; | |
2080 | int32_t numDigits = 0; | |
2081 | int32_t idx = start; | |
2082 | int32_t digitLen = 0; | |
2083 | ||
2084 | while (idx < text.length() && numDigits < maxDigits) { | |
2085 | int32_t digit = parseSingleLocalizedDigit(text, idx, digitLen); | |
2086 | if (digit < 0) { | |
2087 | break; | |
2088 | } | |
2089 | int32_t tmpVal = decVal * 10 + digit; | |
2090 | if (tmpVal > maxVal) { | |
2091 | break; | |
2092 | } | |
2093 | decVal = tmpVal; | |
2094 | numDigits++; | |
2095 | idx += digitLen; | |
2096 | } | |
2097 | ||
2098 | // Note: maxVal is checked in the while loop | |
2099 | if (numDigits < minDigits || decVal < minVal) { | |
2100 | decVal = -1; | |
2101 | numDigits = 0; | |
2102 | } else { | |
2103 | parsedLen = idx - start; | |
2104 | } | |
2105 | ||
2106 | return decVal; | |
2107 | } | |
2108 | ||
2109 | int32_t | |
2110 | TimeZoneFormat::parseSingleLocalizedDigit(const UnicodeString& text, int32_t start, int32_t& len) const { | |
2111 | int32_t digit = -1; | |
2112 | len = 0; | |
2113 | if (start < text.length()) { | |
2114 | UChar32 cp = text.char32At(start); | |
2115 | ||
2116 | // First, try digits configured for this instance | |
2117 | for (int32_t i = 0; i < 10; i++) { | |
2118 | if (cp == fGMTOffsetDigits[i]) { | |
2119 | digit = i; | |
2120 | break; | |
2121 | } | |
2122 | } | |
2123 | // If failed, check if this is a Unicode digit | |
2124 | if (digit < 0) { | |
2125 | int32_t tmp = u_charDigitValue(cp); | |
2126 | digit = (tmp >= 0 && tmp <= 9) ? tmp : -1; | |
2127 | } | |
2128 | ||
2129 | if (digit >= 0) { | |
2130 | int32_t next = text.moveIndex32(start, 1); | |
2131 | len = next - start; | |
2132 | } | |
2133 | } | |
2134 | return digit; | |
2135 | } | |
2136 | ||
2137 | UnicodeString& | |
2138 | TimeZoneFormat::formatOffsetWithAsciiDigits(int32_t offset, UChar sep, OffsetFields minFields, OffsetFields maxFields, UnicodeString& result) { | |
2139 | U_ASSERT(maxFields >= minFields); | |
2140 | U_ASSERT(offset > -MAX_OFFSET && offset < MAX_OFFSET); | |
2141 | ||
2142 | UChar sign = PLUS; | |
2143 | if (offset < 0) { | |
2144 | sign = MINUS; | |
2145 | offset = -offset; | |
2146 | } | |
2147 | result.setTo(sign); | |
2148 | ||
2149 | int fields[3]; | |
2150 | fields[0] = offset / MILLIS_PER_HOUR; | |
2151 | offset = offset % MILLIS_PER_HOUR; | |
2152 | fields[1] = offset / MILLIS_PER_MINUTE; | |
2153 | offset = offset % MILLIS_PER_MINUTE; | |
2154 | fields[2] = offset / MILLIS_PER_SECOND; | |
2155 | ||
2156 | U_ASSERT(fields[0] >= 0 && fields[0] <= MAX_OFFSET_HOUR); | |
2157 | U_ASSERT(fields[1] >= 0 && fields[1] <= MAX_OFFSET_MINUTE); | |
2158 | U_ASSERT(fields[2] >= 0 && fields[2] <= MAX_OFFSET_SECOND); | |
2159 | ||
2160 | int32_t lastIdx = maxFields; | |
2161 | while (lastIdx > minFields) { | |
2162 | if (fields[lastIdx] != 0) { | |
2163 | break; | |
2164 | } | |
2165 | lastIdx--; | |
2166 | } | |
2167 | ||
2168 | for (int32_t idx = 0; idx <= lastIdx; idx++) { | |
2169 | if (sep && idx != 0) { | |
2170 | result.append(sep); | |
2171 | } | |
2172 | result.append((UChar)(0x0030 + fields[idx]/10)); | |
2173 | result.append((UChar)(0x0030 + fields[idx]%10)); | |
2174 | } | |
2175 | ||
2176 | return result; | |
2177 | } | |
2178 | ||
2179 | int32_t | |
2180 | TimeZoneFormat::parseAbuttingAsciiOffsetFields(const UnicodeString& text, ParsePosition& pos, OffsetFields minFields, OffsetFields maxFields, UBool fixedHourWidth) { | |
2181 | int32_t start = pos.getIndex(); | |
2182 | ||
2183 | int32_t minDigits = 2 * (minFields + 1) - (fixedHourWidth ? 0 : 1); | |
2184 | int32_t maxDigits = 2 * (maxFields + 1); | |
2185 | ||
2186 | U_ASSERT(maxDigits <= MAX_OFFSET_DIGITS); | |
2187 | ||
51004dcb | 2188 | int32_t digits[MAX_OFFSET_DIGITS] = {}; |
4388f060 A |
2189 | int32_t numDigits = 0; |
2190 | int32_t idx = start; | |
2191 | while (numDigits < maxDigits && idx < text.length()) { | |
2192 | UChar uch = text.charAt(idx); | |
2193 | int32_t digit = DIGIT_VAL(uch); | |
2194 | if (digit < 0) { | |
2195 | break; | |
2196 | } | |
2197 | digits[numDigits] = digit; | |
2198 | numDigits++; | |
2199 | idx++; | |
2200 | } | |
2201 | ||
2202 | if (fixedHourWidth && (numDigits & 1)) { | |
2203 | // Fixed digits, so the number of digits must be even number. Truncating. | |
2204 | numDigits--; | |
2205 | } | |
2206 | ||
2207 | if (numDigits < minDigits) { | |
2208 | pos.setErrorIndex(start); | |
2209 | return 0; | |
2210 | } | |
2211 | ||
2212 | int32_t hour = 0, min = 0, sec = 0; | |
2213 | UBool bParsed = FALSE; | |
2214 | while (numDigits >= minDigits) { | |
2215 | switch (numDigits) { | |
2216 | case 1: //H | |
2217 | hour = digits[0]; | |
2218 | break; | |
2219 | case 2: //HH | |
2220 | hour = digits[0] * 10 + digits[1]; | |
2221 | break; | |
2222 | case 3: //Hmm | |
2223 | hour = digits[0]; | |
2224 | min = digits[1] * 10 + digits[2]; | |
2225 | break; | |
2226 | case 4: //HHmm | |
2227 | hour = digits[0] * 10 + digits[1]; | |
2228 | min = digits[2] * 10 + digits[3]; | |
2229 | break; | |
2230 | case 5: //Hmmss | |
2231 | hour = digits[0]; | |
2232 | min = digits[1] * 10 + digits[2]; | |
2233 | sec = digits[3] * 10 + digits[4]; | |
2234 | break; | |
2235 | case 6: //HHmmss | |
2236 | hour = digits[0] * 10 + digits[1]; | |
2237 | min = digits[2] * 10 + digits[3]; | |
2238 | sec = digits[4] * 10 + digits[5]; | |
2239 | break; | |
2240 | } | |
2241 | ||
2242 | if (hour <= MAX_OFFSET_HOUR && min <= MAX_OFFSET_MINUTE && sec <= MAX_OFFSET_SECOND) { | |
2243 | // Successfully parsed | |
2244 | bParsed = true; | |
2245 | break; | |
2246 | } | |
2247 | ||
2248 | // Truncating | |
2249 | numDigits -= (fixedHourWidth ? 2 : 1); | |
2250 | hour = min = sec = 0; | |
2251 | } | |
2252 | ||
2253 | if (!bParsed) { | |
2254 | pos.setErrorIndex(start); | |
2255 | return 0; | |
2256 | } | |
2257 | pos.setIndex(start + numDigits); | |
2258 | return ((((hour * 60) + min) * 60) + sec) * 1000; | |
2259 | } | |
2260 | ||
2261 | int32_t | |
51004dcb | 2262 | TimeZoneFormat::parseAsciiOffsetFields(const UnicodeString& text, ParsePosition& pos, UChar sep, OffsetFields minFields, OffsetFields maxFields) { |
4388f060 A |
2263 | int32_t start = pos.getIndex(); |
2264 | int32_t fieldVal[] = {0, 0, 0}; | |
2265 | int32_t fieldLen[] = {0, -1, -1}; | |
2266 | for (int32_t idx = start, fieldIdx = 0; idx < text.length() && fieldIdx <= maxFields; idx++) { | |
2267 | UChar c = text.charAt(idx); | |
2268 | if (c == sep) { | |
51004dcb A |
2269 | if (fieldIdx == 0) { |
2270 | if (fieldLen[0] == 0) { | |
2271 | // no hours field | |
2272 | break; | |
2273 | } | |
4388f060 | 2274 | // 1 digit hour, move to next field |
4388f060 | 2275 | } else { |
51004dcb A |
2276 | if (fieldLen[fieldIdx] != -1) { |
2277 | // premature minute or seconds field | |
2278 | break; | |
2279 | } | |
2280 | fieldLen[fieldIdx] = 0; | |
4388f060 A |
2281 | } |
2282 | continue; | |
51004dcb A |
2283 | } else if (fieldLen[fieldIdx] == -1) { |
2284 | // no separator after 2 digit field | |
2285 | break; | |
4388f060 A |
2286 | } |
2287 | int32_t digit = DIGIT_VAL(c); | |
2288 | if (digit < 0) { | |
2289 | // not a digit | |
2290 | break; | |
2291 | } | |
2292 | fieldVal[fieldIdx] = fieldVal[fieldIdx] * 10 + digit; | |
2293 | fieldLen[fieldIdx]++; | |
2294 | if (fieldLen[fieldIdx] >= 2) { | |
2295 | // parsed 2 digits, move to next field | |
2296 | fieldIdx++; | |
2297 | } | |
2298 | } | |
2299 | ||
2300 | int32_t offset = 0; | |
2301 | int32_t parsedLen = 0; | |
2302 | int32_t parsedFields = -1; | |
2303 | do { | |
2304 | // hour | |
51004dcb | 2305 | if (fieldLen[0] == 0) { |
4388f060 A |
2306 | break; |
2307 | } | |
2308 | if (fieldVal[0] > MAX_OFFSET_HOUR) { | |
4388f060 A |
2309 | offset = (fieldVal[0] / 10) * MILLIS_PER_HOUR; |
2310 | parsedFields = FIELDS_H; | |
2311 | parsedLen = 1; | |
2312 | break; | |
2313 | } | |
2314 | offset = fieldVal[0] * MILLIS_PER_HOUR; | |
2315 | parsedLen = fieldLen[0]; | |
2316 | parsedFields = FIELDS_H; | |
2317 | ||
2318 | // minute | |
2319 | if (fieldLen[1] != 2 || fieldVal[1] > MAX_OFFSET_MINUTE) { | |
2320 | break; | |
2321 | } | |
2322 | offset += fieldVal[1] * MILLIS_PER_MINUTE; | |
2323 | parsedLen += (1 + fieldLen[1]); | |
2324 | parsedFields = FIELDS_HM; | |
2325 | ||
2326 | // second | |
2327 | if (fieldLen[2] != 2 || fieldVal[2] > MAX_OFFSET_SECOND) { | |
2328 | break; | |
2329 | } | |
2330 | offset += fieldVal[2] * MILLIS_PER_SECOND; | |
2331 | parsedLen += (1 + fieldLen[2]); | |
2332 | parsedFields = FIELDS_HMS; | |
2333 | } while (false); | |
2334 | ||
2335 | if (parsedFields < minFields) { | |
2336 | pos.setErrorIndex(start); | |
2337 | return 0; | |
2338 | } | |
2339 | ||
2340 | pos.setIndex(start + parsedLen); | |
2341 | return offset; | |
2342 | } | |
2343 | ||
2344 | void | |
2345 | TimeZoneFormat::appendOffsetDigits(UnicodeString& buf, int32_t n, uint8_t minDigits) const { | |
2346 | U_ASSERT(n >= 0 && n < 60); | |
2347 | int32_t numDigits = n >= 10 ? 2 : 1; | |
2348 | for (int32_t i = 0; i < minDigits - numDigits; i++) { | |
2349 | buf.append(fGMTOffsetDigits[0]); | |
2350 | } | |
2351 | if (numDigits == 2) { | |
2352 | buf.append(fGMTOffsetDigits[n / 10]); | |
2353 | } | |
2354 | buf.append(fGMTOffsetDigits[n % 10]); | |
2355 | } | |
2356 | ||
2357 | // ------------------------------------------------------------------ | |
2358 | // Private misc | |
2359 | void | |
2360 | TimeZoneFormat::initGMTPattern(const UnicodeString& gmtPattern, UErrorCode& status) { | |
2361 | if (U_FAILURE(status)) { | |
2362 | return; | |
2363 | } | |
2364 | // This implementation not perfect, but sufficient practically. | |
2365 | int32_t idx = gmtPattern.indexOf(ARG0, ARG0_LEN, 0); | |
2366 | if (idx < 0) { | |
2367 | status = U_ILLEGAL_ARGUMENT_ERROR; | |
2368 | return; | |
2369 | } | |
2370 | fGMTPattern.setTo(gmtPattern); | |
2371 | unquote(gmtPattern.tempSubString(0, idx), fGMTPatternPrefix); | |
2372 | unquote(gmtPattern.tempSubString(idx + ARG0_LEN), fGMTPatternSuffix); | |
2373 | } | |
2374 | ||
2375 | UnicodeString& | |
2376 | TimeZoneFormat::unquote(const UnicodeString& pattern, UnicodeString& result) { | |
2377 | if (pattern.indexOf(SINGLEQUOTE) < 0) { | |
2378 | result.setTo(pattern); | |
2379 | return result; | |
2380 | } | |
2381 | result.remove(); | |
2382 | UBool isPrevQuote = FALSE; | |
2383 | UBool inQuote = FALSE; | |
2384 | for (int32_t i = 0; i < pattern.length(); i++) { | |
2385 | UChar c = pattern.charAt(i); | |
2386 | if (c == SINGLEQUOTE) { | |
2387 | if (isPrevQuote) { | |
2388 | result.append(c); | |
2389 | isPrevQuote = FALSE; | |
2390 | } else { | |
2391 | isPrevQuote = TRUE; | |
2392 | } | |
2393 | inQuote = !inQuote; | |
2394 | } else { | |
2395 | isPrevQuote = FALSE; | |
2396 | result.append(c); | |
2397 | } | |
2398 | } | |
2399 | return result; | |
2400 | } | |
2401 | ||
2402 | UVector* | |
2403 | TimeZoneFormat::parseOffsetPattern(const UnicodeString& pattern, OffsetFields required, UErrorCode& status) { | |
2404 | if (U_FAILURE(status)) { | |
2405 | return NULL; | |
2406 | } | |
2407 | UVector* result = new UVector(deleteGMTOffsetField, NULL, status); | |
2408 | if (result == NULL) { | |
2409 | status = U_MEMORY_ALLOCATION_ERROR; | |
2410 | return NULL; | |
2411 | } | |
2412 | ||
2413 | int32_t checkBits = 0; | |
2414 | UBool isPrevQuote = FALSE; | |
2415 | UBool inQuote = FALSE; | |
b331163b A |
2416 | UChar textBuf[32]; |
2417 | UnicodeString text(textBuf, 0, UPRV_LENGTHOF(textBuf)); | |
4388f060 A |
2418 | GMTOffsetField::FieldType itemType = GMTOffsetField::TEXT; |
2419 | int32_t itemLength = 1; | |
2420 | ||
2421 | for (int32_t i = 0; i < pattern.length(); i++) { | |
2422 | UChar ch = pattern.charAt(i); | |
2423 | if (ch == SINGLEQUOTE) { | |
2424 | if (isPrevQuote) { | |
2425 | text.append(SINGLEQUOTE); | |
2426 | isPrevQuote = FALSE; | |
2427 | } else { | |
2428 | isPrevQuote = TRUE; | |
2429 | if (itemType != GMTOffsetField::TEXT) { | |
2430 | if (GMTOffsetField::isValid(itemType, itemLength)) { | |
2431 | GMTOffsetField* fld = GMTOffsetField::createTimeField(itemType, (uint8_t)itemLength, status); | |
2432 | result->addElement(fld, status); | |
2433 | if (U_FAILURE(status)) { | |
2434 | break; | |
2435 | } | |
2436 | } else { | |
2437 | status = U_ILLEGAL_ARGUMENT_ERROR; | |
2438 | break; | |
2439 | } | |
2440 | itemType = GMTOffsetField::TEXT; | |
2441 | } | |
2442 | } | |
2443 | inQuote = !inQuote; | |
2444 | } else { | |
2445 | isPrevQuote = FALSE; | |
2446 | if (inQuote) { | |
2447 | text.append(ch); | |
2448 | } else { | |
2449 | GMTOffsetField::FieldType tmpType = GMTOffsetField::getTypeByLetter(ch); | |
2450 | if (tmpType != GMTOffsetField::TEXT) { | |
2451 | // an offset time pattern character | |
2452 | if (tmpType == itemType) { | |
2453 | itemLength++; | |
2454 | } else { | |
2455 | if (itemType == GMTOffsetField::TEXT) { | |
2456 | if (text.length() > 0) { | |
2457 | GMTOffsetField* textfld = GMTOffsetField::createText(text, status); | |
2458 | result->addElement(textfld, status); | |
2459 | if (U_FAILURE(status)) { | |
2460 | break; | |
2461 | } | |
2462 | text.remove(); | |
2463 | } | |
2464 | } else { | |
2465 | if (GMTOffsetField::isValid(itemType, itemLength)) { | |
2466 | GMTOffsetField* fld = GMTOffsetField::createTimeField(itemType, itemLength, status); | |
2467 | result->addElement(fld, status); | |
2468 | if (U_FAILURE(status)) { | |
2469 | break; | |
2470 | } | |
2471 | } else { | |
2472 | status = U_ILLEGAL_ARGUMENT_ERROR; | |
2473 | break; | |
2474 | } | |
2475 | } | |
2476 | itemType = tmpType; | |
2477 | itemLength = 1; | |
2478 | checkBits |= tmpType; | |
2479 | } | |
2480 | } else { | |
2481 | // a string literal | |
2482 | if (itemType != GMTOffsetField::TEXT) { | |
2483 | if (GMTOffsetField::isValid(itemType, itemLength)) { | |
2484 | GMTOffsetField* fld = GMTOffsetField::createTimeField(itemType, itemLength, status); | |
2485 | result->addElement(fld, status); | |
2486 | if (U_FAILURE(status)) { | |
2487 | break; | |
2488 | } | |
2489 | } else { | |
2490 | status = U_ILLEGAL_ARGUMENT_ERROR; | |
2491 | break; | |
2492 | } | |
2493 | itemType = GMTOffsetField::TEXT; | |
2494 | } | |
2495 | text.append(ch); | |
2496 | } | |
2497 | } | |
2498 | } | |
2499 | } | |
2500 | // handle last item | |
2501 | if (U_SUCCESS(status)) { | |
2502 | if (itemType == GMTOffsetField::TEXT) { | |
2503 | if (text.length() > 0) { | |
2504 | GMTOffsetField* tfld = GMTOffsetField::createText(text, status); | |
2505 | result->addElement(tfld, status); | |
2506 | } | |
2507 | } else { | |
2508 | if (GMTOffsetField::isValid(itemType, itemLength)) { | |
2509 | GMTOffsetField* fld = GMTOffsetField::createTimeField(itemType, itemLength, status); | |
2510 | result->addElement(fld, status); | |
2511 | } else { | |
2512 | status = U_ILLEGAL_ARGUMENT_ERROR; | |
2513 | } | |
2514 | } | |
2515 | ||
2516 | // Check all required fields are set | |
2517 | if (U_SUCCESS(status)) { | |
2518 | int32_t reqBits = 0; | |
2519 | switch (required) { | |
2520 | case FIELDS_H: | |
2521 | reqBits = GMTOffsetField::HOUR; | |
2522 | break; | |
2523 | case FIELDS_HM: | |
2524 | reqBits = GMTOffsetField::HOUR | GMTOffsetField::MINUTE; | |
2525 | break; | |
2526 | case FIELDS_HMS: | |
2527 | reqBits = GMTOffsetField::HOUR | GMTOffsetField::MINUTE | GMTOffsetField::SECOND; | |
2528 | break; | |
2529 | } | |
2530 | if (checkBits == reqBits) { | |
2531 | // all required fields are set, no extra fields | |
2532 | return result; | |
2533 | } | |
2534 | } | |
2535 | } | |
2536 | ||
2537 | // error | |
2538 | delete result; | |
2539 | return NULL; | |
2540 | } | |
2541 | ||
2542 | UnicodeString& | |
51004dcb A |
2543 | TimeZoneFormat::expandOffsetPattern(const UnicodeString& offsetHM, UnicodeString& result, UErrorCode& status) { |
2544 | result.setToBogus(); | |
2545 | if (U_FAILURE(status)) { | |
2546 | return result; | |
2547 | } | |
4388f060 A |
2548 | U_ASSERT(u_strlen(DEFAULT_GMT_OFFSET_MINUTE_PATTERN) == 2); |
2549 | ||
2550 | int32_t idx_mm = offsetHM.indexOf(DEFAULT_GMT_OFFSET_MINUTE_PATTERN, 2, 0); | |
2551 | if (idx_mm < 0) { | |
51004dcb A |
2552 | // Bad time zone hour pattern data |
2553 | status = U_ILLEGAL_ARGUMENT_ERROR; | |
4388f060 A |
2554 | return result; |
2555 | } | |
2556 | ||
2557 | UnicodeString sep; | |
51004dcb | 2558 | int32_t idx_H = offsetHM.tempSubString(0, idx_mm).lastIndexOf((UChar)0x0048 /* H */); |
4388f060 A |
2559 | if (idx_H >= 0) { |
2560 | sep = offsetHM.tempSubString(idx_H + 1, idx_mm - (idx_H + 1)); | |
2561 | } | |
2562 | result.setTo(offsetHM.tempSubString(0, idx_mm + 2)); | |
2563 | result.append(sep); | |
2564 | result.append(DEFAULT_GMT_OFFSET_SECOND_PATTERN, -1); | |
2565 | result.append(offsetHM.tempSubString(idx_mm + 2)); | |
2566 | return result; | |
2567 | } | |
2568 | ||
51004dcb A |
2569 | UnicodeString& |
2570 | TimeZoneFormat::truncateOffsetPattern(const UnicodeString& offsetHM, UnicodeString& result, UErrorCode& status) { | |
2571 | result.setToBogus(); | |
2572 | if (U_FAILURE(status)) { | |
2573 | return result; | |
2574 | } | |
2575 | U_ASSERT(u_strlen(DEFAULT_GMT_OFFSET_MINUTE_PATTERN) == 2); | |
2576 | ||
2577 | int32_t idx_mm = offsetHM.indexOf(DEFAULT_GMT_OFFSET_MINUTE_PATTERN, 2, 0); | |
2578 | if (idx_mm < 0) { | |
2579 | // Bad time zone hour pattern data | |
2580 | status = U_ILLEGAL_ARGUMENT_ERROR; | |
2581 | return result; | |
2582 | } | |
2583 | UChar HH[] = {0x0048, 0x0048}; | |
2584 | int32_t idx_HH = offsetHM.tempSubString(0, idx_mm).lastIndexOf(HH, 2, 0); | |
2585 | if (idx_HH >= 0) { | |
2586 | return result.setTo(offsetHM.tempSubString(0, idx_HH + 2)); | |
2587 | } | |
2588 | int32_t idx_H = offsetHM.tempSubString(0, idx_mm).lastIndexOf((UChar)0x0048, 0); | |
2589 | if (idx_H >= 0) { | |
2590 | return result.setTo(offsetHM.tempSubString(0, idx_H + 1)); | |
2591 | } | |
2592 | // Bad time zone hour pattern data | |
2593 | status = U_ILLEGAL_ARGUMENT_ERROR; | |
2594 | return result; | |
2595 | } | |
2596 | ||
4388f060 A |
2597 | void |
2598 | TimeZoneFormat::initGMTOffsetPatterns(UErrorCode& status) { | |
51004dcb | 2599 | for (int32_t type = 0; type < UTZFMT_PAT_COUNT; type++) { |
4388f060 | 2600 | switch (type) { |
51004dcb A |
2601 | case UTZFMT_PAT_POSITIVE_H: |
2602 | case UTZFMT_PAT_NEGATIVE_H: | |
2603 | fGMTOffsetPatternItems[type] = parseOffsetPattern(fGMTOffsetPatterns[type], FIELDS_H, status); | |
2604 | break; | |
4388f060 A |
2605 | case UTZFMT_PAT_POSITIVE_HM: |
2606 | case UTZFMT_PAT_NEGATIVE_HM: | |
2607 | fGMTOffsetPatternItems[type] = parseOffsetPattern(fGMTOffsetPatterns[type], FIELDS_HM, status); | |
2608 | break; | |
2609 | case UTZFMT_PAT_POSITIVE_HMS: | |
2610 | case UTZFMT_PAT_NEGATIVE_HMS: | |
2611 | fGMTOffsetPatternItems[type] = parseOffsetPattern(fGMTOffsetPatterns[type], FIELDS_HMS, status); | |
2612 | break; | |
2613 | } | |
2614 | } | |
51004dcb A |
2615 | checkAbuttingHoursAndMinutes(); |
2616 | } | |
2617 | ||
2618 | void | |
2619 | TimeZoneFormat::checkAbuttingHoursAndMinutes() { | |
2620 | fAbuttingOffsetHoursAndMinutes= FALSE; | |
2621 | for (int32_t type = 0; type < UTZFMT_PAT_COUNT; type++) { | |
2622 | UBool afterH = FALSE; | |
2623 | UVector *items = fGMTOffsetPatternItems[type]; | |
2624 | for (int32_t i = 0; i < items->size(); i++) { | |
2625 | const GMTOffsetField* item = (GMTOffsetField*)items->elementAt(i); | |
2626 | GMTOffsetField::FieldType type = item->getType(); | |
2627 | if (type != GMTOffsetField::TEXT) { | |
2628 | if (afterH) { | |
2629 | fAbuttingOffsetHoursAndMinutes = TRUE; | |
2630 | break; | |
2631 | } else if (type == GMTOffsetField::HOUR) { | |
2632 | afterH = TRUE; | |
2633 | } | |
2634 | } else if (afterH) { | |
2635 | break; | |
2636 | } | |
2637 | } | |
2638 | if (fAbuttingOffsetHoursAndMinutes) { | |
2639 | break; | |
2640 | } | |
2641 | } | |
4388f060 A |
2642 | } |
2643 | ||
2644 | UBool | |
2645 | TimeZoneFormat::toCodePoints(const UnicodeString& str, UChar32* codeArray, int32_t size) { | |
2646 | int32_t count = str.countChar32(); | |
2647 | if (count != size) { | |
2648 | return FALSE; | |
2649 | } | |
2650 | ||
2651 | for (int32_t idx = 0, start = 0; idx < size; idx++) { | |
2652 | codeArray[idx] = str.char32At(start); | |
2653 | start = str.moveIndex32(start, 1); | |
2654 | } | |
2655 | ||
2656 | return TRUE; | |
2657 | } | |
2658 | ||
2659 | TimeZone* | |
2660 | TimeZoneFormat::createTimeZoneForOffset(int32_t offset) const { | |
2661 | if (offset == 0) { | |
2662 | // when offset is 0, we should use "Etc/GMT" | |
b331163b | 2663 | return TimeZone::createTimeZone(UnicodeString(TRUE, TZID_GMT, -1)); |
4388f060 A |
2664 | } |
2665 | return ZoneMeta::createCustomTimeZone(offset); | |
2666 | } | |
2667 | ||
2668 | UTimeZoneFormatTimeType | |
2669 | TimeZoneFormat::getTimeType(UTimeZoneNameType nameType) { | |
2670 | switch (nameType) { | |
2671 | case UTZNM_LONG_STANDARD: | |
2672 | case UTZNM_SHORT_STANDARD: | |
2673 | return UTZFMT_TIME_TYPE_STANDARD; | |
2674 | ||
2675 | case UTZNM_LONG_DAYLIGHT: | |
2676 | case UTZNM_SHORT_DAYLIGHT: | |
2677 | return UTZFMT_TIME_TYPE_DAYLIGHT; | |
2678 | ||
2679 | default: | |
b331163b | 2680 | return UTZFMT_TIME_TYPE_UNKNOWN; |
4388f060 | 2681 | } |
4388f060 A |
2682 | } |
2683 | ||
2684 | UnicodeString& | |
2685 | TimeZoneFormat::getTimeZoneID(const TimeZoneNames::MatchInfoCollection* matches, int32_t idx, UnicodeString& tzID) const { | |
2686 | if (!matches->getTimeZoneIDAt(idx, tzID)) { | |
b331163b A |
2687 | UChar mzIDBuf[32]; |
2688 | UnicodeString mzID(mzIDBuf, 0, UPRV_LENGTHOF(mzIDBuf)); | |
4388f060 A |
2689 | if (matches->getMetaZoneIDAt(idx, mzID)) { |
2690 | fTimeZoneNames->getReferenceZoneID(mzID, fTargetRegion, tzID); | |
2691 | } | |
2692 | } | |
2693 | return tzID; | |
2694 | } | |
2695 | ||
51004dcb A |
2696 | |
2697 | class ZoneIdMatchHandler : public TextTrieMapSearchResultHandler { | |
2698 | public: | |
2699 | ZoneIdMatchHandler(); | |
2700 | virtual ~ZoneIdMatchHandler(); | |
2701 | ||
2702 | UBool handleMatch(int32_t matchLength, const CharacterNode *node, UErrorCode &status); | |
2703 | const UChar* getID(); | |
2704 | int32_t getMatchLen(); | |
2705 | private: | |
2706 | int32_t fLen; | |
2707 | const UChar* fID; | |
2708 | }; | |
2709 | ||
2710 | ZoneIdMatchHandler::ZoneIdMatchHandler() | |
2711 | : fLen(0), fID(NULL) { | |
2712 | } | |
2713 | ||
2714 | ZoneIdMatchHandler::~ZoneIdMatchHandler() { | |
2715 | } | |
2716 | ||
2717 | UBool | |
2718 | ZoneIdMatchHandler::handleMatch(int32_t matchLength, const CharacterNode *node, UErrorCode &status) { | |
2719 | if (U_FAILURE(status)) { | |
2720 | return FALSE; | |
2721 | } | |
2722 | if (node->hasValues()) { | |
2723 | const UChar* id = (const UChar*)node->getValue(0); | |
2724 | if (id != NULL) { | |
2725 | if (fLen < matchLength) { | |
2726 | fID = id; | |
2727 | fLen = matchLength; | |
2728 | } | |
2729 | } | |
2730 | } | |
2731 | return TRUE; | |
2732 | } | |
2733 | ||
2734 | const UChar* | |
2735 | ZoneIdMatchHandler::getID() { | |
2736 | return fID; | |
2737 | } | |
2738 | ||
2739 | int32_t | |
2740 | ZoneIdMatchHandler::getMatchLen() { | |
2741 | return fLen; | |
2742 | } | |
2743 | ||
57a6839d A |
2744 | |
2745 | static void U_CALLCONV initZoneIdTrie(UErrorCode &status) { | |
2746 | U_ASSERT(gZoneIdTrie == NULL); | |
2747 | ucln_i18n_registerCleanup(UCLN_I18N_TIMEZONEFORMAT, tzfmt_cleanup); | |
2748 | gZoneIdTrie = new TextTrieMap(TRUE, NULL); // No deleter, because values are pooled by ZoneMeta | |
2749 | if (gZoneIdTrie == NULL) { | |
2750 | status = U_MEMORY_ALLOCATION_ERROR; | |
2751 | return; | |
2752 | } | |
2753 | StringEnumeration *tzenum = TimeZone::createEnumeration(); | |
2754 | const UnicodeString *id; | |
2755 | while ((id = tzenum->snext(status))) { | |
2756 | const UChar* uid = ZoneMeta::findTimeZoneID(*id); | |
2757 | if (uid) { | |
2758 | gZoneIdTrie->put(uid, const_cast<UChar *>(uid), status); | |
2759 | } | |
2760 | } | |
2761 | delete tzenum; | |
2762 | } | |
2763 | ||
2764 | ||
51004dcb A |
2765 | UnicodeString& |
2766 | TimeZoneFormat::parseZoneID(const UnicodeString& text, ParsePosition& pos, UnicodeString& tzID) const { | |
2767 | UErrorCode status = U_ZERO_ERROR; | |
57a6839d | 2768 | umtx_initOnce(gZoneIdTrieInitOnce, &initZoneIdTrie, status); |
51004dcb A |
2769 | |
2770 | int32_t start = pos.getIndex(); | |
2771 | int32_t len = 0; | |
2772 | tzID.setToBogus(); | |
2773 | ||
57a6839d | 2774 | if (U_SUCCESS(status)) { |
51004dcb A |
2775 | LocalPointer<ZoneIdMatchHandler> handler(new ZoneIdMatchHandler()); |
2776 | gZoneIdTrie->search(text, start, handler.getAlias(), status); | |
2777 | len = handler->getMatchLen(); | |
2778 | if (len > 0) { | |
2779 | tzID.setTo(handler->getID(), -1); | |
2780 | } | |
2781 | } | |
2782 | ||
2783 | if (len > 0) { | |
2784 | pos.setIndex(start + len); | |
2785 | } else { | |
2786 | pos.setErrorIndex(start); | |
2787 | } | |
2788 | ||
2789 | return tzID; | |
2790 | } | |
2791 | ||
57a6839d A |
2792 | static void U_CALLCONV initShortZoneIdTrie(UErrorCode &status) { |
2793 | U_ASSERT(gShortZoneIdTrie == NULL); | |
2794 | ucln_i18n_registerCleanup(UCLN_I18N_TIMEZONEFORMAT, tzfmt_cleanup); | |
2795 | StringEnumeration *tzenum = TimeZone::createTimeZoneIDEnumeration(UCAL_ZONE_TYPE_CANONICAL, NULL, NULL, status); | |
2796 | if (U_SUCCESS(status)) { | |
2797 | gShortZoneIdTrie = new TextTrieMap(TRUE, NULL); // No deleter, because values are pooled by ZoneMeta | |
2798 | if (gShortZoneIdTrie == NULL) { | |
2799 | status = U_MEMORY_ALLOCATION_ERROR; | |
2800 | } else { | |
2801 | const UnicodeString *id; | |
2802 | while ((id = tzenum->snext(status))) { | |
2803 | const UChar* uID = ZoneMeta::findTimeZoneID(*id); | |
2804 | const UChar* shortID = ZoneMeta::getShortID(*id); | |
2805 | if (shortID && uID) { | |
2806 | gShortZoneIdTrie->put(shortID, const_cast<UChar *>(uID), status); | |
51004dcb | 2807 | } |
51004dcb A |
2808 | } |
2809 | } | |
51004dcb | 2810 | } |
57a6839d A |
2811 | delete tzenum; |
2812 | } | |
2813 | ||
2814 | ||
2815 | UnicodeString& | |
2816 | TimeZoneFormat::parseShortZoneID(const UnicodeString& text, ParsePosition& pos, UnicodeString& tzID) const { | |
2817 | UErrorCode status = U_ZERO_ERROR; | |
2818 | umtx_initOnce(gShortZoneIdTrieInitOnce, &initShortZoneIdTrie, status); | |
51004dcb A |
2819 | |
2820 | int32_t start = pos.getIndex(); | |
2821 | int32_t len = 0; | |
2822 | tzID.setToBogus(); | |
2823 | ||
57a6839d | 2824 | if (U_SUCCESS(status)) { |
51004dcb A |
2825 | LocalPointer<ZoneIdMatchHandler> handler(new ZoneIdMatchHandler()); |
2826 | gShortZoneIdTrie->search(text, start, handler.getAlias(), status); | |
2827 | len = handler->getMatchLen(); | |
2828 | if (len > 0) { | |
2829 | tzID.setTo(handler->getID(), -1); | |
2830 | } | |
2831 | } | |
2832 | ||
2833 | if (len > 0) { | |
2834 | pos.setIndex(start + len); | |
2835 | } else { | |
2836 | pos.setErrorIndex(start); | |
2837 | } | |
2838 | ||
2839 | return tzID; | |
2840 | } | |
2841 | ||
2842 | ||
2843 | UnicodeString& | |
2844 | TimeZoneFormat::parseExemplarLocation(const UnicodeString& text, ParsePosition& pos, UnicodeString& tzID) const { | |
2845 | int32_t startIdx = pos.getIndex(); | |
2846 | int32_t parsedPos = -1; | |
2847 | tzID.setToBogus(); | |
2848 | ||
2849 | UErrorCode status = U_ZERO_ERROR; | |
2850 | LocalPointer<TimeZoneNames::MatchInfoCollection> exemplarMatches(fTimeZoneNames->find(text, startIdx, UTZNM_EXEMPLAR_LOCATION, status)); | |
2851 | if (U_FAILURE(status)) { | |
2852 | pos.setErrorIndex(startIdx); | |
2853 | return tzID; | |
2854 | } | |
2855 | int32_t matchIdx = -1; | |
2856 | if (!exemplarMatches.isNull()) { | |
2857 | for (int32_t i = 0; i < exemplarMatches->size(); i++) { | |
2858 | if (startIdx + exemplarMatches->getMatchLengthAt(i) > parsedPos) { | |
2859 | matchIdx = i; | |
2860 | parsedPos = startIdx + exemplarMatches->getMatchLengthAt(i); | |
2861 | } | |
2862 | } | |
2863 | if (parsedPos > 0) { | |
2864 | pos.setIndex(parsedPos); | |
2865 | getTimeZoneID(exemplarMatches.getAlias(), matchIdx, tzID); | |
2866 | } | |
2867 | } | |
2868 | ||
2869 | if (tzID.length() == 0) { | |
2870 | pos.setErrorIndex(startIdx); | |
2871 | } | |
2872 | ||
2873 | return tzID; | |
2874 | } | |
2875 | ||
4388f060 A |
2876 | U_NAMESPACE_END |
2877 | ||
2878 | #endif |