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