reldatefmt.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  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. * In the future, we may add more forms, such as abbreviated/short forms
  334. * (3 secs ago), and relative day periods ("yesterday afternoon"), etc.
  335. *
  336. * The RelativeDateTimeFormatter class is not intended for public subclassing.
  337. *
  338. * @stable ICU 53
  339. */
  340. class U_I18N_API RelativeDateTimeFormatter : public UObject {
  341. public:
  342. /**
  343. * Create RelativeDateTimeFormatter with default locale.
  344. * @stable ICU 53
  345. */
  346. RelativeDateTimeFormatter(UErrorCode& status);
  347. /**
  348. * Create RelativeDateTimeFormatter with given locale.
  349. * @stable ICU 53
  350. */
  351. RelativeDateTimeFormatter(const Locale& locale, UErrorCode& status);
  352. /**
  353. * Create RelativeDateTimeFormatter with given locale and NumberFormat.
  354. *
  355. * @param locale the locale
  356. * @param nfToAdopt Constructed object takes ownership of this pointer.
  357. * It is an error for caller to delete this pointer or change its
  358. * contents after calling this constructor.
  359. * @param status Any error is returned here.
  360. * @stable ICU 53
  361. */
  362. RelativeDateTimeFormatter(
  363. const Locale& locale, NumberFormat *nfToAdopt, UErrorCode& status);
  364. /**
  365. * Create RelativeDateTimeFormatter with given locale, NumberFormat,
  366. * and capitalization context.
  367. *
  368. * @param locale the locale
  369. * @param nfToAdopt Constructed object takes ownership of this pointer.
  370. * It is an error for caller to delete this pointer or change its
  371. * contents after calling this constructor. Caller may pass nullptr for
  372. * this argument if they want default number format behavior.
  373. * @param style the format style. The UDAT_RELATIVE bit field has no effect.
  374. * @param capitalizationContext A value from UDisplayContext that pertains to
  375. * capitalization.
  376. * @param status Any error is returned here.
  377. * @stable ICU 54
  378. */
  379. RelativeDateTimeFormatter(
  380. const Locale& locale,
  381. NumberFormat *nfToAdopt,
  382. UDateRelativeDateTimeFormatterStyle style,
  383. UDisplayContext capitalizationContext,
  384. UErrorCode& status);
  385. /**
  386. * Copy constructor.
  387. * @stable ICU 53
  388. */
  389. RelativeDateTimeFormatter(const RelativeDateTimeFormatter& other);
  390. /**
  391. * Assignment operator.
  392. * @stable ICU 53
  393. */
  394. RelativeDateTimeFormatter& operator=(
  395. const RelativeDateTimeFormatter& other);
  396. /**
  397. * Destructor.
  398. * @stable ICU 53
  399. */
  400. virtual ~RelativeDateTimeFormatter();
  401. /**
  402. * Formats a relative date with a quantity such as "in 5 days" or
  403. * "3 months ago"
  404. *
  405. * This method returns a String. To get more information about the
  406. * formatting result, use formatToValue().
  407. *
  408. * @param quantity The numerical amount e.g 5. This value is formatted
  409. * according to this object's NumberFormat object.
  410. * @param direction NEXT means a future relative date; LAST means a past
  411. * relative date. If direction is anything else, this method sets
  412. * status to U_ILLEGAL_ARGUMENT_ERROR.
  413. * @param unit the unit e.g day? month? year?
  414. * @param appendTo The string to which the formatted result will be
  415. * appended
  416. * @param status ICU error code returned here.
  417. * @return appendTo
  418. * @stable ICU 53
  419. */
  420. UnicodeString& format(
  421. double quantity,
  422. UDateDirection direction,
  423. UDateRelativeUnit unit,
  424. UnicodeString& appendTo,
  425. UErrorCode& status) const;
  426. /**
  427. * Formats a relative date with a quantity such as "in 5 days" or
  428. * "3 months ago"
  429. *
  430. * This method returns a FormattedRelativeDateTime, which exposes more
  431. * information than the String returned by format().
  432. *
  433. * @param quantity The numerical amount e.g 5. This value is formatted
  434. * according to this object's NumberFormat object.
  435. * @param direction NEXT means a future relative date; LAST means a past
  436. * relative date. If direction is anything else, this method sets
  437. * status to U_ILLEGAL_ARGUMENT_ERROR.
  438. * @param unit the unit e.g day? month? year?
  439. * @param status ICU error code returned here.
  440. * @return The formatted relative datetime
  441. * @stable ICU 64
  442. */
  443. FormattedRelativeDateTime formatToValue(
  444. double quantity,
  445. UDateDirection direction,
  446. UDateRelativeUnit unit,
  447. UErrorCode& status) const;
  448. /**
  449. * Formats a relative date without a quantity.
  450. *
  451. * This method returns a String. To get more information about the
  452. * formatting result, use formatToValue().
  453. *
  454. * @param direction NEXT, LAST, THIS, etc.
  455. * @param unit e.g SATURDAY, DAY, MONTH
  456. * @param appendTo The string to which the formatted result will be
  457. * appended. If the value of direction is documented as not being fully
  458. * supported in all locales then this method leaves appendTo unchanged if
  459. * no format string is available.
  460. * @param status ICU error code returned here.
  461. * @return appendTo
  462. * @stable ICU 53
  463. */
  464. UnicodeString& format(
  465. UDateDirection direction,
  466. UDateAbsoluteUnit unit,
  467. UnicodeString& appendTo,
  468. UErrorCode& status) const;
  469. /**
  470. * Formats a relative date without a quantity.
  471. *
  472. * This method returns a FormattedRelativeDateTime, which exposes more
  473. * information than the String returned by format().
  474. *
  475. * If the string is not available in the requested locale, the return
  476. * value will be empty (calling toString will give an empty string).
  477. *
  478. * @param direction NEXT, LAST, THIS, etc.
  479. * @param unit e.g SATURDAY, DAY, MONTH
  480. * @param status ICU error code returned here.
  481. * @return The formatted relative datetime
  482. * @stable ICU 64
  483. */
  484. FormattedRelativeDateTime formatToValue(
  485. UDateDirection direction,
  486. UDateAbsoluteUnit unit,
  487. UErrorCode& status) const;
  488. /**
  489. * Format a combination of URelativeDateTimeUnit and numeric offset
  490. * using a numeric style, e.g. "1 week ago", "in 1 week",
  491. * "5 weeks ago", "in 5 weeks".
  492. *
  493. * This method returns a String. To get more information about the
  494. * formatting result, use formatNumericToValue().
  495. *
  496. * @param offset The signed offset for the specified unit. This
  497. * will be formatted according to this object's
  498. * NumberFormat object.
  499. * @param unit The unit to use when formatting the relative
  500. * date, e.g. UDAT_REL_UNIT_WEEK,
  501. * UDAT_REL_UNIT_FRIDAY.
  502. * @param appendTo The string to which the formatted result will be
  503. * appended.
  504. * @param status ICU error code returned here.
  505. * @return appendTo
  506. * @stable ICU 57
  507. */
  508. UnicodeString& formatNumeric(
  509. double offset,
  510. URelativeDateTimeUnit unit,
  511. UnicodeString& appendTo,
  512. UErrorCode& status) const;
  513. /**
  514. * Format a combination of URelativeDateTimeUnit and numeric offset
  515. * using a numeric style, e.g. "1 week ago", "in 1 week",
  516. * "5 weeks ago", "in 5 weeks".
  517. *
  518. * This method returns a FormattedRelativeDateTime, which exposes more
  519. * information than the String returned by formatNumeric().
  520. *
  521. * @param offset The signed offset for the specified unit. This
  522. * will be formatted according to this object's
  523. * NumberFormat object.
  524. * @param unit The unit to use when formatting the relative
  525. * date, e.g. UDAT_REL_UNIT_WEEK,
  526. * UDAT_REL_UNIT_FRIDAY.
  527. * @param status ICU error code returned here.
  528. * @return The formatted relative datetime
  529. * @stable ICU 64
  530. */
  531. FormattedRelativeDateTime formatNumericToValue(
  532. double offset,
  533. URelativeDateTimeUnit unit,
  534. UErrorCode& status) const;
  535. /**
  536. * Format a combination of URelativeDateTimeUnit and numeric offset
  537. * using a text style if possible, e.g. "last week", "this week",
  538. * "next week", "yesterday", "tomorrow". Falls back to numeric
  539. * style if no appropriate text term is available for the specified
  540. * offset in the object's locale.
  541. *
  542. * This method returns a String. To get more information about the
  543. * formatting result, use formatToValue().
  544. *
  545. * @param offset The signed offset for the specified unit.
  546. * @param unit The unit to use when formatting the relative
  547. * date, e.g. UDAT_REL_UNIT_WEEK,
  548. * UDAT_REL_UNIT_FRIDAY.
  549. * @param appendTo The string to which the formatted result will be
  550. * appended.
  551. * @param status ICU error code returned here.
  552. * @return appendTo
  553. * @stable ICU 57
  554. */
  555. UnicodeString& format(
  556. double offset,
  557. URelativeDateTimeUnit unit,
  558. UnicodeString& appendTo,
  559. UErrorCode& status) const;
  560. /**
  561. * Format a combination of URelativeDateTimeUnit and numeric offset
  562. * using a text style if possible, e.g. "last week", "this week",
  563. * "next week", "yesterday", "tomorrow". Falls back to numeric
  564. * style if no appropriate text term is available for the specified
  565. * offset in the object's locale.
  566. *
  567. * This method returns a FormattedRelativeDateTime, which exposes more
  568. * information than the String returned by format().
  569. *
  570. * @param offset The signed offset for the specified unit.
  571. * @param unit The unit to use when formatting the relative
  572. * date, e.g. UDAT_REL_UNIT_WEEK,
  573. * UDAT_REL_UNIT_FRIDAY.
  574. * @param status ICU error code returned here.
  575. * @return The formatted relative datetime
  576. * @stable ICU 64
  577. */
  578. FormattedRelativeDateTime formatToValue(
  579. double offset,
  580. URelativeDateTimeUnit unit,
  581. UErrorCode& status) const;
  582. /**
  583. * Combines a relative date string and a time string in this object's
  584. * locale. This is done with the same date-time separator used for the
  585. * default calendar in this locale.
  586. *
  587. * @param relativeDateString the relative date, e.g 'yesterday'
  588. * @param timeString the time e.g '3:45'
  589. * @param appendTo concatenated date and time appended here
  590. * @param status ICU error code returned here.
  591. * @return appendTo
  592. * @stable ICU 53
  593. */
  594. UnicodeString& combineDateAndTime(
  595. const UnicodeString& relativeDateString,
  596. const UnicodeString& timeString,
  597. UnicodeString& appendTo,
  598. UErrorCode& status) const;
  599. /**
  600. * Returns the NumberFormat this object is using.
  601. *
  602. * @stable ICU 53
  603. */
  604. const NumberFormat& getNumberFormat() const;
  605. /**
  606. * Returns the capitalization context.
  607. *
  608. * @stable ICU 54
  609. */
  610. UDisplayContext getCapitalizationContext() const;
  611. /**
  612. * Returns the format style.
  613. *
  614. * @stable ICU 54
  615. */
  616. UDateRelativeDateTimeFormatterStyle getFormatStyle() const;
  617. private:
  618. const RelativeDateTimeCacheData* fCache;
  619. const SharedNumberFormat *fNumberFormat;
  620. const SharedPluralRules *fPluralRules;
  621. UDateRelativeDateTimeFormatterStyle fStyle;
  622. UDisplayContext fContext;
  623. #if !UCONFIG_NO_BREAK_ITERATION
  624. const SharedBreakIterator *fOptBreakIterator;
  625. #else
  626. std::nullptr_t fOptBreakIterator = nullptr;
  627. #endif // !UCONFIG_NO_BREAK_ITERATION
  628. Locale fLocale;
  629. void init(
  630. NumberFormat *nfToAdopt,
  631. #if !UCONFIG_NO_BREAK_ITERATION
  632. BreakIterator *brkIter,
  633. #else
  634. std::nullptr_t,
  635. #endif // !UCONFIG_NO_BREAK_ITERATION
  636. UErrorCode &status);
  637. UnicodeString& adjustForContext(UnicodeString &) const;
  638. UBool checkNoAdjustForContext(UErrorCode& status) const;
  639. template<typename F, typename... Args>
  640. UnicodeString& doFormat(
  641. F callback,
  642. UnicodeString& appendTo,
  643. UErrorCode& status,
  644. Args... args) const;
  645. template<typename F, typename... Args>
  646. FormattedRelativeDateTime doFormatToValue(
  647. F callback,
  648. UErrorCode& status,
  649. Args... args) const;
  650. void formatImpl(
  651. double quantity,
  652. UDateDirection direction,
  653. UDateRelativeUnit unit,
  654. FormattedRelativeDateTimeData& output,
  655. UErrorCode& status) const;
  656. void formatAbsoluteImpl(
  657. UDateDirection direction,
  658. UDateAbsoluteUnit unit,
  659. FormattedRelativeDateTimeData& output,
  660. UErrorCode& status) const;
  661. void formatNumericImpl(
  662. double offset,
  663. URelativeDateTimeUnit unit,
  664. FormattedRelativeDateTimeData& output,
  665. UErrorCode& status) const;
  666. void formatRelativeImpl(
  667. double offset,
  668. URelativeDateTimeUnit unit,
  669. FormattedRelativeDateTimeData& output,
  670. UErrorCode& status) const;
  671. };
  672. U_NAMESPACE_END
  673. #endif /* !UCONFIG_NO_FORMATTING */
  674. #endif /* U_SHOW_CPLUSPLUS_API */
  675. #endif /* __RELDATEFMT_H */