ureldatefmt.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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) 2016, International Business Machines
  6. * Corporation and others. All Rights Reserved.
  7. *****************************************************************************************
  8. */
  9. #ifndef URELDATEFMT_H
  10. #define URELDATEFMT_H
  11. #include "unicode/utypes.h"
  12. #if !UCONFIG_NO_FORMATTING && !UCONFIG_NO_BREAK_ITERATION
  13. #include "unicode/unum.h"
  14. #include "unicode/udisplaycontext.h"
  15. #include "unicode/localpointer.h"
  16. /**
  17. * \file
  18. * \brief C API: URelativeDateTimeFormatter, relative date formatting of unit + numeric offset.
  19. *
  20. * Provides simple formatting of relative dates, in two ways
  21. * <ul>
  22. * <li>relative dates with a quantity e.g "in 5 days"</li>
  23. * <li>relative dates without a quantity e.g "next Tuesday"</li>
  24. * </ul>
  25. * <p>
  26. * This does not provide compound formatting for multiple units,
  27. * other than the ability to combine a time string with a relative date,
  28. * as in "next Tuesday at 3:45 PM". It also does not provide support
  29. * for determining which unit to use, such as deciding between "in 7 days"
  30. * and "in 1 week".
  31. *
  32. * @draft ICU 57
  33. */
  34. /**
  35. * The formatting style
  36. * @stable ICU 54
  37. */
  38. typedef enum UDateRelativeDateTimeFormatterStyle {
  39. /**
  40. * Everything spelled out.
  41. * @stable ICU 54
  42. */
  43. UDAT_STYLE_LONG,
  44. /**
  45. * Abbreviations used when possible.
  46. * @stable ICU 54
  47. */
  48. UDAT_STYLE_SHORT,
  49. /**
  50. * Use the shortest possible form.
  51. * @stable ICU 54
  52. */
  53. UDAT_STYLE_NARROW,
  54. #ifndef U_HIDE_DEPRECATED_API
  55. /**
  56. * One more than the highest normal UDateRelativeDateTimeFormatterStyle value.
  57. * @deprecated ICU 58 The numeric value may change over time, see ICU ticket #12420.
  58. */
  59. UDAT_STYLE_COUNT
  60. #endif // U_HIDE_DEPRECATED_API
  61. } UDateRelativeDateTimeFormatterStyle;
  62. #ifndef U_HIDE_DRAFT_API
  63. /**
  64. * Represents the unit for formatting a relative date. e.g "in 5 days"
  65. * or "next year"
  66. * @draft ICU 57
  67. */
  68. typedef enum URelativeDateTimeUnit {
  69. /**
  70. * Specifies that relative unit is year, e.g. "last year",
  71. * "in 5 years".
  72. * @draft ICU 57
  73. */
  74. UDAT_REL_UNIT_YEAR,
  75. /**
  76. * Specifies that relative unit is quarter, e.g. "last quarter",
  77. * "in 5 quarters".
  78. * @draft ICU 57
  79. */
  80. UDAT_REL_UNIT_QUARTER,
  81. /**
  82. * Specifies that relative unit is month, e.g. "last month",
  83. * "in 5 months".
  84. * @draft ICU 57
  85. */
  86. UDAT_REL_UNIT_MONTH,
  87. /**
  88. * Specifies that relative unit is week, e.g. "last week",
  89. * "in 5 weeks".
  90. * @draft ICU 57
  91. */
  92. UDAT_REL_UNIT_WEEK,
  93. /**
  94. * Specifies that relative unit is day, e.g. "yesterday",
  95. * "in 5 days".
  96. * @draft ICU 57
  97. */
  98. UDAT_REL_UNIT_DAY,
  99. /**
  100. * Specifies that relative unit is hour, e.g. "1 hour ago",
  101. * "in 5 hours".
  102. * @draft ICU 57
  103. */
  104. UDAT_REL_UNIT_HOUR,
  105. /**
  106. * Specifies that relative unit is minute, e.g. "1 minute ago",
  107. * "in 5 minutes".
  108. * @draft ICU 57
  109. */
  110. UDAT_REL_UNIT_MINUTE,
  111. /**
  112. * Specifies that relative unit is second, e.g. "1 second ago",
  113. * "in 5 seconds".
  114. * @draft ICU 57
  115. */
  116. UDAT_REL_UNIT_SECOND,
  117. /**
  118. * Specifies that relative unit is Sunday, e.g. "last Sunday",
  119. * "this Sunday", "next Sunday", "in 5 Sundays".
  120. * @draft ICU 57
  121. */
  122. UDAT_REL_UNIT_SUNDAY,
  123. /**
  124. * Specifies that relative unit is Monday, e.g. "last Monday",
  125. * "this Monday", "next Monday", "in 5 Mondays".
  126. * @draft ICU 57
  127. */
  128. UDAT_REL_UNIT_MONDAY,
  129. /**
  130. * Specifies that relative unit is Tuesday, e.g. "last Tuesday",
  131. * "this Tuesday", "next Tuesday", "in 5 Tuesdays".
  132. * @draft ICU 57
  133. */
  134. UDAT_REL_UNIT_TUESDAY,
  135. /**
  136. * Specifies that relative unit is Wednesday, e.g. "last Wednesday",
  137. * "this Wednesday", "next Wednesday", "in 5 Wednesdays".
  138. * @draft ICU 57
  139. */
  140. UDAT_REL_UNIT_WEDNESDAY,
  141. /**
  142. * Specifies that relative unit is Thursday, e.g. "last Thursday",
  143. * "this Thursday", "next Thursday", "in 5 Thursdays".
  144. * @draft ICU 57
  145. */
  146. UDAT_REL_UNIT_THURSDAY,
  147. /**
  148. * Specifies that relative unit is Friday, e.g. "last Friday",
  149. * "this Friday", "next Friday", "in 5 Fridays".
  150. * @draft ICU 57
  151. */
  152. UDAT_REL_UNIT_FRIDAY,
  153. /**
  154. * Specifies that relative unit is Saturday, e.g. "last Saturday",
  155. * "this Saturday", "next Saturday", "in 5 Saturdays".
  156. * @draft ICU 57
  157. */
  158. UDAT_REL_UNIT_SATURDAY,
  159. #ifndef U_HIDE_DEPRECATED_API
  160. /**
  161. * One more than the highest normal URelativeDateTimeUnit value.
  162. * @deprecated ICU 58 The numeric value may change over time, see ICU ticket #12420.
  163. */
  164. UDAT_REL_UNIT_COUNT
  165. #endif // U_HIDE_DEPRECATED_API
  166. } URelativeDateTimeUnit;
  167. #endif /* U_HIDE_DRAFT_API */
  168. #ifndef U_HIDE_DRAFT_API
  169. /**
  170. * Opaque URelativeDateTimeFormatter object for use in C programs.
  171. * @draft ICU 57
  172. */
  173. struct URelativeDateTimeFormatter;
  174. typedef struct URelativeDateTimeFormatter URelativeDateTimeFormatter; /**< C typedef for struct URelativeDateTimeFormatter. @draft ICU 57 */
  175. /**
  176. * Open a new URelativeDateTimeFormatter object for a given locale using the
  177. * specified width and capitalizationContext, along with a number formatter
  178. * (if desired) to override the default formatter that would be used for
  179. * display of numeric field offsets. The default formatter typically rounds
  180. * toward 0 and has a minimum of 0 fraction digits and a maximum of 3
  181. * fraction digits (i.e. it will show as many decimal places as necessary
  182. * up to 3, without showing trailing 0s).
  183. *
  184. * @param locale
  185. * The locale
  186. * @param nfToAdopt
  187. * A number formatter to set for this URelativeDateTimeFormatter
  188. * object (instead of the default decimal formatter). Ownership of
  189. * this UNumberFormat object will pass to the URelativeDateTimeFormatter
  190. * object (the URelativeDateTimeFormatter adopts the UNumberFormat),
  191. * which becomes responsible for closing it. If the caller wishes to
  192. * retain ownership of the UNumberFormat object, the caller must clone
  193. * it (with unum_clone) and pass the clone to ureldatefmt_open. May be
  194. * NULL to use the default decimal formatter.
  195. * @param width
  196. * The width - wide, short, narrow, etc.
  197. * @param capitalizationContext
  198. * A value from UDisplayContext that pertains to capitalization, e.g.
  199. * UDISPCTX_CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE.
  200. * @param status
  201. * A pointer to a UErrorCode to receive any errors.
  202. * @return
  203. * A pointer to a URelativeDateTimeFormatter object for the specified locale,
  204. * or NULL if an error occurred.
  205. * @draft ICU 57
  206. */
  207. U_DRAFT URelativeDateTimeFormatter* U_EXPORT2
  208. ureldatefmt_open( const char* locale,
  209. UNumberFormat* nfToAdopt,
  210. UDateRelativeDateTimeFormatterStyle width,
  211. UDisplayContext capitalizationContext,
  212. UErrorCode* status );
  213. /**
  214. * Close a URelativeDateTimeFormatter object. Once closed it may no longer be used.
  215. * @param reldatefmt
  216. * The URelativeDateTimeFormatter object to close.
  217. * @draft ICU 57
  218. */
  219. U_DRAFT void U_EXPORT2
  220. ureldatefmt_close(URelativeDateTimeFormatter *reldatefmt);
  221. #if U_SHOW_CPLUSPLUS_API
  222. U_NAMESPACE_BEGIN
  223. /**
  224. * \class LocalURelativeDateTimeFormatterPointer
  225. * "Smart pointer" class, closes a URelativeDateTimeFormatter via ureldatefmt_close().
  226. * For most methods see the LocalPointerBase base class.
  227. *
  228. * @see LocalPointerBase
  229. * @see LocalPointer
  230. * @draft ICU 57
  231. */
  232. U_DEFINE_LOCAL_OPEN_POINTER(LocalURelativeDateTimeFormatterPointer, URelativeDateTimeFormatter, ureldatefmt_close);
  233. U_NAMESPACE_END
  234. #endif
  235. /**
  236. * Format a combination of URelativeDateTimeUnit and numeric
  237. * offset using a numeric style, e.g. "1 week ago", "in 1 week",
  238. * "5 weeks ago", "in 5 weeks".
  239. *
  240. * @param reldatefmt
  241. * The URelativeDateTimeFormatter object specifying the
  242. * format conventions.
  243. * @param offset
  244. * The signed offset for the specified unit. This will
  245. * be formatted according to this object's UNumberFormat
  246. * object.
  247. * @param unit
  248. * The unit to use when formatting the relative
  249. * date, e.g. UDAT_REL_UNIT_WEEK, UDAT_REL_UNIT_FRIDAY.
  250. * @param result
  251. * A pointer to a buffer to receive the formatted result.
  252. * @param resultCapacity
  253. * The maximum size of result.
  254. * @param status
  255. * A pointer to a UErrorCode to receive any errors. In
  256. * case of error status, the contents of result are
  257. * undefined.
  258. * @return
  259. * The length of the formatted result; may be greater
  260. * than resultCapacity, in which case an error is returned.
  261. * @draft ICU 57
  262. */
  263. U_DRAFT int32_t U_EXPORT2
  264. ureldatefmt_formatNumeric( const URelativeDateTimeFormatter* reldatefmt,
  265. double offset,
  266. URelativeDateTimeUnit unit,
  267. UChar* result,
  268. int32_t resultCapacity,
  269. UErrorCode* status);
  270. /**
  271. * Format a combination of URelativeDateTimeUnit and numeric offset
  272. * using a text style if possible, e.g. "last week", "this week",
  273. * "next week", "yesterday", "tomorrow". Falls back to numeric
  274. * style if no appropriate text term is available for the specified
  275. * offset in the object's locale.
  276. *
  277. * @param reldatefmt
  278. * The URelativeDateTimeFormatter object specifying the
  279. * format conventions.
  280. * @param offset
  281. * The signed offset for the specified unit.
  282. * @param unit
  283. * The unit to use when formatting the relative
  284. * date, e.g. UDAT_REL_UNIT_WEEK, UDAT_REL_UNIT_FRIDAY.
  285. * @param result
  286. * A pointer to a buffer to receive the formatted result.
  287. * @param resultCapacity
  288. * The maximum size of result.
  289. * @param status
  290. * A pointer to a UErrorCode to receive any errors. In
  291. * case of error status, the contents of result are
  292. * undefined.
  293. * @return
  294. * The length of the formatted result; may be greater
  295. * than resultCapacity, in which case an error is returned.
  296. * @draft ICU 57
  297. */
  298. U_DRAFT int32_t U_EXPORT2
  299. ureldatefmt_format( const URelativeDateTimeFormatter* reldatefmt,
  300. double offset,
  301. URelativeDateTimeUnit unit,
  302. UChar* result,
  303. int32_t resultCapacity,
  304. UErrorCode* status);
  305. /**
  306. * Combines a relative date string and a time string in this object's
  307. * locale. This is done with the same date-time separator used for the
  308. * default calendar in this locale to produce a result such as
  309. * "yesterday at 3:45 PM".
  310. *
  311. * @param reldatefmt
  312. * The URelativeDateTimeFormatter object specifying the format conventions.
  313. * @param relativeDateString
  314. * The relative date string.
  315. * @param relativeDateStringLen
  316. * The length of relativeDateString; may be -1 if relativeDateString
  317. * is zero-terminated.
  318. * @param timeString
  319. * The time string.
  320. * @param timeStringLen
  321. * The length of timeString; may be -1 if timeString is zero-terminated.
  322. * @param result
  323. * A pointer to a buffer to receive the formatted result.
  324. * @param resultCapacity
  325. * The maximum size of result.
  326. * @param status
  327. * A pointer to a UErrorCode to receive any errors. In case of error status,
  328. * the contents of result are undefined.
  329. * @return
  330. * The length of the formatted result; may be greater than resultCapacity,
  331. * in which case an error is returned.
  332. * @draft ICU 57
  333. */
  334. U_DRAFT int32_t U_EXPORT2
  335. ureldatefmt_combineDateAndTime( const URelativeDateTimeFormatter* reldatefmt,
  336. const UChar * relativeDateString,
  337. int32_t relativeDateStringLen,
  338. const UChar * timeString,
  339. int32_t timeStringLen,
  340. UChar* result,
  341. int32_t resultCapacity,
  342. UErrorCode* status );
  343. #endif /* U_HIDE_DRAFT_API */
  344. #endif /* !UCONFIG_NO_FORMATTING && !UCONFIG_NO_BREAK_ITERATION */
  345. #endif