reldatefmt.h 12 KB

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