reldatefmt.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  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. U_NAMESPACE_BEGIN
  211. class BreakIterator;
  212. class RelativeDateTimeCacheData;
  213. class SharedNumberFormat;
  214. class SharedPluralRules;
  215. class SharedBreakIterator;
  216. class NumberFormat;
  217. class UnicodeString;
  218. class FormattedRelativeDateTime;
  219. class FormattedRelativeDateTimeData;
  220. /**
  221. * An immutable class containing the result of a relative datetime formatting operation.
  222. *
  223. * Instances of this class are immutable and thread-safe.
  224. *
  225. * Not intended for public subclassing.
  226. *
  227. * @stable ICU 64
  228. */
  229. class U_I18N_API FormattedRelativeDateTime : public UMemory, public FormattedValue {
  230. public:
  231. /**
  232. * Default constructor; makes an empty FormattedRelativeDateTime.
  233. * @stable ICU 64
  234. */
  235. FormattedRelativeDateTime() : fData(nullptr), fErrorCode(U_INVALID_STATE_ERROR) {}
  236. /**
  237. * Move constructor: Leaves the source FormattedRelativeDateTime in an undefined state.
  238. * @stable ICU 64
  239. */
  240. FormattedRelativeDateTime(FormattedRelativeDateTime&& src) noexcept;
  241. /**
  242. * Destruct an instance of FormattedRelativeDateTime.
  243. * @stable ICU 64
  244. */
  245. virtual ~FormattedRelativeDateTime() override;
  246. /** Copying not supported; use move constructor instead. */
  247. FormattedRelativeDateTime(const FormattedRelativeDateTime&) = delete;
  248. /** Copying not supported; use move assignment instead. */
  249. FormattedRelativeDateTime& operator=(const FormattedRelativeDateTime&) = delete;
  250. /**
  251. * Move assignment: Leaves the source FormattedRelativeDateTime in an undefined state.
  252. * @stable ICU 64
  253. */
  254. FormattedRelativeDateTime& operator=(FormattedRelativeDateTime&& src) noexcept;
  255. /** @copydoc FormattedValue::toString() */
  256. UnicodeString toString(UErrorCode& status) const override;
  257. /** @copydoc FormattedValue::toTempString() */
  258. UnicodeString toTempString(UErrorCode& status) const override;
  259. /** @copydoc FormattedValue::appendTo() */
  260. Appendable &appendTo(Appendable& appendable, UErrorCode& status) const override;
  261. /** @copydoc FormattedValue::nextPosition() */
  262. UBool nextPosition(ConstrainedFieldPosition& cfpos, UErrorCode& status) const override;
  263. private:
  264. FormattedRelativeDateTimeData *fData;
  265. UErrorCode fErrorCode;
  266. explicit FormattedRelativeDateTime(FormattedRelativeDateTimeData *results)
  267. : fData(results), fErrorCode(U_ZERO_ERROR) {}
  268. explicit FormattedRelativeDateTime(UErrorCode errorCode)
  269. : fData(nullptr), fErrorCode(errorCode) {}
  270. friend class RelativeDateTimeFormatter;
  271. };
  272. /**
  273. * Formats simple relative dates. There are two types of relative dates that
  274. * it handles:
  275. * <ul>
  276. * <li>relative dates with a quantity e.g "in 5 days"</li>
  277. * <li>relative dates without a quantity e.g "next Tuesday"</li>
  278. * </ul>
  279. * <p>
  280. * This API is very basic and is intended to be a building block for more
  281. * fancy APIs. The caller tells it exactly what to display in a locale
  282. * independent way. While this class automatically provides the correct plural
  283. * forms, the grammatical form is otherwise as neutral as possible. It is the
  284. * caller's responsibility to handle cut-off logic such as deciding between
  285. * displaying "in 7 days" or "in 1 week." This API supports relative dates
  286. * involving one single unit. This API does not support relative dates
  287. * involving compound units,
  288. * e.g "in 5 days and 4 hours" nor does it support parsing.
  289. * <p>
  290. * This class is mostly thread safe and immutable with the following caveats:
  291. * 1. The assignment operator violates Immutability. It must not be used
  292. * concurrently with other operations.
  293. * 2. Caller must not hold onto adopted pointers.
  294. * <p>
  295. * This class is not intended for public subclassing.
  296. * <p>
  297. * Here are some examples of use:
  298. * <blockquote>
  299. * <pre>
  300. * UErrorCode status = U_ZERO_ERROR;
  301. * UnicodeString appendTo;
  302. * RelativeDateTimeFormatter fmt(status);
  303. * // Appends "in 1 day"
  304. * fmt.format(
  305. * 1, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_DAYS, appendTo, status);
  306. * // Appends "in 3 days"
  307. * fmt.format(
  308. * 3, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_DAYS, appendTo, status);
  309. * // Appends "3.2 years ago"
  310. * fmt.format(
  311. * 3.2, UDAT_DIRECTION_LAST, UDAT_RELATIVE_YEARS, appendTo, status);
  312. * // Appends "last Sunday"
  313. * fmt.format(UDAT_DIRECTION_LAST, UDAT_ABSOLUTE_SUNDAY, appendTo, status);
  314. * // Appends "this Sunday"
  315. * fmt.format(UDAT_DIRECTION_THIS, UDAT_ABSOLUTE_SUNDAY, appendTo, status);
  316. * // Appends "next Sunday"
  317. * fmt.format(UDAT_DIRECTION_NEXT, UDAT_ABSOLUTE_SUNDAY, appendTo, status);
  318. * // Appends "Sunday"
  319. * fmt.format(UDAT_DIRECTION_PLAIN, UDAT_ABSOLUTE_SUNDAY, appendTo, status);
  320. *
  321. * // Appends "yesterday"
  322. * fmt.format(UDAT_DIRECTION_LAST, UDAT_ABSOLUTE_DAY, appendTo, status);
  323. * // Appends "today"
  324. * fmt.format(UDAT_DIRECTION_THIS, UDAT_ABSOLUTE_DAY, appendTo, status);
  325. * // Appends "tomorrow"
  326. * fmt.format(UDAT_DIRECTION_NEXT, UDAT_ABSOLUTE_DAY, appendTo, status);
  327. * // Appends "now"
  328. * fmt.format(UDAT_DIRECTION_PLAIN, UDAT_ABSOLUTE_NOW, appendTo, status);
  329. *
  330. * </pre>
  331. * </blockquote>
  332. * <p>
  333. * The UDateRelativeDateTimeFormatterStyle parameter allows selection of
  334. * different length styles: LONG ("3 seconds ago"), SHORT ("3 sec. ago"),
  335. * NARROW ("3s ago"). In the future, we may add more forms, such as
  336. * relative day periods ("yesterday afternoon"), etc.
  337. *
  338. * The RelativeDateTimeFormatter class is not intended for public subclassing.
  339. *
  340. * @stable ICU 53
  341. */
  342. class U_I18N_API RelativeDateTimeFormatter : public UObject {
  343. public:
  344. /**
  345. * Create RelativeDateTimeFormatter with default locale.
  346. * @stable ICU 53
  347. */
  348. RelativeDateTimeFormatter(UErrorCode& status);
  349. /**
  350. * Create RelativeDateTimeFormatter with given locale.
  351. * @stable ICU 53
  352. */
  353. RelativeDateTimeFormatter(const Locale& locale, UErrorCode& status);
  354. /**
  355. * Create RelativeDateTimeFormatter with given locale and NumberFormat.
  356. *
  357. * @param locale the locale
  358. * @param nfToAdopt Constructed object takes ownership of this pointer.
  359. * It is an error for caller to delete this pointer or change its
  360. * contents after calling this constructor.
  361. * @param status Any error is returned here.
  362. * @stable ICU 53
  363. */
  364. RelativeDateTimeFormatter(
  365. const Locale& locale, NumberFormat *nfToAdopt, UErrorCode& status);
  366. /**
  367. * Create RelativeDateTimeFormatter with given locale, NumberFormat,
  368. * and capitalization context.
  369. *
  370. * @param locale the locale
  371. * @param nfToAdopt Constructed object takes ownership of this pointer.
  372. * It is an error for caller to delete this pointer or change its
  373. * contents after calling this constructor. Caller may pass nullptr for
  374. * this argument if they want default number format behavior.
  375. * @param style the format style. The UDAT_RELATIVE bit field has no effect.
  376. * @param capitalizationContext A value from UDisplayContext that pertains to
  377. * capitalization.
  378. * @param status Any error is returned here.
  379. * @stable ICU 54
  380. */
  381. RelativeDateTimeFormatter(
  382. const Locale& locale,
  383. NumberFormat *nfToAdopt,
  384. UDateRelativeDateTimeFormatterStyle style,
  385. UDisplayContext capitalizationContext,
  386. UErrorCode& status);
  387. /**
  388. * Copy constructor.
  389. * @stable ICU 53
  390. */
  391. RelativeDateTimeFormatter(const RelativeDateTimeFormatter& other);
  392. /**
  393. * Assignment operator.
  394. * @stable ICU 53
  395. */
  396. RelativeDateTimeFormatter& operator=(
  397. const RelativeDateTimeFormatter& other);
  398. /**
  399. * Destructor.
  400. * @stable ICU 53
  401. */
  402. virtual ~RelativeDateTimeFormatter();
  403. /**
  404. * Formats a relative date with a quantity such as "in 5 days" or
  405. * "3 months ago"
  406. *
  407. * This method returns a String. To get more information about the
  408. * formatting result, use formatToValue().
  409. *
  410. * @param quantity The numerical amount e.g 5. This value is formatted
  411. * according to this object's NumberFormat object.
  412. * @param direction NEXT means a future relative date; LAST means a past
  413. * relative date. If direction is anything else, this method sets
  414. * status to U_ILLEGAL_ARGUMENT_ERROR.
  415. * @param unit the unit e.g day? month? year?
  416. * @param appendTo The string to which the formatted result will be
  417. * appended
  418. * @param status ICU error code returned here.
  419. * @return appendTo
  420. * @stable ICU 53
  421. */
  422. UnicodeString& format(
  423. double quantity,
  424. UDateDirection direction,
  425. UDateRelativeUnit unit,
  426. UnicodeString& appendTo,
  427. UErrorCode& status) const;
  428. /**
  429. * Formats a relative date with a quantity such as "in 5 days" or
  430. * "3 months ago"
  431. *
  432. * This method returns a FormattedRelativeDateTime, which exposes more
  433. * information than the String returned by format().
  434. *
  435. * @param quantity The numerical amount e.g 5. This value is formatted
  436. * according to this object's NumberFormat object.
  437. * @param direction NEXT means a future relative date; LAST means a past
  438. * relative date. If direction is anything else, this method sets
  439. * status to U_ILLEGAL_ARGUMENT_ERROR.
  440. * @param unit the unit e.g day? month? year?
  441. * @param status ICU error code returned here.
  442. * @return The formatted relative datetime
  443. * @stable ICU 64
  444. */
  445. FormattedRelativeDateTime formatToValue(
  446. double quantity,
  447. UDateDirection direction,
  448. UDateRelativeUnit unit,
  449. UErrorCode& status) const;
  450. /**
  451. * Formats a relative date without a quantity.
  452. *
  453. * This method returns a String. To get more information about the
  454. * formatting result, use formatToValue().
  455. *
  456. * @param direction NEXT, LAST, THIS, etc.
  457. * @param unit e.g SATURDAY, DAY, MONTH
  458. * @param appendTo The string to which the formatted result will be
  459. * appended. If the value of direction is documented as not being fully
  460. * supported in all locales then this method leaves appendTo unchanged if
  461. * no format string is available.
  462. * @param status ICU error code returned here.
  463. * @return appendTo
  464. * @stable ICU 53
  465. */
  466. UnicodeString& format(
  467. UDateDirection direction,
  468. UDateAbsoluteUnit unit,
  469. UnicodeString& appendTo,
  470. UErrorCode& status) const;
  471. /**
  472. * Formats a relative date without a quantity.
  473. *
  474. * This method returns a FormattedRelativeDateTime, which exposes more
  475. * information than the String returned by format().
  476. *
  477. * If the string is not available in the requested locale, the return
  478. * value will be empty (calling toString will give an empty string).
  479. *
  480. * @param direction NEXT, LAST, THIS, etc.
  481. * @param unit e.g SATURDAY, DAY, MONTH
  482. * @param status ICU error code returned here.
  483. * @return The formatted relative datetime
  484. * @stable ICU 64
  485. */
  486. FormattedRelativeDateTime formatToValue(
  487. UDateDirection direction,
  488. UDateAbsoluteUnit unit,
  489. UErrorCode& status) const;
  490. /**
  491. * Format a combination of URelativeDateTimeUnit and numeric offset
  492. * using a numeric style, e.g. "1 week ago", "in 1 week",
  493. * "5 weeks ago", "in 5 weeks".
  494. *
  495. * This method returns a String. To get more information about the
  496. * formatting result, use formatNumericToValue().
  497. *
  498. * @param offset The signed offset for the specified unit. This
  499. * will be formatted according to this object's
  500. * NumberFormat object.
  501. * @param unit The unit to use when formatting the relative
  502. * date, e.g. UDAT_REL_UNIT_WEEK,
  503. * UDAT_REL_UNIT_FRIDAY.
  504. * @param appendTo The string to which the formatted result will be
  505. * appended.
  506. * @param status ICU error code returned here.
  507. * @return appendTo
  508. * @stable ICU 57
  509. */
  510. UnicodeString& formatNumeric(
  511. double offset,
  512. URelativeDateTimeUnit unit,
  513. UnicodeString& appendTo,
  514. UErrorCode& status) const;
  515. /**
  516. * Format a combination of URelativeDateTimeUnit and numeric offset
  517. * using a numeric style, e.g. "1 week ago", "in 1 week",
  518. * "5 weeks ago", "in 5 weeks".
  519. *
  520. * This method returns a FormattedRelativeDateTime, which exposes more
  521. * information than the String returned by formatNumeric().
  522. *
  523. * @param offset The signed offset for the specified unit. This
  524. * will be formatted according to this object's
  525. * NumberFormat object.
  526. * @param unit The unit to use when formatting the relative
  527. * date, e.g. UDAT_REL_UNIT_WEEK,
  528. * UDAT_REL_UNIT_FRIDAY.
  529. * @param status ICU error code returned here.
  530. * @return The formatted relative datetime
  531. * @stable ICU 64
  532. */
  533. FormattedRelativeDateTime formatNumericToValue(
  534. double offset,
  535. URelativeDateTimeUnit unit,
  536. UErrorCode& status) const;
  537. /**
  538. * Format a combination of URelativeDateTimeUnit and numeric offset
  539. * using a text style if possible, e.g. "last week", "this week",
  540. * "next week", "yesterday", "tomorrow". Falls back to numeric
  541. * style if no appropriate text term is available for the specified
  542. * offset in the object's locale.
  543. *
  544. * This method returns a String. To get more information about the
  545. * formatting result, use formatToValue().
  546. *
  547. * @param offset The signed offset for the specified unit.
  548. * @param unit The unit to use when formatting the relative
  549. * date, e.g. UDAT_REL_UNIT_WEEK,
  550. * UDAT_REL_UNIT_FRIDAY.
  551. * @param appendTo The string to which the formatted result will be
  552. * appended.
  553. * @param status ICU error code returned here.
  554. * @return appendTo
  555. * @stable ICU 57
  556. */
  557. UnicodeString& format(
  558. double offset,
  559. URelativeDateTimeUnit unit,
  560. UnicodeString& appendTo,
  561. UErrorCode& status) const;
  562. /**
  563. * Format a combination of URelativeDateTimeUnit and numeric offset
  564. * using a text style if possible, e.g. "last week", "this week",
  565. * "next week", "yesterday", "tomorrow". Falls back to numeric
  566. * style if no appropriate text term is available for the specified
  567. * offset in the object's locale.
  568. *
  569. * This method returns a FormattedRelativeDateTime, which exposes more
  570. * information than the String returned by format().
  571. *
  572. * @param offset The signed offset for the specified unit.
  573. * @param unit The unit to use when formatting the relative
  574. * date, e.g. UDAT_REL_UNIT_WEEK,
  575. * UDAT_REL_UNIT_FRIDAY.
  576. * @param status ICU error code returned here.
  577. * @return The formatted relative datetime
  578. * @stable ICU 64
  579. */
  580. FormattedRelativeDateTime formatToValue(
  581. double offset,
  582. URelativeDateTimeUnit unit,
  583. UErrorCode& status) const;
  584. /**
  585. * Combines a relative date string and a time string in this object's
  586. * locale. This is done with the same date-time separator used for the
  587. * default calendar in this locale.
  588. *
  589. * @param relativeDateString the relative date, e.g 'yesterday'
  590. * @param timeString the time e.g '3:45'
  591. * @param appendTo concatenated date and time appended here
  592. * @param status ICU error code returned here.
  593. * @return appendTo
  594. * @stable ICU 53
  595. */
  596. UnicodeString& combineDateAndTime(
  597. const UnicodeString& relativeDateString,
  598. const UnicodeString& timeString,
  599. UnicodeString& appendTo,
  600. UErrorCode& status) const;
  601. /**
  602. * Returns the NumberFormat this object is using.
  603. *
  604. * @stable ICU 53
  605. */
  606. const NumberFormat& getNumberFormat() const;
  607. /**
  608. * Returns the capitalization context.
  609. *
  610. * @stable ICU 54
  611. */
  612. UDisplayContext getCapitalizationContext() const;
  613. /**
  614. * Returns the format style.
  615. *
  616. * @stable ICU 54
  617. */
  618. UDateRelativeDateTimeFormatterStyle getFormatStyle() const;
  619. private:
  620. const RelativeDateTimeCacheData* fCache;
  621. const SharedNumberFormat *fNumberFormat;
  622. const SharedPluralRules *fPluralRules;
  623. UDateRelativeDateTimeFormatterStyle fStyle;
  624. UDisplayContext fContext;
  625. #if !UCONFIG_NO_BREAK_ITERATION
  626. const SharedBreakIterator *fOptBreakIterator;
  627. #else
  628. std::nullptr_t fOptBreakIterator = nullptr;
  629. #endif // !UCONFIG_NO_BREAK_ITERATION
  630. Locale fLocale;
  631. void init(
  632. NumberFormat *nfToAdopt,
  633. #if !UCONFIG_NO_BREAK_ITERATION
  634. BreakIterator *brkIter,
  635. #else
  636. std::nullptr_t,
  637. #endif // !UCONFIG_NO_BREAK_ITERATION
  638. UErrorCode &status);
  639. UnicodeString& adjustForContext(UnicodeString &) const;
  640. UBool checkNoAdjustForContext(UErrorCode& status) const;
  641. template<typename F, typename... Args>
  642. UnicodeString& doFormat(
  643. F callback,
  644. UnicodeString& appendTo,
  645. UErrorCode& status,
  646. Args... args) const;
  647. template<typename F, typename... Args>
  648. FormattedRelativeDateTime doFormatToValue(
  649. F callback,
  650. UErrorCode& status,
  651. Args... args) const;
  652. void formatImpl(
  653. double quantity,
  654. UDateDirection direction,
  655. UDateRelativeUnit unit,
  656. FormattedRelativeDateTimeData& output,
  657. UErrorCode& status) const;
  658. void formatAbsoluteImpl(
  659. UDateDirection direction,
  660. UDateAbsoluteUnit unit,
  661. FormattedRelativeDateTimeData& output,
  662. UErrorCode& status) const;
  663. void formatNumericImpl(
  664. double offset,
  665. URelativeDateTimeUnit unit,
  666. FormattedRelativeDateTimeData& output,
  667. UErrorCode& status) const;
  668. void formatRelativeImpl(
  669. double offset,
  670. URelativeDateTimeUnit unit,
  671. FormattedRelativeDateTimeData& output,
  672. UErrorCode& status) const;
  673. };
  674. U_NAMESPACE_END
  675. #endif /* !UCONFIG_NO_FORMATTING */
  676. #endif /* U_SHOW_CPLUSPLUS_API */
  677. #endif /* __RELDATEFMT_H */