udateintervalformat.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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) 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/umisc.h"
  14. #include "unicode/localpointer.h"
  15. /**
  16. * \file
  17. * \brief C API: Format a date interval.
  18. *
  19. * A UDateIntervalFormat is used to format the range between two UDate values
  20. * in a locale-sensitive way, using a skeleton that specifies the precision and
  21. * completeness of the information to show. If the range smaller than the resolution
  22. * specified by the skeleton, a single date format will be produced. If the range
  23. * is larger than the format specified by the skeleton, a locale-specific fallback
  24. * will be used to format the items missing from the skeleton.
  25. *
  26. * For example, if the range is 2010-03-04 07:56 - 2010-03-04 19:56 (12 hours)
  27. * - The skeleton jm will produce
  28. * for en_US, "7:56 AM - 7:56 PM"
  29. * for en_GB, "7:56 - 19:56"
  30. * - The skeleton MMMd will produce
  31. * for en_US, "Mar 4"
  32. * for en_GB, "4 Mar"
  33. * If the range is 2010-03-04 07:56 - 2010-03-08 16:11 (4 days, 8 hours, 15 minutes)
  34. * - The skeleton jm will produce
  35. * for en_US, "3/4/2010 7:56 AM - 3/8/2010 4:11 PM"
  36. * for en_GB, "4/3/2010 7:56 - 8/3/2010 16:11"
  37. * - The skeleton MMMd will produce
  38. * for en_US, "Mar 4-8"
  39. * for en_GB, "4-8 Mar"
  40. *
  41. * Note: the "-" characters in the above sample output will actually be
  42. * Unicode 2013, EN_DASH, in all but the last example.
  43. *
  44. * Note, in ICU 4.4 the standard skeletons for which date interval format data
  45. * is usually available are as follows; best results will be obtained by using
  46. * skeletons from this set, or those formed by combining these standard skeletons
  47. * (note that for these skeletons, the length of digit field such as d, y, or
  48. * M vs MM is irrelevant (but for non-digit fields such as MMM vs MMMM it is
  49. * relevant). Note that a skeleton involving h or H generally explicitly requests
  50. * that time style (12- or 24-hour time respectively). For a skeleton that
  51. * requests the locale's default time style (h or H), use 'j' instead of h or H.
  52. * h, H, hm, Hm,
  53. * hv, Hv, hmv, Hmv,
  54. * d,
  55. * M, MMM, MMMM,
  56. * Md, MMMd,
  57. * MEd, MMMEd,
  58. * y,
  59. * yM, yMMM, yMMMM,
  60. * yMd, yMMMd,
  61. * yMEd, yMMMEd
  62. *
  63. * Locales for which ICU 4.4 seems to have a reasonable amount of this data
  64. * include:
  65. * af, am, ar, be, bg, bn, ca, cs, da, de (_AT), el, en (_AU,_CA,_GB,_IE,_IN...),
  66. * eo, es (_AR,_CL,_CO,...,_US) et, fa, fi, fo, fr (_BE,_CH,_CA), fur, gsw, he,
  67. * hr, hu, hy, is, it (_CH), ja, kk, km, ko, lt, lv, mk, ml, mt, nb, nl )_BE),
  68. * nn, pl, pt (_PT), rm, ro, ru (_UA), sk, sl, so, sq, sr, sr_Latn, sv, th, to,
  69. * tr, uk, ur, vi, zh (_SG), zh_Hant (_HK,_MO)
  70. */
  71. /**
  72. * Opaque UDateIntervalFormat object for use in C programs.
  73. * @stable ICU 4.8
  74. */
  75. struct UDateIntervalFormat;
  76. typedef struct UDateIntervalFormat UDateIntervalFormat; /**< C typedef for struct UDateIntervalFormat. @stable ICU 4.8 */
  77. /**
  78. * Open a new UDateIntervalFormat object using the predefined rules for a
  79. * given locale plus a specified skeleton.
  80. * @param locale
  81. * The locale for whose rules should be used; may be NULL for
  82. * default locale.
  83. * @param skeleton
  84. * A pattern containing only the fields desired for the interval
  85. * format, for example "Hm", "yMMMd", or "yMMMEdHm".
  86. * @param skeletonLength
  87. * The length of skeleton; may be -1 if the skeleton is zero-terminated.
  88. * @param tzID
  89. * A timezone ID specifying the timezone to use. If 0, use the default
  90. * timezone.
  91. * @param tzIDLength
  92. * The length of tzID, or -1 if null-terminated. If 0, use the default
  93. * timezone.
  94. * @param status
  95. * A pointer to a UErrorCode to receive any errors.
  96. * @return
  97. * A pointer to a UDateIntervalFormat object for the specified locale,
  98. * or NULL if an error occurred.
  99. * @stable ICU 4.8
  100. */
  101. U_STABLE UDateIntervalFormat* U_EXPORT2
  102. udtitvfmt_open(const char* locale,
  103. const UChar* skeleton,
  104. int32_t skeletonLength,
  105. const UChar* tzID,
  106. int32_t tzIDLength,
  107. UErrorCode* status);
  108. /**
  109. * Close a UDateIntervalFormat object. Once closed it may no longer be used.
  110. * @param formatter
  111. * The UDateIntervalFormat object to close.
  112. * @stable ICU 4.8
  113. */
  114. U_STABLE void U_EXPORT2
  115. udtitvfmt_close(UDateIntervalFormat *formatter);
  116. #if U_SHOW_CPLUSPLUS_API
  117. U_NAMESPACE_BEGIN
  118. /**
  119. * \class LocalUDateIntervalFormatPointer
  120. * "Smart pointer" class, closes a UDateIntervalFormat via udtitvfmt_close().
  121. * For most methods see the LocalPointerBase base class.
  122. *
  123. * @see LocalPointerBase
  124. * @see LocalPointer
  125. * @stable ICU 4.8
  126. */
  127. U_DEFINE_LOCAL_OPEN_POINTER(LocalUDateIntervalFormatPointer, UDateIntervalFormat, udtitvfmt_close);
  128. U_NAMESPACE_END
  129. #endif
  130. /**
  131. * Formats a date/time range using the conventions established for the
  132. * UDateIntervalFormat object.
  133. * @param formatter
  134. * The UDateIntervalFormat object specifying the format conventions.
  135. * @param fromDate
  136. * The starting point of the range.
  137. * @param toDate
  138. * The ending point of the range.
  139. * @param result
  140. * A pointer to a buffer to receive the formatted range.
  141. * @param resultCapacity
  142. * The maximum size of result.
  143. * @param position
  144. * A pointer to a UFieldPosition. On input, position->field is read.
  145. * On output, position->beginIndex and position->endIndex indicate
  146. * the beginning and ending indices of field number position->field,
  147. * if such a field exists. This parameter may be NULL, in which case
  148. * no field position data is returned.
  149. * There may be multiple instances of a given field type in an
  150. * interval format; in this case the position indices refer to the
  151. * first instance.
  152. * @param status
  153. * A pointer to a UErrorCode to receive any errors.
  154. * @return
  155. * The total buffer size needed; if greater than resultLength, the
  156. * output was truncated.
  157. * @stable ICU 4.8
  158. */
  159. U_STABLE int32_t U_EXPORT2
  160. udtitvfmt_format(const UDateIntervalFormat* formatter,
  161. UDate fromDate,
  162. UDate toDate,
  163. UChar* result,
  164. int32_t resultCapacity,
  165. UFieldPosition* position,
  166. UErrorCode* status);
  167. #endif /* #if !UCONFIG_NO_FORMATTING */
  168. #endif