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
10#ifndef UDATEINTERVALFORMAT_H
11#define UDATEINTERVALFORMAT_H
12
13#include "unicode/utypes.h"
14
15#if !UCONFIG_NO_FORMATTING
16
17#include "unicode/ucal.h"
18#include "unicode/umisc.h"
19#include "unicode/localpointer.h"
20#include "unicode/uformattedvalue.h"
21
22/**
23 * \file
24 * \brief C API: Format a date interval.
25 *
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.
32 *
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
38 * for en_US, "Mar 4"
39 * for en_GB, "4 Mar"
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"
47 *
48 * Note: the "-" characters in the above sample output will actually be
49 * Unicode 2013, EN_DASH, in all but the last example.
50 *
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.
59 * h, H, hm, Hm,
60 * hv, Hv, hmv, Hmv,
61 * d,
62 * M, MMM, MMMM,
63 * Md, MMMd,
64 * MEd, MMMEd,
65 * y,
66 * yM, yMMM, yMMMM,
67 * yMd, yMMMd,
68 * yMEd, yMMMEd
69 *
70 * Locales for which ICU 4.4 seems to have a reasonable amount of this data
71 * include:
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)
77 */
78
79/**
80 * Opaque UDateIntervalFormat object for use in C programs.
81 * @stable ICU 4.8
82 */
83struct UDateIntervalFormat;
84typedef struct UDateIntervalFormat UDateIntervalFormat; /**< C typedef for struct UDateIntervalFormat. @stable ICU 4.8 */
85
86#ifndef U_HIDE_DRAFT_API
87struct UFormattedDateInterval;
88/**
89 * Opaque struct to contain the results of a UDateIntervalFormat operation.
90 * @draft ICU 64
91 */
92typedef struct UFormattedDateInterval UFormattedDateInterval;
93#endif /* U_HIDE_DRAFT_API */
94
95/**
96 * Open a new UDateIntervalFormat object using the predefined rules for a
97 * given locale plus a specified skeleton.
98 * @param locale
99 * The locale for whose rules should be used; may be NULL for
100 * default locale.
101 * @param skeleton
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.
106 * @param tzID
107 * A timezone ID specifying the timezone to use. If 0, use the default
108 * timezone.
109 * @param tzIDLength
110 * The length of tzID, or -1 if null-terminated. If 0, use the default
111 * timezone.
112 * @param status
113 * A pointer to a UErrorCode to receive any errors.
114 * @return
115 * A pointer to a UDateIntervalFormat object for the specified locale,
116 * or NULL if an error occurred.
117 * @stable ICU 4.8
118 */
119U_STABLE UDateIntervalFormat* U_EXPORT2
120udtitvfmt_open(const char* locale,
121 const UChar* skeleton,
122 int32_t skeletonLength,
123 const UChar* tzID,
124 int32_t tzIDLength,
125 UErrorCode* status);
126
127/**
128 * Close a UDateIntervalFormat object. Once closed it may no longer be used.
129 * @param formatter
130 * The UDateIntervalFormat object to close.
131 * @stable ICU 4.8
132 */
133U_STABLE void U_EXPORT2
134udtitvfmt_close(UDateIntervalFormat *formatter);
135
136
137#ifndef U_HIDE_DRAFT_API
138/**
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.
142 *
143 * @param ec Set if an error occurs.
144 * @return A pointer needing ownership.
145 * @draft ICU 64
146 */
147U_CAPI UFormattedDateInterval* U_EXPORT2
148udtitvfmt_openResult(UErrorCode* ec);
149
150/**
151 * Returns a representation of a UFormattedDateInterval as a UFormattedValue,
152 * which can be subsequently passed to any API requiring that type.
153 *
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.
156 *
157 * You can think of this method as a cast between types.
158 *
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.
166 *
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.
170 * @draft ICU 64
171 */
172U_CAPI const UFormattedValue* U_EXPORT2
173udtitvfmt_resultAsValue(const UFormattedDateInterval* uresult, UErrorCode* ec);
174
175/**
176 * Releases the UFormattedDateInterval created by udtitvfmt_openResult().
177 *
178 * @param uresult The object to release.
179 * @draft ICU 64
180 */
181U_CAPI void U_EXPORT2
182udtitvfmt_closeResult(UFormattedDateInterval* uresult);
183#endif /* U_HIDE_DRAFT_API */
184
185
186#if U_SHOW_CPLUSPLUS_API
187
188U_NAMESPACE_BEGIN
189
190/**
191 * \class LocalUDateIntervalFormatPointer
192 * "Smart pointer" class, closes a UDateIntervalFormat via udtitvfmt_close().
193 * For most methods see the LocalPointerBase base class.
194 *
195 * @see LocalPointerBase
196 * @see LocalPointer
197 * @stable ICU 4.8
198 */
199U_DEFINE_LOCAL_OPEN_POINTER(LocalUDateIntervalFormatPointer, UDateIntervalFormat, udtitvfmt_close);
200
201#ifndef U_HIDE_DRAFT_API
202/**
203 * \class LocalUFormattedDateIntervalPointer
204 * "Smart pointer" class, closes a UFormattedDateInterval via udtitvfmt_close().
205 * For most methods see the LocalPointerBase base class.
206 *
207 * @see LocalPointerBase
208 * @see LocalPointer
209 * @draft ICU 64
210 */
211U_DEFINE_LOCAL_OPEN_POINTER(LocalUFormattedDateIntervalPointer, UFormattedDateInterval, udtitvfmt_closeResult);
212#endif /* U_HIDE_DRAFT_API */
213
214U_NAMESPACE_END
215
216#endif
217
218
219/**
220 * Formats a date/time range using the conventions established for the
221 * UDateIntervalFormat object.
222 * @param formatter
223 * The UDateIntervalFormat object specifying the format conventions.
224 * @param fromDate
225 * The starting point of the range.
226 * @param toDate
227 * The ending point of the range.
228 * @param result
229 * A pointer to a buffer to receive the formatted range.
230 * @param resultCapacity
231 * The maximum size of result.
232 * @param position
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
240 * first instance.
241 * @param status
242 * A pointer to a UErrorCode to receive any errors.
243 * @return
244 * The total buffer size needed; if greater than resultLength, the
245 * output was truncated.
246 * @stable ICU 4.8
247 */
248U_STABLE int32_t U_EXPORT2
249udtitvfmt_format(const UDateIntervalFormat* formatter,
250 UDate fromDate,
251 UDate toDate,
252 UChar* result,
253 int32_t resultCapacity,
254 UFieldPosition* position,
255 UErrorCode* status);
256
257
258#ifndef U_HIDE_DRAFT_API
259/**
260 * Formats a date/time range using the conventions established for the
261 * UDateIntervalFormat object.
262 * @param formatter
263 * The UDateIntervalFormat object specifying the format conventions.
264 * @param fromDate
265 * The starting point of the range.
266 * @param toDate
267 * The ending point of the range.
268 * @param result
269 * The UFormattedDateInterval to contain the result of the
270 * formatting operation.
271 * @param status
272 * A pointer to a UErrorCode to receive any errors.
273 * @draft ICU 67
274 */
275U_DRAFT void U_EXPORT2
276udtitvfmt_formatToResult(
277 const UDateIntervalFormat* formatter,
278 UDate fromDate,
279 UDate toDate,
280 UFormattedDateInterval* result,
281 UErrorCode* status);
282
283/**
284 * Formats a date/time range using the conventions established for the
285 * UDateIntervalFormat object.
286 * @param formatter
287 * The UDateIntervalFormat object specifying the format conventions.
288 * @param fromCalendar
289 * The starting point of the range.
290 * @param toCalendar
291 * The ending point of the range.
292 * @param result
293 * The UFormattedDateInterval to contain the result of the
294 * formatting operation.
295 * @param status
296 * A pointer to a UErrorCode to receive any errors.
297 * @draft ICU 67
298 */
299
300U_DRAFT void U_EXPORT2
301udtitvfmt_formatCalendarToResult(
302 const UDateIntervalFormat* formatter,
303 UCalendar* fromCalendar,
304 UCalendar* toCalendar,
305 UFormattedDateInterval* result,
306 UErrorCode* status);
307#endif /* U_HIDE_DRAFT_API */
308
309
310#endif /* #if !UCONFIG_NO_FORMATTING */
311
312#endif
313