tzfmt.h 43 KB

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