]>
Commit | Line | Data |
---|---|---|
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 | ||
26 | U_NAMESPACE_USE | |
27 | ||
28 | ||
29 | U_CAPI UATimeUnitFormat* U_EXPORT2 | |
30 | uatmufmt_open(const char* locale, | |
31 | UATimeUnitStyle style, | |
32 | UErrorCode* status) | |
33 | { | |
34 | return uatmufmt_openWithNumberFormat(locale, style, NULL, status); | |
35 | } | |
36 | ||
37 | ||
38 | U_CAPI UATimeUnitFormat* U_EXPORT2 | |
39 | uatmufmt_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 | ||
68 | U_CAPI void U_EXPORT2 | |
69 | uatmufmt_close(UATimeUnitFormat *mfmt) | |
70 | { | |
71 | delete (MeasureFormat*)mfmt; | |
72 | } | |
73 | ||
74 | ||
75 | U_CAPI void U_EXPORT2 | |
76 | uatmufmt_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 | ||
87 | U_CAPI int32_t U_EXPORT2 | |
88 | uatmufmt_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; | |
111 | default: *status = U_ILLEGAL_ARGUMENT_ERROR; break; | |
112 | } | |
113 | if (U_FAILURE(*status)) { | |
114 | return 0; | |
115 | } | |
116 | LocalPointer<Measure> meas(new Measure(value, munit, *status)); | |
117 | if (U_FAILURE(*status)) { | |
118 | return 0; | |
119 | } | |
120 | Formattable fmt; | |
121 | fmt.adoptObject(meas.orphan()); | |
122 | UnicodeString res; | |
123 | res.setTo(result, 0, resultCapacity); | |
124 | FieldPosition pos(0); | |
125 | ((MeasureFormat*)mfmt)->format(fmt, res, pos, *status); | |
126 | return res.extract(result, resultCapacity, *status); | |
127 | } | |
128 | ||
129 | ||
130 | U_CAPI double U_EXPORT2 | |
131 | uatmufmt_parse( const UATimeUnitFormat* mfmt, | |
132 | const UChar* text, | |
133 | int32_t textLength, | |
134 | int32_t* parsePos, | |
135 | UATimeUnitField* field, | |
136 | UErrorCode* status) | |
137 | { | |
138 | double doubleVal = 0.0; | |
139 | if (U_FAILURE(*status)) { | |
140 | return doubleVal; | |
141 | } | |
142 | *status = U_UNSUPPORTED_ERROR; | |
143 | return doubleVal; | |
144 | } | |
145 | ||
146 | ||
147 | U_CAPI int32_t U_EXPORT2 | |
148 | uatmufmt_getTimePattern(const char* locale, | |
149 | UATimeUnitTimePattern type, | |
150 | UChar* result, | |
151 | int32_t resultCapacity, | |
152 | UErrorCode* status) | |
153 | { | |
154 | if (U_FAILURE(*status)) { | |
155 | return 0; | |
156 | } | |
157 | if ( (result==NULL)? resultCapacity!=0: resultCapacity<0 ) { | |
158 | *status = U_ILLEGAL_ARGUMENT_ERROR; | |
159 | return 0; | |
160 | } | |
161 | const char* key = NULL; | |
162 | switch (type) { | |
163 | case UATIMEUNITTIMEPAT_HM: { key = "hm"; break; } | |
164 | case UATIMEUNITTIMEPAT_HMS: { key = "hms"; break; } | |
165 | case UATIMEUNITTIMEPAT_MS: { key = "ms"; break; } | |
166 | default: { *status = U_ILLEGAL_ARGUMENT_ERROR; return 0; } | |
167 | } | |
168 | int32_t resLen = 0; | |
169 | const UChar* resPtr = NULL; | |
b331163b | 170 | UResourceBundle* rb = ures_open(U_ICUDATA_UNIT, locale, status); |
57a6839d A |
171 | rb = ures_getByKeyWithFallback(rb, "durationUnits", rb, status); |
172 | resPtr = ures_getStringByKeyWithFallback(rb, key, &resLen, status); | |
173 | if (U_SUCCESS(*status)) { | |
174 | u_strncpy(result, resPtr, resultCapacity); | |
175 | } | |
176 | ures_close(rb); | |
177 | return u_terminateUChars(result, resultCapacity, resLen, status); | |
178 | } | |
179 | ||
180 | ||
181 | U_CAPI int32_t U_EXPORT2 | |
182 | uatmufmt_getListPattern(const char* locale, | |
183 | UATimeUnitStyle style, | |
184 | UATimeUnitListPattern type, | |
185 | UChar* result, | |
186 | int32_t resultCapacity, | |
187 | UErrorCode* status) | |
188 | { | |
189 | if (U_FAILURE(*status)) { | |
190 | return 0; | |
191 | } | |
192 | if ( (result==NULL)? resultCapacity!=0: resultCapacity<0 ) { | |
193 | *status = U_ILLEGAL_ARGUMENT_ERROR; | |
194 | return 0; | |
195 | } | |
196 | const char* styleKey = NULL; | |
197 | switch (style) { | |
198 | case UATIMEUNITSTYLE_FULL: { styleKey = "unit"; break; } | |
199 | case UATIMEUNITSTYLE_ABBREVIATED: { styleKey = "unit-short"; break; } | |
200 | case UATIMEUNITSTYLE_NARROW: { styleKey = "unit-narrow"; break; } | |
2ca993e8 | 201 | case UATIMEUNITSTYLE_SHORTER: { styleKey = "unit-narrow"; break; } |
57a6839d A |
202 | default: { *status = U_ILLEGAL_ARGUMENT_ERROR; return 0; } |
203 | } | |
204 | const char* typeKey = NULL; | |
205 | switch (type) { | |
206 | case UATIMEUNITLISTPAT_TWO_ONLY: { typeKey = "2"; break; } | |
207 | case UATIMEUNITLISTPAT_END_PIECE: { typeKey = "end"; break; } | |
208 | case UATIMEUNITLISTPAT_MIDDLE_PIECE: { typeKey = "middle"; break; } | |
209 | case UATIMEUNITLISTPAT_START_PIECE: { typeKey = "start"; break; } | |
210 | default: { *status = U_ILLEGAL_ARGUMENT_ERROR; return 0; } | |
211 | } | |
212 | int32_t resLen = 0; | |
213 | const UChar* resPtr = NULL; | |
214 | UResourceBundle* rb = ures_open(NULL, locale, status); | |
215 | rb = ures_getByKeyWithFallback(rb, "listPattern", rb, status); | |
216 | rb = ures_getByKeyWithFallback(rb, styleKey, rb, status); | |
217 | resPtr = ures_getStringByKeyWithFallback(rb, typeKey, &resLen, status); | |
218 | if (U_SUCCESS(*status)) { | |
219 | u_strncpy(result, resPtr, resultCapacity); | |
220 | } | |
221 | ures_close(rb); | |
222 | return u_terminateUChars(result, resultCapacity, resLen, status); | |
223 | } | |
224 | ||
225 | ||
226 | #endif /* #if !UCONFIG_NO_FORMATTING */ |