reldatefmt.h 22 KB

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