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