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
,
57 typedef enum UATimeUnitField
{
61 UATIMEUNITFIELD_MONTH
,
69 UATIMEUNITFIELD_MINUTE
,
71 UATIMEUNITFIELD_SECOND
,
77 * Open a new UATimeUnitFormat object for a given locale using the specified style,
78 * using the default decimal formatter.
82 * The style (width) - full, abbreviated, etc.
84 * A pointer to a UErrorCode to receive any errors.
86 * A pointer to a UATimeUnitFormat object for the specified locale,
87 * or NULL if an error occurred.
90 U_DRAFT UATimeUnitFormat
* U_EXPORT2
91 uatmufmt_open(const char* locale
,
92 UATimeUnitStyle style
,
96 * Open a new UATimeUnitFormat object for a given locale using the specified style,
97 * along with the number formatter to use for the numeric part of the time unit,
98 * e.g. "1 min", "1.0 min", etc.
102 * The style (width) - full, abbreviated, etc.
104 * A number formatter to set for this UATimeUnitFormat object (instead of
105 * the default decimal formatter). Ownership of this UNumberFormat object
106 * will pass to the UATimeUnitFormat object (the UATimeUnitFormat adopts the
107 * UNumberFormat), which becomes responsible for closing it. If the caller
108 * wishes to retain ownership of the UNumberFormat object, the caller must
109 * clone it (with unum_clone) and pass the clone to
110 * uatmufmt_openWithNumberFormat. May be NULL to use the default decimal
113 * A pointer to a UErrorCode to receive any errors.
115 * A pointer to a UATimeUnitFormat object for the specified locale,
116 * or NULL if an error occurred.
119 U_DRAFT UATimeUnitFormat
* U_EXPORT2
120 uatmufmt_openWithNumberFormat(const char* locale
,
121 UATimeUnitStyle style
,
122 UNumberFormat
* nfToAdopt
,
126 * Close a UATimeUnitFormat object. Once closed it may no longer be used.
128 * The UATimeUnitFormat object to close.
131 U_DRAFT
void U_EXPORT2
132 uatmufmt_close(UATimeUnitFormat
*tufmt
);
135 #if U_SHOW_CPLUSPLUS_API
140 * \class LocalUDateIntervalFormatPointer
141 * "Smart pointer" class, closes a UATimeUnitFormat via uatmufmt_close().
142 * For most methods see the LocalPointerBase base class.
144 * @see LocalPointerBase
148 U_DEFINE_LOCAL_OPEN_POINTER(LocalUATimeUnitFormatPointer
, UATimeUnitFormat
, uatmufmt_close
);
156 * Set the number formatter to use for the numeric part of the time unit,
157 * e.g. "1 min", "1.0 min", etc.
158 * DO NOT USE - use uatmufmt_openWithNumberFormat instead, this will be
161 * The UATimeUnitFormat object specifying the format conventions.
163 * The number formatter to set for this UATimeUnitFormat object;
164 * uatmufmt_setNumberFormat clones this for its own use, so the
165 * caller retains ownership of the passed-in UNumberFormat object.
167 * A pointer to a UErrorCode to receive any errors.
168 * @deprecated ICU 53, use uatmufmt_openWithNumberFormat
170 U_DEPRECATED
void U_EXPORT2
171 uatmufmt_setNumberFormat(UATimeUnitFormat
* tufmt
,
172 UNumberFormat
* numfmt
,
176 * Format a value like 1.0 and a field like UATIMEUNIT_MINUTE to e.g. "1.0 minutes".
178 * The UATimeUnitFormat object specifying the format conventions.
180 * The numeric value to format
182 * The time field to format with the specified numeric value
184 * A pointer to a buffer to receive the formatted result.
185 * @param resultCapacity
186 * The maximum size of result.
188 * A pointer to a UErrorCode to receive any errors. In case of error status,
189 * the contents of result are undefined.
191 * The length of the formatted result; may be greater than resultCapacity,
192 * in which case an error is returned.
195 U_DRAFT
int32_t U_EXPORT2
196 uatmufmt_format(const UATimeUnitFormat
* tufmt
,
198 UATimeUnitField field
,
200 int32_t resultCapacity
,
205 * Parse a single formatted time unit like "1.0 minutes" into a numeric value and unit type.
206 * NOT CURRENTLY SUPPORTED, sets status to U_UNSUPPORTED_ERROR and returns 0.0.
208 * The UATimeUnitFormat object specifying the format conventions.
212 * The length of text, or -1 if null-terminated
214 * A pointer to an offset index into text at which to begin parsing. On output,
215 * *parsePos will point after the last parsed character. This parameter may be
216 * NULL, in which case parsing begins at offset 0.
218 * A pointer to a UATimeUnitField to be set to the parsed firled type.
220 * A pointer to a UErrorCode to receive any errors.
222 * The parsed double value.
225 U_DRAFT
double U_EXPORT2
226 uatmufmt_parse( const UATimeUnitFormat
* tufmt
,
230 UATimeUnitField
* field
,
235 * TimeUnit time duration positional pattern types
238 typedef enum UATimeUnitTimePattern
{
242 UATIMEUNITTIMEPAT_HM
,
246 UATIMEUNITTIMEPAT_HMS
,
250 UATIMEUNITTIMEPAT_MS
,
252 UATIMEUNITTIMEPAT_COUNT
253 } UATimeUnitTimePattern
;
256 * Get a localized pattern for positional duration style, e.g. "h:mm:ss".
257 * This comes from the durationUnits CLDR data in ICU, e.g.
263 * For usage see CLDR documentation on durationUnit under
264 * <a href="http://www.unicode.org/reports/tr35/tr35-general.html#Unit_Elements">Unit Elements</a>.
269 * The type of time pattern to get (hm, hms, ms)
271 * A pointer to a buffer to receive the formatted result.
272 * @param resultCapacity
273 * The maximum size of result.
275 * A pointer to a UErrorCode to receive any errors. In case of error status,
276 * the contents of result are undefined.
278 * The length of the formatted result; may be greater than resultCapacity,
279 * in which case an error is returned.
282 U_DRAFT
int32_t U_EXPORT2
283 uatmufmt_getTimePattern(const char* locale
,
284 UATimeUnitTimePattern type
,
286 int32_t resultCapacity
,
291 * TimeUnit list pattern types
294 typedef enum UATimeUnitListPattern
{
296 * for two only, e.g. "{0} and {1}"
298 UATIMEUNITLISTPAT_TWO_ONLY
,
300 * for end of list with 3 or more, e.g. "{0}, and {1}"
302 UATIMEUNITLISTPAT_END_PIECE
,
304 * for middle of list with 3 or more, e.g. "{0}, {1}"
306 UATIMEUNITLISTPAT_MIDDLE_PIECE
,
308 * for start of list with 3 or more, e.g. "{0}, {1}"
310 UATIMEUNITLISTPAT_START_PIECE
,
312 UATIMEUNITLISTPAT_COUNT
313 } UATimeUnitListPattern
;
316 * Get a localized pattern for combining units in a list, e.g. "3 min, 42 sec".
317 * This comes from the listPattern/unit* CLDR data in ICU, e.g.
320 * .. use short if not present
329 * .. use short if not present
332 * For usage see CLDR documentation on
333 * <a href="http://www.unicode.org/reports/tr35/tr35-general.html#ListPatterns">List Patterns</a>.
338 * The style (width) - full, abbreviated, etc.
340 * The type of list pattern to get (for two items, end part for >= 3 items, etc.)
342 * A pointer to a buffer to receive the formatted result.
343 * @param resultCapacity
344 * The maximum size of result.
346 * A pointer to a UErrorCode to receive any errors. In case of error status,
347 * the contents of result are undefined.
349 * The length of the formatted result; may be greater than resultCapacity,
350 * in which case an error is returned.
353 U_DRAFT
int32_t U_EXPORT2
354 uatmufmt_getListPattern(const char* locale
,
355 UATimeUnitStyle style
,
356 UATimeUnitListPattern type
,
358 int32_t resultCapacity
,
362 #endif /* U_HIDE_DRAFT_API */
363 #endif /* #if !UCONFIG_NO_FORMATTING */
365 #endif /* #ifndef UATIMEUNITFORMAT_H */