udateintervalformat.h 6.7 KB

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