2 *****************************************************************************************
3 * Copyright (C) 2014-2015 Apple Inc. All Rights Reserved.
4 *****************************************************************************************
7 #ifndef UAMEASUREFORMAT_H
8 #define UAMEASUREFORMAT_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"
17 #include "unicode/umisc.h"
21 * \brief C API: Format combinations of measurement units and numeric values.
23 * This is a somewhat temporary Apple-specific wrapper for using C++ MeasureFormat
24 * to format Measure objects, until the official ICU C API is available.
28 * Opaque UAMeasureFormat object for use in C programs.
31 struct UAMeasureFormat
;
32 typedef struct UAMeasureFormat UAMeasureFormat
; /**< C typedef for struct UAMeasureFormat. @draft ICU 53 */
35 * Constants for various widths.
38 typedef enum UAMeasureFormatWidth
{
40 * Full unit names, e.g. "5 hours, 37 minutes"
46 * Abbreviated unit names, e.g. "5 hrs, 37 mins"
49 UAMEASFMT_WIDTH_SHORT
,
52 * Use unit symbols if possible, e.g. "5 h, 37 m"
55 UAMEASFMT_WIDTH_NARROW
,
58 * Completely omit unit designatins if possible, e.g. "5:37"
61 UAMEASFMT_WIDTH_NUMERIC
,
64 * Count of values in this enum.
68 } UAMeasureFormatWidth
;
74 typedef enum UAMeasureUnit
{
75 UAMEASUNIT_ACCELERATION_G_FORCE
= (0 << 8) + 0,
76 UAMEASUNIT_ACCELERATION_METER_PER_SECOND_SQUARED
= (0 << 8) + 1, // (CLDR 26, ICU-541)
78 UAMEASUNIT_ANGLE_DEGREE
= (1 << 8) + 0,
79 UAMEASUNIT_ANGLE_ARC_MINUTE
= (1 << 8) + 1,
80 UAMEASUNIT_ANGLE_ARC_SECOND
= (1 << 8) + 2,
81 UAMEASUNIT_ANGLE_RADIAN
= (1 << 8) + 3, // (CLDR 26, ICU-541)
83 UAMEASUNIT_AREA_SQUARE_METER
= (2 << 8) + 0,
84 UAMEASUNIT_AREA_SQUARE_KILOMETER
= (2 << 8) + 1,
85 UAMEASUNIT_AREA_SQUARE_FOOT
= (2 << 8) + 2,
86 UAMEASUNIT_AREA_SQUARE_MILE
= (2 << 8) + 3,
87 UAMEASUNIT_AREA_ACRE
= (2 << 8) + 4,
88 UAMEASUNIT_AREA_HECTARE
= (2 << 8) + 5,
89 UAMEASUNIT_AREA_SQUARE_CENTIMETER
= (2 << 8) + 6, // (CLDR 26, ICU-541)
90 UAMEASUNIT_AREA_SQUARE_INCH
= (2 << 8) + 7, // (CLDR 26, ICU-541)
91 UAMEASUNIT_AREA_SQUARE_YARD
= (2 << 8) + 8, // (CLDR 26, ICU-541)
93 // (3 reserved for currency, handled separately)
95 UAMEASUNIT_DURATION_YEAR
= (4 << 8) + 0,
96 UAMEASUNIT_DURATION_MONTH
= (4 << 8) + 1,
97 UAMEASUNIT_DURATION_WEEK
= (4 << 8) + 2,
98 UAMEASUNIT_DURATION_DAY
= (4 << 8) + 3,
99 UAMEASUNIT_DURATION_HOUR
= (4 << 8) + 4,
100 UAMEASUNIT_DURATION_MINUTE
= (4 << 8) + 5,
101 UAMEASUNIT_DURATION_SECOND
= (4 << 8) + 6,
102 UAMEASUNIT_DURATION_MILLISECOND
= (4 << 8) + 7,
103 UAMEASUNIT_DURATION_MICROSECOND
= (4 << 8) + 8, // (CLDR 26, ICU-541)
104 UAMEASUNIT_DURATION_NANOSECOND
= (4 << 8) + 9, // (CLDR 26, ICU-541)
106 UAMEASUNIT_LENGTH_METER
= (5 << 8) + 0,
107 UAMEASUNIT_LENGTH_CENTIMETER
= (5 << 8) + 1,
108 UAMEASUNIT_LENGTH_KILOMETER
= (5 << 8) + 2,
109 UAMEASUNIT_LENGTH_MILLIMETER
= (5 << 8) + 3,
110 UAMEASUNIT_LENGTH_PICOMETER
= (5 << 8) + 4,
111 UAMEASUNIT_LENGTH_FOOT
= (5 << 8) + 5,
112 UAMEASUNIT_LENGTH_INCH
= (5 << 8) + 6,
113 UAMEASUNIT_LENGTH_MILE
= (5 << 8) + 7,
114 UAMEASUNIT_LENGTH_YARD
= (5 << 8) + 8,
115 UAMEASUNIT_LENGTH_LIGHT_YEAR
= (5 << 8) + 9,
116 UAMEASUNIT_LENGTH_DECIMETER
= (5 << 8) + 10, // (CLDR 26, ICU-541)
117 UAMEASUNIT_LENGTH_MICROMETER
= (5 << 8) + 11, // (CLDR 26, ICU-541)
118 UAMEASUNIT_LENGTH_NANOMETER
= (5 << 8) + 12, // (CLDR 26, ICU-541)
119 UAMEASUNIT_LENGTH_NAUTICAL_MILE
= (5 << 8) + 13, // (CLDR 26, ICU-541)
120 UAMEASUNIT_LENGTH_FATHOM
= (5 << 8) + 14, // (CLDR 26, ICU-541)
121 UAMEASUNIT_LENGTH_FURLONG
= (5 << 8) + 15, // (CLDR 26, ICU-541)
122 UAMEASUNIT_LENGTH_ASTRONOMICAL_UNIT
= (5 << 8) + 16, // (CLDR 26, ICU-541)
123 UAMEASUNIT_LENGTH_PARSEC
= (5 << 8) + 17, // (CLDR 26, ICU-541)
125 UAMEASUNIT_MASS_GRAM
= (6 << 8) + 0,
126 UAMEASUNIT_MASS_KILOGRAM
= (6 << 8) + 1,
127 UAMEASUNIT_MASS_OUNCE
= (6 << 8) + 2,
128 UAMEASUNIT_MASS_POUND
= (6 << 8) + 3,
129 UAMEASUNIT_MASS_STONE
= (6 << 8) + 4, // = 14 pounds / 6.35 kg, abbr "st", used in UK/Ireland for body weight (CLDR 26)
130 UAMEASUNIT_MASS_MICROGRAM
= (6 << 8) + 5, // (CLDR 26, ICU-541)
131 UAMEASUNIT_MASS_MILLIGRAM
= (6 << 8) + 6, // (CLDR 26, ICU-541)
132 UAMEASUNIT_MASS_METRIC_TON
= (6 << 8) + 7, // = "tonne" (CLDR 26, ICU-541)
133 UAMEASUNIT_MASS_TON
= (6 << 8) + 8, // = "short ton", U.S. ton (CLDR 26, ICU-541)
134 UAMEASUNIT_MASS_CARAT
= (6 << 8) + 9, // (CLDR 26, ICU-541)
135 UAMEASUNIT_MASS_OUNCE_TROY
= (6 << 8) + 10, // (CLDR 26, ICU-541)
137 UAMEASUNIT_POWER_WATT
= (7 << 8) + 0,
138 UAMEASUNIT_POWER_KILOWATT
= (7 << 8) + 1,
139 UAMEASUNIT_POWER_HORSEPOWER
= (7 << 8) + 2,
140 UAMEASUNIT_POWER_MILLIWATT
= (7 << 8) + 3, // (CLDR 26, ICU-541)
141 UAMEASUNIT_POWER_MEGAWATT
= (7 << 8) + 4, // (CLDR 26, ICU-541)
142 UAMEASUNIT_POWER_GIGAWATT
= (7 << 8) + 5, // (CLDR 26, ICU-541)
144 UAMEASUNIT_PRESSURE_HECTOPASCAL
= (8 << 8) + 0,
145 UAMEASUNIT_PRESSURE_INCH_HG
= (8 << 8) + 1,
146 UAMEASUNIT_PRESSURE_MILLIBAR
= (8 << 8) + 2,
147 UAMEASUNIT_PRESSURE_MILLIMETER_OF_MERCURY
= (8 << 8) + 3, // (CLDR 26, ICU-541)
148 UAMEASUNIT_PRESSURE_POUND_PER_SQUARE_INCH
= (8 << 8) + 4, // (CLDR 26, ICU-541)
150 UAMEASUNIT_SPEED_METER_PER_SECOND
= (9 << 8) + 0,
151 UAMEASUNIT_SPEED_KILOMETER_PER_HOUR
= (9 << 8) + 1,
152 UAMEASUNIT_SPEED_MILE_PER_HOUR
= (9 << 8) + 2,
154 UAMEASUNIT_TEMPERATURE_CELSIUS
= (10 << 8) + 0,
155 UAMEASUNIT_TEMPERATURE_FAHRENHEIT
= (10 << 8) + 1,
156 UAMEASUNIT_TEMPERATURE_KELVIN
= (10 << 8) + 2, // (CLDR 26, ICU-541)
157 UAMEASUNIT_TEMPERATURE_GENERIC
= (10 << 8) + 3, // (CLDR 27, ICU-550.2)
159 UAMEASUNIT_VOLUME_LITER
= (11 << 8) + 0,
160 UAMEASUNIT_VOLUME_CUBIC_KILOMETER
= (11 << 8) + 1,
161 UAMEASUNIT_VOLUME_CUBIC_MILE
= (11 << 8) + 2,
162 UAMEASUNIT_VOLUME_MILLILITER
= (11 << 8) + 3, // (CLDR 26, ICU-541)
163 UAMEASUNIT_VOLUME_CENTILITER
= (11 << 8) + 4, // (CLDR 26, ICU-541)
164 UAMEASUNIT_VOLUME_DECILITER
= (11 << 8) + 5, // (CLDR 26, ICU-541)
165 UAMEASUNIT_VOLUME_HECTOLITER
= (11 << 8) + 6, // (CLDR 26, ICU-541)
166 UAMEASUNIT_VOLUME_MEGALITER
= (11 << 8) + 7, // (CLDR 26, ICU-541)
167 UAMEASUNIT_VOLUME_CUBIC_CENTIMETER
= (11 << 8) + 8, // (CLDR 26, ICU-541)
168 UAMEASUNIT_VOLUME_CUBIC_METER
= (11 << 8) + 9, // (CLDR 26, ICU-541)
169 UAMEASUNIT_VOLUME_CUBIC_INCH
= (11 << 8) + 10, // (CLDR 26, ICU-541)
170 UAMEASUNIT_VOLUME_CUBIC_FOOT
= (11 << 8) + 11, // (CLDR 26, ICU-541)
171 UAMEASUNIT_VOLUME_CUBIC_YARD
= (11 << 8) + 12, // (CLDR 26, ICU-541)
172 UAMEASUNIT_VOLUME_ACRE_FOOT
= (11 << 8) + 13, // (CLDR 26, ICU-541)
173 UAMEASUNIT_VOLUME_BUSHEL
= (11 << 8) + 14, // (CLDR 26, ICU-541)
174 UAMEASUNIT_VOLUME_TEASPOON
= (11 << 8) + 15, // (CLDR 26, ICU-541)
175 UAMEASUNIT_VOLUME_TABLESPOON
= (11 << 8) + 16, // (CLDR 26, ICU-541)
176 UAMEASUNIT_VOLUME_FLUID_OUNCE
= (11 << 8) + 17, // (CLDR 26, ICU-541)
177 UAMEASUNIT_VOLUME_CUP
= (11 << 8) + 18, // (CLDR 26, ICU-541)
178 UAMEASUNIT_VOLUME_PINT
= (11 << 8) + 19, // (CLDR 26, ICU-541)
179 UAMEASUNIT_VOLUME_QUART
= (11 << 8) + 20, // (CLDR 26, ICU-541)
180 UAMEASUNIT_VOLUME_GALLON
= (11 << 8) + 21, // (CLDR 26, ICU-541)
182 // new categories/values in CLDR 26
184 UAMEASUNIT_ENERGY_JOULE
= (12 << 8) + 2,
185 UAMEASUNIT_ENERGY_KILOJOULE
= (12 << 8) + 4,
186 UAMEASUNIT_ENERGY_CALORIE
= (12 << 8) + 0, // chemistry "calories", abbr "cal"
187 UAMEASUNIT_ENERGY_KILOCALORIE
= (12 << 8) + 3, // kilocalories in general (chemistry, food), abbr "kcal"
188 UAMEASUNIT_ENERGY_FOODCALORIE
= (12 << 8) + 1, // kilocalories specifically for food; in US/UK/? "Calories" abbr "C", elsewhere same as "kcal"
189 UAMEASUNIT_ENERGY_KILOWATT_HOUR
= (12 << 8) + 5, // (ICU-541)
191 // new categories/values in CLDR 26 & ICU-541
193 UAMEASUNIT_CONSUMPTION_LITER_PER_KILOMETER
= (13 << 8) + 0,
194 UAMEASUNIT_CONSUMPTION_MILE_PER_GALLON
= (13 << 8) + 1,
196 UAMEASUNIT_DIGITAL_BIT
= (14 << 8) + 0,
197 UAMEASUNIT_DIGITAL_BYTE
= (14 << 8) + 1,
198 UAMEASUNIT_DIGITAL_GIGABIT
= (14 << 8) + 2,
199 UAMEASUNIT_DIGITAL_GIGABYTE
= (14 << 8) + 3,
200 UAMEASUNIT_DIGITAL_KILOBIT
= (14 << 8) + 4,
201 UAMEASUNIT_DIGITAL_KILOBYTE
= (14 << 8) + 5,
202 UAMEASUNIT_DIGITAL_MEGABIT
= (14 << 8) + 6,
203 UAMEASUNIT_DIGITAL_MEGABYTE
= (14 << 8) + 7,
204 UAMEASUNIT_DIGITAL_TERABIT
= (14 << 8) + 8,
205 UAMEASUNIT_DIGITAL_TERABYTE
= (14 << 8) + 9,
207 UAMEASUNIT_ELECTRIC_AMPERE
= (15 << 8) + 0,
208 UAMEASUNIT_ELECTRIC_MILLIAMPERE
= (15 << 8) + 1,
209 UAMEASUNIT_ELECTRIC_OHM
= (15 << 8) + 2,
210 UAMEASUNIT_ELECTRIC_VOLT
= (15 << 8) + 3,
212 UAMEASUNIT_FREQUENCY_HERTZ
= (16 << 8) + 0,
213 UAMEASUNIT_FREQUENCY_KILOHERTZ
= (16 << 8) + 1,
214 UAMEASUNIT_FREQUENCY_MEGAHERTZ
= (16 << 8) + 2,
215 UAMEASUNIT_FREQUENCY_GIGAHERTZ
= (16 << 8) + 3,
217 UAMEASUNIT_LIGHT_LUX
= (17 << 8) + 0,
219 //UAMEASUNIT_PROPORTION_KARAT = (18 << 8) + 0, // wait on this one
223 * Structure that combines value and UAMeasureUnit,
224 * for use with uameasfmt_formatMultiple to specify a
225 * list of value/unit combinations to format.
228 typedef struct UAMeasure
{
235 * Open a new UAMeasureFormat object for a given locale using the specified width,
236 * along with a number formatter (if desired) to override the default formatter
237 * that would be used for the numeric part of the unit in uameasfmt_format, or the
238 * numeric part of the *last unit* (only) in uameasfmt_formatMultiple. The default
239 * formatter uses zero decimal places and rounds toward 0; an alternate number formatter
240 * can be used to produce (e.g.) "37.2 mins" instead of "37 mins", or
241 * "5 hours, 37.2 minutes" instead of "5 hours, 37 minutes".
246 * The width - wide, short, narrow, etc.
248 * A number formatter to set for this UAMeasureFormat object (instead of
249 * the default decimal formatter). Ownership of this UNumberFormat object
250 * will pass to the UAMeasureFormat object (the UAMeasureFormat adopts the
251 * UNumberFormat), which becomes responsible for closing it. If the caller
252 * wishes to retain ownership of the UNumberFormat object, the caller must
253 * clone it (with unum_clone) and pass the clone to
254 * uatmufmt_openWithNumberFormat. May be NULL to use the default decimal
257 * A pointer to a UErrorCode to receive any errors.
259 * A pointer to a UAMeasureFormat object for the specified locale,
260 * or NULL if an error occurred.
263 U_DRAFT UAMeasureFormat
* U_EXPORT2
264 uameasfmt_open( const char* locale
,
265 UAMeasureFormatWidth width
,
266 UNumberFormat
* nfToAdopt
,
267 UErrorCode
* status
);
270 * Close a UAMeasureFormat object. Once closed it may no longer be used.
272 * The UATimeUnitFormat object to close.
275 U_DRAFT
void U_EXPORT2
276 uameasfmt_close(UAMeasureFormat
*measfmt
);
278 #if U_SHOW_CPLUSPLUS_API
283 * \class LocalUAMeasureFormatPointer
284 * "Smart pointer" class, closes a UAMeasureFormat via uameasfmt_close().
285 * For most methods see the LocalPointerBase base class.
287 * @see LocalPointerBase
291 U_DEFINE_LOCAL_OPEN_POINTER(LocalUAMeasureFormatPointer
, UAMeasureFormat
, uameasfmt_close
);
299 * Format a value like 1.0 and a field like UAMEASUNIT_DURATION_MINUTE to e.g. "1.0 minutes".
302 * The UAMeasureFormat object specifying the format conventions.
304 * The numeric value to format
306 * The unit to format with the specified numeric value
308 * A pointer to a buffer to receive the formatted result.
309 * @param resultCapacity
310 * The maximum size of result.
312 * A pointer to a UErrorCode to receive any errors. In case of error status,
313 * the contents of result are undefined.
315 * The length of the formatted result; may be greater than resultCapacity,
316 * in which case an error is returned.
319 U_DRAFT
int32_t U_EXPORT2
320 uameasfmt_format( const UAMeasureFormat
* measfmt
,
324 int32_t resultCapacity
,
325 UErrorCode
* status
);
328 * Format a value like 1.0 and a field like UAMEASUNIT_DURATION_MINUTE to e.g. "1.0 minutes",
329 * and get the position in the formatted result for certain types for fields.
332 * The UAMeasureFormat object specifying the format conventions.
334 * The numeric value to format
336 * The unit to format with the specified numeric value
338 * A pointer to a buffer to receive the formatted result.
339 * @param resultCapacity
340 * The maximum size of result.
342 * A pointer to a UFieldPosition. On input, pos->field is read; this should
343 * be a value from the UNumberFormatFields enum in unum.h. On output,
344 * pos->beginIndex and pos->endIndex indicate the beginning and ending offsets
345 * of that field in the formatted output, if relevant. This parameter may be
346 * NULL if no position information is desired.
348 * A pointer to a UErrorCode to receive any errors. In case of error status,
349 * the contents of result are undefined.
351 * The length of the formatted result; may be greater than resultCapacity,
352 * in which case an error is returned.
355 U_DRAFT
int32_t U_EXPORT2
356 uameasfmt_formatGetPosition( const UAMeasureFormat
* measfmt
,
360 int32_t resultCapacity
,
362 UErrorCode
* status
);
365 * Format a list of value and unit combinations, using locale-appropriate
366 * conventions for the list. Each combination is represented by a UAMeasure
367 * that combines a value and unit, such as 5.3 + UAMEASUNIT_DURATION_HOUR or
368 * 37.2 + UAMEASUNIT_DURATION_MINUTE. For all except the last UAMeasure in the
369 * list, the numeric part will be formatted using the default formatter (zero
370 * decimal places, rounds toward 0); for the last UAMeasure, the default may
371 * be overriden by passing a number formatter in uameasfmt_open. The result
372 * can thus be something like "5 hours, 37.2 minutes" or "5 hrs, 37.2 mins".
375 * The UAMeasureFormat object specifying the format conventions.
377 * A list of UAMeasure structs each specifying a numeric value
378 * and a UAMeasureUnit.
379 * @param measureCount
380 * The count of UAMeasureUnits in measures. Currently this has a limit of 8.
382 * A pointer to a buffer to receive the formatted result.
383 * @param resultCapacity
384 * The maximum size of result.
386 * A pointer to a UErrorCode to receive any errors. In case of error status,
387 * the contents of result are undefined.
389 * The length of the formatted result; may be greater than resultCapacity,
390 * in which case an error is returned.
393 U_DRAFT
int32_t U_EXPORT2
394 uameasfmt_formatMultiple( const UAMeasureFormat
* measfmt
,
395 const UAMeasure
* measures
,
396 int32_t measureCount
,
398 int32_t resultCapacity
,
399 UErrorCode
* status
);
402 #endif /* U_HIDE_DRAFT_API */
403 #endif /* #if !UCONFIG_NO_FORMATTING */
405 #endif /* #ifndef UAMEASUREFORMAT_H */