1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 *****************************************************************************************
5 * Copyright (C) 2010-2012,2015-2016 International Business Machines
6 * Corporation and others. All Rights Reserved.
7 *****************************************************************************************
10 #ifndef UDATEINTERVALFORMAT_H
11 #define UDATEINTERVALFORMAT_H
13 #include "unicode/utypes.h"
15 #if !UCONFIG_NO_FORMATTING
17 #include "unicode/umisc.h"
18 #include "unicode/localpointer.h"
19 #include "unicode/uformattedvalue.h"
20 #include "unicode/udisplaycontext.h"
24 * \brief C API: Format a date interval.
26 * A UDateIntervalFormat is used to format the range between two UDate values
27 * in a locale-sensitive way, using a skeleton that specifies the precision and
28 * completeness of the information to show. If the range smaller than the resolution
29 * specified by the skeleton, a single date format will be produced. If the range
30 * is larger than the format specified by the skeleton, a locale-specific fallback
31 * will be used to format the items missing from the skeleton.
33 * For example, if the range is 2010-03-04 07:56 - 2010-03-04 19:56 (12 hours)
34 * - The skeleton jm will produce
35 * for en_US, "7:56 AM - 7:56 PM"
36 * for en_GB, "7:56 - 19:56"
37 * - The skeleton MMMd will produce
40 * If the range is 2010-03-04 07:56 - 2010-03-08 16:11 (4 days, 8 hours, 15 minutes)
41 * - The skeleton jm will produce
42 * for en_US, "3/4/2010 7:56 AM - 3/8/2010 4:11 PM"
43 * for en_GB, "4/3/2010 7:56 - 8/3/2010 16:11"
44 * - The skeleton MMMd will produce
45 * for en_US, "Mar 4-8"
46 * for en_GB, "4-8 Mar"
48 * Note: the "-" characters in the above sample output will actually be
49 * Unicode 2013, EN_DASH, in all but the last example.
51 * Note, in ICU 4.4 the standard skeletons for which date interval format data
52 * is usually available are as follows; best results will be obtained by using
53 * skeletons from this set, or those formed by combining these standard skeletons
54 * (note that for these skeletons, the length of digit field such as d, y, or
55 * M vs MM is irrelevant (but for non-digit fields such as MMM vs MMMM it is
56 * relevant). Note that a skeleton involving h or H generally explicitly requests
57 * that time style (12- or 24-hour time respectively). For a skeleton that
58 * requests the locale's default time style (h or H), use 'j' instead of h or H.
70 * Locales for which ICU 4.4 seems to have a reasonable amount of this data
72 * af, am, ar, be, bg, bn, ca, cs, da, de (_AT), el, en (_AU,_CA,_GB,_IE,_IN...),
73 * eo, es (_AR,_CL,_CO,...,_US) et, fa, fi, fo, fr (_BE,_CH,_CA), fur, gsw, he,
74 * hr, hu, hy, is, it (_CH), ja, kk, km, ko, lt, lv, mk, ml, mt, nb, nl )_BE),
75 * nn, pl, pt (_PT), rm, ro, ru (_UA), sk, sl, so, sq, sr, sr_Latn, sv, th, to,
76 * tr, uk, ur, vi, zh (_SG), zh_Hant (_HK,_MO)
80 * Opaque UDateIntervalFormat object for use in C programs.
83 struct UDateIntervalFormat
;
84 typedef struct UDateIntervalFormat UDateIntervalFormat
; /**< C typedef for struct UDateIntervalFormat. @stable ICU 4.8 */
86 #ifndef U_HIDE_DRAFT_API
87 struct UFormattedDateInterval
;
89 * Opaque struct to contain the results of a UDateIntervalFormat operation.
92 typedef struct UFormattedDateInterval UFormattedDateInterval
;
93 #endif /* U_HIDE_DRAFT_API */
96 * Open a new UDateIntervalFormat object using the predefined rules for a
97 * given locale plus a specified skeleton.
99 * The locale for whose rules should be used; may be NULL for
102 * A pattern containing only the fields desired for the interval
103 * format, for example "Hm", "yMMMd", or "yMMMEdHm".
104 * @param skeletonLength
105 * The length of skeleton; may be -1 if the skeleton is zero-terminated.
107 * A timezone ID specifying the timezone to use. If 0, use the default
110 * The length of tzID, or -1 if null-terminated. If 0, use the default
113 * A pointer to a UErrorCode to receive any errors.
115 * A pointer to a UDateIntervalFormat object for the specified locale,
116 * or NULL if an error occurred.
119 U_STABLE UDateIntervalFormat
* U_EXPORT2
120 udtitvfmt_open(const char* locale
,
121 const UChar
* skeleton
,
122 int32_t skeletonLength
,
128 * Close a UDateIntervalFormat object. Once closed it may no longer be used.
130 * The UDateIntervalFormat object to close.
133 U_STABLE
void U_EXPORT2
134 udtitvfmt_close(UDateIntervalFormat
*formatter
);
137 #ifndef U_HIDE_DRAFT_API
139 * Creates an object to hold the result of a UDateIntervalFormat
140 * operation. The object can be used repeatedly; it is cleared whenever
141 * passed to a format function.
143 * @param ec Set if an error occurs.
144 * @return A pointer needing ownership.
147 U_CAPI UFormattedDateInterval
* U_EXPORT2
148 udtitvfmt_openResult(UErrorCode
* ec
);
151 * Returns a representation of a UFormattedDateInterval as a UFormattedValue,
152 * which can be subsequently passed to any API requiring that type.
154 * The returned object is owned by the UFormattedDateInterval and is valid
155 * only as long as the UFormattedDateInterval is present and unchanged in memory.
157 * You can think of this method as a cast between types.
159 * When calling ufmtval_nextPosition():
160 * The fields are returned from left to right. The special field category
161 * UFIELD_CATEGORY_DATE_INTERVAL_SPAN is used to indicate which datetime
162 * primitives came from which arguments: 0 means fromCalendar, and 1 means
163 * toCalendar. The span category will always occur before the
164 * corresponding fields in UFIELD_CATEGORY_DATE
165 * in the ufmtval_nextPosition() iterator.
167 * @param uresult The object containing the formatted string.
168 * @param ec Set if an error occurs.
169 * @return A UFormattedValue owned by the input object.
172 U_CAPI
const UFormattedValue
* U_EXPORT2
173 udtitvfmt_resultAsValue(const UFormattedDateInterval
* uresult
, UErrorCode
* ec
);
176 * Releases the UFormattedDateInterval created by udtitvfmt_openResult().
178 * @param uresult The object to release.
181 U_CAPI
void U_EXPORT2
182 udtitvfmt_closeResult(UFormattedDateInterval
* uresult
);
183 #endif /* U_HIDE_DRAFT_API */
186 #if U_SHOW_CPLUSPLUS_API
191 * \class LocalUDateIntervalFormatPointer
192 * "Smart pointer" class, closes a UDateIntervalFormat via udtitvfmt_close().
193 * For most methods see the LocalPointerBase base class.
195 * @see LocalPointerBase
199 U_DEFINE_LOCAL_OPEN_POINTER(LocalUDateIntervalFormatPointer
, UDateIntervalFormat
, udtitvfmt_close
);
201 #ifndef U_HIDE_DRAFT_API
203 * \class LocalUFormattedDateIntervalPointer
204 * "Smart pointer" class, closes a UFormattedDateInterval via udtitvfmt_close().
205 * For most methods see the LocalPointerBase base class.
207 * @see LocalPointerBase
211 U_DEFINE_LOCAL_OPEN_POINTER(LocalUFormattedDateIntervalPointer
, UFormattedDateInterval
, udtitvfmt_closeResult
);
212 #endif /* U_HIDE_DRAFT_API */
220 * Formats a date/time range using the conventions established for the
221 * UDateIntervalFormat object.
223 * The UDateIntervalFormat object specifying the format conventions.
225 * The starting point of the range.
227 * The ending point of the range.
229 * A pointer to a buffer to receive the formatted range.
230 * @param resultCapacity
231 * The maximum size of result.
233 * A pointer to a UFieldPosition. On input, position->field is read.
234 * On output, position->beginIndex and position->endIndex indicate
235 * the beginning and ending indices of field number position->field,
236 * if such a field exists. This parameter may be NULL, in which case
237 * no field position data is returned.
238 * There may be multiple instances of a given field type in an
239 * interval format; in this case the position indices refer to the
242 * A pointer to a UErrorCode to receive any errors.
244 * The total buffer size needed; if greater than resultLength, the
245 * output was truncated.
248 U_STABLE
int32_t U_EXPORT2
249 udtitvfmt_format(const UDateIntervalFormat
* formatter
,
253 int32_t resultCapacity
,
254 UFieldPosition
* position
,
257 #ifndef U_HIDE_DRAFT_API
259 * Attributes and values to control the behavior of udtitvfmt_format.
262 typedef enum UDateIntervalFormatAttribute
{
266 UDTITVFMT_MINIMIZE_TYPE
267 } UDateIntervalFormatAttribute
;
269 typedef enum UDateIntervalFormatAttributeValue
{
271 * Standard behavior, no additional minimization.
274 UDTITVFMT_MINIMIZE_NONE
= 0,
276 * For intervals of less than 1 month that cross month boundaries,
277 * only show one month (use format for greatestDifference=d).
280 UDTITVFMT_MINIMIZE_ADJACENT_MONTHS
,
282 * For intervals of less than 12 hours that cross day boundaries,
283 * only show one day (use format for greatestDifference=h).
286 UDTITVFMT_MINIMIZE_ADJACENT_DAYS
287 } UDateIntervalFormatAttributeValue
;
290 * Change attributes for the UDateIntervalFormat object.
292 * The UDateIntervalFormat object whose attributes are to be changed.
294 * The attribute to change.
296 * The new value for the attribute.
298 * A pointer to a UErrorCode to receive any errors.
301 U_INTERNAL
void U_EXPORT2
302 udtitvfmt_setAttribute(UDateIntervalFormat
* formatter
,
303 UDateIntervalFormatAttribute attr
,
304 UDateIntervalFormatAttributeValue value
,
307 #endif /* U_HIDE_DRAFT_API */
309 #ifndef U_HIDE_DRAFT_API
311 * Formats a date/time range using the conventions established for the
312 * UDateIntervalFormat object.
314 * The UDateIntervalFormat object specifying the format conventions.
316 * The UFormattedDateInterval to contain the result of the
317 * formatting operation.
319 * The starting point of the range.
321 * The ending point of the range.
323 * A pointer to a UErrorCode to receive any errors.
326 U_DRAFT
void U_EXPORT2
327 udtitvfmt_formatToResult(
328 const UDateIntervalFormat
* formatter
,
329 UFormattedDateInterval
* result
,
333 #endif /* U_HIDE_DRAFT_API */
335 #ifndef U_HIDE_DRAFT_API
337 * Set a particular UDisplayContext value in the formatter, such as
338 * UDISPCTX_CAPITALIZATION_FOR_STANDALONE. This causes the formatted
339 * result to be capitalized appropriately for the context in which
340 * it is intended to be used, considering both the locale and the
341 * type of field at the beginning of the formatted result.
342 * @param formatter The formatter for which to set a UDisplayContext value.
343 * @param value The UDisplayContext value to set.
344 * @param status A pointer to an UErrorCode to receive any errors
347 U_DRAFT
void U_EXPORT2
348 udtitvfmt_setContext(UDateIntervalFormat
* formatter
, UDisplayContext value
, UErrorCode
* status
);
351 * Get the formatter's UDisplayContext value for the specified UDisplayContextType,
352 * such as UDISPCTX_TYPE_CAPITALIZATION.
353 * @param formatter The formatter to query.
354 * @param type The UDisplayContextType whose value to return
355 * @param status A pointer to an UErrorCode to receive any errors
356 * @return The UDisplayContextValue for the specified type.
359 U_DRAFT UDisplayContext U_EXPORT2
360 udtitvfmt_getContext(const UDateIntervalFormat
* formatter
, UDisplayContextType type
, UErrorCode
* status
);
362 #endif /* U_HIDE_DRAFT_API */
364 #endif /* #if !UCONFIG_NO_FORMATTING */