]>
Commit | Line | Data |
---|---|---|
b75a7d8f A |
1 | /* |
2 | ******************************************************************************* | |
729e4ab9 | 3 | * Copyright (C) 1996-2011, International Business Machines |
b75a7d8f A |
4 | * Corporation and others. All Rights Reserved. |
5 | ******************************************************************************* | |
6 | */ | |
7 | ||
729e4ab9 A |
8 | #include <typeinfo> // for 'typeid' to work |
9 | ||
b75a7d8f A |
10 | #include "unicode/utypes.h" |
11 | ||
12 | #if !UCONFIG_NO_FORMATTING | |
13 | ||
14 | #include "unicode/ucal.h" | |
15 | #include "unicode/uloc.h" | |
16 | #include "unicode/calendar.h" | |
17 | #include "unicode/timezone.h" | |
73c04bcf | 18 | #include "unicode/gregocal.h" |
b75a7d8f A |
19 | #include "unicode/simpletz.h" |
20 | #include "unicode/ustring.h" | |
21 | #include "unicode/strenum.h" | |
22 | #include "cmemory.h" | |
46f4442e | 23 | #include "cstring.h" |
b75a7d8f | 24 | #include "ustrenum.h" |
729e4ab9 A |
25 | #include "uenumimp.h" |
26 | #include "ulist.h" | |
b75a7d8f A |
27 | |
28 | U_NAMESPACE_USE | |
29 | ||
30 | static TimeZone* | |
31 | _createTimeZone(const UChar* zoneID, int32_t len, UErrorCode* ec) { | |
32 | TimeZone* zone = NULL; | |
33 | if (ec!=NULL && U_SUCCESS(*ec)) { | |
34 | // Note that if zoneID is invalid, we get back GMT. This odd | |
35 | // behavior is by design and goes back to the JDK. The only | |
36 | // failure we will see is a memory allocation failure. | |
37 | int32_t l = (len<0 ? u_strlen(zoneID) : len); | |
374ca955 A |
38 | UnicodeString zoneStrID; |
39 | zoneStrID.setTo((UBool)(len < 0), zoneID, l); /* temporary read-only alias */ | |
40 | zone = TimeZone::createTimeZone(zoneStrID); | |
b75a7d8f A |
41 | if (zone == NULL) { |
42 | *ec = U_MEMORY_ALLOCATION_ERROR; | |
43 | } | |
44 | } | |
45 | return zone; | |
46 | } | |
47 | ||
48 | U_CAPI UEnumeration* U_EXPORT2 | |
49 | ucal_openTimeZones(UErrorCode* ec) { | |
729e4ab9 | 50 | return uenum_openFromStringEnumeration(TimeZone::createEnumeration(), ec); |
b75a7d8f A |
51 | } |
52 | ||
53 | U_CAPI UEnumeration* U_EXPORT2 | |
54 | ucal_openCountryTimeZones(const char* country, UErrorCode* ec) { | |
729e4ab9 | 55 | return uenum_openFromStringEnumeration(TimeZone::createEnumeration(country), ec); |
b75a7d8f A |
56 | } |
57 | ||
58 | U_CAPI int32_t U_EXPORT2 | |
59 | ucal_getDefaultTimeZone(UChar* result, int32_t resultCapacity, UErrorCode* ec) { | |
60 | int32_t len = 0; | |
61 | if (ec!=NULL && U_SUCCESS(*ec)) { | |
62 | TimeZone* zone = TimeZone::createDefault(); | |
63 | if (zone == NULL) { | |
64 | *ec = U_MEMORY_ALLOCATION_ERROR; | |
65 | } else { | |
66 | UnicodeString id; | |
67 | zone->getID(id); | |
68 | delete zone; | |
69 | len = id.extract(result, resultCapacity, *ec); | |
70 | } | |
71 | } | |
72 | return len; | |
73 | } | |
74 | ||
75 | U_CAPI void U_EXPORT2 | |
76 | ucal_setDefaultTimeZone(const UChar* zoneID, UErrorCode* ec) { | |
77 | TimeZone* zone = _createTimeZone(zoneID, -1, ec); | |
78 | if (zone != NULL) { | |
79 | TimeZone::adoptDefault(zone); | |
80 | } | |
81 | } | |
82 | ||
83 | U_CAPI int32_t U_EXPORT2 | |
84 | ucal_getDSTSavings(const UChar* zoneID, UErrorCode* ec) { | |
85 | int32_t result = 0; | |
86 | TimeZone* zone = _createTimeZone(zoneID, -1, ec); | |
374ca955 | 87 | if (U_SUCCESS(*ec)) { |
729e4ab9 A |
88 | SimpleTimeZone* stz = dynamic_cast<SimpleTimeZone*>(zone); |
89 | if (stz != NULL) { | |
90 | result = stz->getDSTSavings(); | |
374ca955 A |
91 | } else { |
92 | // Since there is no getDSTSavings on TimeZone, we use a | |
93 | // heuristic: Starting with the current time, march | |
94 | // forwards for one year, looking for DST savings. | |
95 | // Stepping by weeks is sufficient. | |
96 | UDate d = Calendar::getNow(); | |
97 | for (int32_t i=0; i<53; ++i, d+=U_MILLIS_PER_DAY*7.0) { | |
98 | int32_t raw, dst; | |
99 | zone->getOffset(d, FALSE, raw, dst, *ec); | |
100 | if (U_FAILURE(*ec)) { | |
101 | break; | |
102 | } else if (dst != 0) { | |
103 | result = dst; | |
104 | break; | |
105 | } | |
106 | } | |
107 | } | |
b75a7d8f A |
108 | } |
109 | delete zone; | |
110 | return result; | |
111 | } | |
112 | ||
b75a7d8f A |
113 | U_CAPI UDate U_EXPORT2 |
114 | ucal_getNow() | |
115 | { | |
116 | ||
117 | return Calendar::getNow(); | |
118 | } | |
119 | ||
46f4442e A |
120 | #define ULOC_LOCALE_IDENTIFIER_CAPACITY (ULOC_FULLNAME_CAPACITY + 1 + ULOC_KEYWORD_AND_VALUES_CAPACITY) |
121 | ||
b75a7d8f | 122 | U_CAPI UCalendar* U_EXPORT2 |
46f4442e A |
123 | ucal_open( const UChar* zoneID, |
124 | int32_t len, | |
125 | const char* locale, | |
126 | UCalendarType caltype, | |
127 | UErrorCode* status) | |
b75a7d8f A |
128 | { |
129 | ||
130 | if(U_FAILURE(*status)) return 0; | |
131 | ||
132 | TimeZone* zone = (zoneID==NULL) ? TimeZone::createDefault() | |
133 | : _createTimeZone(zoneID, len, status); | |
134 | ||
135 | if (U_FAILURE(*status)) { | |
136 | return NULL; | |
137 | } | |
46f4442e A |
138 | |
139 | if ( caltype == UCAL_GREGORIAN ) { | |
140 | char localeBuf[ULOC_LOCALE_IDENTIFIER_CAPACITY]; | |
729e4ab9 A |
141 | if ( locale == NULL ) { |
142 | locale = uloc_getDefault(); | |
143 | } | |
46f4442e A |
144 | uprv_strncpy(localeBuf, locale, ULOC_LOCALE_IDENTIFIER_CAPACITY); |
145 | uloc_setKeywordValue("calendar", "gregorian", localeBuf, ULOC_LOCALE_IDENTIFIER_CAPACITY, status); | |
146 | if (U_FAILURE(*status)) { | |
147 | return NULL; | |
148 | } | |
149 | return (UCalendar*)Calendar::createInstance(zone, Locale(localeBuf), *status); | |
150 | } | |
b75a7d8f A |
151 | return (UCalendar*)Calendar::createInstance(zone, Locale(locale), *status); |
152 | } | |
153 | ||
154 | U_CAPI void U_EXPORT2 | |
155 | ucal_close(UCalendar *cal) | |
156 | { | |
157 | ||
158 | delete (Calendar*) cal; | |
159 | } | |
160 | ||
46f4442e A |
161 | U_CAPI UCalendar* U_EXPORT2 |
162 | ucal_clone(const UCalendar* cal, | |
163 | UErrorCode* status) | |
164 | { | |
165 | if(U_FAILURE(*status)) return 0; | |
166 | ||
167 | Calendar* res = ((Calendar*)cal)->clone(); | |
168 | ||
169 | if(res == 0) { | |
170 | *status = U_MEMORY_ALLOCATION_ERROR; | |
171 | return 0; | |
172 | } | |
173 | ||
174 | return (UCalendar*) res; | |
175 | } | |
176 | ||
b75a7d8f A |
177 | U_CAPI void U_EXPORT2 |
178 | ucal_setTimeZone( UCalendar* cal, | |
179 | const UChar* zoneID, | |
180 | int32_t len, | |
181 | UErrorCode *status) | |
182 | { | |
183 | ||
184 | if(U_FAILURE(*status)) | |
185 | return; | |
186 | ||
187 | TimeZone* zone = (zoneID==NULL) ? TimeZone::createDefault() | |
188 | : _createTimeZone(zoneID, len, status); | |
189 | ||
190 | if (zone != NULL) { | |
191 | ((Calendar*)cal)->adoptTimeZone(zone); | |
192 | } | |
193 | } | |
194 | ||
195 | U_CAPI int32_t U_EXPORT2 | |
196 | ucal_getTimeZoneDisplayName(const UCalendar* cal, | |
197 | UCalendarDisplayNameType type, | |
198 | const char *locale, | |
199 | UChar* result, | |
200 | int32_t resultLength, | |
201 | UErrorCode* status) | |
202 | { | |
203 | ||
46f4442e | 204 | if(U_FAILURE(*status)) return -1; |
b75a7d8f | 205 | |
46f4442e A |
206 | const TimeZone& tz = ((Calendar*)cal)->getTimeZone(); |
207 | UnicodeString id; | |
208 | if(!(result==NULL && resultLength==0)) { | |
209 | // NULL destination for pure preflighting: empty dummy string | |
210 | // otherwise, alias the destination buffer | |
211 | id.setTo(result, 0, resultLength); | |
212 | } | |
b75a7d8f | 213 | |
46f4442e | 214 | switch(type) { |
b75a7d8f | 215 | case UCAL_STANDARD: |
46f4442e A |
216 | tz.getDisplayName(FALSE, TimeZone::LONG, Locale(locale), id); |
217 | break; | |
b75a7d8f A |
218 | |
219 | case UCAL_SHORT_STANDARD: | |
46f4442e A |
220 | tz.getDisplayName(FALSE, TimeZone::SHORT, Locale(locale), id); |
221 | break; | |
b75a7d8f A |
222 | |
223 | case UCAL_DST: | |
46f4442e A |
224 | tz.getDisplayName(TRUE, TimeZone::LONG, Locale(locale), id); |
225 | break; | |
b75a7d8f A |
226 | |
227 | case UCAL_SHORT_DST: | |
46f4442e A |
228 | tz.getDisplayName(TRUE, TimeZone::SHORT, Locale(locale), id); |
229 | break; | |
230 | } | |
b75a7d8f | 231 | |
46f4442e | 232 | return id.extract(result, resultLength, *status); |
b75a7d8f A |
233 | } |
234 | ||
235 | U_CAPI UBool U_EXPORT2 | |
236 | ucal_inDaylightTime( const UCalendar* cal, | |
46f4442e | 237 | UErrorCode* status ) |
b75a7d8f A |
238 | { |
239 | ||
46f4442e A |
240 | if(U_FAILURE(*status)) return (UBool) -1; |
241 | return ((Calendar*)cal)->inDaylightTime(*status); | |
b75a7d8f A |
242 | } |
243 | ||
46f4442e | 244 | U_CAPI void U_EXPORT2 |
73c04bcf | 245 | ucal_setGregorianChange(UCalendar *cal, UDate date, UErrorCode *pErrorCode) { |
46f4442e A |
246 | if(U_FAILURE(*pErrorCode)) { |
247 | return; | |
248 | } | |
249 | Calendar *cpp_cal = (Calendar *)cal; | |
729e4ab9 A |
250 | GregorianCalendar *gregocal = dynamic_cast<GregorianCalendar *>(cpp_cal); |
251 | // Not if(gregocal == NULL) { | |
252 | // because we really want to work only with a GregorianCalendar, not with | |
253 | // its subclasses like BuddhistCalendar. | |
254 | if(typeid(*cpp_cal) != typeid(GregorianCalendar)) { | |
46f4442e A |
255 | *pErrorCode = U_UNSUPPORTED_ERROR; |
256 | return; | |
257 | } | |
729e4ab9 | 258 | gregocal->setGregorianChange(date, *pErrorCode); |
73c04bcf A |
259 | } |
260 | ||
46f4442e | 261 | U_CAPI UDate U_EXPORT2 |
73c04bcf | 262 | ucal_getGregorianChange(const UCalendar *cal, UErrorCode *pErrorCode) { |
46f4442e A |
263 | if(U_FAILURE(*pErrorCode)) { |
264 | return (UDate)0; | |
265 | } | |
729e4ab9 A |
266 | const Calendar *cpp_cal = (const Calendar *)cal; |
267 | const GregorianCalendar *gregocal = dynamic_cast<const GregorianCalendar *>(cpp_cal); | |
268 | // Not if(gregocal == NULL) { | |
269 | // see comments in ucal_setGregorianChange(). | |
270 | if(typeid(*cpp_cal) != typeid(GregorianCalendar)) { | |
46f4442e A |
271 | *pErrorCode = U_UNSUPPORTED_ERROR; |
272 | return (UDate)0; | |
273 | } | |
729e4ab9 | 274 | return gregocal->getGregorianChange(); |
73c04bcf A |
275 | } |
276 | ||
b75a7d8f A |
277 | U_CAPI int32_t U_EXPORT2 |
278 | ucal_getAttribute( const UCalendar* cal, | |
46f4442e | 279 | UCalendarAttribute attr) |
b75a7d8f A |
280 | { |
281 | ||
46f4442e | 282 | switch(attr) { |
b75a7d8f | 283 | case UCAL_LENIENT: |
46f4442e A |
284 | return ((Calendar*)cal)->isLenient(); |
285 | ||
b75a7d8f | 286 | case UCAL_FIRST_DAY_OF_WEEK: |
46f4442e A |
287 | return ((Calendar*)cal)->getFirstDayOfWeek(); |
288 | ||
b75a7d8f | 289 | case UCAL_MINIMAL_DAYS_IN_FIRST_WEEK: |
46f4442e | 290 | return ((Calendar*)cal)->getMinimalDaysInFirstWeek(); |
b75a7d8f A |
291 | |
292 | default: | |
46f4442e A |
293 | break; |
294 | } | |
295 | return -1; | |
b75a7d8f A |
296 | } |
297 | ||
298 | U_CAPI void U_EXPORT2 | |
299 | ucal_setAttribute( UCalendar* cal, | |
46f4442e A |
300 | UCalendarAttribute attr, |
301 | int32_t newValue) | |
b75a7d8f A |
302 | { |
303 | ||
46f4442e | 304 | switch(attr) { |
b75a7d8f | 305 | case UCAL_LENIENT: |
46f4442e A |
306 | ((Calendar*)cal)->setLenient((UBool)newValue); |
307 | break; | |
308 | ||
b75a7d8f | 309 | case UCAL_FIRST_DAY_OF_WEEK: |
46f4442e A |
310 | ((Calendar*)cal)->setFirstDayOfWeek((UCalendarDaysOfWeek)newValue); |
311 | break; | |
312 | ||
b75a7d8f | 313 | case UCAL_MINIMAL_DAYS_IN_FIRST_WEEK: |
46f4442e A |
314 | ((Calendar*)cal)->setMinimalDaysInFirstWeek((uint8_t)newValue); |
315 | break; | |
316 | } | |
b75a7d8f A |
317 | } |
318 | ||
319 | U_CAPI const char* U_EXPORT2 | |
320 | ucal_getAvailable(int32_t index) | |
321 | { | |
322 | ||
46f4442e | 323 | return uloc_getAvailable(index); |
b75a7d8f A |
324 | } |
325 | ||
326 | U_CAPI int32_t U_EXPORT2 | |
327 | ucal_countAvailable() | |
328 | { | |
329 | ||
46f4442e | 330 | return uloc_countAvailable(); |
b75a7d8f A |
331 | } |
332 | ||
333 | U_CAPI UDate U_EXPORT2 | |
334 | ucal_getMillis( const UCalendar* cal, | |
46f4442e | 335 | UErrorCode* status) |
b75a7d8f A |
336 | { |
337 | ||
46f4442e | 338 | if(U_FAILURE(*status)) return (UDate) 0; |
b75a7d8f | 339 | |
46f4442e | 340 | return ((Calendar*)cal)->getTime(*status); |
b75a7d8f A |
341 | } |
342 | ||
343 | U_CAPI void U_EXPORT2 | |
344 | ucal_setMillis( UCalendar* cal, | |
46f4442e A |
345 | UDate dateTime, |
346 | UErrorCode* status ) | |
b75a7d8f | 347 | { |
46f4442e | 348 | if(U_FAILURE(*status)) return; |
b75a7d8f | 349 | |
46f4442e | 350 | ((Calendar*)cal)->setTime(dateTime, *status); |
b75a7d8f A |
351 | } |
352 | ||
353 | // TBD: why does this take an UErrorCode? | |
354 | U_CAPI void U_EXPORT2 | |
355 | ucal_setDate( UCalendar* cal, | |
46f4442e A |
356 | int32_t year, |
357 | int32_t month, | |
358 | int32_t date, | |
359 | UErrorCode *status) | |
b75a7d8f A |
360 | { |
361 | ||
46f4442e | 362 | if(U_FAILURE(*status)) return; |
b75a7d8f | 363 | |
46f4442e | 364 | ((Calendar*)cal)->set(year, month, date); |
b75a7d8f A |
365 | } |
366 | ||
367 | // TBD: why does this take an UErrorCode? | |
368 | U_CAPI void U_EXPORT2 | |
369 | ucal_setDateTime( UCalendar* cal, | |
46f4442e A |
370 | int32_t year, |
371 | int32_t month, | |
372 | int32_t date, | |
373 | int32_t hour, | |
374 | int32_t minute, | |
375 | int32_t second, | |
376 | UErrorCode *status) | |
b75a7d8f | 377 | { |
46f4442e | 378 | if(U_FAILURE(*status)) return; |
b75a7d8f | 379 | |
46f4442e | 380 | ((Calendar*)cal)->set(year, month, date, hour, minute, second); |
b75a7d8f A |
381 | } |
382 | ||
383 | U_CAPI UBool U_EXPORT2 | |
384 | ucal_equivalentTo( const UCalendar* cal1, | |
46f4442e | 385 | const UCalendar* cal2) |
b75a7d8f A |
386 | { |
387 | ||
46f4442e | 388 | return ((Calendar*)cal1)->isEquivalentTo(*((Calendar*)cal2)); |
b75a7d8f A |
389 | } |
390 | ||
391 | U_CAPI void U_EXPORT2 | |
392 | ucal_add( UCalendar* cal, | |
46f4442e A |
393 | UCalendarDateFields field, |
394 | int32_t amount, | |
395 | UErrorCode* status) | |
b75a7d8f A |
396 | { |
397 | ||
46f4442e | 398 | if(U_FAILURE(*status)) return; |
b75a7d8f | 399 | |
46f4442e | 400 | ((Calendar*)cal)->add(field, amount, *status); |
b75a7d8f A |
401 | } |
402 | ||
403 | U_CAPI void U_EXPORT2 | |
404 | ucal_roll( UCalendar* cal, | |
46f4442e A |
405 | UCalendarDateFields field, |
406 | int32_t amount, | |
407 | UErrorCode* status) | |
b75a7d8f A |
408 | { |
409 | ||
46f4442e | 410 | if(U_FAILURE(*status)) return; |
b75a7d8f | 411 | |
46f4442e | 412 | ((Calendar*)cal)->roll(field, amount, *status); |
b75a7d8f A |
413 | } |
414 | ||
415 | U_CAPI int32_t U_EXPORT2 | |
416 | ucal_get( const UCalendar* cal, | |
46f4442e A |
417 | UCalendarDateFields field, |
418 | UErrorCode* status ) | |
b75a7d8f A |
419 | { |
420 | ||
46f4442e | 421 | if(U_FAILURE(*status)) return -1; |
b75a7d8f | 422 | |
46f4442e | 423 | return ((Calendar*)cal)->get(field, *status); |
b75a7d8f A |
424 | } |
425 | ||
426 | U_CAPI void U_EXPORT2 | |
427 | ucal_set( UCalendar* cal, | |
46f4442e A |
428 | UCalendarDateFields field, |
429 | int32_t value) | |
b75a7d8f A |
430 | { |
431 | ||
46f4442e | 432 | ((Calendar*)cal)->set(field, value); |
b75a7d8f A |
433 | } |
434 | ||
435 | U_CAPI UBool U_EXPORT2 | |
436 | ucal_isSet( const UCalendar* cal, | |
46f4442e | 437 | UCalendarDateFields field) |
b75a7d8f A |
438 | { |
439 | ||
46f4442e | 440 | return ((Calendar*)cal)->isSet(field); |
b75a7d8f A |
441 | } |
442 | ||
443 | U_CAPI void U_EXPORT2 | |
444 | ucal_clearField( UCalendar* cal, | |
46f4442e | 445 | UCalendarDateFields field) |
b75a7d8f A |
446 | { |
447 | ||
46f4442e | 448 | ((Calendar*)cal)->clear(field); |
b75a7d8f A |
449 | } |
450 | ||
451 | U_CAPI void U_EXPORT2 | |
452 | ucal_clear(UCalendar* calendar) | |
453 | { | |
454 | ||
46f4442e | 455 | ((Calendar*)calendar)->clear(); |
b75a7d8f A |
456 | } |
457 | ||
458 | U_CAPI int32_t U_EXPORT2 | |
459 | ucal_getLimit( const UCalendar* cal, | |
46f4442e A |
460 | UCalendarDateFields field, |
461 | UCalendarLimitType type, | |
462 | UErrorCode *status) | |
b75a7d8f A |
463 | { |
464 | ||
46f4442e A |
465 | if(status==0 || U_FAILURE(*status)) { |
466 | return -1; | |
467 | } | |
468 | ||
469 | switch(type) { | |
b75a7d8f | 470 | case UCAL_MINIMUM: |
46f4442e | 471 | return ((Calendar*)cal)->getMinimum(field); |
b75a7d8f A |
472 | |
473 | case UCAL_MAXIMUM: | |
46f4442e | 474 | return ((Calendar*)cal)->getMaximum(field); |
b75a7d8f A |
475 | |
476 | case UCAL_GREATEST_MINIMUM: | |
46f4442e | 477 | return ((Calendar*)cal)->getGreatestMinimum(field); |
b75a7d8f A |
478 | |
479 | case UCAL_LEAST_MAXIMUM: | |
46f4442e | 480 | return ((Calendar*)cal)->getLeastMaximum(field); |
b75a7d8f A |
481 | |
482 | case UCAL_ACTUAL_MINIMUM: | |
46f4442e A |
483 | return ((Calendar*)cal)->getActualMinimum(field, |
484 | *status); | |
b75a7d8f A |
485 | |
486 | case UCAL_ACTUAL_MAXIMUM: | |
46f4442e A |
487 | return ((Calendar*)cal)->getActualMaximum(field, |
488 | *status); | |
b75a7d8f A |
489 | |
490 | default: | |
46f4442e A |
491 | break; |
492 | } | |
493 | return -1; | |
b75a7d8f A |
494 | } |
495 | ||
374ca955 A |
496 | U_CAPI const char * U_EXPORT2 |
497 | ucal_getLocaleByType(const UCalendar *cal, ULocDataLocaleType type, UErrorCode* status) | |
498 | { | |
499 | if (cal == NULL) { | |
500 | if (U_SUCCESS(*status)) { | |
501 | *status = U_ILLEGAL_ARGUMENT_ERROR; | |
502 | } | |
503 | return NULL; | |
504 | } | |
505 | return ((Calendar*)cal)->getLocaleID(type, *status); | |
506 | } | |
507 | ||
46f4442e A |
508 | U_CAPI const char * U_EXPORT2 |
509 | ucal_getTZDataVersion(UErrorCode* status) | |
510 | { | |
511 | return TimeZone::getTZDataVersion(*status); | |
512 | } | |
513 | ||
514 | U_CAPI int32_t U_EXPORT2 | |
515 | ucal_getCanonicalTimeZoneID(const UChar* id, int32_t len, | |
516 | UChar* result, int32_t resultCapacity, UBool *isSystemID, UErrorCode* status) { | |
517 | if(status == 0 || U_FAILURE(*status)) { | |
518 | return 0; | |
519 | } | |
520 | if (isSystemID) { | |
521 | *isSystemID = FALSE; | |
522 | } | |
523 | if (id == 0 || len == 0 || result == 0 || resultCapacity <= 0) { | |
524 | *status = U_ILLEGAL_ARGUMENT_ERROR; | |
525 | return 0; | |
526 | } | |
527 | int32_t reslen = 0; | |
528 | UnicodeString canonical; | |
529 | UBool systemID = FALSE; | |
530 | TimeZone::getCanonicalID(UnicodeString(id, len), canonical, systemID, *status); | |
531 | if (U_SUCCESS(*status)) { | |
532 | if (isSystemID) { | |
533 | *isSystemID = systemID; | |
534 | } | |
535 | reslen = canonical.extract(result, resultCapacity, *status); | |
536 | } | |
537 | return reslen; | |
538 | } | |
539 | ||
540 | U_CAPI const char * U_EXPORT2 | |
541 | ucal_getType(const UCalendar *cal, UErrorCode* status) | |
542 | { | |
543 | if (U_FAILURE(*status)) { | |
544 | return NULL; | |
545 | } | |
546 | return ((Calendar*)cal)->getType(); | |
547 | } | |
548 | ||
729e4ab9 A |
549 | U_CAPI UCalendarWeekdayType U_EXPORT2 |
550 | ucal_getDayOfWeekType(const UCalendar *cal, UCalendarDaysOfWeek dayOfWeek, UErrorCode* status) | |
551 | { | |
552 | if (U_FAILURE(*status)) { | |
553 | return UCAL_WEEKDAY; | |
554 | } | |
555 | return ((Calendar*)cal)->getDayOfWeekType(dayOfWeek, *status); | |
556 | } | |
557 | ||
558 | U_CAPI int32_t U_EXPORT2 | |
559 | ucal_getWeekendTransition(const UCalendar *cal, UCalendarDaysOfWeek dayOfWeek, UErrorCode *status) | |
560 | { | |
561 | if (U_FAILURE(*status)) { | |
562 | return 0; | |
563 | } | |
564 | return ((Calendar*)cal)->getWeekendTransition(dayOfWeek, *status); | |
565 | } | |
566 | ||
567 | U_CAPI UBool U_EXPORT2 | |
568 | ucal_isWeekend(const UCalendar *cal, UDate date, UErrorCode *status) | |
46f4442e | 569 | { |
729e4ab9 A |
570 | if (U_FAILURE(*status)) { |
571 | return FALSE; | |
572 | } | |
573 | return ((Calendar*)cal)->isWeekend(date, *status); | |
574 | } | |
575 | ||
576 | U_CAPI int32_t U_EXPORT2 | |
577 | ucal_getFieldDifference(UCalendar* cal, UDate target, | |
578 | UCalendarDateFields field, | |
579 | UErrorCode* status ) | |
580 | { | |
581 | if (U_FAILURE(*status)) { | |
582 | return 0; | |
583 | } | |
584 | return ((Calendar*)cal)->fieldDifference(target, field, *status); | |
585 | } | |
586 | ||
587 | ||
588 | static const UEnumeration defaultKeywordValues = { | |
589 | NULL, | |
590 | NULL, | |
591 | ulist_close_keyword_values_iterator, | |
592 | ulist_count_keyword_values, | |
593 | uenum_unextDefault, | |
594 | ulist_next_keyword_value, | |
595 | ulist_reset_keyword_values_iterator | |
596 | }; | |
597 | ||
598 | static const char * const CAL_TYPES[] = { | |
599 | "gregorian", | |
600 | "japanese", | |
601 | "buddhist", | |
602 | "roc", | |
603 | "persian", | |
604 | "islamic-civil", | |
605 | "islamic", | |
606 | "hebrew", | |
607 | "chinese", | |
608 | "indian", | |
609 | "coptic", | |
610 | "ethiopic", | |
611 | "ethiopic-amete-alem", | |
612 | NULL | |
613 | }; | |
614 | ||
615 | U_CAPI UEnumeration* U_EXPORT2 | |
616 | ucal_getKeywordValuesForLocale(const char * /* key */, const char* locale, UBool commonlyUsed, UErrorCode *status) { | |
617 | // Resolve region | |
618 | char prefRegion[ULOC_FULLNAME_CAPACITY] = ""; | |
619 | int32_t prefRegionLength = 0; | |
620 | prefRegionLength = uloc_getCountry(locale, prefRegion, sizeof(prefRegion), status); | |
621 | if (prefRegionLength == 0) { | |
622 | char loc[ULOC_FULLNAME_CAPACITY] = ""; | |
623 | int32_t locLength = 0; | |
624 | locLength = uloc_addLikelySubtags(locale, loc, sizeof(loc), status); | |
625 | ||
626 | prefRegionLength = uloc_getCountry(loc, prefRegion, sizeof(prefRegion), status); | |
627 | } | |
628 | ||
629 | // Read preferred calendar values from supplementalData calendarPreference | |
630 | UResourceBundle *rb = ures_openDirect(NULL, "supplementalData", status); | |
631 | ures_getByKey(rb, "calendarPreferenceData", rb, status); | |
632 | UResourceBundle *order = ures_getByKey(rb, prefRegion, NULL, status); | |
633 | if (*status == U_MISSING_RESOURCE_ERROR && rb != NULL) { | |
634 | *status = U_ZERO_ERROR; | |
635 | order = ures_getByKey(rb, "001", NULL, status); | |
636 | } | |
637 | ||
638 | // Create a list of calendar type strings | |
639 | UList *values = NULL; | |
640 | if (U_SUCCESS(*status)) { | |
641 | values = ulist_createEmptyList(status); | |
642 | if (U_SUCCESS(*status)) { | |
643 | for (int i = 0; i < ures_getSize(order); i++) { | |
644 | int32_t len; | |
645 | const UChar *type = ures_getStringByIndex(order, i, &len, status); | |
646 | char *caltype = (char*)uprv_malloc(len + 1); | |
647 | if (caltype == NULL) { | |
648 | *status = U_MEMORY_ALLOCATION_ERROR; | |
649 | break; | |
650 | } | |
651 | u_UCharsToChars(type, caltype, len); | |
652 | *(caltype + len) = 0; | |
653 | ||
654 | ulist_addItemEndList(values, caltype, TRUE, status); | |
655 | if (U_FAILURE(*status)) { | |
656 | break; | |
657 | } | |
658 | } | |
659 | ||
660 | if (U_SUCCESS(*status) && !commonlyUsed) { | |
661 | // If not commonlyUsed, add other available values | |
662 | for (int32_t i = 0; CAL_TYPES[i] != NULL; i++) { | |
663 | if (!ulist_containsString(values, CAL_TYPES[i], (int32_t)uprv_strlen(CAL_TYPES[i]))) { | |
664 | ulist_addItemEndList(values, CAL_TYPES[i], FALSE, status); | |
665 | if (U_FAILURE(*status)) { | |
666 | break; | |
667 | } | |
668 | } | |
669 | } | |
670 | } | |
671 | if (U_FAILURE(*status)) { | |
672 | ulist_deleteList(values); | |
673 | values = NULL; | |
674 | } | |
675 | } | |
676 | } | |
677 | ||
678 | ures_close(order); | |
679 | ures_close(rb); | |
680 | ||
681 | if (U_FAILURE(*status) || values == NULL) { | |
682 | return NULL; | |
683 | } | |
684 | ||
685 | // Create string enumeration | |
686 | UEnumeration *en = (UEnumeration*)uprv_malloc(sizeof(UEnumeration)); | |
687 | if (en == NULL) { | |
688 | *status = U_MEMORY_ALLOCATION_ERROR; | |
689 | ulist_deleteList(values); | |
690 | return NULL; | |
691 | } | |
692 | ulist_resetList(values); | |
693 | memcpy(en, &defaultKeywordValues, sizeof(UEnumeration)); | |
694 | en->context = values; | |
695 | return en; | |
46f4442e A |
696 | } |
697 | ||
b75a7d8f | 698 | #endif /* #if !UCONFIG_NO_FORMATTING */ |