tzfmt.cpp 96 KB

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