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