ureldatefmt.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. // © 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
  13. #include "unicode/unum.h"
  14. #include "unicode/udisplaycontext.h"
  15. #include "unicode/uformattedvalue.h"
  16. #if U_SHOW_CPLUSPLUS_API
  17. #include "unicode/localpointer.h"
  18. #endif // U_SHOW_CPLUSPLUS_API
  19. /**
  20. * \file
  21. * \brief C API: URelativeDateTimeFormatter, relative date formatting of unit + numeric offset.
  22. *
  23. * Provides simple formatting of relative dates, in two ways
  24. * <ul>
  25. * <li>relative dates with a quantity e.g "in 5 days"</li>
  26. * <li>relative dates without a quantity e.g "next Tuesday"</li>
  27. * </ul>
  28. * <p>
  29. * This does not provide compound formatting for multiple units,
  30. * other than the ability to combine a time string with a relative date,
  31. * as in "next Tuesday at 3:45 PM". It also does not provide support
  32. * for determining which unit to use, such as deciding between "in 7 days"
  33. * and "in 1 week".
  34. *
  35. * @stable ICU 57
  36. */
  37. /**
  38. * The formatting style
  39. * @stable ICU 54
  40. */
  41. typedef enum UDateRelativeDateTimeFormatterStyle {
  42. /**
  43. * Everything spelled out.
  44. * @stable ICU 54
  45. */
  46. UDAT_STYLE_LONG,
  47. /**
  48. * Abbreviations used when possible.
  49. * @stable ICU 54
  50. */
  51. UDAT_STYLE_SHORT,
  52. /**
  53. * Use the shortest possible form.
  54. * @stable ICU 54
  55. */
  56. UDAT_STYLE_NARROW,
  57. #ifndef U_HIDE_DEPRECATED_API
  58. /**
  59. * One more than the highest normal UDateRelativeDateTimeFormatterStyle value.
  60. * @deprecated ICU 58 The numeric value may change over time, see ICU ticket #12420.
  61. */
  62. UDAT_STYLE_COUNT
  63. #endif /* U_HIDE_DEPRECATED_API */
  64. } UDateRelativeDateTimeFormatterStyle;
  65. /**
  66. * Represents the unit for formatting a relative date. e.g "in 5 days"
  67. * or "next year"
  68. * @stable ICU 57
  69. */
  70. typedef enum URelativeDateTimeUnit {
  71. /**
  72. * Specifies that relative unit is year, e.g. "last year",
  73. * "in 5 years".
  74. * @stable ICU 57
  75. */
  76. UDAT_REL_UNIT_YEAR,
  77. /**
  78. * Specifies that relative unit is quarter, e.g. "last quarter",
  79. * "in 5 quarters".
  80. * @stable ICU 57
  81. */
  82. UDAT_REL_UNIT_QUARTER,
  83. /**
  84. * Specifies that relative unit is month, e.g. "last month",
  85. * "in 5 months".
  86. * @stable ICU 57
  87. */
  88. UDAT_REL_UNIT_MONTH,
  89. /**
  90. * Specifies that relative unit is week, e.g. "last week",
  91. * "in 5 weeks".
  92. * @stable ICU 57
  93. */
  94. UDAT_REL_UNIT_WEEK,
  95. /**
  96. * Specifies that relative unit is day, e.g. "yesterday",
  97. * "in 5 days".
  98. * @stable ICU 57
  99. */
  100. UDAT_REL_UNIT_DAY,
  101. /**
  102. * Specifies that relative unit is hour, e.g. "1 hour ago",
  103. * "in 5 hours".
  104. * @stable ICU 57
  105. */
  106. UDAT_REL_UNIT_HOUR,
  107. /**
  108. * Specifies that relative unit is minute, e.g. "1 minute ago",
  109. * "in 5 minutes".
  110. * @stable ICU 57
  111. */
  112. UDAT_REL_UNIT_MINUTE,
  113. /**
  114. * Specifies that relative unit is second, e.g. "1 second ago",
  115. * "in 5 seconds".
  116. * @stable ICU 57
  117. */
  118. UDAT_REL_UNIT_SECOND,
  119. /**
  120. * Specifies that relative unit is Sunday, e.g. "last Sunday",
  121. * "this Sunday", "next Sunday", "in 5 Sundays".
  122. * @stable ICU 57
  123. */
  124. UDAT_REL_UNIT_SUNDAY,
  125. /**
  126. * Specifies that relative unit is Monday, e.g. "last Monday",
  127. * "this Monday", "next Monday", "in 5 Mondays".
  128. * @stable ICU 57
  129. */
  130. UDAT_REL_UNIT_MONDAY,
  131. /**
  132. * Specifies that relative unit is Tuesday, e.g. "last Tuesday",
  133. * "this Tuesday", "next Tuesday", "in 5 Tuesdays".
  134. * @stable ICU 57
  135. */
  136. UDAT_REL_UNIT_TUESDAY,
  137. /**
  138. * Specifies that relative unit is Wednesday, e.g. "last Wednesday",
  139. * "this Wednesday", "next Wednesday", "in 5 Wednesdays".
  140. * @stable ICU 57
  141. */
  142. UDAT_REL_UNIT_WEDNESDAY,
  143. /**
  144. * Specifies that relative unit is Thursday, e.g. "last Thursday",
  145. * "this Thursday", "next Thursday", "in 5 Thursdays".
  146. * @stable ICU 57
  147. */
  148. UDAT_REL_UNIT_THURSDAY,
  149. /**
  150. * Specifies that relative unit is Friday, e.g. "last Friday",
  151. * "this Friday", "next Friday", "in 5 Fridays".
  152. * @stable ICU 57
  153. */
  154. UDAT_REL_UNIT_FRIDAY,
  155. /**
  156. * Specifies that relative unit is Saturday, e.g. "last Saturday",
  157. * "this Saturday", "next Saturday", "in 5 Saturdays".
  158. * @stable ICU 57
  159. */
  160. UDAT_REL_UNIT_SATURDAY,
  161. #ifndef U_HIDE_DEPRECATED_API
  162. /**
  163. * One more than the highest normal URelativeDateTimeUnit value.
  164. * @deprecated ICU 58 The numeric value may change over time, see ICU ticket #12420.
  165. */
  166. UDAT_REL_UNIT_COUNT
  167. #endif /* U_HIDE_DEPRECATED_API */
  168. } URelativeDateTimeUnit;
  169. /**
  170. * FieldPosition and UFieldPosition selectors for format fields
  171. * defined by RelativeDateTimeFormatter.
  172. * @stable ICU 64
  173. */
  174. typedef enum URelativeDateTimeFormatterField {
  175. /**
  176. * Represents a literal text string, like "tomorrow" or "days ago".
  177. * @stable ICU 64
  178. */
  179. UDAT_REL_LITERAL_FIELD,
  180. /**
  181. * Represents a number quantity, like "3" in "3 days ago".
  182. * @stable ICU 64
  183. */
  184. UDAT_REL_NUMERIC_FIELD,
  185. } URelativeDateTimeFormatterField;
  186. /**
  187. * Opaque URelativeDateTimeFormatter object for use in C programs.
  188. * @stable ICU 57
  189. */
  190. struct URelativeDateTimeFormatter;
  191. typedef struct URelativeDateTimeFormatter URelativeDateTimeFormatter; /**< C typedef for struct URelativeDateTimeFormatter. @stable ICU 57 */
  192. /**
  193. * Open a new URelativeDateTimeFormatter object for a given locale using the
  194. * specified width and capitalizationContext, along with a number formatter
  195. * (if desired) to override the default formatter that would be used for
  196. * display of numeric field offsets. The default formatter typically rounds
  197. * toward 0 and has a minimum of 0 fraction digits and a maximum of 3
  198. * fraction digits (i.e. it will show as many decimal places as necessary
  199. * up to 3, without showing trailing 0s).
  200. *
  201. * @param locale
  202. * The locale
  203. * @param nfToAdopt
  204. * A number formatter to set for this URelativeDateTimeFormatter
  205. * object (instead of the default decimal formatter). Ownership of
  206. * this UNumberFormat object will pass to the URelativeDateTimeFormatter
  207. * object (the URelativeDateTimeFormatter adopts the UNumberFormat),
  208. * which becomes responsible for closing it. If the caller wishes to
  209. * retain ownership of the UNumberFormat object, the caller must clone
  210. * it (with unum_clone) and pass the clone to ureldatefmt_open. May be
  211. * NULL to use the default decimal formatter.
  212. * @param width
  213. * The width - wide, short, narrow, etc.
  214. * @param capitalizationContext
  215. * A value from UDisplayContext that pertains to capitalization, e.g.
  216. * UDISPCTX_CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE.
  217. * @param status
  218. * A pointer to a UErrorCode to receive any errors.
  219. * @return
  220. * A pointer to a URelativeDateTimeFormatter object for the specified locale,
  221. * or NULL if an error occurred.
  222. * @stable ICU 57
  223. */
  224. U_CAPI URelativeDateTimeFormatter* U_EXPORT2
  225. ureldatefmt_open( const char* locale,
  226. UNumberFormat* nfToAdopt,
  227. UDateRelativeDateTimeFormatterStyle width,
  228. UDisplayContext capitalizationContext,
  229. UErrorCode* status );
  230. /**
  231. * Close a URelativeDateTimeFormatter object. Once closed it may no longer be used.
  232. * @param reldatefmt
  233. * The URelativeDateTimeFormatter object to close.
  234. * @stable ICU 57
  235. */
  236. U_CAPI void U_EXPORT2
  237. ureldatefmt_close(URelativeDateTimeFormatter *reldatefmt);
  238. struct UFormattedRelativeDateTime;
  239. /**
  240. * Opaque struct to contain the results of a URelativeDateTimeFormatter operation.
  241. * @stable ICU 64
  242. */
  243. typedef struct UFormattedRelativeDateTime UFormattedRelativeDateTime;
  244. /**
  245. * Creates an object to hold the result of a URelativeDateTimeFormatter
  246. * operation. The object can be used repeatedly; it is cleared whenever
  247. * passed to a format function.
  248. *
  249. * @param ec Set if an error occurs.
  250. * @return A pointer needing ownership.
  251. * @stable ICU 64
  252. */
  253. U_CAPI UFormattedRelativeDateTime* U_EXPORT2
  254. ureldatefmt_openResult(UErrorCode* ec);
  255. /**
  256. * Returns a representation of a UFormattedRelativeDateTime as a UFormattedValue,
  257. * which can be subsequently passed to any API requiring that type.
  258. *
  259. * The returned object is owned by the UFormattedRelativeDateTime and is valid
  260. * only as long as the UFormattedRelativeDateTime is present and unchanged in memory.
  261. *
  262. * You can think of this method as a cast between types.
  263. *
  264. * @param ufrdt The object containing the formatted string.
  265. * @param ec Set if an error occurs.
  266. * @return A UFormattedValue owned by the input object.
  267. * @stable ICU 64
  268. */
  269. U_CAPI const UFormattedValue* U_EXPORT2
  270. ureldatefmt_resultAsValue(const UFormattedRelativeDateTime* ufrdt, UErrorCode* ec);
  271. /**
  272. * Releases the UFormattedRelativeDateTime created by ureldatefmt_openResult.
  273. *
  274. * @param ufrdt The object to release.
  275. * @stable ICU 64
  276. */
  277. U_CAPI void U_EXPORT2
  278. ureldatefmt_closeResult(UFormattedRelativeDateTime* ufrdt);
  279. #if U_SHOW_CPLUSPLUS_API
  280. U_NAMESPACE_BEGIN
  281. /**
  282. * \class LocalURelativeDateTimeFormatterPointer
  283. * "Smart pointer" class, closes a URelativeDateTimeFormatter via ureldatefmt_close().
  284. * For most methods see the LocalPointerBase base class.
  285. *
  286. * @see LocalPointerBase
  287. * @see LocalPointer
  288. * @stable ICU 57
  289. */
  290. U_DEFINE_LOCAL_OPEN_POINTER(LocalURelativeDateTimeFormatterPointer, URelativeDateTimeFormatter, ureldatefmt_close);
  291. /**
  292. * \class LocalUFormattedRelativeDateTimePointer
  293. * "Smart pointer" class, closes a UFormattedRelativeDateTime via ureldatefmt_closeResult().
  294. * For most methods see the LocalPointerBase base class.
  295. *
  296. * @see LocalPointerBase
  297. * @see LocalPointer
  298. * @stable ICU 64
  299. */
  300. U_DEFINE_LOCAL_OPEN_POINTER(LocalUFormattedRelativeDateTimePointer, UFormattedRelativeDateTime, ureldatefmt_closeResult);
  301. U_NAMESPACE_END
  302. #endif
  303. /**
  304. * Format a combination of URelativeDateTimeUnit and numeric
  305. * offset using a numeric style, e.g. "1 week ago", "in 1 week",
  306. * "5 weeks ago", "in 5 weeks".
  307. *
  308. * @param reldatefmt
  309. * The URelativeDateTimeFormatter object specifying the
  310. * format conventions.
  311. * @param offset
  312. * The signed offset for the specified unit. This will
  313. * be formatted according to this object's UNumberFormat
  314. * object.
  315. * @param unit
  316. * The unit to use when formatting the relative
  317. * date, e.g. UDAT_REL_UNIT_WEEK, UDAT_REL_UNIT_FRIDAY.
  318. * @param result
  319. * A pointer to a buffer to receive the formatted result.
  320. * @param resultCapacity
  321. * The maximum size of result.
  322. * @param status
  323. * A pointer to a UErrorCode to receive any errors. In
  324. * case of error status, the contents of result are
  325. * undefined.
  326. * @return
  327. * The length of the formatted result; may be greater
  328. * than resultCapacity, in which case an error is returned.
  329. * @stable ICU 57
  330. */
  331. U_CAPI int32_t U_EXPORT2
  332. ureldatefmt_formatNumeric( const URelativeDateTimeFormatter* reldatefmt,
  333. double offset,
  334. URelativeDateTimeUnit unit,
  335. UChar* result,
  336. int32_t resultCapacity,
  337. UErrorCode* status);
  338. /**
  339. * Format a combination of URelativeDateTimeUnit and numeric
  340. * offset using a numeric style, e.g. "1 week ago", "in 1 week",
  341. * "5 weeks ago", "in 5 weeks".
  342. *
  343. * @param reldatefmt
  344. * The URelativeDateTimeFormatter object specifying the
  345. * format conventions.
  346. * @param offset
  347. * The signed offset for the specified unit. This will
  348. * be formatted according to this object's UNumberFormat
  349. * object.
  350. * @param unit
  351. * The unit to use when formatting the relative
  352. * date, e.g. UDAT_REL_UNIT_WEEK, UDAT_REL_UNIT_FRIDAY.
  353. * @param result
  354. * A pointer to a UFormattedRelativeDateTime to populate.
  355. * @param status
  356. * A pointer to a UErrorCode to receive any errors. In
  357. * case of error status, the contents of result are
  358. * undefined.
  359. * @stable ICU 64
  360. */
  361. U_CAPI void U_EXPORT2
  362. ureldatefmt_formatNumericToResult(
  363. const URelativeDateTimeFormatter* reldatefmt,
  364. double offset,
  365. URelativeDateTimeUnit unit,
  366. UFormattedRelativeDateTime* result,
  367. UErrorCode* status);
  368. /**
  369. * Format a combination of URelativeDateTimeUnit and numeric offset
  370. * using a text style if possible, e.g. "last week", "this week",
  371. * "next week", "yesterday", "tomorrow". Falls back to numeric
  372. * style if no appropriate text term is available for the specified
  373. * offset in the object's locale.
  374. *
  375. * @param reldatefmt
  376. * The URelativeDateTimeFormatter object specifying the
  377. * format conventions.
  378. * @param offset
  379. * The signed offset for the specified unit.
  380. * @param unit
  381. * The unit to use when formatting the relative
  382. * date, e.g. UDAT_REL_UNIT_WEEK, UDAT_REL_UNIT_FRIDAY.
  383. * @param result
  384. * A pointer to a buffer to receive the formatted result.
  385. * @param resultCapacity
  386. * The maximum size of result.
  387. * @param status
  388. * A pointer to a UErrorCode to receive any errors. In
  389. * case of error status, the contents of result are
  390. * undefined.
  391. * @return
  392. * The length of the formatted result; may be greater
  393. * than resultCapacity, in which case an error is returned.
  394. * @stable ICU 57
  395. */
  396. U_CAPI int32_t U_EXPORT2
  397. ureldatefmt_format( const URelativeDateTimeFormatter* reldatefmt,
  398. double offset,
  399. URelativeDateTimeUnit unit,
  400. UChar* result,
  401. int32_t resultCapacity,
  402. UErrorCode* status);
  403. /**
  404. * Format a combination of URelativeDateTimeUnit and numeric offset
  405. * using a text style if possible, e.g. "last week", "this week",
  406. * "next week", "yesterday", "tomorrow". Falls back to numeric
  407. * style if no appropriate text term is available for the specified
  408. * offset in the object's locale.
  409. *
  410. * This method populates a UFormattedRelativeDateTime, which exposes more
  411. * information than the string populated by format().
  412. *
  413. * @param reldatefmt
  414. * The URelativeDateTimeFormatter object specifying the
  415. * format conventions.
  416. * @param offset
  417. * The signed offset for the specified unit.
  418. * @param unit
  419. * The unit to use when formatting the relative
  420. * date, e.g. UDAT_REL_UNIT_WEEK, UDAT_REL_UNIT_FRIDAY.
  421. * @param result
  422. * A pointer to a UFormattedRelativeDateTime to populate.
  423. * @param status
  424. * A pointer to a UErrorCode to receive any errors. In
  425. * case of error status, the contents of result are
  426. * undefined.
  427. * @stable ICU 64
  428. */
  429. U_CAPI void U_EXPORT2
  430. ureldatefmt_formatToResult(
  431. const URelativeDateTimeFormatter* reldatefmt,
  432. double offset,
  433. URelativeDateTimeUnit unit,
  434. UFormattedRelativeDateTime* result,
  435. UErrorCode* status);
  436. /**
  437. * Combines a relative date string and a time string in this object's
  438. * locale. This is done with the same date-time separator used for the
  439. * default calendar in this locale to produce a result such as
  440. * "yesterday at 3:45 PM".
  441. *
  442. * @param reldatefmt
  443. * The URelativeDateTimeFormatter object specifying the format conventions.
  444. * @param relativeDateString
  445. * The relative date string.
  446. * @param relativeDateStringLen
  447. * The length of relativeDateString; may be -1 if relativeDateString
  448. * is zero-terminated.
  449. * @param timeString
  450. * The time string.
  451. * @param timeStringLen
  452. * The length of timeString; may be -1 if timeString is zero-terminated.
  453. * @param result
  454. * A pointer to a buffer to receive the formatted result.
  455. * @param resultCapacity
  456. * The maximum size of result.
  457. * @param status
  458. * A pointer to a UErrorCode to receive any errors. In case of error status,
  459. * the contents of result are undefined.
  460. * @return
  461. * The length of the formatted result; may be greater than resultCapacity,
  462. * in which case an error is returned.
  463. * @stable ICU 57
  464. */
  465. U_CAPI int32_t U_EXPORT2
  466. ureldatefmt_combineDateAndTime( const URelativeDateTimeFormatter* reldatefmt,
  467. const UChar * relativeDateString,
  468. int32_t relativeDateStringLen,
  469. const UChar * timeString,
  470. int32_t timeStringLen,
  471. UChar* result,
  472. int32_t resultCapacity,
  473. UErrorCode* status );
  474. #endif /* !UCONFIG_NO_FORMATTING */
  475. #endif