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