2 *****************************************************************************************
3 * Copyright (C) 2014 Apple Inc. All Rights Reserved.
4 *****************************************************************************************
7 #ifndef UATIMEUNITFORMAT_H
8 #define UATIMEUNITFORMAT_H
10 #include "unicode/utypes.h"
12 #if !UCONFIG_NO_FORMATTING
13 #ifndef U_HIDE_DRAFT_API
15 #include "unicode/localpointer.h"
16 #include "unicode/unum.h"
20 * \brief C API: Support functions for formatting time units or portions thereof.
22 * These are somewhat temporary Apple-only functions to support NSDateComponentsFormatter.
26 * Opaque UATimeUnitFormat object for use in C programs.
29 struct UATimeUnitFormat
;
30 typedef struct UATimeUnitFormat UATimeUnitFormat
; /**< C typedef for struct UATimeUnitFormat. @draft ICU 53 */
33 * TimeUnit format styles
36 typedef enum UATimeUnitStyle
{
38 * full style, e.g. "1.0 minutes"
42 * abbreviated/short style, e.g. "1.0 min"
44 UATIMEUNITSTYLE_ABBREVIATED
,
46 * narrow style, e.g. "1.0 m"
48 UATIMEUNITSTYLE_NARROW
,
50 * shorter style,between abbreviated and narrow"
52 UATIMEUNITSTYLE_SHORTER
,
61 typedef enum UATimeUnitField
{
65 UATIMEUNITFIELD_MONTH
,
73 UATIMEUNITFIELD_MINUTE
,
75 UATIMEUNITFIELD_SECOND
,
81 * Open a new UATimeUnitFormat object for a given locale using the specified style,
82 * using the default decimal formatter.
86 * The style (width) - full, abbreviated, etc.
88 * A pointer to a UErrorCode to receive any errors.
90 * A pointer to a UATimeUnitFormat object for the specified locale,
91 * or NULL if an error occurred.
94 U_DRAFT UATimeUnitFormat
* U_EXPORT2
95 uatmufmt_open(const char* locale
,
96 UATimeUnitStyle style
,
100 * Open a new UATimeUnitFormat object for a given locale using the specified style,
101 * along with the number formatter to use for the numeric part of the time unit,
102 * e.g. "1 min", "1.0 min", etc.
106 * The style (width) - full, abbreviated, etc.
108 * A number formatter to set for this UATimeUnitFormat object (instead of
109 * the default decimal formatter). Ownership of this UNumberFormat object
110 * will pass to the UATimeUnitFormat object (the UATimeUnitFormat adopts the
111 * UNumberFormat), which becomes responsible for closing it. If the caller
112 * wishes to retain ownership of the UNumberFormat object, the caller must
113 * clone it (with unum_clone) and pass the clone to
114 * uatmufmt_openWithNumberFormat. May be NULL to use the default decimal
117 * A pointer to a UErrorCode to receive any errors.
119 * A pointer to a UATimeUnitFormat object for the specified locale,
120 * or NULL if an error occurred.
123 U_DRAFT UATimeUnitFormat
* U_EXPORT2
124 uatmufmt_openWithNumberFormat(const char* locale
,
125 UATimeUnitStyle style
,
126 UNumberFormat
* nfToAdopt
,
130 * Close a UATimeUnitFormat object. Once closed it may no longer be used.
132 * The UATimeUnitFormat object to close.
135 U_DRAFT
void U_EXPORT2
136 uatmufmt_close(UATimeUnitFormat
*tufmt
);
139 #if U_SHOW_CPLUSPLUS_API
144 * \class LocalUDateIntervalFormatPointer
145 * "Smart pointer" class, closes a UATimeUnitFormat via uatmufmt_close().
146 * For most methods see the LocalPointerBase base class.
148 * @see LocalPointerBase
152 U_DEFINE_LOCAL_OPEN_POINTER(LocalUATimeUnitFormatPointer
, UATimeUnitFormat
, uatmufmt_close
);
156 #endif // U_SHOW_CPLUSPLUS_API
160 * Set the number formatter to use for the numeric part of the time unit,
161 * e.g. "1 min", "1.0 min", etc.
162 * DO NOT USE - use uatmufmt_openWithNumberFormat instead, this will be
165 * The UATimeUnitFormat object specifying the format conventions.
167 * The number formatter to set for this UATimeUnitFormat object;
168 * uatmufmt_setNumberFormat clones this for its own use, so the
169 * caller retains ownership of the passed-in UNumberFormat object.
171 * A pointer to a UErrorCode to receive any errors.
172 * @deprecated ICU 53, use uatmufmt_openWithNumberFormat
174 U_DEPRECATED
void U_EXPORT2
175 uatmufmt_setNumberFormat(UATimeUnitFormat
* tufmt
,
176 UNumberFormat
* numfmt
,
180 * Format a value like 1.0 and a field like UATIMEUNIT_MINUTE to e.g. "1.0 minutes".
182 * The UATimeUnitFormat object specifying the format conventions.
184 * The numeric value to format
186 * The time field to format with the specified numeric value
188 * A pointer to a buffer to receive the formatted result.
189 * @param resultCapacity
190 * The maximum size of result.
192 * A pointer to a UErrorCode to receive any errors. In case of error status,
193 * the contents of result are undefined.
195 * The length of the formatted result; may be greater than resultCapacity,
196 * in which case an error is returned.
199 U_DRAFT
int32_t U_EXPORT2
200 uatmufmt_format(const UATimeUnitFormat
* tufmt
,
202 UATimeUnitField field
,
204 int32_t resultCapacity
,
209 * Parse a single formatted time unit like "1.0 minutes" into a numeric value and unit type.
210 * NOT CURRENTLY SUPPORTED, sets status to U_UNSUPPORTED_ERROR and returns 0.0.
212 * The UATimeUnitFormat object specifying the format conventions.
216 * The length of text, or -1 if null-terminated
218 * A pointer to an offset index into text at which to begin parsing. On output,
219 * *parsePos will point after the last parsed character. This parameter may be
220 * NULL, in which case parsing begins at offset 0.
222 * A pointer to a UATimeUnitField to be set to the parsed firled type.
224 * A pointer to a UErrorCode to receive any errors.
226 * The parsed double value.
229 U_DRAFT
double U_EXPORT2
230 uatmufmt_parse( const UATimeUnitFormat
* tufmt
,
234 UATimeUnitField
* field
,
239 * TimeUnit time duration positional pattern types
242 typedef enum UATimeUnitTimePattern
{
246 UATIMEUNITTIMEPAT_HM
,
250 UATIMEUNITTIMEPAT_HMS
,
254 UATIMEUNITTIMEPAT_MS
,
256 UATIMEUNITTIMEPAT_COUNT
257 } UATimeUnitTimePattern
;
260 * Get a localized pattern for positional duration style, e.g. "h:mm:ss".
261 * This comes from the durationUnits CLDR data in ICU, e.g.
267 * For usage see CLDR documentation on durationUnit under
268 * <a href="http://www.unicode.org/reports/tr35/tr35-general.html#Unit_Elements">Unit Elements</a>.
273 * The type of time pattern to get (hm, hms, ms)
275 * A pointer to a buffer to receive the formatted result.
276 * @param resultCapacity
277 * The maximum size of result.
279 * A pointer to a UErrorCode to receive any errors. In case of error status,
280 * the contents of result are undefined.
282 * The length of the formatted result; may be greater than resultCapacity,
283 * in which case an error is returned.
286 U_DRAFT
int32_t U_EXPORT2
287 uatmufmt_getTimePattern(const char* locale
,
288 UATimeUnitTimePattern type
,
290 int32_t resultCapacity
,
295 * TimeUnit list pattern types
298 typedef enum UATimeUnitListPattern
{
300 * for two only, e.g. "{0} and {1}"
302 UATIMEUNITLISTPAT_TWO_ONLY
,
304 * for end of list with 3 or more, e.g. "{0}, and {1}"
306 UATIMEUNITLISTPAT_END_PIECE
,
308 * for middle of list with 3 or more, e.g. "{0}, {1}"
310 UATIMEUNITLISTPAT_MIDDLE_PIECE
,
312 * for start of list with 3 or more, e.g. "{0}, {1}"
314 UATIMEUNITLISTPAT_START_PIECE
,
316 UATIMEUNITLISTPAT_COUNT
317 } UATimeUnitListPattern
;
320 * Get a localized pattern for combining units in a list, e.g. "3 min, 42 sec".
321 * This comes from the listPattern/unit* CLDR data in ICU, e.g.
324 * .. use short if not present
333 * .. use short if not present
336 * For usage see CLDR documentation on
337 * <a href="http://www.unicode.org/reports/tr35/tr35-general.html#ListPatterns">List Patterns</a>.
342 * The style (width) - full, abbreviated, etc.
344 * The type of list pattern to get (for two items, end part for >= 3 items, etc.)
346 * A pointer to a buffer to receive the formatted result.
347 * @param resultCapacity
348 * The maximum size of result.
350 * A pointer to a UErrorCode to receive any errors. In case of error status,
351 * the contents of result are undefined.
353 * The length of the formatted result; may be greater than resultCapacity,
354 * in which case an error is returned.
357 U_DRAFT
int32_t U_EXPORT2
358 uatmufmt_getListPattern(const char* locale
,
359 UATimeUnitStyle style
,
360 UATimeUnitListPattern type
,
362 int32_t resultCapacity
,
366 #endif /* U_HIDE_DRAFT_API */
367 #endif /* #if !UCONFIG_NO_FORMATTING */
369 #endif /* #ifndef UATIMEUNITFORMAT_H */