reldatefmt.h 14 KB

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