ureldatefmt.h 17 KB

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