]> git.saurik.com Git - apple/icu.git/blame - icuSources/i18n/uatimeunitformat.cpp
ICU-64260.0.1.tar.gz
[apple/icu.git] / icuSources / i18n / uatimeunitformat.cpp
CommitLineData
57a6839d
A
1/*
2*****************************************************************************************
b331163b 3* Copyright (C) 2014-2015 Apple Inc. All Rights Reserved.
57a6839d
A
4*****************************************************************************************
5*/
6
7#include "unicode/utypes.h"
8
9#if !UCONFIG_NO_FORMATTING
10
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"
f3c0d7a5 21#include "unicode/ustring.h"
b331163b 22#include "ureslocs.h"
57a6839d
A
23#include "uresimp.h"
24#include "ustr_imp.h"
25
26U_NAMESPACE_USE
27
28
29U_CAPI UATimeUnitFormat* U_EXPORT2
30uatmufmt_open(const char* locale,
31 UATimeUnitStyle style,
32 UErrorCode* status)
33{
34 return uatmufmt_openWithNumberFormat(locale, style, NULL, status);
35}
36
37
38U_CAPI UATimeUnitFormat* U_EXPORT2
39uatmufmt_openWithNumberFormat(const char* locale,
40 UATimeUnitStyle style,
41 UNumberFormat* nfToAdopt,
42 UErrorCode* status)
43{
44 if (U_FAILURE(*status)) {
45 return NULL;
46 }
47 UMeasureFormatWidth mfWidth = UMEASFMT_WIDTH_WIDE;
48 switch (style) {
49 case UATIMEUNITSTYLE_FULL:
50 break;
51 case UATIMEUNITSTYLE_ABBREVIATED:
52 mfWidth = UMEASFMT_WIDTH_SHORT; break;
53 case UATIMEUNITSTYLE_NARROW:
54 mfWidth = UMEASFMT_WIDTH_NARROW; break;
2ca993e8
A
55 case UATIMEUNITSTYLE_SHORTER:
56 mfWidth = UMEASFMT_WIDTH_SHORTER; break;
57a6839d
A
57 default:
58 *status = U_ILLEGAL_ARGUMENT_ERROR; return NULL;
59 }
60 LocalPointer<MeasureFormat> mfmt( new MeasureFormat(Locale(locale), mfWidth, (NumberFormat*)nfToAdopt, *status) );
61 if (U_FAILURE(*status)) {
62 return NULL;
63 }
64 return (UATimeUnitFormat*)mfmt.orphan();
65}
66
67
68U_CAPI void U_EXPORT2
69uatmufmt_close(UATimeUnitFormat *mfmt)
70{
71 delete (MeasureFormat*)mfmt;
72}
73
74
75U_CAPI void U_EXPORT2
76uatmufmt_setNumberFormat(UATimeUnitFormat* mfmt,
77 UNumberFormat* numfmt,
78 UErrorCode* status)
79{
80 if (U_FAILURE(*status)) {
81 return;
82 }
83 ((MeasureFormat*)mfmt)->adoptNumberFormat( (NumberFormat*)(((NumberFormat*)numfmt)->clone()), *status );
84}
85
86
87U_CAPI int32_t U_EXPORT2
88uatmufmt_format(const UATimeUnitFormat* mfmt,
89 double value,
90 UATimeUnitField field,
91 UChar* result,
92 int32_t resultCapacity,
93 UErrorCode* status)
94{
95 if (U_FAILURE(*status)) {
96 return 0;
97 }
98 if ( ((result==NULL)? resultCapacity!=0: resultCapacity<0) ) {
99 *status = U_ILLEGAL_ARGUMENT_ERROR;
100 return 0;
101 }
102 MeasureUnit * munit = NULL;
103 switch (field) {
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;
3d1f044b
A
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;
57a6839d
A
114 default: *status = U_ILLEGAL_ARGUMENT_ERROR; break;
115 }
116 if (U_FAILURE(*status)) {
117 return 0;
118 }
119 LocalPointer<Measure> meas(new Measure(value, munit, *status));
120 if (U_FAILURE(*status)) {
121 return 0;
122 }
123 Formattable fmt;
124 fmt.adoptObject(meas.orphan());
125 UnicodeString res;
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);
130}
131
132
133U_CAPI double U_EXPORT2
3d1f044b
A
134uatmufmt_parse( const UATimeUnitFormat*,
135 const UChar*,
136 int32_t,
137 int32_t*,
138 UATimeUnitField*,
57a6839d
A
139 UErrorCode* status)
140{
3d1f044b
A
141 if (!U_FAILURE(*status)) {
142 *status = U_UNSUPPORTED_ERROR;
57a6839d 143 }
3d1f044b 144 return 0.0;
57a6839d
A
145}
146
147
148U_CAPI int32_t U_EXPORT2
149uatmufmt_getTimePattern(const char* locale,
150 UATimeUnitTimePattern type,
151 UChar* result,
152 int32_t resultCapacity,
153 UErrorCode* status)
154{
155 if (U_FAILURE(*status)) {
156 return 0;
157 }
158 if ( (result==NULL)? resultCapacity!=0: resultCapacity<0 ) {
159 *status = U_ILLEGAL_ARGUMENT_ERROR;
160 return 0;
161 }
162 const char* key = NULL;
163 switch (type) {
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; }
168 }
169 int32_t resLen = 0;
170 const UChar* resPtr = NULL;
b331163b 171 UResourceBundle* rb = ures_open(U_ICUDATA_UNIT, locale, status);
57a6839d
A
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);
176 }
177 ures_close(rb);
178 return u_terminateUChars(result, resultCapacity, resLen, status);
179}
180
181
182U_CAPI int32_t U_EXPORT2
183uatmufmt_getListPattern(const char* locale,
184 UATimeUnitStyle style,
185 UATimeUnitListPattern type,
186 UChar* result,
187 int32_t resultCapacity,
188 UErrorCode* status)
189{
190 if (U_FAILURE(*status)) {
191 return 0;
192 }
193 if ( (result==NULL)? resultCapacity!=0: resultCapacity<0 ) {
194 *status = U_ILLEGAL_ARGUMENT_ERROR;
195 return 0;
196 }
197 const char* styleKey = NULL;
198 switch (style) {
199 case UATIMEUNITSTYLE_FULL: { styleKey = "unit"; break; }
200 case UATIMEUNITSTYLE_ABBREVIATED: { styleKey = "unit-short"; break; }
201 case UATIMEUNITSTYLE_NARROW: { styleKey = "unit-narrow"; break; }
2ca993e8 202 case UATIMEUNITSTYLE_SHORTER: { styleKey = "unit-narrow"; break; }
57a6839d
A
203 default: { *status = U_ILLEGAL_ARGUMENT_ERROR; return 0; }
204 }
205 const char* typeKey = NULL;
206 switch (type) {
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; }
212 }
213 int32_t resLen = 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);
221 }
222 ures_close(rb);
223 return u_terminateUChars(result, resultCapacity, resLen, status);
224}
225
226
227#endif /* #if !UCONFIG_NO_FORMATTING */