tzfmt.h 43 KB

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