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
,
77 UATIMEUNITFIELD_MILLISECOND
,
79 UATIMEUNITFIELD_MICROSECOND
,
81 UATIMEUNITFIELD_NANOSECOND
,
82 #ifndef U_HIDE_DEPRECATED_API
83 /** @deprecated The numeric value may change over time */
85 #endif /* U_HIDE_DEPRECATED_API */
89 * Open a new UATimeUnitFormat object for a given locale using the specified style,
90 * using the default decimal formatter.
94 * The style (width) - full, abbreviated, etc.
96 * A pointer to a UErrorCode to receive any errors.
98 * A pointer to a UATimeUnitFormat object for the specified locale,
99 * or NULL if an error occurred.
102 U_DRAFT UATimeUnitFormat
* U_EXPORT2
103 uatmufmt_open(const char* locale
,
104 UATimeUnitStyle style
,
108 * Open a new UATimeUnitFormat object for a given locale using the specified style,
109 * along with the number formatter to use for the numeric part of the time unit,
110 * e.g. "1 min", "1.0 min", etc.
114 * The style (width) - full, abbreviated, etc.
116 * A number formatter to set for this UATimeUnitFormat object (instead of
117 * the default decimal formatter). Ownership of this UNumberFormat object
118 * will pass to the UATimeUnitFormat object (the UATimeUnitFormat adopts the
119 * UNumberFormat), which becomes responsible for closing it. If the caller
120 * wishes to retain ownership of the UNumberFormat object, the caller must
121 * clone it (with unum_clone) and pass the clone to
122 * uatmufmt_openWithNumberFormat. May be NULL to use the default decimal
125 * A pointer to a UErrorCode to receive any errors.
127 * A pointer to a UATimeUnitFormat object for the specified locale,
128 * or NULL if an error occurred.
131 U_DRAFT UATimeUnitFormat
* U_EXPORT2
132 uatmufmt_openWithNumberFormat(const char* locale
,
133 UATimeUnitStyle style
,
134 UNumberFormat
* nfToAdopt
,
138 * Close a UATimeUnitFormat object. Once closed it may no longer be used.
140 * The UATimeUnitFormat object to close.
143 U_DRAFT
void U_EXPORT2
144 uatmufmt_close(UATimeUnitFormat
*tufmt
);
147 #if U_SHOW_CPLUSPLUS_API
152 * \class LocalUDateIntervalFormatPointer
153 * "Smart pointer" class, closes a UATimeUnitFormat via uatmufmt_close().
154 * For most methods see the LocalPointerBase base class.
156 * @see LocalPointerBase
160 U_DEFINE_LOCAL_OPEN_POINTER(LocalUATimeUnitFormatPointer
, UATimeUnitFormat
, uatmufmt_close
);
164 #endif // U_SHOW_CPLUSPLUS_API
168 * Set the number formatter to use for the numeric part of the time unit,
169 * e.g. "1 min", "1.0 min", etc.
170 * DO NOT USE - use uatmufmt_openWithNumberFormat instead, this will be
173 * The UATimeUnitFormat object specifying the format conventions.
175 * The number formatter to set for this UATimeUnitFormat object;
176 * uatmufmt_setNumberFormat clones this for its own use, so the
177 * caller retains ownership of the passed-in UNumberFormat object.
179 * A pointer to a UErrorCode to receive any errors.
180 * @deprecated ICU 53, use uatmufmt_openWithNumberFormat
182 U_DEPRECATED
void U_EXPORT2
183 uatmufmt_setNumberFormat(UATimeUnitFormat
* tufmt
,
184 UNumberFormat
* numfmt
,
188 * Format a value like 1.0 and a field like UATIMEUNIT_MINUTE to e.g. "1.0 minutes".
190 * The UATimeUnitFormat object specifying the format conventions.
192 * The numeric value to format
194 * The time field to format with the specified numeric value
196 * A pointer to a buffer to receive the formatted result.
197 * @param resultCapacity
198 * The maximum size of result.
200 * A pointer to a UErrorCode to receive any errors. In case of error status,
201 * the contents of result are undefined.
203 * The length of the formatted result; may be greater than resultCapacity,
204 * in which case an error is returned.
207 U_DRAFT
int32_t U_EXPORT2
208 uatmufmt_format(const UATimeUnitFormat
* tufmt
,
210 UATimeUnitField field
,
212 int32_t resultCapacity
,
217 * Parse a single formatted time unit like "1.0 minutes" into a numeric value and unit type.
218 * NOT CURRENTLY SUPPORTED, sets status to U_UNSUPPORTED_ERROR and returns 0.0.
220 * The UATimeUnitFormat object specifying the format conventions.
224 * The length of text, or -1 if null-terminated
226 * A pointer to an offset index into text at which to begin parsing. On output,
227 * *parsePos will point after the last parsed character. This parameter may be
228 * NULL, in which case parsing begins at offset 0.
230 * A pointer to a UATimeUnitField to be set to the parsed firled type.
232 * A pointer to a UErrorCode to receive any errors.
234 * The parsed double value.
237 U_DRAFT
double U_EXPORT2
238 uatmufmt_parse( const UATimeUnitFormat
* tufmt
,
242 UATimeUnitField
* field
,
247 * TimeUnit time duration positional pattern types
250 typedef enum UATimeUnitTimePattern
{
254 UATIMEUNITTIMEPAT_HM
,
258 UATIMEUNITTIMEPAT_HMS
,
262 UATIMEUNITTIMEPAT_MS
,
264 UATIMEUNITTIMEPAT_COUNT
265 } UATimeUnitTimePattern
;
268 * Get a localized pattern for positional duration style, e.g. "h:mm:ss".
269 * This comes from the durationUnits CLDR data in ICU, e.g.
275 * For usage see CLDR documentation on durationUnit under
276 * <a href="http://www.unicode.org/reports/tr35/tr35-general.html#Unit_Elements">Unit Elements</a>.
281 * The type of time pattern to get (hm, hms, ms)
283 * A pointer to a buffer to receive the formatted result.
284 * @param resultCapacity
285 * The maximum size of result.
287 * A pointer to a UErrorCode to receive any errors. In case of error status,
288 * the contents of result are undefined.
290 * The length of the formatted result; may be greater than resultCapacity,
291 * in which case an error is returned.
294 U_DRAFT
int32_t U_EXPORT2
295 uatmufmt_getTimePattern(const char* locale
,
296 UATimeUnitTimePattern type
,
298 int32_t resultCapacity
,
303 * TimeUnit list pattern types
306 typedef enum UATimeUnitListPattern
{
308 * for two only, e.g. "{0} and {1}"
310 UATIMEUNITLISTPAT_TWO_ONLY
,
312 * for end of list with 3 or more, e.g. "{0}, and {1}"
314 UATIMEUNITLISTPAT_END_PIECE
,
316 * for middle of list with 3 or more, e.g. "{0}, {1}"
318 UATIMEUNITLISTPAT_MIDDLE_PIECE
,
320 * for start of list with 3 or more, e.g. "{0}, {1}"
322 UATIMEUNITLISTPAT_START_PIECE
,
324 UATIMEUNITLISTPAT_COUNT
325 } UATimeUnitListPattern
;
328 * Get a localized pattern for combining units in a list, e.g. "3 min, 42 sec".
329 * This comes from the listPattern/unit* CLDR data in ICU, e.g.
332 * .. use short if not present
341 * .. use short if not present
344 * For usage see CLDR documentation on
345 * <a href="http://www.unicode.org/reports/tr35/tr35-general.html#ListPatterns">List Patterns</a>.
350 * The style (width) - full, abbreviated, etc.
352 * The type of list pattern to get (for two items, end part for >= 3 items, etc.)
354 * A pointer to a buffer to receive the formatted result.
355 * @param resultCapacity
356 * The maximum size of result.
358 * A pointer to a UErrorCode to receive any errors. In case of error status,
359 * the contents of result are undefined.
361 * The length of the formatted result; may be greater than resultCapacity,
362 * in which case an error is returned.
365 U_DRAFT
int32_t U_EXPORT2
366 uatmufmt_getListPattern(const char* locale
,
367 UATimeUnitStyle style
,
368 UATimeUnitListPattern type
,
370 int32_t resultCapacity
,
374 #endif /* U_HIDE_DRAFT_API */
375 #endif /* #if !UCONFIG_NO_FORMATTING */
377 #endif /* #ifndef UATIMEUNITFORMAT_H */