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