reldatefmt.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  1. // © 2016 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. /*
  4. *****************************************************************************
  5. * Copyright (C) 2014-2016, International Business Machines Corporation and
  6. * others.
  7. * All Rights Reserved.
  8. *****************************************************************************
  9. *
  10. * File RELDATEFMT.H
  11. *****************************************************************************
  12. */
  13. #ifndef __RELDATEFMT_H
  14. #define __RELDATEFMT_H
  15. #include "unicode/utypes.h"
  16. #if U_SHOW_CPLUSPLUS_API
  17. #include "unicode/uobject.h"
  18. #include "unicode/udisplaycontext.h"
  19. #include "unicode/ureldatefmt.h"
  20. #include "unicode/locid.h"
  21. #include "unicode/formattedvalue.h"
  22. /**
  23. * \file
  24. * \brief C++ API: Formats relative dates such as "1 day ago" or "tomorrow"
  25. */
  26. #if !UCONFIG_NO_FORMATTING
  27. /**
  28. * Represents the unit for formatting a relative date. e.g "in 5 days"
  29. * or "in 3 months"
  30. * @stable ICU 53
  31. */
  32. typedef enum UDateRelativeUnit {
  33. /**
  34. * Seconds
  35. * @stable ICU 53
  36. */
  37. UDAT_RELATIVE_SECONDS,
  38. /**
  39. * Minutes
  40. * @stable ICU 53
  41. */
  42. UDAT_RELATIVE_MINUTES,
  43. /**
  44. * Hours
  45. * @stable ICU 53
  46. */
  47. UDAT_RELATIVE_HOURS,
  48. /**
  49. * Days
  50. * @stable ICU 53
  51. */
  52. UDAT_RELATIVE_DAYS,
  53. /**
  54. * Weeks
  55. * @stable ICU 53
  56. */
  57. UDAT_RELATIVE_WEEKS,
  58. /**
  59. * Months
  60. * @stable ICU 53
  61. */
  62. UDAT_RELATIVE_MONTHS,
  63. /**
  64. * Years
  65. * @stable ICU 53
  66. */
  67. UDAT_RELATIVE_YEARS,
  68. #ifndef U_HIDE_DEPRECATED_API
  69. /**
  70. * One more than the highest normal UDateRelativeUnit value.
  71. * @deprecated ICU 58 The numeric value may change over time, see ICU ticket #12420.
  72. */
  73. UDAT_RELATIVE_UNIT_COUNT
  74. #endif // U_HIDE_DEPRECATED_API
  75. } UDateRelativeUnit;
  76. /**
  77. * Represents an absolute unit.
  78. * @stable ICU 53
  79. */
  80. typedef enum UDateAbsoluteUnit {
  81. // Days of week have to remain together and in order from Sunday to
  82. // Saturday.
  83. /**
  84. * Sunday
  85. * @stable ICU 53
  86. */
  87. UDAT_ABSOLUTE_SUNDAY,
  88. /**
  89. * Monday
  90. * @stable ICU 53
  91. */
  92. UDAT_ABSOLUTE_MONDAY,
  93. /**
  94. * Tuesday
  95. * @stable ICU 53
  96. */
  97. UDAT_ABSOLUTE_TUESDAY,
  98. /**
  99. * Wednesday
  100. * @stable ICU 53
  101. */
  102. UDAT_ABSOLUTE_WEDNESDAY,
  103. /**
  104. * Thursday
  105. * @stable ICU 53
  106. */
  107. UDAT_ABSOLUTE_THURSDAY,
  108. /**
  109. * Friday
  110. * @stable ICU 53
  111. */
  112. UDAT_ABSOLUTE_FRIDAY,
  113. /**
  114. * Saturday
  115. * @stable ICU 53
  116. */
  117. UDAT_ABSOLUTE_SATURDAY,
  118. /**
  119. * Day
  120. * @stable ICU 53
  121. */
  122. UDAT_ABSOLUTE_DAY,
  123. /**
  124. * Week
  125. * @stable ICU 53
  126. */
  127. UDAT_ABSOLUTE_WEEK,
  128. /**
  129. * Month
  130. * @stable ICU 53
  131. */
  132. UDAT_ABSOLUTE_MONTH,
  133. /**
  134. * Year
  135. * @stable ICU 53
  136. */
  137. UDAT_ABSOLUTE_YEAR,
  138. /**
  139. * Now
  140. * @stable ICU 53
  141. */
  142. UDAT_ABSOLUTE_NOW,
  143. /**
  144. * Quarter
  145. * @stable ICU 63
  146. */
  147. UDAT_ABSOLUTE_QUARTER,
  148. /**
  149. * Hour
  150. * @stable ICU 65
  151. */
  152. UDAT_ABSOLUTE_HOUR,
  153. /**
  154. * Minute
  155. * @stable ICU 65
  156. */
  157. UDAT_ABSOLUTE_MINUTE,
  158. #ifndef U_HIDE_DEPRECATED_API
  159. /**
  160. * One more than the highest normal UDateAbsoluteUnit value.
  161. * @deprecated ICU 58 The numeric value may change over time, see ICU ticket #12420.
  162. */
  163. UDAT_ABSOLUTE_UNIT_COUNT = UDAT_ABSOLUTE_NOW + 4
  164. #endif // U_HIDE_DEPRECATED_API
  165. } UDateAbsoluteUnit;
  166. /**
  167. * Represents a direction for an absolute unit e.g "Next Tuesday"
  168. * or "Last Tuesday"
  169. * @stable ICU 53
  170. */
  171. typedef enum UDateDirection {
  172. /**
  173. * Two before. Not fully supported in every locale.
  174. * @stable ICU 53
  175. */
  176. UDAT_DIRECTION_LAST_2,
  177. /**
  178. * Last
  179. * @stable ICU 53
  180. */
  181. UDAT_DIRECTION_LAST,
  182. /**
  183. * This
  184. * @stable ICU 53
  185. */
  186. UDAT_DIRECTION_THIS,
  187. /**
  188. * Next
  189. * @stable ICU 53
  190. */
  191. UDAT_DIRECTION_NEXT,
  192. /**
  193. * Two after. Not fully supported in every locale.
  194. * @stable ICU 53
  195. */
  196. UDAT_DIRECTION_NEXT_2,
  197. /**
  198. * Plain, which means the absence of a qualifier.
  199. * @stable ICU 53
  200. */
  201. UDAT_DIRECTION_PLAIN,
  202. #ifndef U_HIDE_DEPRECATED_API
  203. /**
  204. * One more than the highest normal UDateDirection value.
  205. * @deprecated ICU 58 The numeric value may change over time, see ICU ticket #12420.
  206. */
  207. UDAT_DIRECTION_COUNT
  208. #endif // U_HIDE_DEPRECATED_API
  209. } UDateDirection;
  210. #if !UCONFIG_NO_BREAK_ITERATION
  211. U_NAMESPACE_BEGIN
  212. class BreakIterator;
  213. class RelativeDateTimeCacheData;
  214. class SharedNumberFormat;
  215. class SharedPluralRules;
  216. class SharedBreakIterator;
  217. class NumberFormat;
  218. class UnicodeString;
  219. class FormattedRelativeDateTime;
  220. class FormattedRelativeDateTimeData;
  221. /**
  222. * An immutable class containing the result of a relative datetime formatting operation.
  223. *
  224. * Instances of this class are immutable and thread-safe.
  225. *
  226. * Not intended for public subclassing.
  227. *
  228. * @stable ICU 64
  229. */
  230. class U_I18N_API FormattedRelativeDateTime : public UMemory, public FormattedValue {
  231. public:
  232. /**
  233. * Default constructor; makes an empty FormattedRelativeDateTime.
  234. * @stable ICU 64
  235. */
  236. FormattedRelativeDateTime() : fData(nullptr), fErrorCode(U_INVALID_STATE_ERROR) {}
  237. /**
  238. * Move constructor: Leaves the source FormattedRelativeDateTime in an undefined state.
  239. * @stable ICU 64
  240. */
  241. FormattedRelativeDateTime(FormattedRelativeDateTime&& src) noexcept;
  242. /**
  243. * Destruct an instance of FormattedRelativeDateTime.
  244. * @stable ICU 64
  245. */
  246. virtual ~FormattedRelativeDateTime() override;
  247. /** Copying not supported; use move constructor instead. */
  248. FormattedRelativeDateTime(const FormattedRelativeDateTime&) = delete;
  249. /** Copying not supported; use move assignment instead. */
  250. FormattedRelativeDateTime& operator=(const FormattedRelativeDateTime&) = delete;
  251. /**
  252. * Move assignment: Leaves the source FormattedRelativeDateTime in an undefined state.
  253. * @stable ICU 64
  254. */
  255. FormattedRelativeDateTime& operator=(FormattedRelativeDateTime&& src) noexcept;
  256. /** @copydoc FormattedValue::toString() */
  257. UnicodeString toString(UErrorCode& status) const override;
  258. /** @copydoc FormattedValue::toTempString() */
  259. UnicodeString toTempString(UErrorCode& status) const override;
  260. /** @copydoc FormattedValue::appendTo() */
  261. Appendable &appendTo(Appendable& appendable, UErrorCode& status) const override;
  262. /** @copydoc FormattedValue::nextPosition() */
  263. UBool nextPosition(ConstrainedFieldPosition& cfpos, UErrorCode& status) const override;
  264. private:
  265. FormattedRelativeDateTimeData *fData;
  266. UErrorCode fErrorCode;
  267. explicit FormattedRelativeDateTime(FormattedRelativeDateTimeData *results)
  268. : fData(results), fErrorCode(U_ZERO_ERROR) {}
  269. explicit FormattedRelativeDateTime(UErrorCode errorCode)
  270. : fData(nullptr), fErrorCode(errorCode) {}
  271. friend class RelativeDateTimeFormatter;
  272. };
  273. /**
  274. * Formats simple relative dates. There are two types of relative dates that
  275. * it handles:
  276. * <ul>
  277. * <li>relative dates with a quantity e.g "in 5 days"</li>
  278. * <li>relative dates without a quantity e.g "next Tuesday"</li>
  279. * </ul>
  280. * <p>
  281. * This API is very basic and is intended to be a building block for more
  282. * fancy APIs. The caller tells it exactly what to display in a locale
  283. * independent way. While this class automatically provides the correct plural
  284. * forms, the grammatical form is otherwise as neutral as possible. It is the
  285. * caller's responsibility to handle cut-off logic such as deciding between
  286. * displaying "in 7 days" or "in 1 week." This API supports relative dates
  287. * involving one single unit. This API does not support relative dates
  288. * involving compound units,
  289. * e.g "in 5 days and 4 hours" nor does it support parsing.
  290. * <p>
  291. * This class is mostly thread safe and immutable with the following caveats:
  292. * 1. The assignment operator violates Immutability. It must not be used
  293. * concurrently with other operations.
  294. * 2. Caller must not hold onto adopted pointers.
  295. * <p>
  296. * This class is not intended for public subclassing.
  297. * <p>
  298. * Here are some examples of use:
  299. * <blockquote>
  300. * <pre>
  301. * UErrorCode status = U_ZERO_ERROR;
  302. * UnicodeString appendTo;
  303. * RelativeDateTimeFormatter fmt(status);
  304. * // Appends "in 1 day"
  305. * fmt.format(
  306. * 1, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_DAYS, appendTo, status);
  307. * // Appends "in 3 days"
  308. * fmt.format(
  309. * 3, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_DAYS, appendTo, status);
  310. * // Appends "3.2 years ago"
  311. * fmt.format(
  312. * 3.2, UDAT_DIRECTION_LAST, UDAT_RELATIVE_YEARS, appendTo, status);
  313. * // Appends "last Sunday"
  314. * fmt.format(UDAT_DIRECTION_LAST, UDAT_ABSOLUTE_SUNDAY, appendTo, status);
  315. * // Appends "this Sunday"
  316. * fmt.format(UDAT_DIRECTION_THIS, UDAT_ABSOLUTE_SUNDAY, appendTo, status);
  317. * // Appends "next Sunday"
  318. * fmt.format(UDAT_DIRECTION_NEXT, UDAT_ABSOLUTE_SUNDAY, appendTo, status);
  319. * // Appends "Sunday"
  320. * fmt.format(UDAT_DIRECTION_PLAIN, UDAT_ABSOLUTE_SUNDAY, appendTo, status);
  321. *
  322. * // Appends "yesterday"
  323. * fmt.format(UDAT_DIRECTION_LAST, UDAT_ABSOLUTE_DAY, appendTo, status);
  324. * // Appends "today"
  325. * fmt.format(UDAT_DIRECTION_THIS, UDAT_ABSOLUTE_DAY, appendTo, status);
  326. * // Appends "tomorrow"
  327. * fmt.format(UDAT_DIRECTION_NEXT, UDAT_ABSOLUTE_DAY, appendTo, status);
  328. * // Appends "now"
  329. * fmt.format(UDAT_DIRECTION_PLAIN, UDAT_ABSOLUTE_NOW, appendTo, status);
  330. *
  331. * </pre>
  332. * </blockquote>
  333. * <p>
  334. * In the future, we may add more forms, such as abbreviated/short forms
  335. * (3 secs ago), and relative day periods ("yesterday afternoon"), etc.
  336. *
  337. * The RelativeDateTimeFormatter class is not intended for public subclassing.
  338. *
  339. * @stable ICU 53
  340. */
  341. class U_I18N_API RelativeDateTimeFormatter : public UObject {
  342. public:
  343. /**
  344. * Create RelativeDateTimeFormatter with default locale.
  345. * @stable ICU 53
  346. */
  347. RelativeDateTimeFormatter(UErrorCode& status);
  348. /**
  349. * Create RelativeDateTimeFormatter with given locale.
  350. * @stable ICU 53
  351. */
  352. RelativeDateTimeFormatter(const Locale& locale, UErrorCode& status);
  353. /**
  354. * Create RelativeDateTimeFormatter with given locale and NumberFormat.
  355. *
  356. * @param locale the locale
  357. * @param nfToAdopt Constructed object takes ownership of this pointer.
  358. * It is an error for caller to delete this pointer or change its
  359. * contents after calling this constructor.
  360. * @param status Any error is returned here.
  361. * @stable ICU 53
  362. */
  363. RelativeDateTimeFormatter(
  364. const Locale& locale, NumberFormat *nfToAdopt, UErrorCode& status);
  365. /**
  366. * Create RelativeDateTimeFormatter with given locale, NumberFormat,
  367. * and capitalization context.
  368. *
  369. * @param locale the locale
  370. * @param nfToAdopt Constructed object takes ownership of this pointer.
  371. * It is an error for caller to delete this pointer or change its
  372. * contents after calling this constructor. Caller may pass nullptr for
  373. * this argument if they want default number format behavior.
  374. * @param style the format style. The UDAT_RELATIVE bit field has no effect.
  375. * @param capitalizationContext A value from UDisplayContext that pertains to
  376. * capitalization.
  377. * @param status Any error is returned here.
  378. * @stable ICU 54
  379. */
  380. RelativeDateTimeFormatter(
  381. const Locale& locale,
  382. NumberFormat *nfToAdopt,
  383. UDateRelativeDateTimeFormatterStyle style,
  384. UDisplayContext capitalizationContext,
  385. UErrorCode& status);
  386. /**
  387. * Copy constructor.
  388. * @stable ICU 53
  389. */
  390. RelativeDateTimeFormatter(const RelativeDateTimeFormatter& other);
  391. /**
  392. * Assignment operator.
  393. * @stable ICU 53
  394. */
  395. RelativeDateTimeFormatter& operator=(
  396. const RelativeDateTimeFormatter& other);
  397. /**
  398. * Destructor.
  399. * @stable ICU 53
  400. */
  401. virtual ~RelativeDateTimeFormatter();
  402. /**
  403. * Formats a relative date with a quantity such as "in 5 days" or
  404. * "3 months ago"
  405. *
  406. * This method returns a String. To get more information about the
  407. * formatting result, use formatToValue().
  408. *
  409. * @param quantity The numerical amount e.g 5. This value is formatted
  410. * according to this object's NumberFormat object.
  411. * @param direction NEXT means a future relative date; LAST means a past
  412. * relative date. If direction is anything else, this method sets
  413. * status to U_ILLEGAL_ARGUMENT_ERROR.
  414. * @param unit the unit e.g day? month? year?
  415. * @param appendTo The string to which the formatted result will be
  416. * appended
  417. * @param status ICU error code returned here.
  418. * @return appendTo
  419. * @stable ICU 53
  420. */
  421. UnicodeString& format(
  422. double quantity,
  423. UDateDirection direction,
  424. UDateRelativeUnit unit,
  425. UnicodeString& appendTo,
  426. UErrorCode& status) const;
  427. /**
  428. * Formats a relative date with a quantity such as "in 5 days" or
  429. * "3 months ago"
  430. *
  431. * This method returns a FormattedRelativeDateTime, which exposes more
  432. * information than the String returned by format().
  433. *
  434. * @param quantity The numerical amount e.g 5. This value is formatted
  435. * according to this object's NumberFormat object.
  436. * @param direction NEXT means a future relative date; LAST means a past
  437. * relative date. If direction is anything else, this method sets
  438. * status to U_ILLEGAL_ARGUMENT_ERROR.
  439. * @param unit the unit e.g day? month? year?
  440. * @param status ICU error code returned here.
  441. * @return The formatted relative datetime
  442. * @stable ICU 64
  443. */
  444. FormattedRelativeDateTime formatToValue(
  445. double quantity,
  446. UDateDirection direction,
  447. UDateRelativeUnit unit,
  448. UErrorCode& status) const;
  449. /**
  450. * Formats a relative date without a quantity.
  451. *
  452. * This method returns a String. To get more information about the
  453. * formatting result, use formatToValue().
  454. *
  455. * @param direction NEXT, LAST, THIS, etc.
  456. * @param unit e.g SATURDAY, DAY, MONTH
  457. * @param appendTo The string to which the formatted result will be
  458. * appended. If the value of direction is documented as not being fully
  459. * supported in all locales then this method leaves appendTo unchanged if
  460. * no format string is available.
  461. * @param status ICU error code returned here.
  462. * @return appendTo
  463. * @stable ICU 53
  464. */
  465. UnicodeString& format(
  466. UDateDirection direction,
  467. UDateAbsoluteUnit unit,
  468. UnicodeString& appendTo,
  469. UErrorCode& status) const;
  470. /**
  471. * Formats a relative date without a quantity.
  472. *
  473. * This method returns a FormattedRelativeDateTime, which exposes more
  474. * information than the String returned by format().
  475. *
  476. * If the string is not available in the requested locale, the return
  477. * value will be empty (calling toString will give an empty string).
  478. *
  479. * @param direction NEXT, LAST, THIS, etc.
  480. * @param unit e.g SATURDAY, DAY, MONTH
  481. * @param status ICU error code returned here.
  482. * @return The formatted relative datetime
  483. * @stable ICU 64
  484. */
  485. FormattedRelativeDateTime formatToValue(
  486. UDateDirection direction,
  487. UDateAbsoluteUnit unit,
  488. UErrorCode& status) const;
  489. /**
  490. * Format a combination of URelativeDateTimeUnit and numeric offset
  491. * using a numeric style, e.g. "1 week ago", "in 1 week",
  492. * "5 weeks ago", "in 5 weeks".
  493. *
  494. * This method returns a String. To get more information about the
  495. * formatting result, use formatNumericToValue().
  496. *
  497. * @param offset The signed offset for the specified unit. This
  498. * will be formatted according to this object's
  499. * NumberFormat object.
  500. * @param unit The unit to use when formatting the relative
  501. * date, e.g. UDAT_REL_UNIT_WEEK,
  502. * UDAT_REL_UNIT_FRIDAY.
  503. * @param appendTo The string to which the formatted result will be
  504. * appended.
  505. * @param status ICU error code returned here.
  506. * @return appendTo
  507. * @stable ICU 57
  508. */
  509. UnicodeString& formatNumeric(
  510. double offset,
  511. URelativeDateTimeUnit unit,
  512. UnicodeString& appendTo,
  513. UErrorCode& status) const;
  514. /**
  515. * Format a combination of URelativeDateTimeUnit and numeric offset
  516. * using a numeric style, e.g. "1 week ago", "in 1 week",
  517. * "5 weeks ago", "in 5 weeks".
  518. *
  519. * This method returns a FormattedRelativeDateTime, which exposes more
  520. * information than the String returned by formatNumeric().
  521. *
  522. * @param offset The signed offset for the specified unit. This
  523. * will be formatted according to this object's
  524. * NumberFormat object.
  525. * @param unit The unit to use when formatting the relative
  526. * date, e.g. UDAT_REL_UNIT_WEEK,
  527. * UDAT_REL_UNIT_FRIDAY.
  528. * @param status ICU error code returned here.
  529. * @return The formatted relative datetime
  530. * @stable ICU 64
  531. */
  532. FormattedRelativeDateTime formatNumericToValue(
  533. double offset,
  534. URelativeDateTimeUnit unit,
  535. UErrorCode& status) const;
  536. /**
  537. * Format a combination of URelativeDateTimeUnit and numeric offset
  538. * using a text style if possible, e.g. "last week", "this week",
  539. * "next week", "yesterday", "tomorrow". Falls back to numeric
  540. * style if no appropriate text term is available for the specified
  541. * offset in the object's locale.
  542. *
  543. * This method returns a String. To get more information about the
  544. * formatting result, use formatToValue().
  545. *
  546. * @param offset The signed offset for the specified unit.
  547. * @param unit The unit to use when formatting the relative
  548. * date, e.g. UDAT_REL_UNIT_WEEK,
  549. * UDAT_REL_UNIT_FRIDAY.
  550. * @param appendTo The string to which the formatted result will be
  551. * appended.
  552. * @param status ICU error code returned here.
  553. * @return appendTo
  554. * @stable ICU 57
  555. */
  556. UnicodeString& format(
  557. double offset,
  558. URelativeDateTimeUnit unit,
  559. UnicodeString& appendTo,
  560. UErrorCode& status) const;
  561. /**
  562. * Format a combination of URelativeDateTimeUnit and numeric offset
  563. * using a text style if possible, e.g. "last week", "this week",
  564. * "next week", "yesterday", "tomorrow". Falls back to numeric
  565. * style if no appropriate text term is available for the specified
  566. * offset in the object's locale.
  567. *
  568. * This method returns a FormattedRelativeDateTime, which exposes more
  569. * information than the String returned by format().
  570. *
  571. * @param offset The signed offset for the specified unit.
  572. * @param unit The unit to use when formatting the relative
  573. * date, e.g. UDAT_REL_UNIT_WEEK,
  574. * UDAT_REL_UNIT_FRIDAY.
  575. * @param status ICU error code returned here.
  576. * @return The formatted relative datetime
  577. * @stable ICU 64
  578. */
  579. FormattedRelativeDateTime formatToValue(
  580. double offset,
  581. URelativeDateTimeUnit unit,
  582. UErrorCode& status) const;
  583. /**
  584. * Combines a relative date string and a time string in this object's
  585. * locale. This is done with the same date-time separator used for the
  586. * default calendar in this locale.
  587. *
  588. * @param relativeDateString the relative date, e.g 'yesterday'
  589. * @param timeString the time e.g '3:45'
  590. * @param appendTo concatenated date and time appended here
  591. * @param status ICU error code returned here.
  592. * @return appendTo
  593. * @stable ICU 53
  594. */
  595. UnicodeString& combineDateAndTime(
  596. const UnicodeString& relativeDateString,
  597. const UnicodeString& timeString,
  598. UnicodeString& appendTo,
  599. UErrorCode& status) const;
  600. /**
  601. * Returns the NumberFormat this object is using.
  602. *
  603. * @stable ICU 53
  604. */
  605. const NumberFormat& getNumberFormat() const;
  606. /**
  607. * Returns the capitalization context.
  608. *
  609. * @stable ICU 54
  610. */
  611. UDisplayContext getCapitalizationContext() const;
  612. /**
  613. * Returns the format style.
  614. *
  615. * @stable ICU 54
  616. */
  617. UDateRelativeDateTimeFormatterStyle getFormatStyle() const;
  618. private:
  619. const RelativeDateTimeCacheData* fCache;
  620. const SharedNumberFormat *fNumberFormat;
  621. const SharedPluralRules *fPluralRules;
  622. UDateRelativeDateTimeFormatterStyle fStyle;
  623. UDisplayContext fContext;
  624. const SharedBreakIterator *fOptBreakIterator;
  625. Locale fLocale;
  626. void init(
  627. NumberFormat *nfToAdopt,
  628. BreakIterator *brkIter,
  629. UErrorCode &status);
  630. UnicodeString& adjustForContext(UnicodeString &) const;
  631. UBool checkNoAdjustForContext(UErrorCode& status) const;
  632. template<typename F, typename... Args>
  633. UnicodeString& doFormat(
  634. F callback,
  635. UnicodeString& appendTo,
  636. UErrorCode& status,
  637. Args... args) const;
  638. template<typename F, typename... Args>
  639. FormattedRelativeDateTime doFormatToValue(
  640. F callback,
  641. UErrorCode& status,
  642. Args... args) const;
  643. void formatImpl(
  644. double quantity,
  645. UDateDirection direction,
  646. UDateRelativeUnit unit,
  647. FormattedRelativeDateTimeData& output,
  648. UErrorCode& status) const;
  649. void formatAbsoluteImpl(
  650. UDateDirection direction,
  651. UDateAbsoluteUnit unit,
  652. FormattedRelativeDateTimeData& output,
  653. UErrorCode& status) const;
  654. void formatNumericImpl(
  655. double offset,
  656. URelativeDateTimeUnit unit,
  657. FormattedRelativeDateTimeData& output,
  658. UErrorCode& status) const;
  659. void formatRelativeImpl(
  660. double offset,
  661. URelativeDateTimeUnit unit,
  662. FormattedRelativeDateTimeData& output,
  663. UErrorCode& status) const;
  664. };
  665. U_NAMESPACE_END
  666. #endif /* !UCONFIG_NO_BREAK_ITERATION */
  667. #endif /* !UCONFIG_NO_FORMATTING */
  668. #endif /* U_SHOW_CPLUSPLUS_API */
  669. #endif /* __RELDATEFMT_H */