reldatefmt.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. // Copyright (C) 2016 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. /*
  4. *****************************************************************************
  5. * Copyright (C) 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. #include "unicode/uobject.h"
  17. #include "unicode/udisplaycontext.h"
  18. #include "unicode/ureldatefmt.h"
  19. #include "unicode/locid.h"
  20. /**
  21. * \file
  22. * \brief C++ API: Formats relative dates such as "1 day ago" or "tomorrow"
  23. */
  24. #if !UCONFIG_NO_FORMATTING
  25. /**
  26. * Represents the unit for formatting a relative date. e.g "in 5 days"
  27. * or "in 3 months"
  28. * @stable ICU 53
  29. */
  30. typedef enum UDateRelativeUnit {
  31. /**
  32. * Seconds
  33. * @stable ICU 53
  34. */
  35. UDAT_RELATIVE_SECONDS,
  36. /**
  37. * Minutes
  38. * @stable ICU 53
  39. */
  40. UDAT_RELATIVE_MINUTES,
  41. /**
  42. * Hours
  43. * @stable ICU 53
  44. */
  45. UDAT_RELATIVE_HOURS,
  46. /**
  47. * Days
  48. * @stable ICU 53
  49. */
  50. UDAT_RELATIVE_DAYS,
  51. /**
  52. * Weeks
  53. * @stable ICU 53
  54. */
  55. UDAT_RELATIVE_WEEKS,
  56. /**
  57. * Months
  58. * @stable ICU 53
  59. */
  60. UDAT_RELATIVE_MONTHS,
  61. /**
  62. * Years
  63. * @stable ICU 53
  64. */
  65. UDAT_RELATIVE_YEARS,
  66. #ifndef U_HIDE_DEPRECATED_API
  67. /**
  68. * One more than the highest normal UDateRelativeUnit value.
  69. * @deprecated ICU 58 The numeric value may change over time, see ICU ticket #12420.
  70. */
  71. UDAT_RELATIVE_UNIT_COUNT
  72. #endif // U_HIDE_DEPRECATED_API
  73. } UDateRelativeUnit;
  74. /**
  75. * Represents an absolute unit.
  76. * @stable ICU 53
  77. */
  78. typedef enum UDateAbsoluteUnit {
  79. // Days of week have to remain together and in order from Sunday to
  80. // Saturday.
  81. /**
  82. * Sunday
  83. * @stable ICU 53
  84. */
  85. UDAT_ABSOLUTE_SUNDAY,
  86. /**
  87. * Monday
  88. * @stable ICU 53
  89. */
  90. UDAT_ABSOLUTE_MONDAY,
  91. /**
  92. * Tuesday
  93. * @stable ICU 53
  94. */
  95. UDAT_ABSOLUTE_TUESDAY,
  96. /**
  97. * Wednesday
  98. * @stable ICU 53
  99. */
  100. UDAT_ABSOLUTE_WEDNESDAY,
  101. /**
  102. * Thursday
  103. * @stable ICU 53
  104. */
  105. UDAT_ABSOLUTE_THURSDAY,
  106. /**
  107. * Friday
  108. * @stable ICU 53
  109. */
  110. UDAT_ABSOLUTE_FRIDAY,
  111. /**
  112. * Saturday
  113. * @stable ICU 53
  114. */
  115. UDAT_ABSOLUTE_SATURDAY,
  116. /**
  117. * Day
  118. * @stable ICU 53
  119. */
  120. UDAT_ABSOLUTE_DAY,
  121. /**
  122. * Week
  123. * @stable ICU 53
  124. */
  125. UDAT_ABSOLUTE_WEEK,
  126. /**
  127. * Month
  128. * @stable ICU 53
  129. */
  130. UDAT_ABSOLUTE_MONTH,
  131. /**
  132. * Year
  133. * @stable ICU 53
  134. */
  135. UDAT_ABSOLUTE_YEAR,
  136. /**
  137. * Now
  138. * @stable ICU 53
  139. */
  140. UDAT_ABSOLUTE_NOW,
  141. #ifndef U_HIDE_DEPRECATED_API
  142. /**
  143. * One more than the highest normal UDateAbsoluteUnit value.
  144. * @deprecated ICU 58 The numeric value may change over time, see ICU ticket #12420.
  145. */
  146. UDAT_ABSOLUTE_UNIT_COUNT
  147. #endif // U_HIDE_DEPRECATED_API
  148. } UDateAbsoluteUnit;
  149. /**
  150. * Represents a direction for an absolute unit e.g "Next Tuesday"
  151. * or "Last Tuesday"
  152. * @stable ICU 53
  153. */
  154. typedef enum UDateDirection {
  155. /**
  156. * Two before. Not fully supported in every locale.
  157. * @stable ICU 53
  158. */
  159. UDAT_DIRECTION_LAST_2,
  160. /**
  161. * Last
  162. * @stable ICU 53
  163. */
  164. UDAT_DIRECTION_LAST,
  165. /**
  166. * This
  167. * @stable ICU 53
  168. */
  169. UDAT_DIRECTION_THIS,
  170. /**
  171. * Next
  172. * @stable ICU 53
  173. */
  174. UDAT_DIRECTION_NEXT,
  175. /**
  176. * Two after. Not fully supported in every locale.
  177. * @stable ICU 53
  178. */
  179. UDAT_DIRECTION_NEXT_2,
  180. /**
  181. * Plain, which means the absence of a qualifier.
  182. * @stable ICU 53
  183. */
  184. UDAT_DIRECTION_PLAIN,
  185. #ifndef U_HIDE_DEPRECATED_API
  186. /**
  187. * One more than the highest normal UDateDirection value.
  188. * @deprecated ICU 58 The numeric value may change over time, see ICU ticket #12420.
  189. */
  190. UDAT_DIRECTION_COUNT
  191. #endif // U_HIDE_DEPRECATED_API
  192. } UDateDirection;
  193. #if !UCONFIG_NO_BREAK_ITERATION
  194. U_NAMESPACE_BEGIN
  195. class RelativeDateTimeCacheData;
  196. class SharedNumberFormat;
  197. class SharedPluralRules;
  198. class SharedBreakIterator;
  199. class NumberFormat;
  200. class UnicodeString;
  201. /**
  202. * Formats simple relative dates. There are two types of relative dates that
  203. * it handles:
  204. * <ul>
  205. * <li>relative dates with a quantity e.g "in 5 days"</li>
  206. * <li>relative dates without a quantity e.g "next Tuesday"</li>
  207. * </ul>
  208. * <p>
  209. * This API is very basic and is intended to be a building block for more
  210. * fancy APIs. The caller tells it exactly what to display in a locale
  211. * independent way. While this class automatically provides the correct plural
  212. * forms, the grammatical form is otherwise as neutral as possible. It is the
  213. * caller's responsibility to handle cut-off logic such as deciding between
  214. * displaying "in 7 days" or "in 1 week." This API supports relative dates
  215. * involving one single unit. This API does not support relative dates
  216. * involving compound units,
  217. * e.g "in 5 days and 4 hours" nor does it support parsing.
  218. * <p>
  219. * This class is mostly thread safe and immutable with the following caveats:
  220. * 1. The assignment operator violates Immutability. It must not be used
  221. * concurrently with other operations.
  222. * 2. Caller must not hold onto adopted pointers.
  223. * <p>
  224. * This class is not intended for public subclassing.
  225. * <p>
  226. * Here are some examples of use:
  227. * <blockquote>
  228. * <pre>
  229. * UErrorCode status = U_ZERO_ERROR;
  230. * UnicodeString appendTo;
  231. * RelativeDateTimeFormatter fmt(status);
  232. * // Appends "in 1 day"
  233. * fmt.format(
  234. * 1, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_DAYS, appendTo, status);
  235. * // Appends "in 3 days"
  236. * fmt.format(
  237. * 3, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_DAYS, appendTo, status);
  238. * // Appends "3.2 years ago"
  239. * fmt.format(
  240. * 3.2, UDAT_DIRECTION_LAST, UDAT_RELATIVE_YEARS, appendTo, status);
  241. * // Appends "last Sunday"
  242. * fmt.format(UDAT_DIRECTION_LAST, UDAT_ABSOLUTE_SUNDAY, appendTo, status);
  243. * // Appends "this Sunday"
  244. * fmt.format(UDAT_DIRECTION_THIS, UDAT_ABSOLUTE_SUNDAY, appendTo, status);
  245. * // Appends "next Sunday"
  246. * fmt.format(UDAT_DIRECTION_NEXT, UDAT_ABSOLUTE_SUNDAY, appendTo, status);
  247. * // Appends "Sunday"
  248. * fmt.format(UDAT_DIRECTION_PLAIN, UDAT_ABSOLUTE_SUNDAY, appendTo, status);
  249. *
  250. * // Appends "yesterday"
  251. * fmt.format(UDAT_DIRECTION_LAST, UDAT_ABSOLUTE_DAY, appendTo, status);
  252. * // Appends "today"
  253. * fmt.format(UDAT_DIRECTION_THIS, UDAT_ABSOLUTE_DAY, appendTo, status);
  254. * // Appends "tomorrow"
  255. * fmt.format(UDAT_DIRECTION_NEXT, UDAT_ABSOLUTE_DAY, appendTo, status);
  256. * // Appends "now"
  257. * fmt.format(UDAT_DIRECTION_PLAIN, UDAT_ABSOLUTE_NOW, appendTo, status);
  258. *
  259. * </pre>
  260. * </blockquote>
  261. * <p>
  262. * In the future, we may add more forms, such as abbreviated/short forms
  263. * (3 secs ago), and relative day periods ("yesterday afternoon"), etc.
  264. *
  265. * The RelativeDateTimeFormatter class is not intended for public subclassing.
  266. *
  267. * @stable ICU 53
  268. */
  269. class U_I18N_API RelativeDateTimeFormatter : public UObject {
  270. public:
  271. /**
  272. * Create RelativeDateTimeFormatter with default locale.
  273. * @stable ICU 53
  274. */
  275. RelativeDateTimeFormatter(UErrorCode& status);
  276. /**
  277. * Create RelativeDateTimeFormatter with given locale.
  278. * @stable ICU 53
  279. */
  280. RelativeDateTimeFormatter(const Locale& locale, UErrorCode& status);
  281. /**
  282. * Create RelativeDateTimeFormatter with given locale and NumberFormat.
  283. *
  284. * @param locale the locale
  285. * @param nfToAdopt Constructed object takes ownership of this pointer.
  286. * It is an error for caller to delete this pointer or change its
  287. * contents after calling this constructor.
  288. * @status Any error is returned here.
  289. * @stable ICU 53
  290. */
  291. RelativeDateTimeFormatter(
  292. const Locale& locale, NumberFormat *nfToAdopt, UErrorCode& status);
  293. /**
  294. * Create RelativeDateTimeFormatter with given locale, NumberFormat,
  295. * and capitalization context.
  296. *
  297. * @param locale the locale
  298. * @param nfToAdopt Constructed object takes ownership of this pointer.
  299. * It is an error for caller to delete this pointer or change its
  300. * contents after calling this constructor. Caller may pass NULL for
  301. * this argument if they want default number format behavior.
  302. * @param style the format style. The UDAT_RELATIVE bit field has no effect.
  303. * @param capitalizationContext A value from UDisplayContext that pertains to
  304. * capitalization.
  305. * @status Any error is returned here.
  306. * @stable ICU 54
  307. */
  308. RelativeDateTimeFormatter(
  309. const Locale& locale,
  310. NumberFormat *nfToAdopt,
  311. UDateRelativeDateTimeFormatterStyle style,
  312. UDisplayContext capitalizationContext,
  313. UErrorCode& status);
  314. /**
  315. * Copy constructor.
  316. * @stable ICU 53
  317. */
  318. RelativeDateTimeFormatter(const RelativeDateTimeFormatter& other);
  319. /**
  320. * Assignment operator.
  321. * @stable ICU 53
  322. */
  323. RelativeDateTimeFormatter& operator=(
  324. const RelativeDateTimeFormatter& other);
  325. /**
  326. * Destructor.
  327. * @stable ICU 53
  328. */
  329. virtual ~RelativeDateTimeFormatter();
  330. /**
  331. * Formats a relative date with a quantity such as "in 5 days" or
  332. * "3 months ago"
  333. * @param quantity The numerical amount e.g 5. This value is formatted
  334. * according to this object's NumberFormat object.
  335. * @param direction NEXT means a future relative date; LAST means a past
  336. * relative date. If direction is anything else, this method sets
  337. * status to U_ILLEGAL_ARGUMENT_ERROR.
  338. * @param unit the unit e.g day? month? year?
  339. * @param appendTo The string to which the formatted result will be
  340. * appended
  341. * @param status ICU error code returned here.
  342. * @return appendTo
  343. * @stable ICU 53
  344. */
  345. UnicodeString& format(
  346. double quantity,
  347. UDateDirection direction,
  348. UDateRelativeUnit unit,
  349. UnicodeString& appendTo,
  350. UErrorCode& status) const;
  351. /**
  352. * Formats a relative date without a quantity.
  353. * @param direction NEXT, LAST, THIS, etc.
  354. * @param unit e.g SATURDAY, DAY, MONTH
  355. * @param appendTo The string to which the formatted result will be
  356. * appended. If the value of direction is documented as not being fully
  357. * supported in all locales then this method leaves appendTo unchanged if
  358. * no format string is available.
  359. * @param status ICU error code returned here.
  360. * @return appendTo
  361. * @stable ICU 53
  362. */
  363. UnicodeString& format(
  364. UDateDirection direction,
  365. UDateAbsoluteUnit unit,
  366. UnicodeString& appendTo,
  367. UErrorCode& status) const;
  368. #ifndef U_HIDE_DRAFT_API
  369. /**
  370. * Format a combination of URelativeDateTimeUnit and numeric offset
  371. * using a numeric style, e.g. "1 week ago", "in 1 week",
  372. * "5 weeks ago", "in 5 weeks".
  373. *
  374. * @param offset The signed offset for the specified unit. This
  375. * will be formatted according to this object's
  376. * NumberFormat object.
  377. * @param unit The unit to use when formatting the relative
  378. * date, e.g. UDAT_REL_UNIT_WEEK,
  379. * UDAT_REL_UNIT_FRIDAY.
  380. * @param appendTo The string to which the formatted result will be
  381. * appended.
  382. * @param status ICU error code returned here.
  383. * @return appendTo
  384. * @draft ICU 57
  385. */
  386. UnicodeString& formatNumeric(
  387. double offset,
  388. URelativeDateTimeUnit unit,
  389. UnicodeString& appendTo,
  390. UErrorCode& status) const;
  391. /**
  392. * Format a combination of URelativeDateTimeUnit and numeric offset
  393. * using a text style if possible, e.g. "last week", "this week",
  394. * "next week", "yesterday", "tomorrow". Falls back to numeric
  395. * style if no appropriate text term is available for the specified
  396. * offset in the object's locale.
  397. *
  398. * @param offset The signed offset for the specified unit.
  399. * @param unit The unit to use when formatting the relative
  400. * date, e.g. UDAT_REL_UNIT_WEEK,
  401. * UDAT_REL_UNIT_FRIDAY.
  402. * @param appendTo The string to which the formatted result will be
  403. * appended.
  404. * @param status ICU error code returned here.
  405. * @return appendTo
  406. * @draft ICU 57
  407. */
  408. UnicodeString& format(
  409. double offset,
  410. URelativeDateTimeUnit unit,
  411. UnicodeString& appendTo,
  412. UErrorCode& status) const;
  413. #endif /* U_HIDE_DRAFT_API */
  414. /**
  415. * Combines a relative date string and a time string in this object's
  416. * locale. This is done with the same date-time separator used for the
  417. * default calendar in this locale.
  418. *
  419. * @param relativeDateString the relative date, e.g 'yesterday'
  420. * @param timeString the time e.g '3:45'
  421. * @param appendTo concatenated date and time appended here
  422. * @param status ICU error code returned here.
  423. * @return appendTo
  424. * @stable ICU 53
  425. */
  426. UnicodeString& combineDateAndTime(
  427. const UnicodeString& relativeDateString,
  428. const UnicodeString& timeString,
  429. UnicodeString& appendTo,
  430. UErrorCode& status) const;
  431. /**
  432. * Returns the NumberFormat this object is using.
  433. *
  434. * @stable ICU 53
  435. */
  436. const NumberFormat& getNumberFormat() const;
  437. /**
  438. * Returns the capitalization context.
  439. *
  440. * @stable ICU 54
  441. */
  442. UDisplayContext getCapitalizationContext() const;
  443. /**
  444. * Returns the format style.
  445. *
  446. * @stable ICU 54
  447. */
  448. UDateRelativeDateTimeFormatterStyle getFormatStyle() const;
  449. private:
  450. const RelativeDateTimeCacheData* fCache;
  451. const SharedNumberFormat *fNumberFormat;
  452. const SharedPluralRules *fPluralRules;
  453. UDateRelativeDateTimeFormatterStyle fStyle;
  454. UDisplayContext fContext;
  455. const SharedBreakIterator *fOptBreakIterator;
  456. Locale fLocale;
  457. void init(
  458. NumberFormat *nfToAdopt,
  459. BreakIterator *brkIter,
  460. UErrorCode &status);
  461. void adjustForContext(UnicodeString &) const;
  462. };
  463. U_NAMESPACE_END
  464. #endif /* !UCONFIG_NO_BREAK_ITERATION */
  465. #endif /* !UCONFIG_NO_FORMATTING */
  466. #endif /* __RELDATEFMT_H */