ureldatefmt.h 12 KB

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