1 // Copyright (C) 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 *******************************************************************************
5 * Copyright (C) 2011-2015, International Business Machines Corporation and
6 * others. All Rights Reserved.
7 *******************************************************************************
10 #include "unicode/utypes.h"
12 #if !UCONFIG_NO_FORMATTING
14 #include "unicode/calendar.h"
15 #include "unicode/tzfmt.h"
16 #include "unicode/numsys.h"
17 #include "unicode/uchar.h"
18 #include "unicode/udat.h"
30 #include "tznames_impl.h" // TextTrieMap
34 // Bit flags used by the parse method.
35 // The order must match UTimeZoneFormatStyle enum.
36 #define ISO_Z_STYLE_FLAG 0x0080
37 #define ISO_LOCAL_STYLE_FLAG 0x0100
38 static const int16_t STYLE_PARSE_FLAGS[] = {
39 0x0001, // UTZFMT_STYLE_GENERIC_LOCATION,
40 0x0002, // UTZFMT_STYLE_GENERIC_LONG,
41 0x0004, // UTZFMT_STYLE_GENERIC_SHORT,
42 0x0008, // UTZFMT_STYLE_SPECIFIC_LONG,
43 0x0010, // UTZFMT_STYLE_SPECIFIC_SHORT,
44 0x0020, // UTZFMT_STYLE_LOCALIZED_GMT,
45 0x0040, // UTZFMT_STYLE_LOCALIZED_GMT_SHORT,
46 ISO_Z_STYLE_FLAG, // UTZFMT_STYLE_ISO_BASIC_SHORT,
47 ISO_LOCAL_STYLE_FLAG, // UTZFMT_STYLE_ISO_BASIC_LOCAL_SHORT,
48 ISO_Z_STYLE_FLAG, // UTZFMT_STYLE_ISO_BASIC_FIXED,
49 ISO_LOCAL_STYLE_FLAG, // UTZFMT_STYLE_ISO_BASIC_LOCAL_FIXED,
50 ISO_Z_STYLE_FLAG, // UTZFMT_STYLE_ISO_BASIC_FULL,
51 ISO_LOCAL_STYLE_FLAG, // UTZFMT_STYLE_ISO_BASIC_LOCAL_FULL,
52 ISO_Z_STYLE_FLAG, // UTZFMT_STYLE_ISO_EXTENDED_FIXED,
53 ISO_LOCAL_STYLE_FLAG, // UTZFMT_STYLE_ISO_EXTENDED_LOCAL_FIXED,
54 ISO_Z_STYLE_FLAG, // UTZFMT_STYLE_ISO_EXTENDED_FULL,
55 ISO_LOCAL_STYLE_FLAG, // UTZFMT_STYLE_ISO_EXTENDED_LOCAL_FULL,
56 0x0200, // UTZFMT_STYLE_ZONE_ID,
57 0x0400, // UTZFMT_STYLE_ZONE_ID_SHORT,
58 0x0800 // UTZFMT_STYLE_EXEMPLAR_LOCATION
61 static const char gZoneStringsTag[] = "zoneStrings";
62 static const char gGmtFormatTag[]= "gmtFormat";
63 static const char gGmtZeroFormatTag[] = "gmtZeroFormat";
64 static const char gHourFormatTag[]= "hourFormat";
66 static const UChar TZID_GMT[] = {0x0045, 0x0074, 0x0063, 0x002F, 0x0047, 0x004D, 0x0054, 0}; // Etc/GMT
67 static const UChar UNKNOWN_ZONE_ID[] = {
68 0x0045, 0x0074, 0x0063, 0x002F, 0x0055, 0x006E, 0x006B, 0x006E, 0x006F, 0x0077, 0x006E, 0}; // Etc/Unknown
69 static const UChar UNKNOWN_SHORT_ZONE_ID[] = {0x0075, 0x006E, 0x006B, 0}; // unk
70 static const UChar UNKNOWN_LOCATION[] = {0x0055, 0x006E, 0x006B, 0x006E, 0x006F, 0x0077, 0x006E, 0}; // Unknown
72 static const UChar DEFAULT_GMT_PATTERN[] = {0x0047, 0x004D, 0x0054, 0x007B, 0x0030, 0x007D, 0}; // GMT{0}
73 //static const UChar DEFAULT_GMT_ZERO[] = {0x0047, 0x004D, 0x0054, 0}; // GMT
74 static const UChar DEFAULT_GMT_POSITIVE_HM[] = {0x002B, 0x0048, 0x003A, 0x006D, 0x006D, 0}; // +H:mm
75 static const UChar DEFAULT_GMT_POSITIVE_HMS[] = {0x002B, 0x0048, 0x003A, 0x006D, 0x006D, 0x003A, 0x0073, 0x0073, 0}; // +H:mm:ss
76 static const UChar DEFAULT_GMT_NEGATIVE_HM[] = {0x002D, 0x0048, 0x003A, 0x006D, 0x006D, 0}; // -H:mm
77 static const UChar DEFAULT_GMT_NEGATIVE_HMS[] = {0x002D, 0x0048, 0x003A, 0x006D, 0x006D, 0x003A, 0x0073, 0x0073, 0}; // -H:mm:ss
78 static const UChar DEFAULT_GMT_POSITIVE_H[] = {0x002B, 0x0048, 0}; // +H
79 static const UChar DEFAULT_GMT_NEGATIVE_H[] = {0x002D, 0x0048, 0}; // -H
81 static const UChar32 DEFAULT_GMT_DIGITS[] = {
82 0x0030, 0x0031, 0x0032, 0x0033, 0x0034,
83 0x0035, 0x0036, 0x0037, 0x0038, 0x0039
86 static const UChar DEFAULT_GMT_OFFSET_SEP = 0x003A; // ':'
88 static const UChar ARG0[] = {0x007B, 0x0030, 0x007D}; // "{0}"
89 static const int32_t ARG0_LEN = 3;
91 static const UChar DEFAULT_GMT_OFFSET_MINUTE_PATTERN[] = {0x006D, 0x006D, 0}; // "mm"
92 static const UChar DEFAULT_GMT_OFFSET_SECOND_PATTERN[] = {0x0073, 0x0073, 0}; // "ss"
94 static const UChar ALT_GMT_STRINGS[][4] = {
95 {0x0047, 0x004D, 0x0054, 0}, // GMT
96 {0x0055, 0x0054, 0x0043, 0}, // UTC
97 {0x0055, 0x0054, 0, 0}, // UT
101 // Order of GMT offset pattern parsing, *_HMS must be evaluated first
102 // because *_HM is most likely a substring of *_HMS
103 static const int32_t PARSE_GMT_OFFSET_TYPES[] = {
104 UTZFMT_PAT_POSITIVE_HMS,
105 UTZFMT_PAT_NEGATIVE_HMS,
106 UTZFMT_PAT_POSITIVE_HM,
107 UTZFMT_PAT_NEGATIVE_HM,
108 UTZFMT_PAT_POSITIVE_H,
109 UTZFMT_PAT_NEGATIVE_H,
113 static const UChar SINGLEQUOTE = 0x0027;
114 static const UChar PLUS = 0x002B;
115 static const UChar MINUS = 0x002D;
116 static const UChar ISO8601_UTC = 0x005A; // 'Z'
117 static const UChar ISO8601_SEP = 0x003A; // ':'
119 static const int32_t MILLIS_PER_HOUR = 60 * 60 * 1000;
120 static const int32_t MILLIS_PER_MINUTE = 60 * 1000;
121 static const int32_t MILLIS_PER_SECOND = 1000;
123 // Maximum offset (exclusive) in millisecond supported by offset formats
124 static int32_t MAX_OFFSET = 24 * MILLIS_PER_HOUR;
126 // Maximum values for GMT offset fields
127 static const int32_t MAX_OFFSET_HOUR = 23;
128 static const int32_t MAX_OFFSET_MINUTE = 59;
129 static const int32_t MAX_OFFSET_SECOND = 59;
131 static const int32_t UNKNOWN_OFFSET = 0x7FFFFFFF;
133 static const int32_t ALL_SIMPLE_NAME_TYPES = UTZNM_LONG_STANDARD | UTZNM_LONG_DAYLIGHT | UTZNM_SHORT_STANDARD | UTZNM_SHORT_DAYLIGHT | UTZNM_EXEMPLAR_LOCATION;
134 static const int32_t ALL_GENERIC_NAME_TYPES = UTZGNM_LOCATION | UTZGNM_LONG | UTZGNM_SHORT;
136 #define DIGIT_VAL(c) (0x0030 <= (c) && (c) <= 0x0039 ? (c) - 0x0030 : -1)
137 #define MAX_OFFSET_DIGITS 6
139 // Time Zone ID/Short ID trie
140 static TextTrieMap *gZoneIdTrie = NULL;
141 static icu::UInitOnce gZoneIdTrieInitOnce = U_INITONCE_INITIALIZER;
143 static TextTrieMap *gShortZoneIdTrie = NULL;
144 static icu::UInitOnce gShortZoneIdTrieInitOnce = U_INITONCE_INITIALIZER;
146 static UMutex gLock = U_MUTEX_INITIALIZER;
150 * Cleanup callback func
152 static UBool U_CALLCONV tzfmt_cleanup(void)
154 if (gZoneIdTrie != NULL) {
158 gZoneIdTrieInitOnce.reset();
160 if (gShortZoneIdTrie != NULL) {
161 delete gShortZoneIdTrie;
163 gShortZoneIdTrie = NULL;
164 gShortZoneIdTrieInitOnce.reset();
170 // ------------------------------------------------------------------
173 // This class represents a localized GMT offset pattern
174 // item and used by TimeZoneFormat
175 // ------------------------------------------------------------------
176 class GMTOffsetField : public UMemory {
185 virtual ~GMTOffsetField();
187 static GMTOffsetField* createText(const UnicodeString& text, UErrorCode& status);
188 static GMTOffsetField* createTimeField(FieldType type, uint8_t width, UErrorCode& status);
189 static UBool isValid(FieldType type, int32_t width);
190 static FieldType getTypeByLetter(UChar ch);
192 FieldType getType() const;
193 uint8_t getWidth() const;
194 const UChar* getPatternText(void) const;
204 GMTOffsetField::GMTOffsetField()
205 : fText(NULL), fType(TEXT), fWidth(0) {
208 GMTOffsetField::~GMTOffsetField() {
215 GMTOffsetField::createText(const UnicodeString& text, UErrorCode& status) {
216 if (U_FAILURE(status)) {
219 GMTOffsetField* result = new GMTOffsetField();
220 if (result == NULL) {
221 status = U_MEMORY_ALLOCATION_ERROR;
225 int32_t len = text.length();
226 result->fText = (UChar*)uprv_malloc((len + 1) * sizeof(UChar));
227 if (result->fText == NULL) {
228 status = U_MEMORY_ALLOCATION_ERROR;
232 u_strncpy(result->fText, text.getBuffer(), len);
233 result->fText[len] = 0;
234 result->fType = TEXT;
240 GMTOffsetField::createTimeField(FieldType type, uint8_t width, UErrorCode& status) {
241 U_ASSERT(type != TEXT);
242 if (U_FAILURE(status)) {
245 GMTOffsetField* result = new GMTOffsetField();
246 if (result == NULL) {
247 status = U_MEMORY_ALLOCATION_ERROR;
251 result->fType = type;
252 result->fWidth = width;
258 GMTOffsetField::isValid(FieldType type, int32_t width) {
261 return (width == 1 || width == 2);
271 GMTOffsetField::FieldType
272 GMTOffsetField::getTypeByLetter(UChar ch) {
273 if (ch == 0x0048 /* H */) {
275 } else if (ch == 0x006D /* m */) {
277 } else if (ch == 0x0073 /* s */) {
283 inline GMTOffsetField::FieldType
284 GMTOffsetField::getType() const {
289 GMTOffsetField::getWidth() const {
294 GMTOffsetField::getPatternText(void) const {
300 static void U_CALLCONV
301 deleteGMTOffsetField(void *obj) {
302 delete static_cast<GMTOffsetField *>(obj);
307 // ------------------------------------------------------------------
309 // ------------------------------------------------------------------
310 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(TimeZoneFormat)
312 TimeZoneFormat::TimeZoneFormat(const Locale& locale, UErrorCode& status)
313 : fLocale(locale), fTimeZoneNames(NULL), fTimeZoneGenericNames(NULL),
314 fDefParseOptionFlags(0), fTZDBTimeZoneNames(NULL) {
316 for (int32_t i = 0; i < UTZFMT_PAT_COUNT; i++) {
317 fGMTOffsetPatternItems[i] = NULL;
320 const char* region = fLocale.getCountry();
321 int32_t regionLen = uprv_strlen(region);
322 if (regionLen == 0) {
323 char loc[ULOC_FULLNAME_CAPACITY];
324 uloc_addLikelySubtags(fLocale.getName(), loc, sizeof(loc), &status);
326 regionLen = uloc_getCountry(loc, fTargetRegion, sizeof(fTargetRegion), &status);
327 if (U_SUCCESS(status)) {
328 fTargetRegion[regionLen] = 0;
332 } else if (regionLen < (int32_t)sizeof(fTargetRegion)) {
333 uprv_strcpy(fTargetRegion, region);
335 fTargetRegion[0] = 0;
338 fTimeZoneNames = TimeZoneNames::createInstance(locale, status);
339 // fTimeZoneGenericNames is lazily instantiated
340 if (U_FAILURE(status)) {
344 const UChar* gmtPattern = NULL;
345 const UChar* hourFormats = NULL;
347 UResourceBundle *zoneBundle = ures_open(U_ICUDATA_ZONE, locale.getName(), &status);
348 UResourceBundle *zoneStringsArray = ures_getByKeyWithFallback(zoneBundle, gZoneStringsTag, NULL, &status);
349 if (U_SUCCESS(status)) {
352 resStr = ures_getStringByKeyWithFallback(zoneStringsArray, gGmtFormatTag, &len, &status);
356 resStr = ures_getStringByKeyWithFallback(zoneStringsArray, gGmtZeroFormatTag, &len, &status);
358 fGMTZeroFormat.setTo(TRUE, resStr, len);
360 resStr = ures_getStringByKeyWithFallback(zoneStringsArray, gHourFormatTag, &len, &status);
362 hourFormats = resStr;
364 ures_close(zoneStringsArray);
365 ures_close(zoneBundle);
368 if (gmtPattern == NULL) {
369 gmtPattern = DEFAULT_GMT_PATTERN;
371 initGMTPattern(UnicodeString(TRUE, gmtPattern, -1), status);
373 UBool useDefaultOffsetPatterns = TRUE;
375 UChar *sep = u_strchr(hourFormats, (UChar)0x003B /* ';' */);
377 UErrorCode tmpStatus = U_ZERO_ERROR;
378 fGMTOffsetPatterns[UTZFMT_PAT_POSITIVE_HM].setTo(FALSE, hourFormats, (int32_t)(sep - hourFormats));
379 fGMTOffsetPatterns[UTZFMT_PAT_NEGATIVE_HM].setTo(TRUE, sep + 1, -1);
380 expandOffsetPattern(fGMTOffsetPatterns[UTZFMT_PAT_POSITIVE_HM], fGMTOffsetPatterns[UTZFMT_PAT_POSITIVE_HMS], tmpStatus);
381 expandOffsetPattern(fGMTOffsetPatterns[UTZFMT_PAT_NEGATIVE_HM], fGMTOffsetPatterns[UTZFMT_PAT_NEGATIVE_HMS], tmpStatus);
382 truncateOffsetPattern(fGMTOffsetPatterns[UTZFMT_PAT_POSITIVE_HM], fGMTOffsetPatterns[UTZFMT_PAT_POSITIVE_H], tmpStatus);
383 truncateOffsetPattern(fGMTOffsetPatterns[UTZFMT_PAT_NEGATIVE_HM], fGMTOffsetPatterns[UTZFMT_PAT_NEGATIVE_H], tmpStatus);
384 if (U_SUCCESS(tmpStatus)) {
385 useDefaultOffsetPatterns = FALSE;
389 if (useDefaultOffsetPatterns) {
390 fGMTOffsetPatterns[UTZFMT_PAT_POSITIVE_H].setTo(TRUE, DEFAULT_GMT_POSITIVE_H, -1);
391 fGMTOffsetPatterns[UTZFMT_PAT_POSITIVE_HM].setTo(TRUE, DEFAULT_GMT_POSITIVE_HM, -1);
392 fGMTOffsetPatterns[UTZFMT_PAT_POSITIVE_HMS].setTo(TRUE, DEFAULT_GMT_POSITIVE_HMS, -1);
393 fGMTOffsetPatterns[UTZFMT_PAT_NEGATIVE_H].setTo(TRUE, DEFAULT_GMT_NEGATIVE_H, -1);
394 fGMTOffsetPatterns[UTZFMT_PAT_NEGATIVE_HM].setTo(TRUE, DEFAULT_GMT_NEGATIVE_HM, -1);
395 fGMTOffsetPatterns[UTZFMT_PAT_NEGATIVE_HMS].setTo(TRUE, DEFAULT_GMT_NEGATIVE_HMS, -1);
397 initGMTOffsetPatterns(status);
399 NumberingSystem* ns = NumberingSystem::createInstance(locale, status);
400 UBool useDefDigits = TRUE;
401 if (ns && !ns->isAlgorithmic()) {
402 UnicodeString digits = ns->getDescription();
403 useDefDigits = !toCodePoints(digits, fGMTOffsetDigits, 10);
406 uprv_memcpy(fGMTOffsetDigits, DEFAULT_GMT_DIGITS, sizeof(UChar32) * 10);
411 TimeZoneFormat::TimeZoneFormat(const TimeZoneFormat& other)
412 : Format(other), fTimeZoneNames(NULL), fTimeZoneGenericNames(NULL),
413 fTZDBTimeZoneNames(NULL) {
415 for (int32_t i = 0; i < UTZFMT_PAT_COUNT; i++) {
416 fGMTOffsetPatternItems[i] = NULL;
422 TimeZoneFormat::~TimeZoneFormat() {
423 delete fTimeZoneNames;
424 delete fTimeZoneGenericNames;
425 delete fTZDBTimeZoneNames;
426 for (int32_t i = 0; i < UTZFMT_PAT_COUNT; i++) {
427 delete fGMTOffsetPatternItems[i];
432 TimeZoneFormat::operator=(const TimeZoneFormat& other) {
433 if (this == &other) {
437 delete fTimeZoneNames;
438 delete fTimeZoneGenericNames;
439 fTimeZoneGenericNames = NULL;
440 delete fTZDBTimeZoneNames;
441 fTZDBTimeZoneNames = NULL;
443 fLocale = other.fLocale;
444 uprv_memcpy(fTargetRegion, other.fTargetRegion, sizeof(fTargetRegion));
446 fTimeZoneNames = other.fTimeZoneNames->clone();
447 if (other.fTimeZoneGenericNames) {
448 // TODO: this test has dubious thread safety.
449 fTimeZoneGenericNames = other.fTimeZoneGenericNames->clone();
452 fGMTPattern = other.fGMTPattern;
453 fGMTPatternPrefix = other.fGMTPatternPrefix;
454 fGMTPatternSuffix = other.fGMTPatternSuffix;
456 UErrorCode status = U_ZERO_ERROR;
457 for (int32_t i = 0; i < UTZFMT_PAT_COUNT; i++) {
458 fGMTOffsetPatterns[i] = other.fGMTOffsetPatterns[i];
459 delete fGMTOffsetPatternItems[i];
460 fGMTOffsetPatternItems[i] = NULL;
462 initGMTOffsetPatterns(status);
463 U_ASSERT(U_SUCCESS(status));
465 fGMTZeroFormat = other.fGMTZeroFormat;
467 uprv_memcpy(fGMTOffsetDigits, other.fGMTOffsetDigits, sizeof(fGMTOffsetDigits));
469 fDefParseOptionFlags = other.fDefParseOptionFlags;
476 TimeZoneFormat::operator==(const Format& other) const {
477 TimeZoneFormat* tzfmt = (TimeZoneFormat*)&other;
480 fLocale == tzfmt->fLocale
481 && fGMTPattern == tzfmt->fGMTPattern
482 && fGMTZeroFormat == tzfmt->fGMTZeroFormat
483 && *fTimeZoneNames == *tzfmt->fTimeZoneNames;
485 for (int32_t i = 0; i < UTZFMT_PAT_COUNT && isEqual; i++) {
486 isEqual = fGMTOffsetPatterns[i] == tzfmt->fGMTOffsetPatterns[i];
488 for (int32_t i = 0; i < 10 && isEqual; i++) {
489 isEqual = fGMTOffsetDigits[i] == tzfmt->fGMTOffsetDigits[i];
492 // Check fTimeZoneGenericNames. For now,
493 // if fTimeZoneNames is same, fTimeZoneGenericNames should
494 // be also equivalent.
499 TimeZoneFormat::clone() const {
500 return new TimeZoneFormat(*this);
503 TimeZoneFormat* U_EXPORT2
504 TimeZoneFormat::createInstance(const Locale& locale, UErrorCode& status) {
505 TimeZoneFormat* tzfmt = new TimeZoneFormat(locale, status);
506 if (U_SUCCESS(status)) {
513 // ------------------------------------------------------------------
517 TimeZoneFormat::getTimeZoneNames() const {
518 return (const TimeZoneNames*)fTimeZoneNames;
522 TimeZoneFormat::adoptTimeZoneNames(TimeZoneNames *tznames) {
523 delete fTimeZoneNames;
524 fTimeZoneNames = tznames;
526 // TODO - We should also update fTimeZoneGenericNames
530 TimeZoneFormat::setTimeZoneNames(const TimeZoneNames &tznames) {
531 delete fTimeZoneNames;
532 fTimeZoneNames = tznames.clone();
534 // TODO - We should also update fTimeZoneGenericNames
538 TimeZoneFormat::setDefaultParseOptions(uint32_t flags) {
539 fDefParseOptionFlags = flags;
543 TimeZoneFormat::getDefaultParseOptions(void) const {
544 return fDefParseOptionFlags;
549 TimeZoneFormat::getGMTPattern(UnicodeString& pattern) const {
550 return pattern.setTo(fGMTPattern);
554 TimeZoneFormat::setGMTPattern(const UnicodeString& pattern, UErrorCode& status) {
555 initGMTPattern(pattern, status);
559 TimeZoneFormat::getGMTOffsetPattern(UTimeZoneFormatGMTOffsetPatternType type, UnicodeString& pattern) const {
560 return pattern.setTo(fGMTOffsetPatterns[type]);
564 TimeZoneFormat::setGMTOffsetPattern(UTimeZoneFormatGMTOffsetPatternType type, const UnicodeString& pattern, UErrorCode& status) {
565 if (U_FAILURE(status)) {
568 if (pattern == fGMTOffsetPatterns[type]) {
573 OffsetFields required = FIELDS_HM;
575 case UTZFMT_PAT_POSITIVE_H:
576 case UTZFMT_PAT_NEGATIVE_H:
579 case UTZFMT_PAT_POSITIVE_HM:
580 case UTZFMT_PAT_NEGATIVE_HM:
581 required = FIELDS_HM;
583 case UTZFMT_PAT_POSITIVE_HMS:
584 case UTZFMT_PAT_NEGATIVE_HMS:
585 required = FIELDS_HMS;
592 UVector* patternItems = parseOffsetPattern(pattern, required, status);
593 if (patternItems == NULL) {
597 fGMTOffsetPatterns[type].setTo(pattern);
598 delete fGMTOffsetPatternItems[type];
599 fGMTOffsetPatternItems[type] = patternItems;
600 checkAbuttingHoursAndMinutes();
604 TimeZoneFormat::getGMTOffsetDigits(UnicodeString& digits) const {
606 for (int32_t i = 0; i < 10; i++) {
607 digits.append(fGMTOffsetDigits[i]);
613 TimeZoneFormat::setGMTOffsetDigits(const UnicodeString& digits, UErrorCode& status) {
614 if (U_FAILURE(status)) {
617 UChar32 digitArray[10];
618 if (!toCodePoints(digits, digitArray, 10)) {
619 status = U_ILLEGAL_ARGUMENT_ERROR;
622 uprv_memcpy(fGMTOffsetDigits, digitArray, sizeof(UChar32)*10);
626 TimeZoneFormat::getGMTZeroFormat(UnicodeString& gmtZeroFormat) const {
627 return gmtZeroFormat.setTo(fGMTZeroFormat);
631 TimeZoneFormat::setGMTZeroFormat(const UnicodeString& gmtZeroFormat, UErrorCode& status) {
632 if (U_SUCCESS(status)) {
633 if (gmtZeroFormat.isEmpty()) {
634 status = U_ILLEGAL_ARGUMENT_ERROR;
635 } else if (gmtZeroFormat != fGMTZeroFormat) {
636 fGMTZeroFormat.setTo(gmtZeroFormat);
641 // ------------------------------------------------------------------
645 TimeZoneFormat::format(UTimeZoneFormatStyle style, const TimeZone& tz, UDate date,
646 UnicodeString& name, UTimeZoneFormatTimeType* timeType /* = NULL */) const {
648 *timeType = UTZFMT_TIME_TYPE_UNKNOWN;
651 UBool noOffsetFormatFallback = FALSE;
654 case UTZFMT_STYLE_GENERIC_LOCATION:
655 formatGeneric(tz, UTZGNM_LOCATION, date, name);
657 case UTZFMT_STYLE_GENERIC_LONG:
658 formatGeneric(tz, UTZGNM_LONG, date, name);
660 case UTZFMT_STYLE_GENERIC_SHORT:
661 formatGeneric(tz, UTZGNM_SHORT, date, name);
663 case UTZFMT_STYLE_SPECIFIC_LONG:
664 formatSpecific(tz, UTZNM_LONG_STANDARD, UTZNM_LONG_DAYLIGHT, date, name, timeType);
666 case UTZFMT_STYLE_SPECIFIC_SHORT:
667 formatSpecific(tz, UTZNM_SHORT_STANDARD, UTZNM_SHORT_DAYLIGHT, date, name, timeType);
670 case UTZFMT_STYLE_ZONE_ID:
672 noOffsetFormatFallback = TRUE;
674 case UTZFMT_STYLE_ZONE_ID_SHORT:
676 const UChar* shortID = ZoneMeta::getShortID(tz);
677 if (shortID == NULL) {
678 shortID = UNKNOWN_SHORT_ZONE_ID;
680 name.setTo(shortID, -1);
682 noOffsetFormatFallback = TRUE;
685 case UTZFMT_STYLE_EXEMPLAR_LOCATION:
686 formatExemplarLocation(tz, name);
687 noOffsetFormatFallback = TRUE;
691 // will be handled below
695 if (name.isEmpty() && !noOffsetFormatFallback) {
696 UErrorCode status = U_ZERO_ERROR;
697 int32_t rawOffset, dstOffset;
698 tz.getOffset(date, FALSE, rawOffset, dstOffset, status);
699 int32_t offset = rawOffset + dstOffset;
700 if (U_SUCCESS(status)) {
702 case UTZFMT_STYLE_GENERIC_LOCATION:
703 case UTZFMT_STYLE_GENERIC_LONG:
704 case UTZFMT_STYLE_SPECIFIC_LONG:
705 case UTZFMT_STYLE_LOCALIZED_GMT:
706 formatOffsetLocalizedGMT(offset, name, status);
709 case UTZFMT_STYLE_GENERIC_SHORT:
710 case UTZFMT_STYLE_SPECIFIC_SHORT:
711 case UTZFMT_STYLE_LOCALIZED_GMT_SHORT:
712 formatOffsetShortLocalizedGMT(offset, name, status);
715 case UTZFMT_STYLE_ISO_BASIC_SHORT:
716 formatOffsetISO8601Basic(offset, TRUE, TRUE, TRUE, name, status);
719 case UTZFMT_STYLE_ISO_BASIC_LOCAL_SHORT:
720 formatOffsetISO8601Basic(offset, FALSE, TRUE, TRUE, name, status);
723 case UTZFMT_STYLE_ISO_BASIC_FIXED:
724 formatOffsetISO8601Basic(offset, TRUE, FALSE, TRUE, name, status);
727 case UTZFMT_STYLE_ISO_BASIC_LOCAL_FIXED:
728 formatOffsetISO8601Basic(offset, FALSE, FALSE, TRUE, name, status);
731 case UTZFMT_STYLE_ISO_EXTENDED_FIXED:
732 formatOffsetISO8601Extended(offset, TRUE, FALSE, TRUE, name, status);
735 case UTZFMT_STYLE_ISO_EXTENDED_LOCAL_FIXED:
736 formatOffsetISO8601Extended(offset, FALSE, FALSE, TRUE, name, status);
739 case UTZFMT_STYLE_ISO_BASIC_FULL:
740 formatOffsetISO8601Basic(offset, TRUE, FALSE, FALSE, name, status);
743 case UTZFMT_STYLE_ISO_BASIC_LOCAL_FULL:
744 formatOffsetISO8601Basic(offset, FALSE, FALSE, FALSE, name, status);
747 case UTZFMT_STYLE_ISO_EXTENDED_FULL:
748 formatOffsetISO8601Extended(offset, TRUE, FALSE, FALSE, name, status);
751 case UTZFMT_STYLE_ISO_EXTENDED_LOCAL_FULL:
752 formatOffsetISO8601Extended(offset, FALSE, FALSE, FALSE, name, status);
756 // UTZFMT_STYLE_ZONE_ID, UTZFMT_STYLE_ZONE_ID_SHORT, UTZFMT_STYLE_EXEMPLAR_LOCATION
761 *timeType = (dstOffset != 0) ? UTZFMT_TIME_TYPE_DAYLIGHT : UTZFMT_TIME_TYPE_STANDARD;
770 TimeZoneFormat::format(const Formattable& obj, UnicodeString& appendTo,
771 FieldPosition& pos, UErrorCode& status) const {
772 if (U_FAILURE(status)) {
775 UDate date = Calendar::getNow();
776 if (obj.getType() == Formattable::kObject) {
777 const UObject* formatObj = obj.getObject();
778 const TimeZone* tz = dynamic_cast<const TimeZone*>(formatObj);
780 const Calendar* cal = dynamic_cast<const Calendar*>(formatObj);
782 tz = &cal->getTimeZone();
783 date = cal->getTime(status);
787 int32_t rawOffset, dstOffset;
788 tz->getOffset(date, FALSE, rawOffset, dstOffset, status);
790 UnicodeString result(buf, 0, UPRV_LENGTHOF(buf));
791 formatOffsetLocalizedGMT(rawOffset + dstOffset, result, status);
792 if (U_SUCCESS(status)) {
793 appendTo.append(result);
794 if (pos.getField() == UDAT_TIMEZONE_FIELD) {
795 pos.setBeginIndex(0);
796 pos.setEndIndex(result.length());
805 TimeZoneFormat::parse(UTimeZoneFormatStyle style, const UnicodeString& text, ParsePosition& pos,
806 UTimeZoneFormatTimeType* timeType /*= NULL*/) const {
807 return parse(style, text, pos, getDefaultParseOptions(), timeType);
811 TimeZoneFormat::parse(UTimeZoneFormatStyle style, const UnicodeString& text, ParsePosition& pos,
812 int32_t parseOptions, UTimeZoneFormatTimeType* timeType /* = NULL */) const {
814 *timeType = UTZFMT_TIME_TYPE_UNKNOWN;
817 int32_t startIdx = pos.getIndex();
818 int32_t maxPos = text.length();
821 // Styles using localized GMT format as fallback
822 UBool fallbackLocalizedGMT =
823 (style == UTZFMT_STYLE_SPECIFIC_LONG || style == UTZFMT_STYLE_GENERIC_LONG || style == UTZFMT_STYLE_GENERIC_LOCATION);
824 UBool fallbackShortLocalizedGMT =
825 (style == UTZFMT_STYLE_SPECIFIC_SHORT || style == UTZFMT_STYLE_GENERIC_SHORT);
827 int32_t evaluated = 0; // bit flags representing already evaluated styles
828 ParsePosition tmpPos(startIdx);
830 int32_t parsedOffset = UNKNOWN_OFFSET; // stores successfully parsed offset for later use
831 int32_t parsedPos = -1; // stores successfully parsed offset position for later use
833 // Try localized GMT format first if necessary
834 if (fallbackLocalizedGMT || fallbackShortLocalizedGMT) {
835 UBool hasDigitOffset = FALSE;
836 offset = parseOffsetLocalizedGMT(text, tmpPos, fallbackShortLocalizedGMT, &hasDigitOffset);
837 if (tmpPos.getErrorIndex() == -1) {
838 // Even when the input text was successfully parsed as a localized GMT format text,
839 // we may still need to evaluate the specified style if -
840 // 1) GMT zero format was used, and
841 // 2) The input text was not completely processed
842 if (tmpPos.getIndex() == maxPos || hasDigitOffset) {
843 pos.setIndex(tmpPos.getIndex());
844 return createTimeZoneForOffset(offset);
846 parsedOffset = offset;
847 parsedPos = tmpPos.getIndex();
849 // Note: For now, no distinction between long/short localized GMT format in the parser.
850 // This might be changed in future.
851 // evaluated |= (fallbackLocalizedGMT ? STYLE_PARSE_FLAGS[UTZFMT_STYLE_LOCALIZED_GMT] : STYLE_PARSE_FLAGS[UTZFMT_STYLE_LOCALIZED_GMT_SHORT]);
852 evaluated |= STYLE_PARSE_FLAGS[UTZFMT_STYLE_LOCALIZED_GMT] | STYLE_PARSE_FLAGS[UTZFMT_STYLE_LOCALIZED_GMT_SHORT];
855 UErrorCode status = U_ZERO_ERROR;
857 UnicodeString tzID(tzIDBuf, 0, UPRV_LENGTHOF(tzIDBuf));
859 UBool parseTZDBAbbrev = ((parseOptions & UTZFMT_PARSE_OPTION_TZ_DATABASE_ABBREVIATIONS) != 0);
861 // Try the specified style
863 case UTZFMT_STYLE_LOCALIZED_GMT:
865 tmpPos.setIndex(startIdx);
866 tmpPos.setErrorIndex(-1);
868 offset = parseOffsetLocalizedGMT(text, tmpPos);
869 if (tmpPos.getErrorIndex() == -1) {
870 pos.setIndex(tmpPos.getIndex());
871 return createTimeZoneForOffset(offset);
874 // Note: For now, no distinction between long/short localized GMT format in the parser.
875 // This might be changed in future.
876 evaluated |= STYLE_PARSE_FLAGS[UTZFMT_STYLE_LOCALIZED_GMT_SHORT];
880 case UTZFMT_STYLE_LOCALIZED_GMT_SHORT:
882 tmpPos.setIndex(startIdx);
883 tmpPos.setErrorIndex(-1);
885 offset = parseOffsetShortLocalizedGMT(text, tmpPos);
886 if (tmpPos.getErrorIndex() == -1) {
887 pos.setIndex(tmpPos.getIndex());
888 return createTimeZoneForOffset(offset);
891 // Note: For now, no distinction between long/short localized GMT format in the parser.
892 // This might be changed in future.
893 evaluated |= STYLE_PARSE_FLAGS[UTZFMT_STYLE_LOCALIZED_GMT];
897 case UTZFMT_STYLE_ISO_BASIC_SHORT:
898 case UTZFMT_STYLE_ISO_BASIC_FIXED:
899 case UTZFMT_STYLE_ISO_BASIC_FULL:
900 case UTZFMT_STYLE_ISO_EXTENDED_FIXED:
901 case UTZFMT_STYLE_ISO_EXTENDED_FULL:
903 tmpPos.setIndex(startIdx);
904 tmpPos.setErrorIndex(-1);
906 offset = parseOffsetISO8601(text, tmpPos);
907 if (tmpPos.getErrorIndex() == -1) {
908 pos.setIndex(tmpPos.getIndex());
909 return createTimeZoneForOffset(offset);
915 case UTZFMT_STYLE_ISO_BASIC_LOCAL_SHORT:
916 case UTZFMT_STYLE_ISO_BASIC_LOCAL_FIXED:
917 case UTZFMT_STYLE_ISO_BASIC_LOCAL_FULL:
918 case UTZFMT_STYLE_ISO_EXTENDED_LOCAL_FIXED:
919 case UTZFMT_STYLE_ISO_EXTENDED_LOCAL_FULL:
921 tmpPos.setIndex(startIdx);
922 tmpPos.setErrorIndex(-1);
924 // Exclude the case of UTC Indicator "Z" here
925 UBool hasDigitOffset = FALSE;
926 offset = parseOffsetISO8601(text, tmpPos, FALSE, &hasDigitOffset);
927 if (tmpPos.getErrorIndex() == -1 && hasDigitOffset) {
928 pos.setIndex(tmpPos.getIndex());
929 return createTimeZoneForOffset(offset);
935 case UTZFMT_STYLE_SPECIFIC_LONG:
936 case UTZFMT_STYLE_SPECIFIC_SHORT:
939 int32_t nameTypes = 0;
940 if (style == UTZFMT_STYLE_SPECIFIC_LONG) {
941 nameTypes = (UTZNM_LONG_STANDARD | UTZNM_LONG_DAYLIGHT);
943 U_ASSERT(style == UTZFMT_STYLE_SPECIFIC_SHORT);
944 nameTypes = (UTZNM_SHORT_STANDARD | UTZNM_SHORT_DAYLIGHT);
946 LocalPointer<TimeZoneNames::MatchInfoCollection> specificMatches(fTimeZoneNames->find(text, startIdx, nameTypes, status));
947 if (U_FAILURE(status)) {
948 pos.setErrorIndex(startIdx);
951 if (!specificMatches.isNull()) {
952 int32_t matchIdx = -1;
953 int32_t matchPos = -1;
954 for (int32_t i = 0; i < specificMatches->size(); i++) {
955 matchPos = startIdx + specificMatches->getMatchLengthAt(i);
956 if (matchPos > parsedPos) {
958 parsedPos = matchPos;
963 *timeType = getTimeType(specificMatches->getNameTypeAt(matchIdx));
965 pos.setIndex(matchPos);
966 getTimeZoneID(specificMatches.getAlias(), matchIdx, tzID);
967 U_ASSERT(!tzID.isEmpty());
968 return TimeZone::createTimeZone(tzID);
972 if (parseTZDBAbbrev && style == UTZFMT_STYLE_SPECIFIC_SHORT) {
973 U_ASSERT((nameTypes & UTZNM_SHORT_STANDARD) != 0);
974 U_ASSERT((nameTypes & UTZNM_SHORT_DAYLIGHT) != 0);
976 const TZDBTimeZoneNames *tzdbTimeZoneNames = getTZDBTimeZoneNames(status);
977 if (U_SUCCESS(status)) {
978 LocalPointer<TimeZoneNames::MatchInfoCollection> tzdbNameMatches(
979 tzdbTimeZoneNames->find(text, startIdx, nameTypes, status));
980 if (U_FAILURE(status)) {
981 pos.setErrorIndex(startIdx);
984 if (!tzdbNameMatches.isNull()) {
985 int32_t matchIdx = -1;
986 int32_t matchPos = -1;
987 for (int32_t i = 0; i < tzdbNameMatches->size(); i++) {
988 matchPos = startIdx + tzdbNameMatches->getMatchLengthAt(i);
989 if (matchPos > parsedPos) {
991 parsedPos = matchPos;
996 *timeType = getTimeType(tzdbNameMatches->getNameTypeAt(matchIdx));
998 pos.setIndex(matchPos);
999 getTimeZoneID(tzdbNameMatches.getAlias(), matchIdx, tzID);
1000 U_ASSERT(!tzID.isEmpty());
1001 return TimeZone::createTimeZone(tzID);
1008 case UTZFMT_STYLE_GENERIC_LONG:
1009 case UTZFMT_STYLE_GENERIC_SHORT:
1010 case UTZFMT_STYLE_GENERIC_LOCATION:
1012 int32_t genericNameTypes = 0;
1014 case UTZFMT_STYLE_GENERIC_LOCATION:
1015 genericNameTypes = UTZGNM_LOCATION;
1018 case UTZFMT_STYLE_GENERIC_LONG:
1019 genericNameTypes = UTZGNM_LONG | UTZGNM_LOCATION;
1022 case UTZFMT_STYLE_GENERIC_SHORT:
1023 genericNameTypes = UTZGNM_SHORT | UTZGNM_LOCATION;
1031 UTimeZoneFormatTimeType tt = UTZFMT_TIME_TYPE_UNKNOWN;
1032 const TimeZoneGenericNames *gnames = getTimeZoneGenericNames(status);
1033 if (U_SUCCESS(status)) {
1034 len = gnames->findBestMatch(text, startIdx, genericNameTypes, tzID, tt, status);
1036 if (U_FAILURE(status)) {
1037 pos.setErrorIndex(startIdx);
1045 pos.setIndex(startIdx + len);
1046 U_ASSERT(!tzID.isEmpty());
1047 return TimeZone::createTimeZone(tzID);
1052 case UTZFMT_STYLE_ZONE_ID:
1054 tmpPos.setIndex(startIdx);
1055 tmpPos.setErrorIndex(-1);
1057 parseZoneID(text, tmpPos, tzID);
1058 if (tmpPos.getErrorIndex() == -1) {
1059 pos.setIndex(tmpPos.getIndex());
1060 return TimeZone::createTimeZone(tzID);
1064 case UTZFMT_STYLE_ZONE_ID_SHORT:
1066 tmpPos.setIndex(startIdx);
1067 tmpPos.setErrorIndex(-1);
1069 parseShortZoneID(text, tmpPos, tzID);
1070 if (tmpPos.getErrorIndex() == -1) {
1071 pos.setIndex(tmpPos.getIndex());
1072 return TimeZone::createTimeZone(tzID);
1076 case UTZFMT_STYLE_EXEMPLAR_LOCATION:
1078 tmpPos.setIndex(startIdx);
1079 tmpPos.setErrorIndex(-1);
1081 parseExemplarLocation(text, tmpPos, tzID);
1082 if (tmpPos.getErrorIndex() == -1) {
1083 pos.setIndex(tmpPos.getIndex());
1084 return TimeZone::createTimeZone(tzID);
1089 evaluated |= STYLE_PARSE_FLAGS[style];
1092 if (parsedPos > startIdx) {
1093 // When the specified style is one of SPECIFIC_XXX or GENERIC_XXX, we tried to parse the input
1094 // as localized GMT format earlier. If parsedOffset is positive, it means it was successfully
1095 // parsed as localized GMT format, but offset digits were not detected (more specifically, GMT
1096 // zero format). Then, it tried to find a match within the set of display names, but could not
1097 // find a match. At this point, we can safely assume the input text contains the localized
1099 U_ASSERT(parsedOffset != UNKNOWN_OFFSET);
1100 pos.setIndex(parsedPos);
1101 return createTimeZoneForOffset(parsedOffset);
1104 // Failed to parse the input text as the time zone format in the specified style.
1105 // Check the longest match among other styles below.
1106 UChar parsedIDBuf[32];
1107 UnicodeString parsedID(parsedIDBuf, 0, UPRV_LENGTHOF(parsedIDBuf));
1108 UTimeZoneFormatTimeType parsedTimeType = UTZFMT_TIME_TYPE_UNKNOWN;
1110 U_ASSERT(parsedPos < 0);
1111 U_ASSERT(parsedOffset == UNKNOWN_OFFSET);
1114 if (parsedPos < maxPos &&
1115 ((evaluated & ISO_Z_STYLE_FLAG) == 0 || (evaluated & ISO_LOCAL_STYLE_FLAG) == 0)) {
1116 tmpPos.setIndex(startIdx);
1117 tmpPos.setErrorIndex(-1);
1119 UBool hasDigitOffset = FALSE;
1120 offset = parseOffsetISO8601(text, tmpPos, FALSE, &hasDigitOffset);
1121 if (tmpPos.getErrorIndex() == -1) {
1122 if (tmpPos.getIndex() == maxPos || hasDigitOffset) {
1123 pos.setIndex(tmpPos.getIndex());
1124 return createTimeZoneForOffset(offset);
1126 // Note: When ISO 8601 format contains offset digits, it should not
1127 // collide with other formats. However, ISO 8601 UTC format "Z" (single letter)
1128 // may collide with other names. In this case, we need to evaluate other names.
1129 if (parsedPos < tmpPos.getIndex()) {
1130 parsedOffset = offset;
1131 parsedID.setToBogus();
1132 parsedTimeType = UTZFMT_TIME_TYPE_UNKNOWN;
1133 parsedPos = tmpPos.getIndex();
1134 U_ASSERT(parsedPos == startIdx + 1); // only when "Z" is used
1139 // Localized GMT format
1140 if (parsedPos < maxPos &&
1141 (evaluated & STYLE_PARSE_FLAGS[UTZFMT_STYLE_LOCALIZED_GMT]) == 0) {
1142 tmpPos.setIndex(startIdx);
1143 tmpPos.setErrorIndex(-1);
1145 UBool hasDigitOffset = FALSE;
1146 offset = parseOffsetLocalizedGMT(text, tmpPos, FALSE, &hasDigitOffset);
1147 if (tmpPos.getErrorIndex() == -1) {
1148 if (tmpPos.getIndex() == maxPos || hasDigitOffset) {
1149 pos.setIndex(tmpPos.getIndex());
1150 return createTimeZoneForOffset(offset);
1152 // Evaluate other names - see the comment earlier in this method.
1153 if (parsedPos < tmpPos.getIndex()) {
1154 parsedOffset = offset;
1155 parsedID.setToBogus();
1156 parsedTimeType = UTZFMT_TIME_TYPE_UNKNOWN;
1157 parsedPos = tmpPos.getIndex();
1162 if (parsedPos < maxPos &&
1163 (evaluated & STYLE_PARSE_FLAGS[UTZFMT_STYLE_LOCALIZED_GMT_SHORT]) == 0) {
1164 tmpPos.setIndex(startIdx);
1165 tmpPos.setErrorIndex(-1);
1167 UBool hasDigitOffset = FALSE;
1168 offset = parseOffsetLocalizedGMT(text, tmpPos, TRUE, &hasDigitOffset);
1169 if (tmpPos.getErrorIndex() == -1) {
1170 if (tmpPos.getIndex() == maxPos || hasDigitOffset) {
1171 pos.setIndex(tmpPos.getIndex());
1172 return createTimeZoneForOffset(offset);
1174 // Evaluate other names - see the comment earlier in this method.
1175 if (parsedPos < tmpPos.getIndex()) {
1176 parsedOffset = offset;
1177 parsedID.setToBogus();
1178 parsedTimeType = UTZFMT_TIME_TYPE_UNKNOWN;
1179 parsedPos = tmpPos.getIndex();
1184 // When ParseOption.ALL_STYLES is available, we also try to look all possible display names and IDs.
1185 // For example, when style is GENERIC_LONG, "EST" (SPECIFIC_SHORT) is never
1186 // used for America/New_York. With parseAllStyles true, this code parses "EST"
1187 // as America/New_York.
1189 // Note: Adding all possible names into the trie used by the implementation is quite heavy operation,
1190 // which we want to avoid normally (note that we cache the trie, so this is applicable to the
1191 // first time only as long as the cache does not expire).
1193 if (parseOptions & UTZFMT_PARSE_OPTION_ALL_STYLES) {
1194 // Try all specific names and exemplar location names
1195 if (parsedPos < maxPos) {
1196 LocalPointer<TimeZoneNames::MatchInfoCollection> specificMatches(fTimeZoneNames->find(text, startIdx, ALL_SIMPLE_NAME_TYPES, status));
1197 if (U_FAILURE(status)) {
1198 pos.setErrorIndex(startIdx);
1201 int32_t specificMatchIdx = -1;
1202 int32_t matchPos = -1;
1203 if (!specificMatches.isNull()) {
1204 for (int32_t i = 0; i < specificMatches->size(); i++) {
1205 if (startIdx + specificMatches->getMatchLengthAt(i) > matchPos) {
1206 specificMatchIdx = i;
1207 matchPos = startIdx + specificMatches->getMatchLengthAt(i);
1211 if (parsedPos < matchPos) {
1212 U_ASSERT(specificMatchIdx >= 0);
1213 parsedPos = matchPos;
1214 getTimeZoneID(specificMatches.getAlias(), specificMatchIdx, parsedID);
1215 parsedTimeType = getTimeType(specificMatches->getNameTypeAt(specificMatchIdx));
1216 parsedOffset = UNKNOWN_OFFSET;
1219 if (parseTZDBAbbrev && parsedPos < maxPos && (evaluated & STYLE_PARSE_FLAGS[UTZFMT_STYLE_SPECIFIC_SHORT]) == 0) {
1220 const TZDBTimeZoneNames *tzdbTimeZoneNames = getTZDBTimeZoneNames(status);
1221 if (U_SUCCESS(status)) {
1222 LocalPointer<TimeZoneNames::MatchInfoCollection> tzdbNameMatches(
1223 tzdbTimeZoneNames->find(text, startIdx, ALL_SIMPLE_NAME_TYPES, status));
1224 if (U_FAILURE(status)) {
1225 pos.setErrorIndex(startIdx);
1228 int32_t tzdbNameMatchIdx = -1;
1229 int32_t matchPos = -1;
1230 if (!tzdbNameMatches.isNull()) {
1231 for (int32_t i = 0; i < tzdbNameMatches->size(); i++) {
1232 if (startIdx + tzdbNameMatches->getMatchLengthAt(i) > matchPos) {
1233 tzdbNameMatchIdx = i;
1234 matchPos = startIdx + tzdbNameMatches->getMatchLengthAt(i);
1238 if (parsedPos < matchPos) {
1239 U_ASSERT(tzdbNameMatchIdx >= 0);
1240 parsedPos = matchPos;
1241 getTimeZoneID(tzdbNameMatches.getAlias(), tzdbNameMatchIdx, parsedID);
1242 parsedTimeType = getTimeType(tzdbNameMatches->getNameTypeAt(tzdbNameMatchIdx));
1243 parsedOffset = UNKNOWN_OFFSET;
1247 // Try generic names
1248 if (parsedPos < maxPos) {
1249 int32_t genMatchLen = -1;
1250 UTimeZoneFormatTimeType tt = UTZFMT_TIME_TYPE_UNKNOWN;
1252 const TimeZoneGenericNames *gnames = getTimeZoneGenericNames(status);
1253 if (U_SUCCESS(status)) {
1254 genMatchLen = gnames->findBestMatch(text, startIdx, ALL_GENERIC_NAME_TYPES, tzID, tt, status);
1256 if (U_FAILURE(status)) {
1257 pos.setErrorIndex(startIdx);
1261 if (genMatchLen > 0 && parsedPos < startIdx + genMatchLen) {
1262 parsedPos = startIdx + genMatchLen;
1263 parsedID.setTo(tzID);
1264 parsedTimeType = tt;
1265 parsedOffset = UNKNOWN_OFFSET;
1270 if (parsedPos < maxPos && (evaluated & STYLE_PARSE_FLAGS[UTZFMT_STYLE_ZONE_ID]) == 0) {
1271 tmpPos.setIndex(startIdx);
1272 tmpPos.setErrorIndex(-1);
1274 parseZoneID(text, tmpPos, tzID);
1275 if (tmpPos.getErrorIndex() == -1 && parsedPos < tmpPos.getIndex()) {
1276 parsedPos = tmpPos.getIndex();
1277 parsedID.setTo(tzID);
1278 parsedTimeType = UTZFMT_TIME_TYPE_UNKNOWN;
1279 parsedOffset = UNKNOWN_OFFSET;
1282 // Try short time zone ID
1283 if (parsedPos < maxPos && (evaluated & STYLE_PARSE_FLAGS[UTZFMT_STYLE_ZONE_ID]) == 0) {
1284 tmpPos.setIndex(startIdx);
1285 tmpPos.setErrorIndex(-1);
1287 parseShortZoneID(text, tmpPos, tzID);
1288 if (tmpPos.getErrorIndex() == -1 && parsedPos < tmpPos.getIndex()) {
1289 parsedPos = tmpPos.getIndex();
1290 parsedID.setTo(tzID);
1291 parsedTimeType = UTZFMT_TIME_TYPE_UNKNOWN;
1292 parsedOffset = UNKNOWN_OFFSET;
1297 if (parsedPos > startIdx) {
1298 // Parsed successfully
1300 if (parsedID.length() > 0) {
1301 parsedTZ = TimeZone::createTimeZone(parsedID);
1303 U_ASSERT(parsedOffset != UNKNOWN_OFFSET);
1304 parsedTZ = createTimeZoneForOffset(parsedOffset);
1307 *timeType = parsedTimeType;
1309 pos.setIndex(parsedPos);
1313 pos.setErrorIndex(startIdx);
1318 TimeZoneFormat::parseObject(const UnicodeString& source, Formattable& result,
1319 ParsePosition& parse_pos) const {
1320 result.adoptObject(parse(UTZFMT_STYLE_GENERIC_LOCATION, source, parse_pos, UTZFMT_PARSE_OPTION_ALL_STYLES));
1324 // ------------------------------------------------------------------
1325 // Private zone name format/parse implementation
1328 TimeZoneFormat::formatGeneric(const TimeZone& tz, int32_t genType, UDate date, UnicodeString& name) const {
1329 UErrorCode status = U_ZERO_ERROR;
1330 const TimeZoneGenericNames* gnames = getTimeZoneGenericNames(status);
1331 if (U_FAILURE(status)) {
1336 if (genType == UTZGNM_LOCATION) {
1337 const UChar* canonicalID = ZoneMeta::getCanonicalCLDRID(tz);
1338 if (canonicalID == NULL) {
1342 return gnames->getGenericLocationName(UnicodeString(TRUE, canonicalID, -1), name);
1344 return gnames->getDisplayName(tz, (UTimeZoneGenericNameType)genType, date, name);
1348 TimeZoneFormat::formatSpecific(const TimeZone& tz, UTimeZoneNameType stdType, UTimeZoneNameType dstType,
1349 UDate date, UnicodeString& name, UTimeZoneFormatTimeType *timeType) const {
1350 if (fTimeZoneNames == NULL) {
1355 UErrorCode status = U_ZERO_ERROR;
1356 UBool isDaylight = tz.inDaylightTime(date, status);
1357 const UChar* canonicalID = ZoneMeta::getCanonicalCLDRID(tz);
1359 if (U_FAILURE(status) || canonicalID == NULL) {
1365 fTimeZoneNames->getDisplayName(UnicodeString(TRUE, canonicalID, -1), dstType, date, name);
1367 fTimeZoneNames->getDisplayName(UnicodeString(TRUE, canonicalID, -1), stdType, date, name);
1370 if (timeType && !name.isEmpty()) {
1371 *timeType = isDaylight ? UTZFMT_TIME_TYPE_DAYLIGHT : UTZFMT_TIME_TYPE_STANDARD;
1376 const TimeZoneGenericNames*
1377 TimeZoneFormat::getTimeZoneGenericNames(UErrorCode& status) const {
1378 if (U_FAILURE(status)) {
1383 if (fTimeZoneGenericNames == NULL) {
1384 TimeZoneFormat *nonConstThis = const_cast<TimeZoneFormat *>(this);
1385 nonConstThis->fTimeZoneGenericNames = TimeZoneGenericNames::createInstance(fLocale, status);
1387 umtx_unlock(&gLock);
1389 return fTimeZoneGenericNames;
1392 const TZDBTimeZoneNames*
1393 TimeZoneFormat::getTZDBTimeZoneNames(UErrorCode& status) const {
1394 if (U_FAILURE(status)) {
1399 if (fTZDBTimeZoneNames == NULL) {
1400 TZDBTimeZoneNames *tzdbNames = new TZDBTimeZoneNames(fLocale);
1401 if (tzdbNames == NULL) {
1402 status = U_MEMORY_ALLOCATION_ERROR;
1404 TimeZoneFormat *nonConstThis = const_cast<TimeZoneFormat *>(this);
1405 nonConstThis->fTZDBTimeZoneNames = tzdbNames;
1408 umtx_unlock(&gLock);
1410 return fTZDBTimeZoneNames;
1414 TimeZoneFormat::formatExemplarLocation(const TimeZone& tz, UnicodeString& name) const {
1415 UChar locationBuf[64];
1416 UnicodeString location(locationBuf, 0, UPRV_LENGTHOF(locationBuf));
1417 const UChar* canonicalID = ZoneMeta::getCanonicalCLDRID(tz);
1420 fTimeZoneNames->getExemplarLocationName(UnicodeString(TRUE, canonicalID, -1), location);
1422 if (location.length() > 0) {
1423 name.setTo(location);
1425 // Use "unknown" location
1426 fTimeZoneNames->getExemplarLocationName(UnicodeString(TRUE, UNKNOWN_ZONE_ID, -1), location);
1427 if (location.length() > 0) {
1428 name.setTo(location);
1431 name.setTo(UNKNOWN_LOCATION, -1);
1438 // ------------------------------------------------------------------
1439 // Zone offset format and parse
1442 TimeZoneFormat::formatOffsetISO8601Basic(int32_t offset, UBool useUtcIndicator, UBool isShort, UBool ignoreSeconds,
1443 UnicodeString& result, UErrorCode& status) const {
1444 return formatOffsetISO8601(offset, TRUE, useUtcIndicator, isShort, ignoreSeconds, result, status);
1448 TimeZoneFormat::formatOffsetISO8601Extended(int32_t offset, UBool useUtcIndicator, UBool isShort, UBool ignoreSeconds,
1449 UnicodeString& result, UErrorCode& status) const {
1450 return formatOffsetISO8601(offset, FALSE, useUtcIndicator, isShort, ignoreSeconds, result, status);
1454 TimeZoneFormat::formatOffsetLocalizedGMT(int32_t offset, UnicodeString& result, UErrorCode& status) const {
1455 return formatOffsetLocalizedGMT(offset, FALSE, result, status);
1459 TimeZoneFormat::formatOffsetShortLocalizedGMT(int32_t offset, UnicodeString& result, UErrorCode& status) const {
1460 return formatOffsetLocalizedGMT(offset, TRUE, result, status);
1464 TimeZoneFormat::parseOffsetISO8601(const UnicodeString& text, ParsePosition& pos) const {
1465 return parseOffsetISO8601(text, pos, FALSE);
1469 TimeZoneFormat::parseOffsetLocalizedGMT(const UnicodeString& text, ParsePosition& pos) const {
1470 return parseOffsetLocalizedGMT(text, pos, FALSE, NULL);
1474 TimeZoneFormat::parseOffsetShortLocalizedGMT(const UnicodeString& text, ParsePosition& pos) const {
1475 return parseOffsetLocalizedGMT(text, pos, TRUE, NULL);
1478 // ------------------------------------------------------------------
1479 // Private zone offset format/parse implementation
1482 TimeZoneFormat::formatOffsetISO8601(int32_t offset, UBool isBasic, UBool useUtcIndicator,
1483 UBool isShort, UBool ignoreSeconds, UnicodeString& result, UErrorCode& status) const {
1484 if (U_FAILURE(status)) {
1485 result.setToBogus();
1488 int32_t absOffset = offset < 0 ? -offset : offset;
1489 if (useUtcIndicator && (absOffset < MILLIS_PER_SECOND || (ignoreSeconds && absOffset < MILLIS_PER_MINUTE))) {
1490 result.setTo(ISO8601_UTC);
1494 OffsetFields minFields = isShort ? FIELDS_H : FIELDS_HM;
1495 OffsetFields maxFields = ignoreSeconds ? FIELDS_HM : FIELDS_HMS;
1496 UChar sep = isBasic ? 0 : ISO8601_SEP;
1498 // Note: FIELDS_HMS as maxFields is a CLDR/ICU extension. ISO 8601 specification does
1499 // not support seconds field.
1501 if (absOffset >= MAX_OFFSET) {
1502 result.setToBogus();
1503 status = U_ILLEGAL_ARGUMENT_ERROR;
1508 fields[0] = absOffset / MILLIS_PER_HOUR;
1509 absOffset = absOffset % MILLIS_PER_HOUR;
1510 fields[1] = absOffset / MILLIS_PER_MINUTE;
1511 absOffset = absOffset % MILLIS_PER_MINUTE;
1512 fields[2] = absOffset / MILLIS_PER_SECOND;
1514 U_ASSERT(fields[0] >= 0 && fields[0] <= MAX_OFFSET_HOUR);
1515 U_ASSERT(fields[1] >= 0 && fields[1] <= MAX_OFFSET_MINUTE);
1516 U_ASSERT(fields[2] >= 0 && fields[2] <= MAX_OFFSET_SECOND);
1518 int32_t lastIdx = maxFields;
1519 while (lastIdx > minFields) {
1520 if (fields[lastIdx] != 0) {
1528 // if all output fields are 0s, do not use negative sign
1529 for (int32_t idx = 0; idx <= lastIdx; idx++) {
1530 if (fields[idx] != 0) {
1538 for (int32_t idx = 0; idx <= lastIdx; idx++) {
1539 if (sep && idx != 0) {
1542 result.append((UChar)(0x0030 + fields[idx]/10));
1543 result.append((UChar)(0x0030 + fields[idx]%10));
1550 TimeZoneFormat::formatOffsetLocalizedGMT(int32_t offset, UBool isShort, UnicodeString& result, UErrorCode& status) const {
1551 if (U_FAILURE(status)) {
1552 result.setToBogus();
1555 if (offset <= -MAX_OFFSET || offset >= MAX_OFFSET) {
1556 result.setToBogus();
1557 status = U_ILLEGAL_ARGUMENT_ERROR;
1562 result.setTo(fGMTZeroFormat);
1566 UBool positive = TRUE;
1572 int32_t offsetH = offset / MILLIS_PER_HOUR;
1573 offset = offset % MILLIS_PER_HOUR;
1574 int32_t offsetM = offset / MILLIS_PER_MINUTE;
1575 offset = offset % MILLIS_PER_MINUTE;
1576 int32_t offsetS = offset / MILLIS_PER_SECOND;
1578 U_ASSERT(offsetH <= MAX_OFFSET_HOUR && offsetM <= MAX_OFFSET_MINUTE && offsetS <= MAX_OFFSET_SECOND);
1580 const UVector* offsetPatternItems = NULL;
1583 offsetPatternItems = fGMTOffsetPatternItems[UTZFMT_PAT_POSITIVE_HMS];
1584 } else if (offsetM != 0 || !isShort) {
1585 offsetPatternItems = fGMTOffsetPatternItems[UTZFMT_PAT_POSITIVE_HM];
1587 offsetPatternItems = fGMTOffsetPatternItems[UTZFMT_PAT_POSITIVE_H];
1591 offsetPatternItems = fGMTOffsetPatternItems[UTZFMT_PAT_NEGATIVE_HMS];
1592 } else if (offsetM != 0 || !isShort) {
1593 offsetPatternItems = fGMTOffsetPatternItems[UTZFMT_PAT_NEGATIVE_HM];
1595 offsetPatternItems = fGMTOffsetPatternItems[UTZFMT_PAT_NEGATIVE_H];
1599 U_ASSERT(offsetPatternItems != NULL);
1601 // Building the GMT format string
1602 result.setTo(fGMTPatternPrefix);
1604 for (int32_t i = 0; i < offsetPatternItems->size(); i++) {
1605 const GMTOffsetField* item = (GMTOffsetField*)offsetPatternItems->elementAt(i);
1606 GMTOffsetField::FieldType type = item->getType();
1609 case GMTOffsetField::TEXT:
1610 result.append(item->getPatternText(), -1);
1613 case GMTOffsetField::HOUR:
1614 appendOffsetDigits(result, offsetH, (isShort ? 1 : 2));
1617 case GMTOffsetField::MINUTE:
1618 appendOffsetDigits(result, offsetM, 2);
1621 case GMTOffsetField::SECOND:
1622 appendOffsetDigits(result, offsetS, 2);
1627 result.append(fGMTPatternSuffix);
1632 TimeZoneFormat::parseOffsetISO8601(const UnicodeString& text, ParsePosition& pos, UBool extendedOnly, UBool* hasDigitOffset /* = NULL */) const {
1633 if (hasDigitOffset) {
1634 *hasDigitOffset = FALSE;
1636 int32_t start = pos.getIndex();
1637 if (start >= text.length()) {
1638 pos.setErrorIndex(start);
1642 UChar firstChar = text.charAt(start);
1643 if (firstChar == ISO8601_UTC || firstChar == (UChar)(ISO8601_UTC + 0x20)) {
1644 // "Z" (or "z") - indicates UTC
1645 pos.setIndex(start + 1);
1650 if (firstChar == PLUS) {
1652 } else if (firstChar == MINUS) {
1655 // Not an ISO 8601 offset string
1656 pos.setErrorIndex(start);
1659 ParsePosition posOffset(start + 1);
1660 int32_t offset = parseAsciiOffsetFields(text, posOffset, ISO8601_SEP, FIELDS_H, FIELDS_HMS);
1661 if (posOffset.getErrorIndex() == -1 && !extendedOnly && (posOffset.getIndex() - start <= 3)) {
1662 // If the text is successfully parsed as extended format with the options above, it can be also parsed
1663 // as basic format. For example, "0230" can be parsed as offset 2:00 (only first digits are valid for
1664 // extended format), but it can be parsed as offset 2:30 with basic format. We use longer result.
1665 ParsePosition posBasic(start + 1);
1666 int32_t tmpOffset = parseAbuttingAsciiOffsetFields(text, posBasic, FIELDS_H, FIELDS_HMS, FALSE);
1667 if (posBasic.getErrorIndex() == -1 && posBasic.getIndex() > posOffset.getIndex()) {
1669 posOffset.setIndex(posBasic.getIndex());
1673 if (posOffset.getErrorIndex() != -1) {
1674 pos.setErrorIndex(start);
1678 pos.setIndex(posOffset.getIndex());
1679 if (hasDigitOffset) {
1680 *hasDigitOffset = TRUE;
1682 return sign * offset;
1686 TimeZoneFormat::parseOffsetLocalizedGMT(const UnicodeString& text, ParsePosition& pos, UBool isShort, UBool* hasDigitOffset) const {
1687 int32_t start = pos.getIndex();
1689 int32_t parsedLength = 0;
1691 if (hasDigitOffset) {
1692 *hasDigitOffset = FALSE;
1695 offset = parseOffsetLocalizedGMTPattern(text, start, isShort, parsedLength);
1697 // For now, parseOffsetLocalizedGMTPattern handles both long and short
1698 // formats, no matter isShort is true or false. This might be changed in future
1699 // when strict parsing is necessary, or different set of patterns are used for
1700 // short/long formats.
1702 if (parsedLength == 0) {
1703 offset = parseOffsetLocalizedGMTPattern(text, start, !isShort, parsedLength);
1707 if (parsedLength > 0) {
1708 if (hasDigitOffset) {
1709 *hasDigitOffset = TRUE;
1711 pos.setIndex(start + parsedLength);
1715 // Try the default patterns
1716 offset = parseOffsetDefaultLocalizedGMT(text, start, parsedLength);
1717 if (parsedLength > 0) {
1718 if (hasDigitOffset) {
1719 *hasDigitOffset = TRUE;
1721 pos.setIndex(start + parsedLength);
1725 // Check if this is a GMT zero format
1726 if (text.caseCompare(start, fGMTZeroFormat.length(), fGMTZeroFormat, 0) == 0) {
1727 pos.setIndex(start + fGMTZeroFormat.length());
1731 // Check if this is a default GMT zero format
1732 for (int32_t i = 0; ALT_GMT_STRINGS[i][0] != 0; i++) {
1733 const UChar* defGMTZero = ALT_GMT_STRINGS[i];
1734 int32_t defGMTZeroLen = u_strlen(defGMTZero);
1735 if (text.caseCompare(start, defGMTZeroLen, defGMTZero, 0) == 0) {
1736 pos.setIndex(start + defGMTZeroLen);
1742 pos.setErrorIndex(start);
1747 TimeZoneFormat::parseOffsetLocalizedGMTPattern(const UnicodeString& text, int32_t start, UBool /*isShort*/, int32_t& parsedLen) const {
1748 int32_t idx = start;
1750 UBool parsed = FALSE;
1754 int32_t len = fGMTPatternPrefix.length();
1755 if (len > 0 && text.caseCompare(idx, len, fGMTPatternPrefix, 0) != 0) {
1756 // prefix match failed
1762 offset = parseOffsetFields(text, idx, FALSE, len);
1764 // offset field match failed
1769 len = fGMTPatternSuffix.length();
1770 if (len > 0 && text.caseCompare(idx, len, fGMTPatternSuffix, 0) != 0) {
1778 parsedLen = parsed ? idx - start : 0;
1783 TimeZoneFormat::parseOffsetFields(const UnicodeString& text, int32_t start, UBool /*isShort*/, int32_t& parsedLen) const {
1790 int32_t offsetH, offsetM, offsetS;
1791 offsetH = offsetM = offsetS = 0;
1793 for (int32_t patidx = 0; PARSE_GMT_OFFSET_TYPES[patidx] >= 0; patidx++) {
1794 int32_t gmtPatType = PARSE_GMT_OFFSET_TYPES[patidx];
1795 UVector* items = fGMTOffsetPatternItems[gmtPatType];
1796 U_ASSERT(items != NULL);
1798 outLen = parseOffsetFieldsWithPattern(text, start, items, FALSE, offsetH, offsetM, offsetS);
1800 sign = (gmtPatType == UTZFMT_PAT_POSITIVE_H || gmtPatType == UTZFMT_PAT_POSITIVE_HM || gmtPatType == UTZFMT_PAT_POSITIVE_HMS) ?
1806 if (outLen > 0 && fAbuttingOffsetHoursAndMinutes) {
1807 // When hours field is sabutting minutes field,
1808 // the parse result above may not be appropriate.
1809 // For example, "01020" is parsed as 01:02: above,
1810 // but it should be parsed as 00:10:20.
1812 int32_t tmpSign = 1;
1813 int32_t tmpH, tmpM, tmpS;
1815 for (int32_t patidx = 0; PARSE_GMT_OFFSET_TYPES[patidx] >= 0; patidx++) {
1816 int32_t gmtPatType = PARSE_GMT_OFFSET_TYPES[patidx];
1817 UVector* items = fGMTOffsetPatternItems[gmtPatType];
1818 U_ASSERT(items != NULL);
1820 // forcing parse to use single hour digit
1821 tmpLen = parseOffsetFieldsWithPattern(text, start, items, TRUE, tmpH, tmpM, tmpS);
1823 tmpSign = (gmtPatType == UTZFMT_PAT_POSITIVE_H || gmtPatType == UTZFMT_PAT_POSITIVE_HM || gmtPatType == UTZFMT_PAT_POSITIVE_HMS) ?
1828 if (tmpLen > outLen) {
1829 // Better parse result with single hour digit
1839 offset = ((((offsetH * 60) + offsetM) * 60) + offsetS) * 1000 * sign;
1847 TimeZoneFormat::parseOffsetFieldsWithPattern(const UnicodeString& text, int32_t start,
1848 UVector* patternItems, UBool forceSingleHourDigit, int32_t& hour, int32_t& min, int32_t& sec) const {
1849 UBool failed = FALSE;
1850 int32_t offsetH, offsetM, offsetS;
1851 offsetH = offsetM = offsetS = 0;
1852 int32_t idx = start;
1854 for (int32_t i = 0; i < patternItems->size(); i++) {
1856 const GMTOffsetField* field = (const GMTOffsetField*)patternItems->elementAt(i);
1857 GMTOffsetField::FieldType fieldType = field->getType();
1858 if (fieldType == GMTOffsetField::TEXT) {
1859 const UChar* patStr = field->getPatternText();
1860 len = u_strlen(patStr);
1861 if (text.caseCompare(idx, len, patStr, 0) != 0) {
1867 if (fieldType == GMTOffsetField::HOUR) {
1868 uint8_t maxDigits = forceSingleHourDigit ? 1 : 2;
1869 offsetH = parseOffsetFieldWithLocalizedDigits(text, idx, 1, maxDigits, 0, MAX_OFFSET_HOUR, len);
1870 } else if (fieldType == GMTOffsetField::MINUTE) {
1871 offsetM = parseOffsetFieldWithLocalizedDigits(text, idx, 2, 2, 0, MAX_OFFSET_MINUTE, len);
1872 } else if (fieldType == GMTOffsetField::SECOND) {
1873 offsetS = parseOffsetFieldWithLocalizedDigits(text, idx, 2, 2, 0, MAX_OFFSET_SECOND, len);
1885 hour = min = sec = 0;
1897 TimeZoneFormat::parseAbuttingOffsetFields(const UnicodeString& text, int32_t start, int32_t& parsedLen) const {
1898 int32_t digits[MAX_OFFSET_DIGITS];
1899 int32_t parsed[MAX_OFFSET_DIGITS]; // accumulative offsets
1901 // Parse digits into int[]
1902 int32_t idx = start;
1904 int32_t numDigits = 0;
1905 for (int32_t i = 0; i < MAX_OFFSET_DIGITS; i++) {
1906 digits[i] = parseSingleLocalizedDigit(text, idx, len);
1907 if (digits[i] < 0) {
1911 parsed[i] = idx - start;
1915 if (numDigits == 0) {
1921 while (numDigits > 0) {
1926 U_ASSERT(numDigits > 0 && numDigits <= MAX_OFFSET_DIGITS);
1927 switch (numDigits) {
1932 hour = digits[0] * 10 + digits[1];
1936 min = digits[1] * 10 + digits[2];
1939 hour = digits[0] * 10 + digits[1];
1940 min = digits[2] * 10 + digits[3];
1944 min = digits[1] * 10 + digits[2];
1945 sec = digits[3] * 10 + digits[4];
1948 hour = digits[0] * 10 + digits[1];
1949 min = digits[2] * 10 + digits[3];
1950 sec = digits[4] * 10 + digits[5];
1953 if (hour <= MAX_OFFSET_HOUR && min <= MAX_OFFSET_MINUTE && sec <= MAX_OFFSET_SECOND) {
1954 // found a valid combination
1955 offset = hour * MILLIS_PER_HOUR + min * MILLIS_PER_MINUTE + sec * MILLIS_PER_SECOND;
1956 parsedLen = parsed[numDigits - 1];
1965 TimeZoneFormat::parseOffsetDefaultLocalizedGMT(const UnicodeString& text, int start, int32_t& parsedLen) const {
1966 int32_t idx = start;
1971 // check global default GMT alternatives
1974 for (int32_t i = 0; ALT_GMT_STRINGS[i][0] != 0; i++) {
1975 const UChar* gmt = ALT_GMT_STRINGS[i];
1976 int32_t len = u_strlen(gmt);
1977 if (text.caseCompare(start, len, gmt, 0) == 0) {
1987 // offset needs a sign char and a digit at minimum
1988 if (idx + 1 >= text.length()) {
1994 UChar c = text.charAt(idx);
1997 } else if (c == MINUS) {
2005 // try the default pattern with the separator first
2006 int32_t lenWithSep = 0;
2007 int32_t offsetWithSep = parseDefaultOffsetFields(text, idx, DEFAULT_GMT_OFFSET_SEP, lenWithSep);
2008 if (lenWithSep == text.length() - idx) {
2010 offset = offsetWithSep * sign;
2013 // try abutting field pattern
2014 int32_t lenAbut = 0;
2015 int32_t offsetAbut = parseAbuttingOffsetFields(text, idx, lenAbut);
2017 if (lenWithSep > lenAbut) {
2018 offset = offsetWithSep * sign;
2021 offset = offsetAbut * sign;
2025 parsed = idx - start;
2033 TimeZoneFormat::parseDefaultOffsetFields(const UnicodeString& text, int32_t start, UChar separator, int32_t& parsedLen) const {
2034 int32_t max = text.length();
2035 int32_t idx = start;
2037 int32_t hour = 0, min = 0, sec = 0;
2042 hour = parseOffsetFieldWithLocalizedDigits(text, idx, 1, 2, 0, MAX_OFFSET_HOUR, len);
2048 if (idx + 1 < max && text.charAt(idx) == separator) {
2049 min = parseOffsetFieldWithLocalizedDigits(text, idx + 1, 2, 2, 0, MAX_OFFSET_MINUTE, len);
2055 if (idx + 1 < max && text.charAt(idx) == separator) {
2056 sec = parseOffsetFieldWithLocalizedDigits(text, idx + 1, 2, 2, 0, MAX_OFFSET_SECOND, len);
2069 parsedLen = idx - start;
2070 return hour * MILLIS_PER_HOUR + min * MILLIS_PER_MINUTE + sec * MILLIS_PER_SECOND;
2074 TimeZoneFormat::parseOffsetFieldWithLocalizedDigits(const UnicodeString& text, int32_t start, uint8_t minDigits, uint8_t maxDigits, uint16_t minVal, uint16_t maxVal, int32_t& parsedLen) const {
2078 int32_t numDigits = 0;
2079 int32_t idx = start;
2080 int32_t digitLen = 0;
2082 while (idx < text.length() && numDigits < maxDigits) {
2083 int32_t digit = parseSingleLocalizedDigit(text, idx, digitLen);
2087 int32_t tmpVal = decVal * 10 + digit;
2088 if (tmpVal > maxVal) {
2096 // Note: maxVal is checked in the while loop
2097 if (numDigits < minDigits || decVal < minVal) {
2101 parsedLen = idx - start;
2108 TimeZoneFormat::parseSingleLocalizedDigit(const UnicodeString& text, int32_t start, int32_t& len) const {
2111 if (start < text.length()) {
2112 UChar32 cp = text.char32At(start);
2114 // First, try digits configured for this instance
2115 for (int32_t i = 0; i < 10; i++) {
2116 if (cp == fGMTOffsetDigits[i]) {
2121 // If failed, check if this is a Unicode digit
2123 int32_t tmp = u_charDigitValue(cp);
2124 digit = (tmp >= 0 && tmp <= 9) ? tmp : -1;
2128 int32_t next = text.moveIndex32(start, 1);
2136 TimeZoneFormat::formatOffsetWithAsciiDigits(int32_t offset, UChar sep, OffsetFields minFields, OffsetFields maxFields, UnicodeString& result) {
2137 U_ASSERT(maxFields >= minFields);
2138 U_ASSERT(offset > -MAX_OFFSET && offset < MAX_OFFSET);
2148 fields[0] = offset / MILLIS_PER_HOUR;
2149 offset = offset % MILLIS_PER_HOUR;
2150 fields[1] = offset / MILLIS_PER_MINUTE;
2151 offset = offset % MILLIS_PER_MINUTE;
2152 fields[2] = offset / MILLIS_PER_SECOND;
2154 U_ASSERT(fields[0] >= 0 && fields[0] <= MAX_OFFSET_HOUR);
2155 U_ASSERT(fields[1] >= 0 && fields[1] <= MAX_OFFSET_MINUTE);
2156 U_ASSERT(fields[2] >= 0 && fields[2] <= MAX_OFFSET_SECOND);
2158 int32_t lastIdx = maxFields;
2159 while (lastIdx > minFields) {
2160 if (fields[lastIdx] != 0) {
2166 for (int32_t idx = 0; idx <= lastIdx; idx++) {
2167 if (sep && idx != 0) {
2170 result.append((UChar)(0x0030 + fields[idx]/10));
2171 result.append((UChar)(0x0030 + fields[idx]%10));
2178 TimeZoneFormat::parseAbuttingAsciiOffsetFields(const UnicodeString& text, ParsePosition& pos, OffsetFields minFields, OffsetFields maxFields, UBool fixedHourWidth) {
2179 int32_t start = pos.getIndex();
2181 int32_t minDigits = 2 * (minFields + 1) - (fixedHourWidth ? 0 : 1);
2182 int32_t maxDigits = 2 * (maxFields + 1);
2184 U_ASSERT(maxDigits <= MAX_OFFSET_DIGITS);
2186 int32_t digits[MAX_OFFSET_DIGITS] = {};
2187 int32_t numDigits = 0;
2188 int32_t idx = start;
2189 while (numDigits < maxDigits && idx < text.length()) {
2190 UChar uch = text.charAt(idx);
2191 int32_t digit = DIGIT_VAL(uch);
2195 digits[numDigits] = digit;
2200 if (fixedHourWidth && (numDigits & 1)) {
2201 // Fixed digits, so the number of digits must be even number. Truncating.
2205 if (numDigits < minDigits) {
2206 pos.setErrorIndex(start);
2210 int32_t hour = 0, min = 0, sec = 0;
2211 UBool bParsed = FALSE;
2212 while (numDigits >= minDigits) {
2213 switch (numDigits) {
2218 hour = digits[0] * 10 + digits[1];
2222 min = digits[1] * 10 + digits[2];
2225 hour = digits[0] * 10 + digits[1];
2226 min = digits[2] * 10 + digits[3];
2230 min = digits[1] * 10 + digits[2];
2231 sec = digits[3] * 10 + digits[4];
2234 hour = digits[0] * 10 + digits[1];
2235 min = digits[2] * 10 + digits[3];
2236 sec = digits[4] * 10 + digits[5];
2240 if (hour <= MAX_OFFSET_HOUR && min <= MAX_OFFSET_MINUTE && sec <= MAX_OFFSET_SECOND) {
2241 // Successfully parsed
2247 numDigits -= (fixedHourWidth ? 2 : 1);
2248 hour = min = sec = 0;
2252 pos.setErrorIndex(start);
2255 pos.setIndex(start + numDigits);
2256 return ((((hour * 60) + min) * 60) + sec) * 1000;
2260 TimeZoneFormat::parseAsciiOffsetFields(const UnicodeString& text, ParsePosition& pos, UChar sep, OffsetFields minFields, OffsetFields maxFields) {
2261 int32_t start = pos.getIndex();
2262 int32_t fieldVal[] = {0, 0, 0};
2263 int32_t fieldLen[] = {0, -1, -1};
2264 for (int32_t idx = start, fieldIdx = 0; idx < text.length() && fieldIdx <= maxFields; idx++) {
2265 UChar c = text.charAt(idx);
2267 if (fieldIdx == 0) {
2268 if (fieldLen[0] == 0) {
2272 // 1 digit hour, move to next field
2274 if (fieldLen[fieldIdx] != -1) {
2275 // premature minute or seconds field
2278 fieldLen[fieldIdx] = 0;
2281 } else if (fieldLen[fieldIdx] == -1) {
2282 // no separator after 2 digit field
2285 int32_t digit = DIGIT_VAL(c);
2290 fieldVal[fieldIdx] = fieldVal[fieldIdx] * 10 + digit;
2291 fieldLen[fieldIdx]++;
2292 if (fieldLen[fieldIdx] >= 2) {
2293 // parsed 2 digits, move to next field
2299 int32_t parsedLen = 0;
2300 int32_t parsedFields = -1;
2303 if (fieldLen[0] == 0) {
2306 if (fieldVal[0] > MAX_OFFSET_HOUR) {
2307 offset = (fieldVal[0] / 10) * MILLIS_PER_HOUR;
2308 parsedFields = FIELDS_H;
2312 offset = fieldVal[0] * MILLIS_PER_HOUR;
2313 parsedLen = fieldLen[0];
2314 parsedFields = FIELDS_H;
2317 if (fieldLen[1] != 2 || fieldVal[1] > MAX_OFFSET_MINUTE) {
2320 offset += fieldVal[1] * MILLIS_PER_MINUTE;
2321 parsedLen += (1 + fieldLen[1]);
2322 parsedFields = FIELDS_HM;
2325 if (fieldLen[2] != 2 || fieldVal[2] > MAX_OFFSET_SECOND) {
2328 offset += fieldVal[2] * MILLIS_PER_SECOND;
2329 parsedLen += (1 + fieldLen[2]);
2330 parsedFields = FIELDS_HMS;
2333 if (parsedFields < minFields) {
2334 pos.setErrorIndex(start);
2338 pos.setIndex(start + parsedLen);
2343 TimeZoneFormat::appendOffsetDigits(UnicodeString& buf, int32_t n, uint8_t minDigits) const {
2344 U_ASSERT(n >= 0 && n < 60);
2345 int32_t numDigits = n >= 10 ? 2 : 1;
2346 for (int32_t i = 0; i < minDigits - numDigits; i++) {
2347 buf.append(fGMTOffsetDigits[0]);
2349 if (numDigits == 2) {
2350 buf.append(fGMTOffsetDigits[n / 10]);
2352 buf.append(fGMTOffsetDigits[n % 10]);
2355 // ------------------------------------------------------------------
2358 TimeZoneFormat::initGMTPattern(const UnicodeString& gmtPattern, UErrorCode& status) {
2359 if (U_FAILURE(status)) {
2362 // This implementation not perfect, but sufficient practically.
2363 int32_t idx = gmtPattern.indexOf(ARG0, ARG0_LEN, 0);
2365 status = U_ILLEGAL_ARGUMENT_ERROR;
2368 fGMTPattern.setTo(gmtPattern);
2369 unquote(gmtPattern.tempSubString(0, idx), fGMTPatternPrefix);
2370 unquote(gmtPattern.tempSubString(idx + ARG0_LEN), fGMTPatternSuffix);
2374 TimeZoneFormat::unquote(const UnicodeString& pattern, UnicodeString& result) {
2375 if (pattern.indexOf(SINGLEQUOTE) < 0) {
2376 result.setTo(pattern);
2380 UBool isPrevQuote = FALSE;
2381 UBool inQuote = FALSE;
2382 for (int32_t i = 0; i < pattern.length(); i++) {
2383 UChar c = pattern.charAt(i);
2384 if (c == SINGLEQUOTE) {
2387 isPrevQuote = FALSE;
2393 isPrevQuote = FALSE;
2401 TimeZoneFormat::parseOffsetPattern(const UnicodeString& pattern, OffsetFields required, UErrorCode& status) {
2402 if (U_FAILURE(status)) {
2405 UVector* result = new UVector(deleteGMTOffsetField, NULL, status);
2406 if (result == NULL) {
2407 status = U_MEMORY_ALLOCATION_ERROR;
2411 int32_t checkBits = 0;
2412 UBool isPrevQuote = FALSE;
2413 UBool inQuote = FALSE;
2415 UnicodeString text(textBuf, 0, UPRV_LENGTHOF(textBuf));
2416 GMTOffsetField::FieldType itemType = GMTOffsetField::TEXT;
2417 int32_t itemLength = 1;
2419 for (int32_t i = 0; i < pattern.length(); i++) {
2420 UChar ch = pattern.charAt(i);
2421 if (ch == SINGLEQUOTE) {
2423 text.append(SINGLEQUOTE);
2424 isPrevQuote = FALSE;
2427 if (itemType != GMTOffsetField::TEXT) {
2428 if (GMTOffsetField::isValid(itemType, itemLength)) {
2429 GMTOffsetField* fld = GMTOffsetField::createTimeField(itemType, (uint8_t)itemLength, status);
2430 result->addElement(fld, status);
2431 if (U_FAILURE(status)) {
2435 status = U_ILLEGAL_ARGUMENT_ERROR;
2438 itemType = GMTOffsetField::TEXT;
2443 isPrevQuote = FALSE;
2447 GMTOffsetField::FieldType tmpType = GMTOffsetField::getTypeByLetter(ch);
2448 if (tmpType != GMTOffsetField::TEXT) {
2449 // an offset time pattern character
2450 if (tmpType == itemType) {
2453 if (itemType == GMTOffsetField::TEXT) {
2454 if (text.length() > 0) {
2455 GMTOffsetField* textfld = GMTOffsetField::createText(text, status);
2456 result->addElement(textfld, status);
2457 if (U_FAILURE(status)) {
2463 if (GMTOffsetField::isValid(itemType, itemLength)) {
2464 GMTOffsetField* fld = GMTOffsetField::createTimeField(itemType, itemLength, status);
2465 result->addElement(fld, status);
2466 if (U_FAILURE(status)) {
2470 status = U_ILLEGAL_ARGUMENT_ERROR;
2476 checkBits |= tmpType;
2480 if (itemType != GMTOffsetField::TEXT) {
2481 if (GMTOffsetField::isValid(itemType, itemLength)) {
2482 GMTOffsetField* fld = GMTOffsetField::createTimeField(itemType, itemLength, status);
2483 result->addElement(fld, status);
2484 if (U_FAILURE(status)) {
2488 status = U_ILLEGAL_ARGUMENT_ERROR;
2491 itemType = GMTOffsetField::TEXT;
2499 if (U_SUCCESS(status)) {
2500 if (itemType == GMTOffsetField::TEXT) {
2501 if (text.length() > 0) {
2502 GMTOffsetField* tfld = GMTOffsetField::createText(text, status);
2503 result->addElement(tfld, status);
2506 if (GMTOffsetField::isValid(itemType, itemLength)) {
2507 GMTOffsetField* fld = GMTOffsetField::createTimeField(itemType, itemLength, status);
2508 result->addElement(fld, status);
2510 status = U_ILLEGAL_ARGUMENT_ERROR;
2514 // Check all required fields are set
2515 if (U_SUCCESS(status)) {
2516 int32_t reqBits = 0;
2519 reqBits = GMTOffsetField::HOUR;
2522 reqBits = GMTOffsetField::HOUR | GMTOffsetField::MINUTE;
2525 reqBits = GMTOffsetField::HOUR | GMTOffsetField::MINUTE | GMTOffsetField::SECOND;
2528 if (checkBits == reqBits) {
2529 // all required fields are set, no extra fields
2541 TimeZoneFormat::expandOffsetPattern(const UnicodeString& offsetHM, UnicodeString& result, UErrorCode& status) {
2542 result.setToBogus();
2543 if (U_FAILURE(status)) {
2546 U_ASSERT(u_strlen(DEFAULT_GMT_OFFSET_MINUTE_PATTERN) == 2);
2548 int32_t idx_mm = offsetHM.indexOf(DEFAULT_GMT_OFFSET_MINUTE_PATTERN, 2, 0);
2550 // Bad time zone hour pattern data
2551 status = U_ILLEGAL_ARGUMENT_ERROR;
2556 int32_t idx_H = offsetHM.tempSubString(0, idx_mm).lastIndexOf((UChar)0x0048 /* H */);
2558 sep = offsetHM.tempSubString(idx_H + 1, idx_mm - (idx_H + 1));
2560 result.setTo(offsetHM.tempSubString(0, idx_mm + 2));
2562 result.append(DEFAULT_GMT_OFFSET_SECOND_PATTERN, -1);
2563 result.append(offsetHM.tempSubString(idx_mm + 2));
2568 TimeZoneFormat::truncateOffsetPattern(const UnicodeString& offsetHM, UnicodeString& result, UErrorCode& status) {
2569 result.setToBogus();
2570 if (U_FAILURE(status)) {
2573 U_ASSERT(u_strlen(DEFAULT_GMT_OFFSET_MINUTE_PATTERN) == 2);
2575 int32_t idx_mm = offsetHM.indexOf(DEFAULT_GMT_OFFSET_MINUTE_PATTERN, 2, 0);
2577 // Bad time zone hour pattern data
2578 status = U_ILLEGAL_ARGUMENT_ERROR;
2581 UChar HH[] = {0x0048, 0x0048};
2582 int32_t idx_HH = offsetHM.tempSubString(0, idx_mm).lastIndexOf(HH, 2, 0);
2584 return result.setTo(offsetHM.tempSubString(0, idx_HH + 2));
2586 int32_t idx_H = offsetHM.tempSubString(0, idx_mm).lastIndexOf((UChar)0x0048, 0);
2588 return result.setTo(offsetHM.tempSubString(0, idx_H + 1));
2590 // Bad time zone hour pattern data
2591 status = U_ILLEGAL_ARGUMENT_ERROR;
2596 TimeZoneFormat::initGMTOffsetPatterns(UErrorCode& status) {
2597 for (int32_t type = 0; type < UTZFMT_PAT_COUNT; type++) {
2599 case UTZFMT_PAT_POSITIVE_H:
2600 case UTZFMT_PAT_NEGATIVE_H:
2601 fGMTOffsetPatternItems[type] = parseOffsetPattern(fGMTOffsetPatterns[type], FIELDS_H, status);
2603 case UTZFMT_PAT_POSITIVE_HM:
2604 case UTZFMT_PAT_NEGATIVE_HM:
2605 fGMTOffsetPatternItems[type] = parseOffsetPattern(fGMTOffsetPatterns[type], FIELDS_HM, status);
2607 case UTZFMT_PAT_POSITIVE_HMS:
2608 case UTZFMT_PAT_NEGATIVE_HMS:
2609 fGMTOffsetPatternItems[type] = parseOffsetPattern(fGMTOffsetPatterns[type], FIELDS_HMS, status);
2613 checkAbuttingHoursAndMinutes();
2617 TimeZoneFormat::checkAbuttingHoursAndMinutes() {
2618 fAbuttingOffsetHoursAndMinutes= FALSE;
2619 for (int32_t type = 0; type < UTZFMT_PAT_COUNT; type++) {
2620 UBool afterH = FALSE;
2621 UVector *items = fGMTOffsetPatternItems[type];
2622 for (int32_t i = 0; i < items->size(); i++) {
2623 const GMTOffsetField* item = (GMTOffsetField*)items->elementAt(i);
2624 GMTOffsetField::FieldType type = item->getType();
2625 if (type != GMTOffsetField::TEXT) {
2627 fAbuttingOffsetHoursAndMinutes = TRUE;
2629 } else if (type == GMTOffsetField::HOUR) {
2632 } else if (afterH) {
2636 if (fAbuttingOffsetHoursAndMinutes) {
2643 TimeZoneFormat::toCodePoints(const UnicodeString& str, UChar32* codeArray, int32_t size) {
2644 int32_t count = str.countChar32();
2645 if (count != size) {
2649 for (int32_t idx = 0, start = 0; idx < size; idx++) {
2650 codeArray[idx] = str.char32At(start);
2651 start = str.moveIndex32(start, 1);
2658 TimeZoneFormat::createTimeZoneForOffset(int32_t offset) const {
2660 // when offset is 0, we should use "Etc/GMT"
2661 return TimeZone::createTimeZone(UnicodeString(TRUE, TZID_GMT, -1));
2663 return ZoneMeta::createCustomTimeZone(offset);
2666 UTimeZoneFormatTimeType
2667 TimeZoneFormat::getTimeType(UTimeZoneNameType nameType) {
2669 case UTZNM_LONG_STANDARD:
2670 case UTZNM_SHORT_STANDARD:
2671 return UTZFMT_TIME_TYPE_STANDARD;
2673 case UTZNM_LONG_DAYLIGHT:
2674 case UTZNM_SHORT_DAYLIGHT:
2675 return UTZFMT_TIME_TYPE_DAYLIGHT;
2678 return UTZFMT_TIME_TYPE_UNKNOWN;
2683 TimeZoneFormat::getTimeZoneID(const TimeZoneNames::MatchInfoCollection* matches, int32_t idx, UnicodeString& tzID) const {
2684 if (!matches->getTimeZoneIDAt(idx, tzID)) {
2686 UnicodeString mzID(mzIDBuf, 0, UPRV_LENGTHOF(mzIDBuf));
2687 if (matches->getMetaZoneIDAt(idx, mzID)) {
2688 fTimeZoneNames->getReferenceZoneID(mzID, fTargetRegion, tzID);
2695 class ZoneIdMatchHandler : public TextTrieMapSearchResultHandler {
2697 ZoneIdMatchHandler();
2698 virtual ~ZoneIdMatchHandler();
2700 UBool handleMatch(int32_t matchLength, const CharacterNode *node, UErrorCode &status);
2701 const UChar* getID();
2702 int32_t getMatchLen();
2708 ZoneIdMatchHandler::ZoneIdMatchHandler()
2709 : fLen(0), fID(NULL) {
2712 ZoneIdMatchHandler::~ZoneIdMatchHandler() {
2716 ZoneIdMatchHandler::handleMatch(int32_t matchLength, const CharacterNode *node, UErrorCode &status) {
2717 if (U_FAILURE(status)) {
2720 if (node->hasValues()) {
2721 const UChar* id = (const UChar*)node->getValue(0);
2723 if (fLen < matchLength) {
2733 ZoneIdMatchHandler::getID() {
2738 ZoneIdMatchHandler::getMatchLen() {
2743 static void U_CALLCONV initZoneIdTrie(UErrorCode &status) {
2744 U_ASSERT(gZoneIdTrie == NULL);
2745 ucln_i18n_registerCleanup(UCLN_I18N_TIMEZONEFORMAT, tzfmt_cleanup);
2746 gZoneIdTrie = new TextTrieMap(TRUE, NULL); // No deleter, because values are pooled by ZoneMeta
2747 if (gZoneIdTrie == NULL) {
2748 status = U_MEMORY_ALLOCATION_ERROR;
2751 StringEnumeration *tzenum = TimeZone::createEnumeration();
2752 const UnicodeString *id;
2753 while ((id = tzenum->snext(status))) {
2754 const UChar* uid = ZoneMeta::findTimeZoneID(*id);
2756 gZoneIdTrie->put(uid, const_cast<UChar *>(uid), status);
2764 TimeZoneFormat::parseZoneID(const UnicodeString& text, ParsePosition& pos, UnicodeString& tzID) const {
2765 UErrorCode status = U_ZERO_ERROR;
2766 umtx_initOnce(gZoneIdTrieInitOnce, &initZoneIdTrie, status);
2768 int32_t start = pos.getIndex();
2772 if (U_SUCCESS(status)) {
2773 LocalPointer<ZoneIdMatchHandler> handler(new ZoneIdMatchHandler());
2774 gZoneIdTrie->search(text, start, handler.getAlias(), status);
2775 len = handler->getMatchLen();
2777 tzID.setTo(handler->getID(), -1);
2782 pos.setIndex(start + len);
2784 pos.setErrorIndex(start);
2790 static void U_CALLCONV initShortZoneIdTrie(UErrorCode &status) {
2791 U_ASSERT(gShortZoneIdTrie == NULL);
2792 ucln_i18n_registerCleanup(UCLN_I18N_TIMEZONEFORMAT, tzfmt_cleanup);
2793 StringEnumeration *tzenum = TimeZone::createTimeZoneIDEnumeration(UCAL_ZONE_TYPE_CANONICAL, NULL, NULL, status);
2794 if (U_SUCCESS(status)) {
2795 gShortZoneIdTrie = new TextTrieMap(TRUE, NULL); // No deleter, because values are pooled by ZoneMeta
2796 if (gShortZoneIdTrie == NULL) {
2797 status = U_MEMORY_ALLOCATION_ERROR;
2799 const UnicodeString *id;
2800 while ((id = tzenum->snext(status))) {
2801 const UChar* uID = ZoneMeta::findTimeZoneID(*id);
2802 const UChar* shortID = ZoneMeta::getShortID(*id);
2803 if (shortID && uID) {
2804 gShortZoneIdTrie->put(shortID, const_cast<UChar *>(uID), status);
2814 TimeZoneFormat::parseShortZoneID(const UnicodeString& text, ParsePosition& pos, UnicodeString& tzID) const {
2815 UErrorCode status = U_ZERO_ERROR;
2816 umtx_initOnce(gShortZoneIdTrieInitOnce, &initShortZoneIdTrie, status);
2818 int32_t start = pos.getIndex();
2822 if (U_SUCCESS(status)) {
2823 LocalPointer<ZoneIdMatchHandler> handler(new ZoneIdMatchHandler());
2824 gShortZoneIdTrie->search(text, start, handler.getAlias(), status);
2825 len = handler->getMatchLen();
2827 tzID.setTo(handler->getID(), -1);
2832 pos.setIndex(start + len);
2834 pos.setErrorIndex(start);
2842 TimeZoneFormat::parseExemplarLocation(const UnicodeString& text, ParsePosition& pos, UnicodeString& tzID) const {
2843 int32_t startIdx = pos.getIndex();
2844 int32_t parsedPos = -1;
2847 UErrorCode status = U_ZERO_ERROR;
2848 LocalPointer<TimeZoneNames::MatchInfoCollection> exemplarMatches(fTimeZoneNames->find(text, startIdx, UTZNM_EXEMPLAR_LOCATION, status));
2849 if (U_FAILURE(status)) {
2850 pos.setErrorIndex(startIdx);
2853 int32_t matchIdx = -1;
2854 if (!exemplarMatches.isNull()) {
2855 for (int32_t i = 0; i < exemplarMatches->size(); i++) {
2856 if (startIdx + exemplarMatches->getMatchLengthAt(i) > parsedPos) {
2858 parsedPos = startIdx + exemplarMatches->getMatchLengthAt(i);
2861 if (parsedPos > 0) {
2862 pos.setIndex(parsedPos);
2863 getTimeZoneID(exemplarMatches.getAlias(), matchIdx, tzID);
2867 if (tzID.length() == 0) {
2868 pos.setErrorIndex(startIdx);