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