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