1 // Copyright (C) 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 *******************************************************************************
5 * Copyright (C) 1996-2016, International Business Machines
6 * Corporation and others. All Rights Reserved.
7 *******************************************************************************
10 #include "utypeinfo.h" // for 'typeid' to work
12 #include "unicode/utypes.h"
14 #if !UCONFIG_NO_FORMATTING
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"
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);
46 *ec = U_MEMORY_ALLOCATION_ERROR;
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);
59 U_CAPI UEnumeration* U_EXPORT2
60 ucal_openTimeZones(UErrorCode* ec) {
61 return uenum_openFromStringEnumeration(TimeZone::createEnumeration(), ec);
64 U_CAPI UEnumeration* U_EXPORT2
65 ucal_openCountryTimeZones(const char* country, UErrorCode* ec) {
66 return uenum_openFromStringEnumeration(TimeZone::createEnumeration(country), ec);
69 U_CAPI int32_t U_EXPORT2
70 ucal_getDefaultTimeZone(UChar* result, int32_t resultCapacity, UErrorCode* ec) {
72 if (ec!=NULL && U_SUCCESS(*ec)) {
73 TimeZone* zone = TimeZone::createDefault();
75 *ec = U_MEMORY_ALLOCATION_ERROR;
80 len = id.extract(result, resultCapacity, *ec);
87 ucal_setDefaultTimeZone(const UChar* zoneID, UErrorCode* ec) {
88 TimeZone* zone = _createTimeZone(zoneID, -1, ec);
90 TimeZone::adoptDefault(zone);
94 U_CAPI int32_t U_EXPORT2
95 ucal_getDSTSavings(const UChar* zoneID, UErrorCode* ec) {
97 TimeZone* zone = _createTimeZone(zoneID, -1, ec);
99 SimpleTimeZone* stz = dynamic_cast<SimpleTimeZone*>(zone);
101 result = stz->getDSTSavings();
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 UDate d = Calendar::getNow();
108 for (int32_t i=0; i<53; ++i, d+=U_MILLIS_PER_DAY*7.0) {
110 zone->getOffset(d, FALSE, raw, dst, *ec);
111 if (U_FAILURE(*ec)) {
113 } else if (dst != 0) {
124 U_CAPI UDate U_EXPORT2
128 return Calendar::getNow();
131 #define ULOC_LOCALE_IDENTIFIER_CAPACITY (ULOC_FULLNAME_CAPACITY + 1 + ULOC_KEYWORD_AND_VALUES_CAPACITY)
133 U_CAPI UCalendar* U_EXPORT2
134 ucal_open( const UChar* zoneID,
137 UCalendarType caltype,
141 if(U_FAILURE(*status)) return 0;
143 TimeZone* zone = (zoneID==NULL) ? TimeZone::createDefault()
144 : _createTimeZone(zoneID, len, status);
146 if (U_FAILURE(*status)) {
150 if ( caltype == UCAL_GREGORIAN ) {
151 char localeBuf[ULOC_LOCALE_IDENTIFIER_CAPACITY];
152 if ( locale == NULL ) {
153 locale = uloc_getDefault();
155 uprv_strncpy(localeBuf, locale, ULOC_LOCALE_IDENTIFIER_CAPACITY);
156 uloc_setKeywordValue("calendar", "gregorian", localeBuf, ULOC_LOCALE_IDENTIFIER_CAPACITY, status);
157 if (U_FAILURE(*status)) {
160 return (UCalendar*)Calendar::createInstance(zone, Locale(localeBuf), *status);
162 return (UCalendar*)Calendar::createInstance(zone, Locale(locale), *status);
165 U_CAPI void U_EXPORT2
166 ucal_close(UCalendar *cal)
169 delete (Calendar*) cal;
172 U_CAPI UCalendar* U_EXPORT2
173 ucal_clone(const UCalendar* cal,
176 if(U_FAILURE(*status)) return 0;
178 Calendar* res = ((Calendar*)cal)->clone();
181 *status = U_MEMORY_ALLOCATION_ERROR;
185 return (UCalendar*) res;
188 U_CAPI void U_EXPORT2
189 ucal_setTimeZone( UCalendar* cal,
195 if(U_FAILURE(*status))
198 TimeZone* zone = (zoneID==NULL) ? TimeZone::createDefault()
199 : _createTimeZone(zoneID, len, status);
202 ((Calendar*)cal)->adoptTimeZone(zone);
206 U_CAPI int32_t U_EXPORT2
207 ucal_getTimeZoneID(const UCalendar *cal,
209 int32_t resultLength,
212 if (U_FAILURE(*status)) {
215 const TimeZone& tz = ((Calendar*)cal)->getTimeZone();
218 return id.extract(result, resultLength, *status);
221 U_CAPI int32_t U_EXPORT2
222 ucal_getTimeZoneDisplayName(const UCalendar* cal,
223 UCalendarDisplayNameType type,
226 int32_t resultLength,
230 if(U_FAILURE(*status)) return -1;
232 const TimeZone& tz = ((Calendar*)cal)->getTimeZone();
234 if(!(result==NULL && resultLength==0)) {
235 // NULL destination for pure preflighting: empty dummy string
236 // otherwise, alias the destination buffer
237 id.setTo(result, 0, resultLength);
242 tz.getDisplayName(FALSE, TimeZone::LONG, Locale(locale), id);
245 case UCAL_SHORT_STANDARD:
246 tz.getDisplayName(FALSE, TimeZone::SHORT, Locale(locale), id);
250 tz.getDisplayName(TRUE, TimeZone::LONG, Locale(locale), id);
254 tz.getDisplayName(TRUE, TimeZone::SHORT, Locale(locale), id);
258 return id.extract(result, resultLength, *status);
261 U_CAPI UBool U_EXPORT2
262 ucal_inDaylightTime( const UCalendar* cal,
266 if(U_FAILURE(*status)) return (UBool) -1;
267 return ((Calendar*)cal)->inDaylightTime(*status);
270 U_CAPI void U_EXPORT2
271 ucal_setGregorianChange(UCalendar *cal, UDate date, UErrorCode *pErrorCode) {
272 if(U_FAILURE(*pErrorCode)) {
275 Calendar *cpp_cal = (Calendar *)cal;
276 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 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 *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR;
286 if(typeid(*cpp_cal) != typeid(GregorianCalendar)) {
287 *pErrorCode = U_UNSUPPORTED_ERROR;
290 gregocal->setGregorianChange(date, *pErrorCode);
293 U_CAPI UDate U_EXPORT2
294 ucal_getGregorianChange(const UCalendar *cal, UErrorCode *pErrorCode) {
295 if(U_FAILURE(*pErrorCode)) {
298 const Calendar *cpp_cal = (const Calendar *)cal;
299 const GregorianCalendar *gregocal = dynamic_cast<const GregorianCalendar *>(cpp_cal);
300 // Not if(gregocal == NULL) {
301 // see comments in ucal_setGregorianChange().
302 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 *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR;
308 if(typeid(*cpp_cal) != typeid(GregorianCalendar)) {
309 *pErrorCode = U_UNSUPPORTED_ERROR;
312 return gregocal->getGregorianChange();
315 U_CAPI int32_t U_EXPORT2
316 ucal_getAttribute( const UCalendar* cal,
317 UCalendarAttribute attr)
322 return ((Calendar*)cal)->isLenient();
324 case UCAL_FIRST_DAY_OF_WEEK:
325 return ((Calendar*)cal)->getFirstDayOfWeek();
327 case UCAL_MINIMAL_DAYS_IN_FIRST_WEEK:
328 return ((Calendar*)cal)->getMinimalDaysInFirstWeek();
330 case UCAL_REPEATED_WALL_TIME:
331 return ((Calendar*)cal)->getRepeatedWallTimeOption();
333 case UCAL_SKIPPED_WALL_TIME:
334 return ((Calendar*)cal)->getSkippedWallTimeOption();
342 U_CAPI void U_EXPORT2
343 ucal_setAttribute( UCalendar* cal,
344 UCalendarAttribute attr,
350 ((Calendar*)cal)->setLenient((UBool)newValue);
353 case UCAL_FIRST_DAY_OF_WEEK:
354 ((Calendar*)cal)->setFirstDayOfWeek((UCalendarDaysOfWeek)newValue);
357 case UCAL_MINIMAL_DAYS_IN_FIRST_WEEK:
358 ((Calendar*)cal)->setMinimalDaysInFirstWeek((uint8_t)newValue);
361 case UCAL_REPEATED_WALL_TIME:
362 ((Calendar*)cal)->setRepeatedWallTimeOption((UCalendarWallTimeOption)newValue);
365 case UCAL_SKIPPED_WALL_TIME:
366 ((Calendar*)cal)->setSkippedWallTimeOption((UCalendarWallTimeOption)newValue);
371 U_CAPI const char* U_EXPORT2
372 ucal_getAvailable(int32_t index)
375 return uloc_getAvailable(index);
378 U_CAPI int32_t U_EXPORT2
379 ucal_countAvailable()
382 return uloc_countAvailable();
385 U_CAPI UDate U_EXPORT2
386 ucal_getMillis( const UCalendar* cal,
390 if(U_FAILURE(*status)) return (UDate) 0;
392 return ((Calendar*)cal)->getTime(*status);
395 U_CAPI void U_EXPORT2
396 ucal_setMillis( UCalendar* cal,
400 if(U_FAILURE(*status)) return;
402 ((Calendar*)cal)->setTime(dateTime, *status);
405 // TBD: why does this take an UErrorCode?
406 U_CAPI void U_EXPORT2
407 ucal_setDate( UCalendar* cal,
414 if(U_FAILURE(*status)) return;
416 ((Calendar*)cal)->set(year, month, date);
419 // TBD: why does this take an UErrorCode?
420 U_CAPI void U_EXPORT2
421 ucal_setDateTime( UCalendar* cal,
430 if(U_FAILURE(*status)) return;
432 ((Calendar*)cal)->set(year, month, date, hour, minute, second);
435 U_CAPI UBool U_EXPORT2
436 ucal_equivalentTo( const UCalendar* cal1,
437 const UCalendar* cal2)
440 return ((Calendar*)cal1)->isEquivalentTo(*((Calendar*)cal2));
443 U_CAPI void U_EXPORT2
444 ucal_add( UCalendar* cal,
445 UCalendarDateFields field,
450 if(U_FAILURE(*status)) return;
452 ((Calendar*)cal)->add(field, amount, *status);
455 U_CAPI void U_EXPORT2
456 ucal_roll( UCalendar* cal,
457 UCalendarDateFields field,
462 if(U_FAILURE(*status)) return;
464 ((Calendar*)cal)->roll(field, amount, *status);
467 U_CAPI int32_t U_EXPORT2
468 ucal_get( const UCalendar* cal,
469 UCalendarDateFields field,
473 if(U_FAILURE(*status)) return -1;
475 return ((Calendar*)cal)->get(field, *status);
478 U_CAPI void U_EXPORT2
479 ucal_set( UCalendar* cal,
480 UCalendarDateFields field,
484 ((Calendar*)cal)->set(field, value);
487 U_CAPI UBool U_EXPORT2
488 ucal_isSet( const UCalendar* cal,
489 UCalendarDateFields field)
492 return ((Calendar*)cal)->isSet(field);
495 U_CAPI void U_EXPORT2
496 ucal_clearField( UCalendar* cal,
497 UCalendarDateFields field)
500 ((Calendar*)cal)->clear(field);
503 U_CAPI void U_EXPORT2
504 ucal_clear(UCalendar* calendar)
507 ((Calendar*)calendar)->clear();
510 U_CAPI int32_t U_EXPORT2
511 ucal_getLimit( const UCalendar* cal,
512 UCalendarDateFields field,
513 UCalendarLimitType type,
517 if(status==0 || U_FAILURE(*status)) {
523 return ((Calendar*)cal)->getMinimum(field);
526 return ((Calendar*)cal)->getMaximum(field);
528 case UCAL_GREATEST_MINIMUM:
529 return ((Calendar*)cal)->getGreatestMinimum(field);
531 case UCAL_LEAST_MAXIMUM:
532 return ((Calendar*)cal)->getLeastMaximum(field);
534 case UCAL_ACTUAL_MINIMUM:
535 return ((Calendar*)cal)->getActualMinimum(field,
538 case UCAL_ACTUAL_MAXIMUM:
539 return ((Calendar*)cal)->getActualMaximum(field,
548 U_CAPI const char * U_EXPORT2
549 ucal_getLocaleByType(const UCalendar *cal, ULocDataLocaleType type, UErrorCode* status)
552 if (U_SUCCESS(*status)) {
553 *status = U_ILLEGAL_ARGUMENT_ERROR;
557 return ((Calendar*)cal)->getLocaleID(type, *status);
560 U_CAPI const char * U_EXPORT2
561 ucal_getTZDataVersion(UErrorCode* status)
563 return TimeZone::getTZDataVersion(*status);
566 U_CAPI int32_t U_EXPORT2
567 ucal_getCanonicalTimeZoneID(const UChar* id, int32_t len,
568 UChar* result, int32_t resultCapacity, UBool *isSystemID, UErrorCode* status) {
569 if(status == 0 || U_FAILURE(*status)) {
575 if (id == 0 || len == 0 || result == 0 || resultCapacity <= 0) {
576 *status = U_ILLEGAL_ARGUMENT_ERROR;
580 UnicodeString canonical;
581 UBool systemID = FALSE;
582 TimeZone::getCanonicalID(UnicodeString(id, len), canonical, systemID, *status);
583 if (U_SUCCESS(*status)) {
585 *isSystemID = systemID;
587 reslen = canonical.extract(result, resultCapacity, *status);
592 U_CAPI const char * U_EXPORT2
593 ucal_getType(const UCalendar *cal, UErrorCode* status)
595 if (U_FAILURE(*status)) {
598 return ((Calendar*)cal)->getType();
601 U_CAPI UCalendarWeekdayType U_EXPORT2
602 ucal_getDayOfWeekType(const UCalendar *cal, UCalendarDaysOfWeek dayOfWeek, UErrorCode* status)
604 if (U_FAILURE(*status)) {
607 return ((Calendar*)cal)->getDayOfWeekType(dayOfWeek, *status);
610 U_CAPI int32_t U_EXPORT2
611 ucal_getWeekendTransition(const UCalendar *cal, UCalendarDaysOfWeek dayOfWeek, UErrorCode *status)
613 if (U_FAILURE(*status)) {
616 return ((Calendar*)cal)->getWeekendTransition(dayOfWeek, *status);
619 U_CAPI UBool U_EXPORT2
620 ucal_isWeekend(const UCalendar *cal, UDate date, UErrorCode *status)
622 if (U_FAILURE(*status)) {
625 return ((Calendar*)cal)->isWeekend(date, *status);
628 U_CAPI int32_t U_EXPORT2
629 ucal_getFieldDifference(UCalendar* cal, UDate target,
630 UCalendarDateFields field,
633 if (U_FAILURE(*status)) {
636 return ((Calendar*)cal)->fieldDifference(target, field, *status);
640 static const UEnumeration defaultKeywordValues = {
643 ulist_close_keyword_values_iterator,
644 ulist_count_keyword_values,
646 ulist_next_keyword_value,
647 ulist_reset_keyword_values_iterator
650 static const char * const CAL_TYPES[] = {
663 "ethiopic-amete-alem",
672 U_CAPI UEnumeration* U_EXPORT2
673 ucal_getKeywordValuesForLocale(const char * /* key */, const char* locale, UBool commonlyUsed, UErrorCode *status) {
675 char prefRegion[ULOC_COUNTRY_CAPACITY];
676 (void)ulocimp_getRegionForSupplementalData(locale, TRUE, prefRegion, sizeof(prefRegion), status);
678 // Read preferred calendar values from supplementalData calendarPreference
679 UResourceBundle *rb = ures_openDirect(NULL, "supplementalData", status);
680 ures_getByKey(rb, "calendarPreferenceData", rb, status);
681 UResourceBundle *order = ures_getByKey(rb, prefRegion, NULL, status);
682 if (*status == U_MISSING_RESOURCE_ERROR && rb != NULL) {
683 *status = U_ZERO_ERROR;
684 order = ures_getByKey(rb, "001", NULL, status);
687 // Create a list of calendar type strings
688 UList *values = NULL;
689 if (U_SUCCESS(*status)) {
690 values = ulist_createEmptyList(status);
691 if (U_SUCCESS(*status)) {
692 for (int i = 0; i < ures_getSize(order); i++) {
694 const UChar *type = ures_getStringByIndex(order, i, &len, status);
695 char *caltype = (char*)uprv_malloc(len + 1);
696 if (caltype == NULL) {
697 *status = U_MEMORY_ALLOCATION_ERROR;
700 u_UCharsToChars(type, caltype, len);
701 *(caltype + len) = 0;
703 ulist_addItemEndList(values, caltype, TRUE, status);
704 if (U_FAILURE(*status)) {
709 if (U_SUCCESS(*status) && !commonlyUsed) {
710 // If not commonlyUsed, add other available values
711 for (int32_t i = 0; CAL_TYPES[i] != NULL; i++) {
712 if (!ulist_containsString(values, CAL_TYPES[i], (int32_t)uprv_strlen(CAL_TYPES[i]))) {
713 ulist_addItemEndList(values, CAL_TYPES[i], FALSE, status);
714 if (U_FAILURE(*status)) {
720 if (U_FAILURE(*status)) {
721 ulist_deleteList(values);
730 if (U_FAILURE(*status) || values == NULL) {
734 // Create string enumeration
735 UEnumeration *en = (UEnumeration*)uprv_malloc(sizeof(UEnumeration));
737 *status = U_MEMORY_ALLOCATION_ERROR;
738 ulist_deleteList(values);
741 ulist_resetList(values);
742 memcpy(en, &defaultKeywordValues, sizeof(UEnumeration));
743 en->context = values;
747 U_CAPI UBool U_EXPORT2
748 ucal_getTimeZoneTransitionDate(const UCalendar* cal, UTimeZoneTransitionType type,
749 UDate* transition, UErrorCode* status)
751 if (U_FAILURE(*status)) {
754 UDate base = ((Calendar*)cal)->getTime(*status);
755 const TimeZone& tz = ((Calendar*)cal)->getTimeZone();
756 const BasicTimeZone * btz = dynamic_cast<const BasicTimeZone *>(&tz);
757 if (btz != NULL && U_SUCCESS(*status)) {
758 TimeZoneTransition tzt;
759 UBool inclusive = (type == UCAL_TZ_TRANSITION_NEXT_INCLUSIVE || type == UCAL_TZ_TRANSITION_PREVIOUS_INCLUSIVE);
760 UBool result = (type == UCAL_TZ_TRANSITION_NEXT || type == UCAL_TZ_TRANSITION_NEXT_INCLUSIVE)?
761 btz->getNextTransition(base, inclusive, tzt):
762 btz->getPreviousTransition(base, inclusive, tzt);
764 *transition = tzt.getTime();
771 U_CAPI int32_t U_EXPORT2
772 ucal_getWindowsTimeZoneID(const UChar* id, int32_t len, UChar* winid, int32_t winidCapacity, UErrorCode* status) {
773 if (U_FAILURE(*status)) {
777 int32_t resultLen = 0;
778 UnicodeString resultWinID;
780 TimeZone::getWindowsID(UnicodeString(id, len), resultWinID, *status);
781 if (U_SUCCESS(*status) && resultWinID.length() > 0) {
782 resultLen = resultWinID.length();
783 resultWinID.extract(winid, winidCapacity, *status);
789 U_CAPI int32_t U_EXPORT2
790 ucal_getTimeZoneIDForWindowsID(const UChar* winid, int32_t len, const char* region, UChar* id, int32_t idCapacity, UErrorCode* status) {
791 if (U_FAILURE(*status)) {
795 int32_t resultLen = 0;
796 UnicodeString resultID;
798 TimeZone::getIDForWindowsID(UnicodeString(winid, len), region, resultID, *status);
799 if (U_SUCCESS(*status) && resultID.length() > 0) {
800 resultLen = resultID.length();
801 resultID.extract(id, idCapacity, *status);
807 #endif /* #if !UCONFIG_NO_FORMATTING */