udateintervalformat.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. // © 2016 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. /*
  4. *****************************************************************************************
  5. * Copyright (C) 2010-2012,2015 International Business Machines
  6. * Corporation and others. All Rights Reserved.
  7. *****************************************************************************************
  8. */
  9. #ifndef UDATEINTERVALFORMAT_H
  10. #define UDATEINTERVALFORMAT_H
  11. #include "unicode/utypes.h"
  12. #if !UCONFIG_NO_FORMATTING
  13. #include "unicode/ucal.h"
  14. #include "unicode/umisc.h"
  15. #include "unicode/uformattedvalue.h"
  16. #include "unicode/udisplaycontext.h"
  17. #if U_SHOW_CPLUSPLUS_API
  18. #include "unicode/localpointer.h"
  19. #endif // U_SHOW_CPLUSPLUS_API
  20. /**
  21. * \file
  22. * \brief C API: Format a date interval.
  23. *
  24. * A UDateIntervalFormat is used to format the range between two UDate values
  25. * in a locale-sensitive way, using a skeleton that specifies the precision and
  26. * completeness of the information to show. If the range smaller than the resolution
  27. * specified by the skeleton, a single date format will be produced. If the range
  28. * is larger than the format specified by the skeleton, a locale-specific fallback
  29. * will be used to format the items missing from the skeleton.
  30. *
  31. * For example, if the range is 2010-03-04 07:56 - 2010-03-04 19:56 (12 hours)
  32. * - The skeleton jm will produce
  33. * for en_US, "7:56 AM - 7:56 PM"
  34. * for en_GB, "7:56 - 19:56"
  35. * - The skeleton MMMd will produce
  36. * for en_US, "Mar 4"
  37. * for en_GB, "4 Mar"
  38. * If the range is 2010-03-04 07:56 - 2010-03-08 16:11 (4 days, 8 hours, 15 minutes)
  39. * - The skeleton jm will produce
  40. * for en_US, "3/4/2010 7:56 AM - 3/8/2010 4:11 PM"
  41. * for en_GB, "4/3/2010 7:56 - 8/3/2010 16:11"
  42. * - The skeleton MMMd will produce
  43. * for en_US, "Mar 4-8"
  44. * for en_GB, "4-8 Mar"
  45. *
  46. * Note: the "-" characters in the above sample output will actually be
  47. * Unicode 2013, EN_DASH, in all but the last example.
  48. *
  49. * Note, in ICU 4.4 the standard skeletons for which date interval format data
  50. * is usually available are as follows; best results will be obtained by using
  51. * skeletons from this set, or those formed by combining these standard skeletons
  52. * (note that for these skeletons, the length of digit field such as d, y, or
  53. * M vs MM is irrelevant (but for non-digit fields such as MMM vs MMMM it is
  54. * relevant). Note that a skeleton involving h or H generally explicitly requests
  55. * that time style (12- or 24-hour time respectively). For a skeleton that
  56. * requests the locale's default time style (h or H), use 'j' instead of h or H.
  57. * h, H, hm, Hm,
  58. * hv, Hv, hmv, Hmv,
  59. * d,
  60. * M, MMM, MMMM,
  61. * Md, MMMd,
  62. * MEd, MMMEd,
  63. * y,
  64. * yM, yMMM, yMMMM,
  65. * yMd, yMMMd,
  66. * yMEd, yMMMEd
  67. *
  68. * Locales for which ICU 4.4 seems to have a reasonable amount of this data
  69. * include:
  70. * af, am, ar, be, bg, bn, ca, cs, da, de (_AT), el, en (_AU,_CA,_GB,_IE,_IN...),
  71. * eo, es (_AR,_CL,_CO,...,_US) et, fa, fi, fo, fr (_BE,_CH,_CA), fur, gsw, he,
  72. * hr, hu, hy, is, it (_CH), ja, kk, km, ko, lt, lv, mk, ml, mt, nb, nl )_BE),
  73. * nn, pl, pt (_PT), rm, ro, ru (_UA), sk, sl, so, sq, sr, sr_Latn, sv, th, to,
  74. * tr, uk, ur, vi, zh (_SG), zh_Hant (_HK,_MO)
  75. */
  76. /**
  77. * Opaque UDateIntervalFormat object for use in C programs.
  78. * @stable ICU 4.8
  79. */
  80. struct UDateIntervalFormat;
  81. typedef struct UDateIntervalFormat UDateIntervalFormat; /**< C typedef for struct UDateIntervalFormat. @stable ICU 4.8 */
  82. struct UFormattedDateInterval;
  83. /**
  84. * Opaque struct to contain the results of a UDateIntervalFormat operation.
  85. * @stable ICU 64
  86. */
  87. typedef struct UFormattedDateInterval UFormattedDateInterval;
  88. /**
  89. * Open a new UDateIntervalFormat object using the predefined rules for a
  90. * given locale plus a specified skeleton.
  91. * @param locale
  92. * The locale for whose rules should be used; may be NULL for
  93. * default locale.
  94. * @param skeleton
  95. * A pattern containing only the fields desired for the interval
  96. * format, for example "Hm", "yMMMd", or "yMMMEdHm".
  97. * @param skeletonLength
  98. * The length of skeleton; may be -1 if the skeleton is zero-terminated.
  99. * @param tzID
  100. * A timezone ID specifying the timezone to use. If 0, use the default
  101. * timezone.
  102. * @param tzIDLength
  103. * The length of tzID, or -1 if null-terminated. If 0, use the default
  104. * timezone.
  105. * @param status
  106. * A pointer to a UErrorCode to receive any errors.
  107. * @return
  108. * A pointer to a UDateIntervalFormat object for the specified locale,
  109. * or NULL if an error occurred.
  110. * @stable ICU 4.8
  111. */
  112. U_CAPI UDateIntervalFormat* U_EXPORT2
  113. udtitvfmt_open(const char* locale,
  114. const UChar* skeleton,
  115. int32_t skeletonLength,
  116. const UChar* tzID,
  117. int32_t tzIDLength,
  118. UErrorCode* status);
  119. /**
  120. * Close a UDateIntervalFormat object. Once closed it may no longer be used.
  121. * @param formatter
  122. * The UDateIntervalFormat object to close.
  123. * @stable ICU 4.8
  124. */
  125. U_CAPI void U_EXPORT2
  126. udtitvfmt_close(UDateIntervalFormat *formatter);
  127. /**
  128. * Creates an object to hold the result of a UDateIntervalFormat
  129. * operation. The object can be used repeatedly; it is cleared whenever
  130. * passed to a format function.
  131. *
  132. * @param ec Set if an error occurs.
  133. * @return A pointer needing ownership.
  134. * @stable ICU 64
  135. */
  136. U_CAPI UFormattedDateInterval* U_EXPORT2
  137. udtitvfmt_openResult(UErrorCode* ec);
  138. /**
  139. * Returns a representation of a UFormattedDateInterval as a UFormattedValue,
  140. * which can be subsequently passed to any API requiring that type.
  141. *
  142. * The returned object is owned by the UFormattedDateInterval and is valid
  143. * only as long as the UFormattedDateInterval is present and unchanged in memory.
  144. *
  145. * You can think of this method as a cast between types.
  146. *
  147. * When calling ufmtval_nextPosition():
  148. * The fields are returned from left to right. The special field category
  149. * UFIELD_CATEGORY_DATE_INTERVAL_SPAN is used to indicate which datetime
  150. * primitives came from which arguments: 0 means fromCalendar, and 1 means
  151. * toCalendar. The span category will always occur before the
  152. * corresponding fields in UFIELD_CATEGORY_DATE
  153. * in the ufmtval_nextPosition() iterator.
  154. *
  155. * @param uresult The object containing the formatted string.
  156. * @param ec Set if an error occurs.
  157. * @return A UFormattedValue owned by the input object.
  158. * @stable ICU 64
  159. */
  160. U_CAPI const UFormattedValue* U_EXPORT2
  161. udtitvfmt_resultAsValue(const UFormattedDateInterval* uresult, UErrorCode* ec);
  162. /**
  163. * Releases the UFormattedDateInterval created by udtitvfmt_openResult().
  164. *
  165. * @param uresult The object to release.
  166. * @stable ICU 64
  167. */
  168. U_CAPI void U_EXPORT2
  169. udtitvfmt_closeResult(UFormattedDateInterval* uresult);
  170. #if U_SHOW_CPLUSPLUS_API
  171. U_NAMESPACE_BEGIN
  172. /**
  173. * \class LocalUDateIntervalFormatPointer
  174. * "Smart pointer" class, closes a UDateIntervalFormat via udtitvfmt_close().
  175. * For most methods see the LocalPointerBase base class.
  176. *
  177. * @see LocalPointerBase
  178. * @see LocalPointer
  179. * @stable ICU 4.8
  180. */
  181. U_DEFINE_LOCAL_OPEN_POINTER(LocalUDateIntervalFormatPointer, UDateIntervalFormat, udtitvfmt_close);
  182. /**
  183. * \class LocalUFormattedDateIntervalPointer
  184. * "Smart pointer" class, closes a UFormattedDateInterval via udtitvfmt_close().
  185. * For most methods see the LocalPointerBase base class.
  186. *
  187. * @see LocalPointerBase
  188. * @see LocalPointer
  189. * @stable ICU 64
  190. */
  191. U_DEFINE_LOCAL_OPEN_POINTER(LocalUFormattedDateIntervalPointer, UFormattedDateInterval, udtitvfmt_closeResult);
  192. U_NAMESPACE_END
  193. #endif
  194. /**
  195. * Formats a date/time range using the conventions established for the
  196. * UDateIntervalFormat object.
  197. * @param formatter
  198. * The UDateIntervalFormat object specifying the format conventions.
  199. * @param fromDate
  200. * The starting point of the range.
  201. * @param toDate
  202. * The ending point of the range.
  203. * @param result
  204. * A pointer to a buffer to receive the formatted range.
  205. * @param resultCapacity
  206. * The maximum size of result.
  207. * @param position
  208. * A pointer to a UFieldPosition. On input, position->field is read.
  209. * On output, position->beginIndex and position->endIndex indicate
  210. * the beginning and ending indices of field number position->field,
  211. * if such a field exists. This parameter may be NULL, in which case
  212. * no field position data is returned.
  213. * There may be multiple instances of a given field type in an
  214. * interval format; in this case the position indices refer to the
  215. * first instance.
  216. * @param status
  217. * A pointer to a UErrorCode to receive any errors.
  218. * @return
  219. * The total buffer size needed; if greater than resultLength, the
  220. * output was truncated.
  221. * @stable ICU 4.8
  222. */
  223. U_CAPI int32_t U_EXPORT2
  224. udtitvfmt_format(const UDateIntervalFormat* formatter,
  225. UDate fromDate,
  226. UDate toDate,
  227. UChar* result,
  228. int32_t resultCapacity,
  229. UFieldPosition* position,
  230. UErrorCode* status);
  231. /**
  232. * Formats a date/time range using the conventions established for the
  233. * UDateIntervalFormat object.
  234. * @param formatter
  235. * The UDateIntervalFormat object specifying the format conventions.
  236. * @param fromDate
  237. * The starting point of the range.
  238. * @param toDate
  239. * The ending point of the range.
  240. * @param result
  241. * The UFormattedDateInterval to contain the result of the
  242. * formatting operation.
  243. * @param status
  244. * A pointer to a UErrorCode to receive any errors.
  245. * @stable ICU 67
  246. */
  247. U_CAPI void U_EXPORT2
  248. udtitvfmt_formatToResult(
  249. const UDateIntervalFormat* formatter,
  250. UDate fromDate,
  251. UDate toDate,
  252. UFormattedDateInterval* result,
  253. UErrorCode* status);
  254. /**
  255. * Formats a date/time range using the conventions established for the
  256. * UDateIntervalFormat object.
  257. * @param formatter
  258. * The UDateIntervalFormat object specifying the format conventions.
  259. * @param fromCalendar
  260. * The starting point of the range.
  261. * @param toCalendar
  262. * The ending point of the range.
  263. * @param result
  264. * The UFormattedDateInterval to contain the result of the
  265. * formatting operation.
  266. * @param status
  267. * A pointer to a UErrorCode to receive any errors.
  268. * @stable ICU 67
  269. */
  270. U_CAPI void U_EXPORT2
  271. udtitvfmt_formatCalendarToResult(
  272. const UDateIntervalFormat* formatter,
  273. UCalendar* fromCalendar,
  274. UCalendar* toCalendar,
  275. UFormattedDateInterval* result,
  276. UErrorCode* status);
  277. /**
  278. * Set a particular UDisplayContext value in the formatter, such as
  279. * UDISPCTX_CAPITALIZATION_FOR_STANDALONE. This causes the formatted
  280. * result to be capitalized appropriately for the context in which
  281. * it is intended to be used, considering both the locale and the
  282. * type of field at the beginning of the formatted result.
  283. * @param formatter The formatter for which to set a UDisplayContext value.
  284. * @param value The UDisplayContext value to set.
  285. * @param status A pointer to an UErrorCode to receive any errors
  286. * @stable ICU 68
  287. */
  288. U_CAPI void U_EXPORT2
  289. udtitvfmt_setContext(UDateIntervalFormat* formatter, UDisplayContext value, UErrorCode* status);
  290. /**
  291. * Get the formatter's UDisplayContext value for the specified UDisplayContextType,
  292. * such as UDISPCTX_TYPE_CAPITALIZATION.
  293. * @param formatter The formatter to query.
  294. * @param type The UDisplayContextType whose value to return
  295. * @param status A pointer to an UErrorCode to receive any errors
  296. * @return The UDisplayContextValue for the specified type.
  297. * @stable ICU 68
  298. */
  299. U_CAPI UDisplayContext U_EXPORT2
  300. udtitvfmt_getContext(const UDateIntervalFormat* formatter, UDisplayContextType type, UErrorCode* status);
  301. #endif /* #if !UCONFIG_NO_FORMATTING */
  302. #endif