2 *****************************************************************************************
3 * Copyright (C) 2014-2015 Apple Inc. All Rights Reserved.
4 *****************************************************************************************
7 #include "unicode/utypes.h"
9 #if !UCONFIG_NO_FORMATTING
11 #include "unicode/uatimeunitformat.h"
12 #include "unicode/fieldpos.h"
13 #include "unicode/localpointer.h"
14 #include "unicode/numfmt.h"
15 #include "unicode/measunit.h"
16 #include "unicode/measure.h"
17 #include "unicode/measfmt.h"
18 #include "unicode/unistr.h"
19 #include "unicode/unum.h"
20 #include "unicode/ures.h"
21 #include "unicode/ustring.h"
29 U_CAPI UATimeUnitFormat
* U_EXPORT2
30 uatmufmt_open(const char* locale
,
31 UATimeUnitStyle style
,
34 return uatmufmt_openWithNumberFormat(locale
, style
, NULL
, status
);
38 U_CAPI UATimeUnitFormat
* U_EXPORT2
39 uatmufmt_openWithNumberFormat(const char* locale
,
40 UATimeUnitStyle style
,
41 UNumberFormat
* nfToAdopt
,
44 if (U_FAILURE(*status
)) {
47 UMeasureFormatWidth mfWidth
= UMEASFMT_WIDTH_WIDE
;
49 case UATIMEUNITSTYLE_FULL
:
51 case UATIMEUNITSTYLE_ABBREVIATED
:
52 mfWidth
= UMEASFMT_WIDTH_SHORT
; break;
53 case UATIMEUNITSTYLE_NARROW
:
54 mfWidth
= UMEASFMT_WIDTH_NARROW
; break;
55 case UATIMEUNITSTYLE_SHORTER
:
56 mfWidth
= UMEASFMT_WIDTH_SHORTER
; break;
58 *status
= U_ILLEGAL_ARGUMENT_ERROR
; return NULL
;
60 LocalPointer
<MeasureFormat
> mfmt( new MeasureFormat(Locale(locale
), mfWidth
, (NumberFormat
*)nfToAdopt
, *status
) );
61 if (U_FAILURE(*status
)) {
64 return (UATimeUnitFormat
*)mfmt
.orphan();
69 uatmufmt_close(UATimeUnitFormat
*mfmt
)
71 delete (MeasureFormat
*)mfmt
;
76 uatmufmt_setNumberFormat(UATimeUnitFormat
* mfmt
,
77 UNumberFormat
* numfmt
,
80 if (U_FAILURE(*status
)) {
83 ((MeasureFormat
*)mfmt
)->adoptNumberFormat( (NumberFormat
*)(((NumberFormat
*)numfmt
)->clone()), *status
);
87 U_CAPI
int32_t U_EXPORT2
88 uatmufmt_format(const UATimeUnitFormat
* mfmt
,
90 UATimeUnitField field
,
92 int32_t resultCapacity
,
95 if (U_FAILURE(*status
)) {
98 if ( ((result
==NULL
)? resultCapacity
!=0: resultCapacity
<0) ) {
99 *status
= U_ILLEGAL_ARGUMENT_ERROR
;
102 MeasureUnit
* munit
= NULL
;
104 case UATIMEUNITFIELD_YEAR
: munit
= MeasureUnit::createYear(*status
); break;
105 case UATIMEUNITFIELD_MONTH
: munit
= MeasureUnit::createMonth(*status
); break;
106 case UATIMEUNITFIELD_DAY
: munit
= MeasureUnit::createDay(*status
); break;
107 case UATIMEUNITFIELD_WEEK
: munit
= MeasureUnit::createWeek(*status
); break;
108 case UATIMEUNITFIELD_HOUR
: munit
= MeasureUnit::createHour(*status
); break;
109 case UATIMEUNITFIELD_MINUTE
: munit
= MeasureUnit::createMinute(*status
); break;
110 case UATIMEUNITFIELD_SECOND
: munit
= MeasureUnit::createSecond(*status
); break;
111 case UATIMEUNITFIELD_MILLISECOND
: munit
= MeasureUnit::createMillisecond(*status
); break;
112 case UATIMEUNITFIELD_MICROSECOND
: munit
= MeasureUnit::createMicrosecond(*status
); break;
113 case UATIMEUNITFIELD_NANOSECOND
: munit
= MeasureUnit::createNanosecond(*status
); break;
114 default: *status
= U_ILLEGAL_ARGUMENT_ERROR
; break;
116 if (U_FAILURE(*status
)) {
119 LocalPointer
<Measure
> meas(new Measure(value
, munit
, *status
));
120 if (U_FAILURE(*status
)) {
124 fmt
.adoptObject(meas
.orphan());
126 res
.setTo(result
, 0, resultCapacity
);
127 FieldPosition
pos(0);
128 ((MeasureFormat
*)mfmt
)->format(fmt
, res
, pos
, *status
);
129 return res
.extract(result
, resultCapacity
, *status
);
133 U_CAPI
double U_EXPORT2
134 uatmufmt_parse( const UATimeUnitFormat
*,
141 if (!U_FAILURE(*status
)) {
142 *status
= U_UNSUPPORTED_ERROR
;
148 U_CAPI
int32_t U_EXPORT2
149 uatmufmt_getTimePattern(const char* locale
,
150 UATimeUnitTimePattern type
,
152 int32_t resultCapacity
,
155 if (U_FAILURE(*status
)) {
158 if ( (result
==NULL
)? resultCapacity
!=0: resultCapacity
<0 ) {
159 *status
= U_ILLEGAL_ARGUMENT_ERROR
;
162 const char* key
= NULL
;
164 case UATIMEUNITTIMEPAT_HM
: { key
= "hm"; break; }
165 case UATIMEUNITTIMEPAT_HMS
: { key
= "hms"; break; }
166 case UATIMEUNITTIMEPAT_MS
: { key
= "ms"; break; }
167 default: { *status
= U_ILLEGAL_ARGUMENT_ERROR
; return 0; }
170 const UChar
* resPtr
= NULL
;
171 UResourceBundle
* rb
= ures_open(U_ICUDATA_UNIT
, locale
, status
);
172 rb
= ures_getByKeyWithFallback(rb
, "durationUnits", rb
, status
);
173 resPtr
= ures_getStringByKeyWithFallback(rb
, key
, &resLen
, status
);
174 if (U_SUCCESS(*status
)) {
175 u_strncpy(result
, resPtr
, resultCapacity
);
178 return u_terminateUChars(result
, resultCapacity
, resLen
, status
);
182 U_CAPI
int32_t U_EXPORT2
183 uatmufmt_getListPattern(const char* locale
,
184 UATimeUnitStyle style
,
185 UATimeUnitListPattern type
,
187 int32_t resultCapacity
,
190 if (U_FAILURE(*status
)) {
193 if ( (result
==NULL
)? resultCapacity
!=0: resultCapacity
<0 ) {
194 *status
= U_ILLEGAL_ARGUMENT_ERROR
;
197 const char* styleKey
= NULL
;
199 case UATIMEUNITSTYLE_FULL
: { styleKey
= "unit"; break; }
200 case UATIMEUNITSTYLE_ABBREVIATED
: { styleKey
= "unit-short"; break; }
201 case UATIMEUNITSTYLE_NARROW
: { styleKey
= "unit-narrow"; break; }
202 case UATIMEUNITSTYLE_SHORTER
: { styleKey
= "unit-narrow"; break; }
203 default: { *status
= U_ILLEGAL_ARGUMENT_ERROR
; return 0; }
205 const char* typeKey
= NULL
;
207 case UATIMEUNITLISTPAT_TWO_ONLY
: { typeKey
= "2"; break; }
208 case UATIMEUNITLISTPAT_END_PIECE
: { typeKey
= "end"; break; }
209 case UATIMEUNITLISTPAT_MIDDLE_PIECE
: { typeKey
= "middle"; break; }
210 case UATIMEUNITLISTPAT_START_PIECE
: { typeKey
= "start"; break; }
211 default: { *status
= U_ILLEGAL_ARGUMENT_ERROR
; return 0; }
214 const UChar
* resPtr
= NULL
;
215 UResourceBundle
* rb
= ures_open(NULL
, locale
, status
);
216 rb
= ures_getByKeyWithFallback(rb
, "listPattern", rb
, status
);
217 rb
= ures_getByKeyWithFallback(rb
, styleKey
, rb
, status
);
218 resPtr
= ures_getStringByKeyWithFallback(rb
, typeKey
, &resLen
, status
);
219 if (U_SUCCESS(*status
)) {
220 u_strncpy(result
, resPtr
, resultCapacity
);
223 return u_terminateUChars(result
, resultCapacity
, resLen
, status
);
227 #endif /* #if !UCONFIG_NO_FORMATTING */