tzfmt.h 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098
  1. /*
  2. *******************************************************************************
  3. * Copyright (C) 2011-2014, International Business Machines Corporation and
  4. * others. All Rights Reserved.
  5. *******************************************************************************
  6. */
  7. #ifndef __TZFMT_H
  8. #define __TZFMT_H
  9. /**
  10. * \file
  11. * \brief C++ API: TimeZoneFormat
  12. */
  13. #include "unicode/utypes.h"
  14. #if !UCONFIG_NO_FORMATTING
  15. #include "unicode/format.h"
  16. #include "unicode/timezone.h"
  17. #include "unicode/tznames.h"
  18. U_CDECL_BEGIN
  19. /**
  20. * Constants for time zone display format style used by format/parse APIs
  21. * in TimeZoneFormat.
  22. * @stable ICU 50
  23. */
  24. typedef enum UTimeZoneFormatStyle {
  25. /**
  26. * Generic location format, such as "United States Time (New York)", "Italy Time"
  27. * @stable ICU 50
  28. */
  29. UTZFMT_STYLE_GENERIC_LOCATION,
  30. /**
  31. * Generic long non-location format, such as "Eastern Time".
  32. * @stable ICU 50
  33. */
  34. UTZFMT_STYLE_GENERIC_LONG,
  35. /**
  36. * Generic short non-location format, such as "ET".
  37. * @stable ICU 50
  38. */
  39. UTZFMT_STYLE_GENERIC_SHORT,
  40. /**
  41. * Specific long format, such as "Eastern Standard Time".
  42. * @stable ICU 50
  43. */
  44. UTZFMT_STYLE_SPECIFIC_LONG,
  45. /**
  46. * Specific short format, such as "EST", "PDT".
  47. * @stable ICU 50
  48. */
  49. UTZFMT_STYLE_SPECIFIC_SHORT,
  50. /**
  51. * Localized GMT offset format, such as "GMT-05:00", "UTC+0100"
  52. * @stable ICU 50
  53. */
  54. UTZFMT_STYLE_LOCALIZED_GMT,
  55. /**
  56. * Short localized GMT offset format, such as "GMT-5", "UTC+1:30"
  57. * This style is equivalent to the LDML date format pattern "O".
  58. * @stable ICU 51
  59. */
  60. UTZFMT_STYLE_LOCALIZED_GMT_SHORT,
  61. /**
  62. * Short ISO 8601 local time difference (basic format) or the UTC indicator.
  63. * For example, "-05", "+0530", and "Z"(UTC).
  64. * This style is equivalent to the LDML date format pattern "X".
  65. * @stable ICU 51
  66. */
  67. UTZFMT_STYLE_ISO_BASIC_SHORT,
  68. /**
  69. * Short ISO 8601 locale time difference (basic format).
  70. * For example, "-05" and "+0530".
  71. * This style is equivalent to the LDML date format pattern "x".
  72. * @stable ICU 51
  73. */
  74. UTZFMT_STYLE_ISO_BASIC_LOCAL_SHORT,
  75. /**
  76. * Fixed width ISO 8601 local time difference (basic format) or the UTC indicator.
  77. * For example, "-0500", "+0530", and "Z"(UTC).
  78. * This style is equivalent to the LDML date format pattern "XX".
  79. * @stable ICU 51
  80. */
  81. UTZFMT_STYLE_ISO_BASIC_FIXED,
  82. /**
  83. * Fixed width ISO 8601 local time difference (basic format).
  84. * For example, "-0500" and "+0530".
  85. * This style is equivalent to the LDML date format pattern "xx".
  86. * @stable ICU 51
  87. */
  88. UTZFMT_STYLE_ISO_BASIC_LOCAL_FIXED,
  89. /**
  90. * ISO 8601 local time difference (basic format) with optional seconds field, or the UTC indicator.
  91. * For example, "-0500", "+052538", and "Z"(UTC).
  92. * This style is equivalent to the LDML date format pattern "XXXX".
  93. * @stable ICU 51
  94. */
  95. UTZFMT_STYLE_ISO_BASIC_FULL,
  96. /**
  97. * ISO 8601 local time difference (basic format) with optional seconds field.
  98. * For example, "-0500" and "+052538".
  99. * This style is equivalent to the LDML date format pattern "xxxx".
  100. * @stable ICU 51
  101. */
  102. UTZFMT_STYLE_ISO_BASIC_LOCAL_FULL,
  103. /**
  104. * Fixed width ISO 8601 local time difference (extended format) or the UTC indicator.
  105. * For example, "-05:00", "+05:30", and "Z"(UTC).
  106. * This style is equivalent to the LDML date format pattern "XXX".
  107. * @stable ICU 51
  108. */
  109. UTZFMT_STYLE_ISO_EXTENDED_FIXED,
  110. /**
  111. * Fixed width ISO 8601 local time difference (extended format).
  112. * For example, "-05:00" and "+05:30".
  113. * This style is equivalent to the LDML date format pattern "xxx" and "ZZZZZ".
  114. * @stable ICU 51
  115. */
  116. UTZFMT_STYLE_ISO_EXTENDED_LOCAL_FIXED,
  117. /**
  118. * ISO 8601 local time difference (extended format) with optional seconds field, or the UTC indicator.
  119. * For example, "-05:00", "+05:25:38", and "Z"(UTC).
  120. * This style is equivalent to the LDML date format pattern "XXXXX".
  121. * @stable ICU 51
  122. */
  123. UTZFMT_STYLE_ISO_EXTENDED_FULL,
  124. /**
  125. * ISO 8601 local time difference (extended format) with optional seconds field.
  126. * For example, "-05:00" and "+05:25:38".
  127. * This style is equivalent to the LDML date format pattern "xxxxx".
  128. * @stable ICU 51
  129. */
  130. UTZFMT_STYLE_ISO_EXTENDED_LOCAL_FULL,
  131. /**
  132. * Time Zone ID, such as "America/Los_Angeles".
  133. * @stable ICU 51
  134. */
  135. UTZFMT_STYLE_ZONE_ID,
  136. /**
  137. * Short Time Zone ID (BCP 47 Unicode location extension, time zone type value), such as "uslax".
  138. * @stable ICU 51
  139. */
  140. UTZFMT_STYLE_ZONE_ID_SHORT,
  141. /**
  142. * Exemplar location, such as "Los Angeles" and "Paris".
  143. * @stable ICU 51
  144. */
  145. UTZFMT_STYLE_EXEMPLAR_LOCATION
  146. } UTimeZoneFormatStyle;
  147. /**
  148. * Constants for GMT offset pattern types.
  149. * @stable ICU 50
  150. */
  151. typedef enum UTimeZoneFormatGMTOffsetPatternType {
  152. /**
  153. * Positive offset with hours and minutes fields
  154. * @stable ICU 50
  155. */
  156. UTZFMT_PAT_POSITIVE_HM,
  157. /**
  158. * Positive offset with hours, minutes and seconds fields
  159. * @stable ICU 50
  160. */
  161. UTZFMT_PAT_POSITIVE_HMS,
  162. /**
  163. * Negative offset with hours and minutes fields
  164. * @stable ICU 50
  165. */
  166. UTZFMT_PAT_NEGATIVE_HM,
  167. /**
  168. * Negative offset with hours, minutes and seconds fields
  169. * @stable ICU 50
  170. */
  171. UTZFMT_PAT_NEGATIVE_HMS,
  172. /**
  173. * Positive offset with hours field
  174. * @stable ICU 51
  175. */
  176. UTZFMT_PAT_POSITIVE_H,
  177. /**
  178. * Negative offset with hours field
  179. * @stable ICU 51
  180. */
  181. UTZFMT_PAT_NEGATIVE_H,
  182. /* The following cannot be #ifndef U_HIDE_INTERNAL_API, needed for other .h declarations */
  183. /**
  184. * Number of UTimeZoneFormatGMTOffsetPatternType types.
  185. * @internal
  186. */
  187. UTZFMT_PAT_COUNT = 6
  188. } UTimeZoneFormatGMTOffsetPatternType;
  189. /**
  190. * Constants for time types used by TimeZoneFormat APIs for
  191. * receiving time type (standard time, daylight time or unknown).
  192. * @stable ICU 50
  193. */
  194. typedef enum UTimeZoneFormatTimeType {
  195. /**
  196. * Unknown
  197. * @stable ICU 50
  198. */
  199. UTZFMT_TIME_TYPE_UNKNOWN,
  200. /**
  201. * Standard time
  202. * @stable ICU 50
  203. */
  204. UTZFMT_TIME_TYPE_STANDARD,
  205. /**
  206. * Daylight saving time
  207. * @stable ICU 50
  208. */
  209. UTZFMT_TIME_TYPE_DAYLIGHT
  210. } UTimeZoneFormatTimeType;
  211. /**
  212. * Constants for parse option flags, used for specifying optional parse behavior.
  213. * @stable ICU 50
  214. */
  215. typedef enum UTimeZoneFormatParseOption {
  216. /**
  217. * No option.
  218. * @stable ICU 50
  219. */
  220. UTZFMT_PARSE_OPTION_NONE = 0x00,
  221. /**
  222. * When a time zone display name is not found within a set of display names
  223. * used for the specified style, look for the name from display names used
  224. * by other styles.
  225. * @stable ICU 50
  226. */
  227. UTZFMT_PARSE_OPTION_ALL_STYLES = 0x01
  228. #ifndef U_HIDE_DRAFT_API
  229. ,
  230. /**
  231. * When parsing a time zone display name in UTZFMT_STYLE_SPECIFIC_SHORT,
  232. * look for the IANA tz database compatible zone abbreviations in addition
  233. * to the localized names coming from the {@link TimeZoneNames} currently
  234. * used by the {@link TimeZoneFormat}.
  235. * @draft ICU 54
  236. */
  237. UTZFMT_PARSE_OPTION_TZ_DATABASE_ABBREVIATIONS = 0x02
  238. #endif /* U_HIDE_DRAFT_API */
  239. } UTimeZoneFormatParseOption;
  240. U_CDECL_END
  241. U_NAMESPACE_BEGIN
  242. class TimeZoneGenericNames;
  243. class TZDBTimeZoneNames;
  244. class UVector;
  245. /**
  246. * <code>TimeZoneFormat</code> supports time zone display name formatting and parsing.
  247. * An instance of TimeZoneFormat works as a subformatter of {@link SimpleDateFormat},
  248. * but you can also directly get a new instance of <code>TimeZoneFormat</code> and
  249. * formatting/parsing time zone display names.
  250. * <p>
  251. * ICU implements the time zone display names defined by <a href="http://www.unicode.org/reports/tr35/">UTS#35
  252. * Unicode Locale Data Markup Language (LDML)</a>. {@link TimeZoneNames} represents the
  253. * time zone display name data model and this class implements the algorithm for actual
  254. * formatting and parsing.
  255. *
  256. * @see SimpleDateFormat
  257. * @see TimeZoneNames
  258. * @stable ICU 50
  259. */
  260. class U_I18N_API TimeZoneFormat : public Format {
  261. public:
  262. /**
  263. * Copy constructor.
  264. * @stable ICU 50
  265. */
  266. TimeZoneFormat(const TimeZoneFormat& other);
  267. /**
  268. * Destructor.
  269. * @stable ICU 50
  270. */
  271. virtual ~TimeZoneFormat();
  272. /**
  273. * Assignment operator.
  274. * @stable ICU 50
  275. */
  276. TimeZoneFormat& operator=(const TimeZoneFormat& other);
  277. /**
  278. * Return true if the given Format objects are semantically equal.
  279. * Objects of different subclasses are considered unequal.
  280. * @param other The object to be compared with.
  281. * @return Return TRUE if the given Format objects are semantically equal.
  282. * Objects of different subclasses are considered unequal.
  283. * @stable ICU 50
  284. */
  285. virtual UBool operator==(const Format& other) const;
  286. /**
  287. * Clone this object polymorphically. The caller is responsible
  288. * for deleting the result when done.
  289. * @return A copy of the object
  290. * @stable ICU 50
  291. */
  292. virtual Format* clone() const;
  293. /**
  294. * Creates an instance of <code>TimeZoneFormat</code> for the given locale.
  295. * @param locale The locale.
  296. * @param status Receives the status.
  297. * @return An instance of <code>TimeZoneFormat</code> for the given locale,
  298. * owned by the caller.
  299. * @stable ICU 50
  300. */
  301. static TimeZoneFormat* U_EXPORT2 createInstance(const Locale& locale, UErrorCode& status);
  302. /**
  303. * Returns the time zone display name data used by this instance.
  304. * @return The time zone display name data.
  305. * @stable ICU 50
  306. */
  307. const TimeZoneNames* getTimeZoneNames() const;
  308. /**
  309. * Sets the time zone display name data to this format instnace.
  310. * The caller should not delete the TimeZoenNames object after it is adopted
  311. * by this call.
  312. * @param tznames TimeZoneNames object to be adopted.
  313. * @stable ICU 50
  314. */
  315. void adoptTimeZoneNames(TimeZoneNames *tznames);
  316. /**
  317. * Sets the time zone display name data to this format instnace.
  318. * @param tznames TimeZoneNames object to be set.
  319. * @stable ICU 50
  320. */
  321. void setTimeZoneNames(const TimeZoneNames &tznames);
  322. /**
  323. * Returns the localized GMT format pattern.
  324. * @param pattern Receives the localized GMT format pattern.
  325. * @return A reference to the result pattern.
  326. * @see #setGMTPattern
  327. * @stable ICU 50
  328. */
  329. UnicodeString& getGMTPattern(UnicodeString& pattern) const;
  330. /**
  331. * Sets the localized GMT format pattern. The pattern must contain
  332. * a single argument {0}, for example "GMT {0}".
  333. * @param pattern The localized GMT format pattern to be used by this object.
  334. * @param status Recieves the status.
  335. * @see #getGMTPattern
  336. * @stable ICU 50
  337. */
  338. void setGMTPattern(const UnicodeString& pattern, UErrorCode& status);
  339. /**
  340. * Returns the offset pattern used for localized GMT format.
  341. * @param type The offset pattern type enum.
  342. * @param pattern Receives the offset pattern.
  343. * @return A reference to the result pattern.
  344. * @see #setGMTOffsetPattern
  345. * @stable ICU 50
  346. */
  347. UnicodeString& getGMTOffsetPattern(UTimeZoneFormatGMTOffsetPatternType type, UnicodeString& pattern) const;
  348. /**
  349. * Sets the offset pattern for the given offset type.
  350. * @param type The offset pattern type enum.
  351. * @param pattern The offset pattern used for localized GMT format for the type.
  352. * @param status Receives the status.
  353. * @see #getGMTOffsetPattern
  354. * @stable ICU 50
  355. */
  356. void setGMTOffsetPattern(UTimeZoneFormatGMTOffsetPatternType type, const UnicodeString& pattern, UErrorCode& status);
  357. /**
  358. * Returns the decimal digit characters used for localized GMT format.
  359. * The return string contains exactly 10 code points (may include Unicode
  360. * supplementary character) representing digit 0 to digit 9 in the ascending
  361. * order.
  362. * @param digits Receives the decimal digits used for localized GMT format.
  363. * @see #setGMTOffsetDigits
  364. * @stable ICU 50
  365. */
  366. UnicodeString& getGMTOffsetDigits(UnicodeString& digits) const;
  367. /**
  368. * Sets the decimal digit characters used for localized GMT format.
  369. * The input <code>digits</code> must contain exactly 10 code points
  370. * (Unicode supplementary characters are also allowed) representing
  371. * digit 0 to digit 9 in the ascending order. When the input <code>digits</code>
  372. * does not satisfy the condition, <code>U_ILLEGAL_ARGUMENT_ERROR</code>
  373. * will be set to the return status.
  374. * @param digits The decimal digits used for localized GMT format.
  375. * @param status Receives the status.
  376. * @see #getGMTOffsetDigits
  377. * @stable ICU 50
  378. */
  379. void setGMTOffsetDigits(const UnicodeString& digits, UErrorCode& status);
  380. /**
  381. * Returns the localized GMT format string for GMT(UTC) itself (GMT offset is 0).
  382. * @param gmtZeroFormat Receives the localized GMT string string for GMT(UTC) itself.
  383. * @return A reference to the result GMT string.
  384. * @see #setGMTZeroFormat
  385. * @stable ICU 50
  386. */
  387. UnicodeString& getGMTZeroFormat(UnicodeString& gmtZeroFormat) const;
  388. /**
  389. * Sets the localized GMT format string for GMT(UTC) itself (GMT offset is 0).
  390. * @param gmtZeroFormat The localized GMT format string for GMT(UTC).
  391. * @param status Receives the status.
  392. * @see #getGMTZeroFormat
  393. * @stable ICU 50
  394. */
  395. void setGMTZeroFormat(const UnicodeString& gmtZeroFormat, UErrorCode& status);
  396. /**
  397. * Returns the bitwise flags of UTimeZoneFormatParseOption representing the default parse
  398. * options used by this object.
  399. * @return the default parse options.
  400. * @see ParseOption
  401. * @stable ICU 50
  402. */
  403. uint32_t getDefaultParseOptions(void) const;
  404. /**
  405. * Sets the default parse options.
  406. * <p><b>Note</b>: By default, an instance of <code>TimeZoneFormat</code>
  407. * created by {@link #createInstance} has no parse options set (UTZFMT_PARSE_OPTION_NONE).
  408. * To specify multipe options, use bitwise flags of UTimeZoneFormatParseOption.
  409. * @see #UTimeZoneFormatParseOption
  410. * @stable ICU 50
  411. */
  412. void setDefaultParseOptions(uint32_t flags);
  413. /**
  414. * Returns the ISO 8601 basic time zone string for the given offset.
  415. * For example, "-08", "-0830" and "Z"
  416. *
  417. * @param offset the offset from GMT(UTC) in milliseconds.
  418. * @param useUtcIndicator true if ISO 8601 UTC indicator "Z" is used when the offset is 0.
  419. * @param isShort true if shortest form is used.
  420. * @param ignoreSeconds true if non-zero offset seconds is appended.
  421. * @param result Receives the ISO format string.
  422. * @param status Receives the status
  423. * @return the ISO 8601 basic format.
  424. * @see #formatOffsetISO8601Extended
  425. * @see #parseOffsetISO8601
  426. * @stable ICU 51
  427. */
  428. UnicodeString& formatOffsetISO8601Basic(int32_t offset, UBool useUtcIndicator, UBool isShort, UBool ignoreSeconds,
  429. UnicodeString& result, UErrorCode& status) const;
  430. /**
  431. * Returns the ISO 8601 extended time zone string for the given offset.
  432. * For example, "-08:00", "-08:30" and "Z"
  433. *
  434. * @param offset the offset from GMT(UTC) in milliseconds.
  435. * @param useUtcIndicator true if ISO 8601 UTC indicator "Z" is used when the offset is 0.
  436. * @param isShort true if shortest form is used.
  437. * @param ignoreSeconds true if non-zero offset seconds is appended.
  438. * @param result Receives the ISO format string.
  439. * @param status Receives the status
  440. * @return the ISO 8601 basic format.
  441. * @see #formatOffsetISO8601Extended
  442. * @see #parseOffsetISO8601
  443. * @stable ICU 51
  444. */
  445. UnicodeString& formatOffsetISO8601Extended(int32_t offset, UBool useUtcIndicator, UBool isShort, UBool ignoreSeconds,
  446. UnicodeString& result, UErrorCode& status) const;
  447. /**
  448. * Returns the localized GMT(UTC) offset format for the given offset.
  449. * The localized GMT offset is defined by;
  450. * <ul>
  451. * <li>GMT format pattern (e.g. "GMT {0}" - see {@link #getGMTPattern})
  452. * <li>Offset time pattern (e.g. "+HH:mm" - see {@link #getGMTOffsetPattern})
  453. * <li>Offset digits (e.g. "0123456789" - see {@link #getGMTOffsetDigits})
  454. * <li>GMT zero format (e.g. "GMT" - see {@link #getGMTZeroFormat})
  455. * </ul>
  456. * This format always uses 2 digit hours and minutes. When the given offset has non-zero
  457. * seconds, 2 digit seconds field will be appended. For example,
  458. * GMT+05:00 and GMT+05:28:06.
  459. * @param offset the offset from GMT(UTC) in milliseconds.
  460. * @param status Receives the status
  461. * @param result Receives the localized GMT format string.
  462. * @return A reference to the result.
  463. * @see #parseOffsetLocalizedGMT
  464. * @stable ICU 50
  465. */
  466. UnicodeString& formatOffsetLocalizedGMT(int32_t offset, UnicodeString& result, UErrorCode& status) const;
  467. /**
  468. * Returns the short localized GMT(UTC) offset format for the given offset.
  469. * The short localized GMT offset is defined by;
  470. * <ul>
  471. * <li>GMT format pattern (e.g. "GMT {0}" - see {@link #getGMTPattern})
  472. * <li>Offset time pattern (e.g. "+HH:mm" - see {@link #getGMTOffsetPattern})
  473. * <li>Offset digits (e.g. "0123456789" - see {@link #getGMTOffsetDigits})
  474. * <li>GMT zero format (e.g. "GMT" - see {@link #getGMTZeroFormat})
  475. * </ul>
  476. * This format uses the shortest representation of offset. The hours field does not
  477. * have leading zero and lower fields with zero will be truncated. For example,
  478. * GMT+5 and GMT+530.
  479. * @param offset the offset from GMT(UTC) in milliseconds.
  480. * @param status Receives the status
  481. * @param result Receives the short localized GMT format string.
  482. * @return A reference to the result.
  483. * @see #parseOffsetShortLocalizedGMT
  484. * @stable ICU 51
  485. */
  486. UnicodeString& formatOffsetShortLocalizedGMT(int32_t offset, UnicodeString& result, UErrorCode& status) const;
  487. using Format::format;
  488. /**
  489. * Returns the display name of the time zone at the given date for the style.
  490. * @param style The style (e.g. <code>UTZFMT_STYLE_GENERIC_LONG</code>, <code>UTZFMT_STYLE_LOCALIZED_GMT</code>...)
  491. * @param tz The time zone.
  492. * @param date The date.
  493. * @param name Receives the display name.
  494. * @param timeType the output argument for receiving the time type (standard/daylight/unknown)
  495. * used for the display name, or NULL if the information is not necessary.
  496. * @return A reference to the result
  497. * @see #UTimeZoneFormatStyle
  498. * @see #UTimeZoneFormatTimeType
  499. * @stable ICU 50
  500. */
  501. virtual UnicodeString& format(UTimeZoneFormatStyle style, const TimeZone& tz, UDate date,
  502. UnicodeString& name, UTimeZoneFormatTimeType* timeType = NULL) const;
  503. /**
  504. * Returns offset from GMT(UTC) in milliseconds for the given ISO 8601
  505. * style time zone string. When the given string is not an ISO 8601 time zone
  506. * string, this method sets the current position as the error index
  507. * to <code>ParsePosition pos</code> and returns 0.
  508. * @param text The text contains ISO8601 style time zone string (e.g. "-08:00", "Z")
  509. * at the position.
  510. * @param pos The ParsePosition object.
  511. * @return The offset from GMT(UTC) in milliseconds for the given ISO 8601 style
  512. * time zone string.
  513. * @see #formatOffsetISO8601Basic
  514. * @see #formatOffsetISO8601Extended
  515. * @stable ICU 50
  516. */
  517. int32_t parseOffsetISO8601(const UnicodeString& text, ParsePosition& pos) const;
  518. /**
  519. * Returns offset from GMT(UTC) in milliseconds for the given localized GMT
  520. * offset format string. When the given string cannot be parsed, this method
  521. * sets the current position as the error index to <code>ParsePosition pos</code>
  522. * and returns 0.
  523. * @param text The text contains a localized GMT offset string at the position.
  524. * @param pos The ParsePosition object.
  525. * @return The offset from GMT(UTC) in milliseconds for the given localized GMT
  526. * offset format string.
  527. * @see #formatOffsetLocalizedGMT
  528. * @stable ICU 50
  529. */
  530. int32_t parseOffsetLocalizedGMT(const UnicodeString& text, ParsePosition& pos) const;
  531. /**
  532. * Returns offset from GMT(UTC) in milliseconds for the given short localized GMT
  533. * offset format string. When the given string cannot be parsed, this method
  534. * sets the current position as the error index to <code>ParsePosition pos</code>
  535. * and returns 0.
  536. * @param text The text contains a short localized GMT offset string at the position.
  537. * @param pos The ParsePosition object.
  538. * @return The offset from GMT(UTC) in milliseconds for the given short localized GMT
  539. * offset format string.
  540. * @see #formatOffsetShortLocalizedGMT
  541. * @stable ICU 51
  542. */
  543. int32_t parseOffsetShortLocalizedGMT(const UnicodeString& text, ParsePosition& pos) const;
  544. /**
  545. * Returns a <code>TimeZone</code> by parsing the time zone string according to
  546. * the given parse position, the specified format style and parse options.
  547. *
  548. * @param text The text contains a time zone string at the position.
  549. * @param style The format style
  550. * @param pos The position.
  551. * @param parseOptions The parse options repesented by bitwise flags of UTimeZoneFormatParseOption.
  552. * @param timeType The output argument for receiving the time type (standard/daylight/unknown),
  553. * or NULL if the information is not necessary.
  554. * @return A <code>TimeZone</code>, or null if the input could not be parsed.
  555. * @see UTimeZoneFormatStyle
  556. * @see UTimeZoneFormatParseOption
  557. * @see UTimeZoneFormatTimeType
  558. * @stable ICU 50
  559. */
  560. virtual TimeZone* parse(UTimeZoneFormatStyle style, const UnicodeString& text, ParsePosition& pos,
  561. int32_t parseOptions, UTimeZoneFormatTimeType* timeType = NULL) const;
  562. /**
  563. * Returns a <code>TimeZone</code> by parsing the time zone string according to
  564. * the given parse position, the specified format style and the default parse options.
  565. *
  566. * @param text The text contains a time zone string at the position.
  567. * @param style The format style
  568. * @param pos The position.
  569. * @param timeType The output argument for receiving the time type (standard/daylight/unknown),
  570. * or NULL if the information is not necessary.
  571. * @return A <code>TimeZone</code>, or null if the input could not be parsed.
  572. * @see UTimeZoneFormatStyle
  573. * @see UTimeZoneFormatParseOption
  574. * @see UTimeZoneFormatTimeType
  575. * @stable ICU 50
  576. */
  577. TimeZone* parse(UTimeZoneFormatStyle style, const UnicodeString& text, ParsePosition& pos,
  578. UTimeZoneFormatTimeType* timeType = NULL) const;
  579. /* ----------------------------------------------
  580. * Format APIs
  581. * ---------------------------------------------- */
  582. /**
  583. * Format an object to produce a time zone display string using localized GMT offset format.
  584. * This method handles Formattable objects with a <code>TimeZone</code>. If a the Formattable
  585. * object type is not a <code>TimeZone</code>, then it returns a failing UErrorCode.
  586. * @param obj The object to format. Must be a <code>TimeZone</code>.
  587. * @param appendTo Output parameter to receive result. Result is appended to existing contents.
  588. * @param pos On input: an alignment field, if desired. On output: the offsets of the alignment field.
  589. * @param status Output param filled with success/failure status.
  590. * @return Reference to 'appendTo' parameter.
  591. * @stable ICU 50
  592. */
  593. virtual UnicodeString& format(const Formattable& obj, UnicodeString& appendTo,
  594. FieldPosition& pos, UErrorCode& status) const;
  595. /**
  596. * Parse a string to produce an object. This methods handles parsing of
  597. * time zone display strings into Formattable objects with <code>TimeZone</code>.
  598. * @param source The string to be parsed into an object.
  599. * @param result Formattable to be set to the parse result. If parse fails, return contents are undefined.
  600. * @param parse_pos The position to start parsing at. Upon return this param is set to the position after the
  601. * last character successfully parsed. If the source is not parsed successfully, this param
  602. * will remain unchanged.
  603. * @return A newly created Formattable* object, or NULL on failure. The caller owns this and should
  604. * delete it when done.
  605. * @stable ICU 50
  606. */
  607. virtual void parseObject(const UnicodeString& source, Formattable& result, ParsePosition& parse_pos) const;
  608. /**
  609. * ICU "poor man's RTTI", returns a UClassID for this class.
  610. * @stable ICU 50
  611. */
  612. static UClassID U_EXPORT2 getStaticClassID(void);
  613. /**
  614. * ICU "poor man's RTTI", returns a UClassID for the actual class.
  615. * @stable ICU 50
  616. */
  617. virtual UClassID getDynamicClassID() const;
  618. protected:
  619. /**
  620. * Constructs a TimeZoneFormat object for the specified locale.
  621. * @param locale the locale
  622. * @param status receives the status.
  623. * @stable ICU 50
  624. */
  625. TimeZoneFormat(const Locale& locale, UErrorCode& status);
  626. private:
  627. /* Locale of this object */
  628. Locale fLocale;
  629. /* Stores the region (could be implicit default) */
  630. char fTargetRegion[ULOC_COUNTRY_CAPACITY];
  631. /* TimeZoneNames object used by this formatter */
  632. TimeZoneNames* fTimeZoneNames;
  633. /* TimeZoneGenericNames object used by this formatter - lazily instantiated */
  634. TimeZoneGenericNames* fTimeZoneGenericNames;
  635. /* Localized GMT format pattern - e.g. "GMT{0}" */
  636. UnicodeString fGMTPattern;
  637. /* Array of offset patterns used by Localized GMT format - e.g. "+HH:mm" */
  638. UnicodeString fGMTOffsetPatterns[UTZFMT_PAT_COUNT];
  639. /* Localized decimal digits used by Localized GMT format */
  640. UChar32 fGMTOffsetDigits[10];
  641. /* Localized GMT zero format - e.g. "GMT" */
  642. UnicodeString fGMTZeroFormat;
  643. /* Bit flags representing parse options */
  644. uint32_t fDefParseOptionFlags;
  645. /* Constant parts of GMT format pattern, populated from localized GMT format pattern*/
  646. UnicodeString fGMTPatternPrefix; /* Substring before {0} */
  647. UnicodeString fGMTPatternSuffix; /* Substring after {0} */
  648. /* Compiled offset patterns generated from fGMTOffsetPatterns[] */
  649. UVector* fGMTOffsetPatternItems[UTZFMT_PAT_COUNT];
  650. UBool fAbuttingOffsetHoursAndMinutes;
  651. /* TZDBTimeZoneNames object used for parsing */
  652. TZDBTimeZoneNames* fTZDBTimeZoneNames;
  653. /**
  654. * Returns the time zone's specific format string.
  655. * @param tz the time zone
  656. * @param stdType the name type used for standard time
  657. * @param dstType the name type used for daylight time
  658. * @param date the date
  659. * @param name receives the time zone's specific format name string
  660. * @param timeType when null, actual time type is set
  661. * @return a reference to name.
  662. */
  663. UnicodeString& formatSpecific(const TimeZone& tz, UTimeZoneNameType stdType, UTimeZoneNameType dstType,
  664. UDate date, UnicodeString& name, UTimeZoneFormatTimeType *timeType) const;
  665. /**
  666. * Returns the time zone's generic format string.
  667. * @param tz the time zone
  668. * @param genType the generic name type
  669. * @param date the date
  670. * @param name receives the time zone's generic format name string
  671. * @return a reference to name.
  672. */
  673. UnicodeString& formatGeneric(const TimeZone& tz, int32_t genType, UDate date, UnicodeString& name) const;
  674. /**
  675. * Lazily create a TimeZoneGenericNames instance
  676. * @param status receives the status
  677. * @return the cached TimeZoneGenericNames.
  678. */
  679. const TimeZoneGenericNames* getTimeZoneGenericNames(UErrorCode& status) const;
  680. /**
  681. * Lazily create a TZDBTimeZoneNames instance
  682. * @param status receives the status
  683. * @return the cached TZDBTimeZoneNames.
  684. */
  685. const TZDBTimeZoneNames* getTZDBTimeZoneNames(UErrorCode& status) const;
  686. /**
  687. * Private method returning the time zone's exemplar location string.
  688. * This method will never return empty.
  689. * @param tz the time zone
  690. * @param name receives the time zone's exemplar location name
  691. * @return a reference to name.
  692. */
  693. UnicodeString& formatExemplarLocation(const TimeZone& tz, UnicodeString& name) const;
  694. /**
  695. * Private enum specifying a combination of offset fields
  696. */
  697. enum OffsetFields {
  698. FIELDS_H,
  699. FIELDS_HM,
  700. FIELDS_HMS
  701. };
  702. /**
  703. * Parses the localized GMT pattern string and initialize
  704. * localized gmt pattern fields.
  705. * @param gmtPattern the localized GMT pattern string such as "GMT {0}"
  706. * @param status U_ILLEGAL_ARGUMENT_ERROR is set when the specified pattern does not
  707. * contain an argument "{0}".
  708. */
  709. void initGMTPattern(const UnicodeString& gmtPattern, UErrorCode& status);
  710. /**
  711. * Parse the GMT offset pattern into runtime optimized format.
  712. * @param pattern the offset pattern string
  713. * @param required the required set of fields, such as FIELDS_HM
  714. * @param status U_ILLEGAL_ARGUMENT is set when the specified pattern does not contain
  715. * pattern letters for the required fields.
  716. * @return A list of GMTOffsetField objects, or NULL on error.
  717. */
  718. static UVector* parseOffsetPattern(const UnicodeString& pattern, OffsetFields required, UErrorCode& status);
  719. /**
  720. * Appends seconds field to the offset pattern with hour/minute
  721. * Note: This code will be obsoleted once we add hour-minute-second pattern data in CLDR.
  722. * @param offsetHM the offset pattern including hours and minutes fields
  723. * @param result the output offset pattern including hour, minute and seconds fields
  724. * @param status receives the status
  725. * @return a reference to result
  726. */
  727. static UnicodeString& expandOffsetPattern(const UnicodeString& offsetHM, UnicodeString& result, UErrorCode& status);
  728. /**
  729. * Truncates minutes field to the offset pattern with hour/minute
  730. * Note: This code will be obsoleted once we add hour pattern data in CLDR.
  731. * @param offsetHM the offset pattern including hours and minutes fields
  732. * @param result the output offset pattern including only hours field
  733. * @param status receives the status
  734. * @return a reference to result
  735. */
  736. static UnicodeString& truncateOffsetPattern(const UnicodeString& offsetHM, UnicodeString& result, UErrorCode& status);
  737. /**
  738. * Break input string into UChar32[]. Each array element represents
  739. * a code point. This method is used for parsing localized digit
  740. * characters and support characters in Unicode supplemental planes.
  741. * @param str the string
  742. * @param codeArray receives the result
  743. * @param capacity the capacity of codeArray
  744. * @return TRUE when the specified code array is fully filled with code points
  745. * (no under/overflow).
  746. */
  747. static UBool toCodePoints(const UnicodeString& str, UChar32* codeArray, int32_t capacity);
  748. /**
  749. * Private method supprting all of ISO8601 formats
  750. * @param offset the offset from GMT(UTC) in milliseconds.
  751. * @param useUtcIndicator true if ISO 8601 UTC indicator "Z" is used when the offset is 0.
  752. * @param isShort true if shortest form is used.
  753. * @param ignoreSeconds true if non-zero offset seconds is appended.
  754. * @param result Receives the result
  755. * @param status Receives the status
  756. * @return the ISO 8601 basic format.
  757. */
  758. UnicodeString& formatOffsetISO8601(int32_t offset, UBool isBasic, UBool useUtcIndicator,
  759. UBool isShort, UBool ignoreSeconds, UnicodeString& result, UErrorCode& status) const;
  760. /**
  761. * Private method used for localized GMT formatting.
  762. * @param offset the zone's UTC offset
  763. * @param isShort true if the short localized GMT format is desired.
  764. * @param result receives the localized GMT format string
  765. * @param status receives the status
  766. */
  767. UnicodeString& formatOffsetLocalizedGMT(int32_t offset, UBool isShort, UnicodeString& result, UErrorCode& status) const;
  768. /**
  769. * Returns offset from GMT(UTC) in milliseconds for the given ISO 8601 style
  770. * (extended format) time zone string. When the given string is not an ISO 8601 time
  771. * zone string, this method sets the current position as the error index
  772. * to <code>ParsePosition pos</code> and returns 0.
  773. * @param text the text contains ISO 8601 style time zone string (e.g. "-08:00", "Z")
  774. * at the position.
  775. * @param pos the position, non-negative error index will be set on failure.
  776. * @param extendedOnly TRUE if parsing the text as ISO 8601 extended offset format (e.g. "-08:00"),
  777. * or FALSE to evaluate the text as basic format.
  778. * @param hasDigitOffset receiving if the parsed zone string contains offset digits.
  779. * @return the offset from GMT(UTC) in milliseconds for the given ISO 8601 style
  780. * time zone string.
  781. */
  782. int32_t parseOffsetISO8601(const UnicodeString& text, ParsePosition& pos, UBool extendedOnly,
  783. UBool* hasDigitOffset = NULL) const;
  784. /**
  785. * Appends localized digits to the buffer.
  786. * This code assumes that the input number is 0 - 59
  787. * @param buf the target buffer
  788. * @param n the integer number
  789. * @param minDigits the minimum digits width
  790. */
  791. void appendOffsetDigits(UnicodeString& buf, int32_t n, uint8_t minDigits) const;
  792. /**
  793. * Returns offset from GMT(UTC) in milliseconds for the given localized GMT
  794. * offset format string. When the given string cannot be parsed, this method
  795. * sets the current position as the error index to <code>ParsePosition pos</code>
  796. * and returns 0.
  797. * @param text the text contains a localized GMT offset string at the position.
  798. * @param pos the position, non-negative error index will be set on failure.
  799. * @param isShort true if this parser to try the short format first
  800. * @param hasDigitOffset receiving if the parsed zone string contains offset digits.
  801. * @return the offset from GMT(UTC) in milliseconds for the given localized GMT
  802. * offset format string.
  803. */
  804. int32_t parseOffsetLocalizedGMT(const UnicodeString& text, ParsePosition& pos,
  805. UBool isShort, UBool* hasDigitOffset) const;
  806. /**
  807. * Parse localized GMT format generated by the patter used by this formatter, except
  808. * GMT Zero format.
  809. * @param text the input text
  810. * @param start the start index
  811. * @param isShort true if the short localized format is parsed.
  812. * @param parsedLen receives the parsed length
  813. * @return the parsed offset in milliseconds
  814. */
  815. int32_t parseOffsetLocalizedGMTPattern(const UnicodeString& text, int32_t start,
  816. UBool isShort, int32_t& parsedLen) const;
  817. /**
  818. * Parses localized GMT offset fields into offset.
  819. * @param text the input text
  820. * @param start the start index
  821. * @param isShort true if this is a short format - currently not used
  822. * @param parsedLen the parsed length, or 0 on failure.
  823. * @return the parsed offset in milliseconds.
  824. */
  825. int32_t parseOffsetFields(const UnicodeString& text, int32_t start, UBool isShort, int32_t& parsedLen) const;
  826. /**
  827. * Parse localized GMT offset fields with the given pattern.
  828. * @param text the input text
  829. * @param start the start index
  830. * @param pattenItems the pattern (already itemized)
  831. * @param forceSingleHourDigit true if hours field is parsed as a single digit
  832. * @param hour receives the hour offset field
  833. * @param min receives the minute offset field
  834. * @param sec receives the second offset field
  835. * @return the parsed length
  836. */
  837. int32_t parseOffsetFieldsWithPattern(const UnicodeString& text, int32_t start,
  838. UVector* patternItems, UBool forceSingleHourDigit, int32_t& hour, int32_t& min, int32_t& sec) const;
  839. /**
  840. * Parses abutting localized GMT offset fields (such as 0800) into offset.
  841. * @param text the input text
  842. * @param start the start index
  843. * @param parsedLen the parsed length, or 0 on failure
  844. * @return the parsed offset in milliseconds.
  845. */
  846. int32_t parseAbuttingOffsetFields(const UnicodeString& text, int32_t start, int32_t& parsedLen) const;
  847. /**
  848. * Parses the input text using the default format patterns (e.g. "UTC{0}").
  849. * @param text the input text
  850. * @param start the start index
  851. * @param parsedLen the parsed length, or 0 on failure
  852. * @return the parsed offset in milliseconds.
  853. */
  854. int32_t parseOffsetDefaultLocalizedGMT(const UnicodeString& text, int start, int32_t& parsedLen) const;
  855. /**
  856. * Parses the input GMT offset fields with the default offset pattern.
  857. * @param text the input text
  858. * @param start the start index
  859. * @param separator the separator character, e.g. ':'
  860. * @param parsedLen the parsed length, or 0 on failure.
  861. * @return the parsed offset in milliseconds.
  862. */
  863. int32_t parseDefaultOffsetFields(const UnicodeString& text, int32_t start, UChar separator,
  864. int32_t& parsedLen) const;
  865. /**
  866. * Reads an offset field value. This method will stop parsing when
  867. * 1) number of digits reaches <code>maxDigits</code>
  868. * 2) just before already parsed number exceeds <code>maxVal</code>
  869. *
  870. * @param text the text
  871. * @param start the start offset
  872. * @param minDigits the minimum number of required digits
  873. * @param maxDigits the maximum number of digits
  874. * @param minVal the minimum value
  875. * @param maxVal the maximum value
  876. * @param parsedLen the actual parsed length.
  877. * @return the integer value parsed
  878. */
  879. int32_t parseOffsetFieldWithLocalizedDigits(const UnicodeString& text, int32_t start,
  880. uint8_t minDigits, uint8_t maxDigits, uint16_t minVal, uint16_t maxVal, int32_t& parsedLen) const;
  881. /**
  882. * Reads a single decimal digit, either localized digits used by this object
  883. * or any Unicode numeric character.
  884. * @param text the text
  885. * @param start the start index
  886. * @param len the actual length read from the text
  887. * the start index is not a decimal number.
  888. * @return the integer value of the parsed digit, or -1 on failure.
  889. */
  890. int32_t parseSingleLocalizedDigit(const UnicodeString& text, int32_t start, int32_t& len) const;
  891. /**
  892. * Formats offset using ASCII digits. The input offset range must be
  893. * within +/-24 hours (exclusive).
  894. * @param offset The offset
  895. * @param sep The field separator character or 0 if not required
  896. * @param minFields The minimum fields
  897. * @param maxFields The maximum fields
  898. * @return The offset string
  899. */
  900. static UnicodeString& formatOffsetWithAsciiDigits(int32_t offset, UChar sep,
  901. OffsetFields minFields, OffsetFields maxFields, UnicodeString& result);
  902. /**
  903. * Parses offset represented by contiguous ASCII digits.
  904. * <p>
  905. * Note: This method expects the input position is already at the start of
  906. * ASCII digits and does not parse sign (+/-).
  907. * @param text The text contains a sequence of ASCII digits
  908. * @param pos The parse position
  909. * @param minFields The minimum Fields to be parsed
  910. * @param maxFields The maximum Fields to be parsed
  911. * @param fixedHourWidth true if hours field must be width of 2
  912. * @return Parsed offset, 0 or positive number.
  913. */
  914. static int32_t parseAbuttingAsciiOffsetFields(const UnicodeString& text, ParsePosition& pos,
  915. OffsetFields minFields, OffsetFields maxFields, UBool fixedHourWidth);
  916. /**
  917. * Parses offset represented by ASCII digits and separators.
  918. * <p>
  919. * Note: This method expects the input position is already at the start of
  920. * ASCII digits and does not parse sign (+/-).
  921. * @param text The text
  922. * @param pos The parse position
  923. * @param sep The separator character
  924. * @param minFields The minimum Fields to be parsed
  925. * @param maxFields The maximum Fields to be parsed
  926. * @return Parsed offset, 0 or positive number.
  927. */
  928. static int32_t parseAsciiOffsetFields(const UnicodeString& text, ParsePosition& pos, UChar sep,
  929. OffsetFields minFields, OffsetFields maxFields);
  930. /**
  931. * Unquotes the message format style pattern.
  932. * @param pattern the pattern
  933. * @param result receive the unquoted pattern.
  934. * @return A reference to result.
  935. */
  936. static UnicodeString& unquote(const UnicodeString& pattern, UnicodeString& result);
  937. /**
  938. * Initialize localized GMT format offset hour/min/sec patterns.
  939. * This method parses patterns into optimized run-time format.
  940. * @param status receives the status.
  941. */
  942. void initGMTOffsetPatterns(UErrorCode& status);
  943. /**
  944. * Check if there are any GMT format offset patterns without
  945. * any separators between hours field and minutes field and update
  946. * fAbuttingOffsetHoursAndMinutes field. This method must be called
  947. * after all patterns are parsed into pattern items.
  948. */
  949. void checkAbuttingHoursAndMinutes();
  950. /**
  951. * Creates an instance of TimeZone for the given offset
  952. * @param offset the offset
  953. * @return A TimeZone with the given offset
  954. */
  955. TimeZone* createTimeZoneForOffset(int32_t offset) const;
  956. /**
  957. * Returns the time type for the given name type
  958. * @param nameType the name type
  959. * @return the time type (unknown/standard/daylight)
  960. */
  961. static UTimeZoneFormatTimeType getTimeType(UTimeZoneNameType nameType);
  962. /**
  963. * Returns the time zone ID of a match at the specified index within
  964. * the MatchInfoCollection.
  965. * @param matches the collection of matches
  966. * @param idx the index withing matches
  967. * @param tzID receives the resolved time zone ID
  968. * @return a reference to tzID.
  969. */
  970. UnicodeString& getTimeZoneID(const TimeZoneNames::MatchInfoCollection* matches, int32_t idx, UnicodeString& tzID) const;
  971. /**
  972. * Parse a zone ID.
  973. * @param text the text contains a time zone ID string at the position.
  974. * @param pos the position
  975. * @param tzID receives the zone ID
  976. * @return a reference to tzID
  977. */
  978. UnicodeString& parseZoneID(const UnicodeString& text, ParsePosition& pos, UnicodeString& tzID) const;
  979. /**
  980. * Parse a short zone ID.
  981. * @param text the text contains a short time zone ID string at the position.
  982. * @param pos the position
  983. * @param tzID receives the short zone ID
  984. * @return a reference to tzID
  985. */
  986. UnicodeString& parseShortZoneID(const UnicodeString& text, ParsePosition& pos, UnicodeString& tzID) const;
  987. /**
  988. * Parse an exemplar location string.
  989. * @param text the text contains an exemplar location string at the position.
  990. * @param pos the position.
  991. * @param tzID receives the time zone ID
  992. * @return a reference to tzID
  993. */
  994. UnicodeString& parseExemplarLocation(const UnicodeString& text, ParsePosition& pos, UnicodeString& tzID) const;
  995. };
  996. U_NAMESPACE_END
  997. #endif /* !UCONFIG_NO_FORMATTING */
  998. #endif