]>
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" | |
b331163b | 21 | #include "ureslocs.h" |
57a6839d A |
22 | #include "uresimp.h" |
23 | #include "ustr_imp.h" | |
24 | ||
25 | U_NAMESPACE_USE | |
26 | ||
27 | ||
28 | U_CAPI UATimeUnitFormat* U_EXPORT2 | |
29 | uatmufmt_open(const char* locale, | |
30 | UATimeUnitStyle style, | |
31 | UErrorCode* status) | |
32 | { | |
33 | return uatmufmt_openWithNumberFormat(locale, style, NULL, status); | |
34 | } | |
35 | ||
36 | ||
37 | U_CAPI UATimeUnitFormat* U_EXPORT2 | |
38 | uatmufmt_openWithNumberFormat(const char* locale, | |
39 | UATimeUnitStyle style, | |
40 | UNumberFormat* nfToAdopt, | |
41 | UErrorCode* status) | |
42 | { | |
43 | if (U_FAILURE(*status)) { | |
44 | return NULL; | |
45 | } | |
46 | UMeasureFormatWidth mfWidth = UMEASFMT_WIDTH_WIDE; | |
47 | switch (style) { | |
48 | case UATIMEUNITSTYLE_FULL: | |
49 | break; | |
50 | case UATIMEUNITSTYLE_ABBREVIATED: | |
51 | mfWidth = UMEASFMT_WIDTH_SHORT; break; | |
52 | case UATIMEUNITSTYLE_NARROW: | |
53 | mfWidth = UMEASFMT_WIDTH_NARROW; break; | |
54 | default: | |
55 | *status = U_ILLEGAL_ARGUMENT_ERROR; return NULL; | |
56 | } | |
57 | LocalPointer<MeasureFormat> mfmt( new MeasureFormat(Locale(locale), mfWidth, (NumberFormat*)nfToAdopt, *status) ); | |
58 | if (U_FAILURE(*status)) { | |
59 | return NULL; | |
60 | } | |
61 | return (UATimeUnitFormat*)mfmt.orphan(); | |
62 | } | |
63 | ||
64 | ||
65 | U_CAPI void U_EXPORT2 | |
66 | uatmufmt_close(UATimeUnitFormat *mfmt) | |
67 | { | |
68 | delete (MeasureFormat*)mfmt; | |
69 | } | |
70 | ||
71 | ||
72 | U_CAPI void U_EXPORT2 | |
73 | uatmufmt_setNumberFormat(UATimeUnitFormat* mfmt, | |
74 | UNumberFormat* numfmt, | |
75 | UErrorCode* status) | |
76 | { | |
77 | if (U_FAILURE(*status)) { | |
78 | return; | |
79 | } | |
80 | ((MeasureFormat*)mfmt)->adoptNumberFormat( (NumberFormat*)(((NumberFormat*)numfmt)->clone()), *status ); | |
81 | } | |
82 | ||
83 | ||
84 | U_CAPI int32_t U_EXPORT2 | |
85 | uatmufmt_format(const UATimeUnitFormat* mfmt, | |
86 | double value, | |
87 | UATimeUnitField field, | |
88 | UChar* result, | |
89 | int32_t resultCapacity, | |
90 | UErrorCode* status) | |
91 | { | |
92 | if (U_FAILURE(*status)) { | |
93 | return 0; | |
94 | } | |
95 | if ( ((result==NULL)? resultCapacity!=0: resultCapacity<0) ) { | |
96 | *status = U_ILLEGAL_ARGUMENT_ERROR; | |
97 | return 0; | |
98 | } | |
99 | MeasureUnit * munit = NULL; | |
100 | switch (field) { | |
101 | case UATIMEUNITFIELD_YEAR: munit = MeasureUnit::createYear(*status); break; | |
102 | case UATIMEUNITFIELD_MONTH: munit = MeasureUnit::createMonth(*status); break; | |
103 | case UATIMEUNITFIELD_DAY: munit = MeasureUnit::createDay(*status); break; | |
104 | case UATIMEUNITFIELD_WEEK: munit = MeasureUnit::createWeek(*status); break; | |
105 | case UATIMEUNITFIELD_HOUR: munit = MeasureUnit::createHour(*status); break; | |
106 | case UATIMEUNITFIELD_MINUTE: munit = MeasureUnit::createMinute(*status); break; | |
107 | case UATIMEUNITFIELD_SECOND: munit = MeasureUnit::createSecond(*status); break; | |
108 | default: *status = U_ILLEGAL_ARGUMENT_ERROR; break; | |
109 | } | |
110 | if (U_FAILURE(*status)) { | |
111 | return 0; | |
112 | } | |
113 | LocalPointer<Measure> meas(new Measure(value, munit, *status)); | |
114 | if (U_FAILURE(*status)) { | |
115 | return 0; | |
116 | } | |
117 | Formattable fmt; | |
118 | fmt.adoptObject(meas.orphan()); | |
119 | UnicodeString res; | |
120 | res.setTo(result, 0, resultCapacity); | |
121 | FieldPosition pos(0); | |
122 | ((MeasureFormat*)mfmt)->format(fmt, res, pos, *status); | |
123 | return res.extract(result, resultCapacity, *status); | |
124 | } | |
125 | ||
126 | ||
127 | U_CAPI double U_EXPORT2 | |
128 | uatmufmt_parse( const UATimeUnitFormat* mfmt, | |
129 | const UChar* text, | |
130 | int32_t textLength, | |
131 | int32_t* parsePos, | |
132 | UATimeUnitField* field, | |
133 | UErrorCode* status) | |
134 | { | |
135 | double doubleVal = 0.0; | |
136 | if (U_FAILURE(*status)) { | |
137 | return doubleVal; | |
138 | } | |
139 | *status = U_UNSUPPORTED_ERROR; | |
140 | return doubleVal; | |
141 | } | |
142 | ||
143 | ||
144 | U_CAPI int32_t U_EXPORT2 | |
145 | uatmufmt_getTimePattern(const char* locale, | |
146 | UATimeUnitTimePattern type, | |
147 | UChar* result, | |
148 | int32_t resultCapacity, | |
149 | UErrorCode* status) | |
150 | { | |
151 | if (U_FAILURE(*status)) { | |
152 | return 0; | |
153 | } | |
154 | if ( (result==NULL)? resultCapacity!=0: resultCapacity<0 ) { | |
155 | *status = U_ILLEGAL_ARGUMENT_ERROR; | |
156 | return 0; | |
157 | } | |
158 | const char* key = NULL; | |
159 | switch (type) { | |
160 | case UATIMEUNITTIMEPAT_HM: { key = "hm"; break; } | |
161 | case UATIMEUNITTIMEPAT_HMS: { key = "hms"; break; } | |
162 | case UATIMEUNITTIMEPAT_MS: { key = "ms"; break; } | |
163 | default: { *status = U_ILLEGAL_ARGUMENT_ERROR; return 0; } | |
164 | } | |
165 | int32_t resLen = 0; | |
166 | const UChar* resPtr = NULL; | |
b331163b | 167 | UResourceBundle* rb = ures_open(U_ICUDATA_UNIT, locale, status); |
57a6839d A |
168 | rb = ures_getByKeyWithFallback(rb, "durationUnits", rb, status); |
169 | resPtr = ures_getStringByKeyWithFallback(rb, key, &resLen, status); | |
170 | if (U_SUCCESS(*status)) { | |
171 | u_strncpy(result, resPtr, resultCapacity); | |
172 | } | |
173 | ures_close(rb); | |
174 | return u_terminateUChars(result, resultCapacity, resLen, status); | |
175 | } | |
176 | ||
177 | ||
178 | U_CAPI int32_t U_EXPORT2 | |
179 | uatmufmt_getListPattern(const char* locale, | |
180 | UATimeUnitStyle style, | |
181 | UATimeUnitListPattern type, | |
182 | UChar* result, | |
183 | int32_t resultCapacity, | |
184 | UErrorCode* status) | |
185 | { | |
186 | if (U_FAILURE(*status)) { | |
187 | return 0; | |
188 | } | |
189 | if ( (result==NULL)? resultCapacity!=0: resultCapacity<0 ) { | |
190 | *status = U_ILLEGAL_ARGUMENT_ERROR; | |
191 | return 0; | |
192 | } | |
193 | const char* styleKey = NULL; | |
194 | switch (style) { | |
195 | case UATIMEUNITSTYLE_FULL: { styleKey = "unit"; break; } | |
196 | case UATIMEUNITSTYLE_ABBREVIATED: { styleKey = "unit-short"; break; } | |
197 | case UATIMEUNITSTYLE_NARROW: { styleKey = "unit-narrow"; break; } | |
198 | default: { *status = U_ILLEGAL_ARGUMENT_ERROR; return 0; } | |
199 | } | |
200 | const char* typeKey = NULL; | |
201 | switch (type) { | |
202 | case UATIMEUNITLISTPAT_TWO_ONLY: { typeKey = "2"; break; } | |
203 | case UATIMEUNITLISTPAT_END_PIECE: { typeKey = "end"; break; } | |
204 | case UATIMEUNITLISTPAT_MIDDLE_PIECE: { typeKey = "middle"; break; } | |
205 | case UATIMEUNITLISTPAT_START_PIECE: { typeKey = "start"; break; } | |
206 | default: { *status = U_ILLEGAL_ARGUMENT_ERROR; return 0; } | |
207 | } | |
208 | int32_t resLen = 0; | |
209 | const UChar* resPtr = NULL; | |
210 | UResourceBundle* rb = ures_open(NULL, locale, status); | |
211 | rb = ures_getByKeyWithFallback(rb, "listPattern", rb, status); | |
212 | rb = ures_getByKeyWithFallback(rb, styleKey, rb, status); | |
213 | resPtr = ures_getStringByKeyWithFallback(rb, typeKey, &resLen, status); | |
214 | if (U_SUCCESS(*status)) { | |
215 | u_strncpy(result, resPtr, resultCapacity); | |
216 | } | |
217 | ures_close(rb); | |
218 | return u_terminateUChars(result, resultCapacity, resLen, status); | |
219 | } | |
220 | ||
221 | ||
222 | #endif /* #if !UCONFIG_NO_FORMATTING */ |