michael@0: /* michael@0: ******************************************************************************* michael@0: * Copyright (C) 1996-2013, International Business Machines michael@0: * Corporation and others. All Rights Reserved. michael@0: ******************************************************************************* michael@0: */ michael@0: michael@0: #include "utypeinfo.h" // for 'typeid' to work michael@0: michael@0: #include "unicode/utypes.h" michael@0: michael@0: #if !UCONFIG_NO_FORMATTING michael@0: michael@0: #include "unicode/ucal.h" michael@0: #include "unicode/uloc.h" michael@0: #include "unicode/calendar.h" michael@0: #include "unicode/timezone.h" michael@0: #include "unicode/gregocal.h" michael@0: #include "unicode/simpletz.h" michael@0: #include "unicode/ustring.h" michael@0: #include "unicode/strenum.h" michael@0: #include "unicode/localpointer.h" michael@0: #include "cmemory.h" michael@0: #include "cstring.h" michael@0: #include "ustrenum.h" michael@0: #include "uenumimp.h" michael@0: #include "ulist.h" michael@0: michael@0: U_NAMESPACE_USE michael@0: michael@0: static TimeZone* michael@0: _createTimeZone(const UChar* zoneID, int32_t len, UErrorCode* ec) { michael@0: TimeZone* zone = NULL; michael@0: if (ec!=NULL && U_SUCCESS(*ec)) { michael@0: // Note that if zoneID is invalid, we get back GMT. This odd michael@0: // behavior is by design and goes back to the JDK. The only michael@0: // failure we will see is a memory allocation failure. michael@0: int32_t l = (len<0 ? u_strlen(zoneID) : len); michael@0: UnicodeString zoneStrID; michael@0: zoneStrID.setTo((UBool)(len < 0), zoneID, l); /* temporary read-only alias */ michael@0: zone = TimeZone::createTimeZone(zoneStrID); michael@0: if (zone == NULL) { michael@0: *ec = U_MEMORY_ALLOCATION_ERROR; michael@0: } michael@0: } michael@0: return zone; michael@0: } michael@0: michael@0: U_CAPI UEnumeration* U_EXPORT2 michael@0: ucal_openTimeZoneIDEnumeration(USystemTimeZoneType zoneType, const char* region, michael@0: const int32_t* rawOffset, UErrorCode* ec) { michael@0: return uenum_openFromStringEnumeration(TimeZone::createTimeZoneIDEnumeration( michael@0: zoneType, region, rawOffset, *ec), ec); michael@0: } michael@0: michael@0: U_CAPI UEnumeration* U_EXPORT2 michael@0: ucal_openTimeZones(UErrorCode* ec) { michael@0: return uenum_openFromStringEnumeration(TimeZone::createEnumeration(), ec); michael@0: } michael@0: michael@0: U_CAPI UEnumeration* U_EXPORT2 michael@0: ucal_openCountryTimeZones(const char* country, UErrorCode* ec) { michael@0: return uenum_openFromStringEnumeration(TimeZone::createEnumeration(country), ec); michael@0: } michael@0: michael@0: U_CAPI int32_t U_EXPORT2 michael@0: ucal_getDefaultTimeZone(UChar* result, int32_t resultCapacity, UErrorCode* ec) { michael@0: int32_t len = 0; michael@0: if (ec!=NULL && U_SUCCESS(*ec)) { michael@0: TimeZone* zone = TimeZone::createDefault(); michael@0: if (zone == NULL) { michael@0: *ec = U_MEMORY_ALLOCATION_ERROR; michael@0: } else { michael@0: UnicodeString id; michael@0: zone->getID(id); michael@0: delete zone; michael@0: len = id.extract(result, resultCapacity, *ec); michael@0: } michael@0: } michael@0: return len; michael@0: } michael@0: michael@0: U_CAPI void U_EXPORT2 michael@0: ucal_setDefaultTimeZone(const UChar* zoneID, UErrorCode* ec) { michael@0: TimeZone* zone = _createTimeZone(zoneID, -1, ec); michael@0: if (zone != NULL) { michael@0: TimeZone::adoptDefault(zone); michael@0: } michael@0: } michael@0: michael@0: U_CAPI int32_t U_EXPORT2 michael@0: ucal_getDSTSavings(const UChar* zoneID, UErrorCode* ec) { michael@0: int32_t result = 0; michael@0: TimeZone* zone = _createTimeZone(zoneID, -1, ec); michael@0: if (U_SUCCESS(*ec)) { michael@0: SimpleTimeZone* stz = dynamic_cast(zone); michael@0: if (stz != NULL) { michael@0: result = stz->getDSTSavings(); michael@0: } else { michael@0: // Since there is no getDSTSavings on TimeZone, we use a michael@0: // heuristic: Starting with the current time, march michael@0: // forwards for one year, looking for DST savings. michael@0: // Stepping by weeks is sufficient. michael@0: UDate d = Calendar::getNow(); michael@0: for (int32_t i=0; i<53; ++i, d+=U_MILLIS_PER_DAY*7.0) { michael@0: int32_t raw, dst; michael@0: zone->getOffset(d, FALSE, raw, dst, *ec); michael@0: if (U_FAILURE(*ec)) { michael@0: break; michael@0: } else if (dst != 0) { michael@0: result = dst; michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: delete zone; michael@0: return result; michael@0: } michael@0: michael@0: U_CAPI UDate U_EXPORT2 michael@0: ucal_getNow() michael@0: { michael@0: michael@0: return Calendar::getNow(); michael@0: } michael@0: michael@0: #define ULOC_LOCALE_IDENTIFIER_CAPACITY (ULOC_FULLNAME_CAPACITY + 1 + ULOC_KEYWORD_AND_VALUES_CAPACITY) michael@0: michael@0: U_CAPI UCalendar* U_EXPORT2 michael@0: ucal_open( const UChar* zoneID, michael@0: int32_t len, michael@0: const char* locale, michael@0: UCalendarType caltype, michael@0: UErrorCode* status) michael@0: { michael@0: michael@0: if(U_FAILURE(*status)) return 0; michael@0: michael@0: TimeZone* zone = (zoneID==NULL) ? TimeZone::createDefault() michael@0: : _createTimeZone(zoneID, len, status); michael@0: michael@0: if (U_FAILURE(*status)) { michael@0: return NULL; michael@0: } michael@0: michael@0: if ( caltype == UCAL_GREGORIAN ) { michael@0: char localeBuf[ULOC_LOCALE_IDENTIFIER_CAPACITY]; michael@0: if ( locale == NULL ) { michael@0: locale = uloc_getDefault(); michael@0: } michael@0: uprv_strncpy(localeBuf, locale, ULOC_LOCALE_IDENTIFIER_CAPACITY); michael@0: uloc_setKeywordValue("calendar", "gregorian", localeBuf, ULOC_LOCALE_IDENTIFIER_CAPACITY, status); michael@0: if (U_FAILURE(*status)) { michael@0: return NULL; michael@0: } michael@0: return (UCalendar*)Calendar::createInstance(zone, Locale(localeBuf), *status); michael@0: } michael@0: return (UCalendar*)Calendar::createInstance(zone, Locale(locale), *status); michael@0: } michael@0: michael@0: U_CAPI void U_EXPORT2 michael@0: ucal_close(UCalendar *cal) michael@0: { michael@0: michael@0: delete (Calendar*) cal; michael@0: } michael@0: michael@0: U_CAPI UCalendar* U_EXPORT2 michael@0: ucal_clone(const UCalendar* cal, michael@0: UErrorCode* status) michael@0: { michael@0: if(U_FAILURE(*status)) return 0; michael@0: michael@0: Calendar* res = ((Calendar*)cal)->clone(); michael@0: michael@0: if(res == 0) { michael@0: *status = U_MEMORY_ALLOCATION_ERROR; michael@0: return 0; michael@0: } michael@0: michael@0: return (UCalendar*) res; michael@0: } michael@0: michael@0: U_CAPI void U_EXPORT2 michael@0: ucal_setTimeZone( UCalendar* cal, michael@0: const UChar* zoneID, michael@0: int32_t len, michael@0: UErrorCode *status) michael@0: { michael@0: michael@0: if(U_FAILURE(*status)) michael@0: return; michael@0: michael@0: TimeZone* zone = (zoneID==NULL) ? TimeZone::createDefault() michael@0: : _createTimeZone(zoneID, len, status); michael@0: michael@0: if (zone != NULL) { michael@0: ((Calendar*)cal)->adoptTimeZone(zone); michael@0: } michael@0: } michael@0: michael@0: U_CAPI int32_t U_EXPORT2 michael@0: ucal_getTimeZoneID(const UCalendar *cal, michael@0: UChar *result, michael@0: int32_t resultLength, michael@0: UErrorCode *status) michael@0: { michael@0: if (U_FAILURE(*status)) { michael@0: return 0; michael@0: } michael@0: const TimeZone& tz = ((Calendar*)cal)->getTimeZone(); michael@0: UnicodeString id; michael@0: tz.getID(id); michael@0: return id.extract(result, resultLength, *status); michael@0: } michael@0: michael@0: U_CAPI int32_t U_EXPORT2 michael@0: ucal_getTimeZoneDisplayName(const UCalendar* cal, michael@0: UCalendarDisplayNameType type, michael@0: const char *locale, michael@0: UChar* result, michael@0: int32_t resultLength, michael@0: UErrorCode* status) michael@0: { michael@0: michael@0: if(U_FAILURE(*status)) return -1; michael@0: michael@0: const TimeZone& tz = ((Calendar*)cal)->getTimeZone(); michael@0: UnicodeString id; michael@0: if(!(result==NULL && resultLength==0)) { michael@0: // NULL destination for pure preflighting: empty dummy string michael@0: // otherwise, alias the destination buffer michael@0: id.setTo(result, 0, resultLength); michael@0: } michael@0: michael@0: switch(type) { michael@0: case UCAL_STANDARD: michael@0: tz.getDisplayName(FALSE, TimeZone::LONG, Locale(locale), id); michael@0: break; michael@0: michael@0: case UCAL_SHORT_STANDARD: michael@0: tz.getDisplayName(FALSE, TimeZone::SHORT, Locale(locale), id); michael@0: break; michael@0: michael@0: case UCAL_DST: michael@0: tz.getDisplayName(TRUE, TimeZone::LONG, Locale(locale), id); michael@0: break; michael@0: michael@0: case UCAL_SHORT_DST: michael@0: tz.getDisplayName(TRUE, TimeZone::SHORT, Locale(locale), id); michael@0: break; michael@0: } michael@0: michael@0: return id.extract(result, resultLength, *status); michael@0: } michael@0: michael@0: U_CAPI UBool U_EXPORT2 michael@0: ucal_inDaylightTime( const UCalendar* cal, michael@0: UErrorCode* status ) michael@0: { michael@0: michael@0: if(U_FAILURE(*status)) return (UBool) -1; michael@0: return ((Calendar*)cal)->inDaylightTime(*status); michael@0: } michael@0: michael@0: U_CAPI void U_EXPORT2 michael@0: ucal_setGregorianChange(UCalendar *cal, UDate date, UErrorCode *pErrorCode) { michael@0: if(U_FAILURE(*pErrorCode)) { michael@0: return; michael@0: } michael@0: Calendar *cpp_cal = (Calendar *)cal; michael@0: GregorianCalendar *gregocal = dynamic_cast(cpp_cal); michael@0: // Not if(gregocal == NULL) { michael@0: // because we really want to work only with a GregorianCalendar, not with michael@0: // its subclasses like BuddhistCalendar. michael@0: if (cpp_cal == NULL) { michael@0: // We normally don't check "this" pointers for NULL, but this here avoids michael@0: // compiler-generated exception-throwing code in case cal == NULL. michael@0: *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR; michael@0: return; michael@0: } michael@0: if(typeid(*cpp_cal) != typeid(GregorianCalendar)) { michael@0: *pErrorCode = U_UNSUPPORTED_ERROR; michael@0: return; michael@0: } michael@0: gregocal->setGregorianChange(date, *pErrorCode); michael@0: } michael@0: michael@0: U_CAPI UDate U_EXPORT2 michael@0: ucal_getGregorianChange(const UCalendar *cal, UErrorCode *pErrorCode) { michael@0: if(U_FAILURE(*pErrorCode)) { michael@0: return (UDate)0; michael@0: } michael@0: const Calendar *cpp_cal = (const Calendar *)cal; michael@0: const GregorianCalendar *gregocal = dynamic_cast(cpp_cal); michael@0: // Not if(gregocal == NULL) { michael@0: // see comments in ucal_setGregorianChange(). michael@0: if (cpp_cal == NULL) { michael@0: // We normally don't check "this" pointers for NULL, but this here avoids michael@0: // compiler-generated exception-throwing code in case cal == NULL. michael@0: *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR; michael@0: return (UDate)0; michael@0: } michael@0: if(typeid(*cpp_cal) != typeid(GregorianCalendar)) { michael@0: *pErrorCode = U_UNSUPPORTED_ERROR; michael@0: return (UDate)0; michael@0: } michael@0: return gregocal->getGregorianChange(); michael@0: } michael@0: michael@0: U_CAPI int32_t U_EXPORT2 michael@0: ucal_getAttribute( const UCalendar* cal, michael@0: UCalendarAttribute attr) michael@0: { michael@0: michael@0: switch(attr) { michael@0: case UCAL_LENIENT: michael@0: return ((Calendar*)cal)->isLenient(); michael@0: michael@0: case UCAL_FIRST_DAY_OF_WEEK: michael@0: return ((Calendar*)cal)->getFirstDayOfWeek(); michael@0: michael@0: case UCAL_MINIMAL_DAYS_IN_FIRST_WEEK: michael@0: return ((Calendar*)cal)->getMinimalDaysInFirstWeek(); michael@0: michael@0: case UCAL_REPEATED_WALL_TIME: michael@0: return ((Calendar*)cal)->getRepeatedWallTimeOption(); michael@0: michael@0: case UCAL_SKIPPED_WALL_TIME: michael@0: return ((Calendar*)cal)->getSkippedWallTimeOption(); michael@0: michael@0: default: michael@0: break; michael@0: } michael@0: return -1; michael@0: } michael@0: michael@0: U_CAPI void U_EXPORT2 michael@0: ucal_setAttribute( UCalendar* cal, michael@0: UCalendarAttribute attr, michael@0: int32_t newValue) michael@0: { michael@0: michael@0: switch(attr) { michael@0: case UCAL_LENIENT: michael@0: ((Calendar*)cal)->setLenient((UBool)newValue); michael@0: break; michael@0: michael@0: case UCAL_FIRST_DAY_OF_WEEK: michael@0: ((Calendar*)cal)->setFirstDayOfWeek((UCalendarDaysOfWeek)newValue); michael@0: break; michael@0: michael@0: case UCAL_MINIMAL_DAYS_IN_FIRST_WEEK: michael@0: ((Calendar*)cal)->setMinimalDaysInFirstWeek((uint8_t)newValue); michael@0: break; michael@0: michael@0: case UCAL_REPEATED_WALL_TIME: michael@0: ((Calendar*)cal)->setRepeatedWallTimeOption((UCalendarWallTimeOption)newValue); michael@0: break; michael@0: michael@0: case UCAL_SKIPPED_WALL_TIME: michael@0: ((Calendar*)cal)->setSkippedWallTimeOption((UCalendarWallTimeOption)newValue); michael@0: break; michael@0: } michael@0: } michael@0: michael@0: U_CAPI const char* U_EXPORT2 michael@0: ucal_getAvailable(int32_t index) michael@0: { michael@0: michael@0: return uloc_getAvailable(index); michael@0: } michael@0: michael@0: U_CAPI int32_t U_EXPORT2 michael@0: ucal_countAvailable() michael@0: { michael@0: michael@0: return uloc_countAvailable(); michael@0: } michael@0: michael@0: U_CAPI UDate U_EXPORT2 michael@0: ucal_getMillis( const UCalendar* cal, michael@0: UErrorCode* status) michael@0: { michael@0: michael@0: if(U_FAILURE(*status)) return (UDate) 0; michael@0: michael@0: return ((Calendar*)cal)->getTime(*status); michael@0: } michael@0: michael@0: U_CAPI void U_EXPORT2 michael@0: ucal_setMillis( UCalendar* cal, michael@0: UDate dateTime, michael@0: UErrorCode* status ) michael@0: { michael@0: if(U_FAILURE(*status)) return; michael@0: michael@0: ((Calendar*)cal)->setTime(dateTime, *status); michael@0: } michael@0: michael@0: // TBD: why does this take an UErrorCode? michael@0: U_CAPI void U_EXPORT2 michael@0: ucal_setDate( UCalendar* cal, michael@0: int32_t year, michael@0: int32_t month, michael@0: int32_t date, michael@0: UErrorCode *status) michael@0: { michael@0: michael@0: if(U_FAILURE(*status)) return; michael@0: michael@0: ((Calendar*)cal)->set(year, month, date); michael@0: } michael@0: michael@0: // TBD: why does this take an UErrorCode? michael@0: U_CAPI void U_EXPORT2 michael@0: ucal_setDateTime( UCalendar* cal, michael@0: int32_t year, michael@0: int32_t month, michael@0: int32_t date, michael@0: int32_t hour, michael@0: int32_t minute, michael@0: int32_t second, michael@0: UErrorCode *status) michael@0: { michael@0: if(U_FAILURE(*status)) return; michael@0: michael@0: ((Calendar*)cal)->set(year, month, date, hour, minute, second); michael@0: } michael@0: michael@0: U_CAPI UBool U_EXPORT2 michael@0: ucal_equivalentTo( const UCalendar* cal1, michael@0: const UCalendar* cal2) michael@0: { michael@0: michael@0: return ((Calendar*)cal1)->isEquivalentTo(*((Calendar*)cal2)); michael@0: } michael@0: michael@0: U_CAPI void U_EXPORT2 michael@0: ucal_add( UCalendar* cal, michael@0: UCalendarDateFields field, michael@0: int32_t amount, michael@0: UErrorCode* status) michael@0: { michael@0: michael@0: if(U_FAILURE(*status)) return; michael@0: michael@0: ((Calendar*)cal)->add(field, amount, *status); michael@0: } michael@0: michael@0: U_CAPI void U_EXPORT2 michael@0: ucal_roll( UCalendar* cal, michael@0: UCalendarDateFields field, michael@0: int32_t amount, michael@0: UErrorCode* status) michael@0: { michael@0: michael@0: if(U_FAILURE(*status)) return; michael@0: michael@0: ((Calendar*)cal)->roll(field, amount, *status); michael@0: } michael@0: michael@0: U_CAPI int32_t U_EXPORT2 michael@0: ucal_get( const UCalendar* cal, michael@0: UCalendarDateFields field, michael@0: UErrorCode* status ) michael@0: { michael@0: michael@0: if(U_FAILURE(*status)) return -1; michael@0: michael@0: return ((Calendar*)cal)->get(field, *status); michael@0: } michael@0: michael@0: U_CAPI void U_EXPORT2 michael@0: ucal_set( UCalendar* cal, michael@0: UCalendarDateFields field, michael@0: int32_t value) michael@0: { michael@0: michael@0: ((Calendar*)cal)->set(field, value); michael@0: } michael@0: michael@0: U_CAPI UBool U_EXPORT2 michael@0: ucal_isSet( const UCalendar* cal, michael@0: UCalendarDateFields field) michael@0: { michael@0: michael@0: return ((Calendar*)cal)->isSet(field); michael@0: } michael@0: michael@0: U_CAPI void U_EXPORT2 michael@0: ucal_clearField( UCalendar* cal, michael@0: UCalendarDateFields field) michael@0: { michael@0: michael@0: ((Calendar*)cal)->clear(field); michael@0: } michael@0: michael@0: U_CAPI void U_EXPORT2 michael@0: ucal_clear(UCalendar* calendar) michael@0: { michael@0: michael@0: ((Calendar*)calendar)->clear(); michael@0: } michael@0: michael@0: U_CAPI int32_t U_EXPORT2 michael@0: ucal_getLimit( const UCalendar* cal, michael@0: UCalendarDateFields field, michael@0: UCalendarLimitType type, michael@0: UErrorCode *status) michael@0: { michael@0: michael@0: if(status==0 || U_FAILURE(*status)) { michael@0: return -1; michael@0: } michael@0: michael@0: switch(type) { michael@0: case UCAL_MINIMUM: michael@0: return ((Calendar*)cal)->getMinimum(field); michael@0: michael@0: case UCAL_MAXIMUM: michael@0: return ((Calendar*)cal)->getMaximum(field); michael@0: michael@0: case UCAL_GREATEST_MINIMUM: michael@0: return ((Calendar*)cal)->getGreatestMinimum(field); michael@0: michael@0: case UCAL_LEAST_MAXIMUM: michael@0: return ((Calendar*)cal)->getLeastMaximum(field); michael@0: michael@0: case UCAL_ACTUAL_MINIMUM: michael@0: return ((Calendar*)cal)->getActualMinimum(field, michael@0: *status); michael@0: michael@0: case UCAL_ACTUAL_MAXIMUM: michael@0: return ((Calendar*)cal)->getActualMaximum(field, michael@0: *status); michael@0: michael@0: default: michael@0: break; michael@0: } michael@0: return -1; michael@0: } michael@0: michael@0: U_CAPI const char * U_EXPORT2 michael@0: ucal_getLocaleByType(const UCalendar *cal, ULocDataLocaleType type, UErrorCode* status) michael@0: { michael@0: if (cal == NULL) { michael@0: if (U_SUCCESS(*status)) { michael@0: *status = U_ILLEGAL_ARGUMENT_ERROR; michael@0: } michael@0: return NULL; michael@0: } michael@0: return ((Calendar*)cal)->getLocaleID(type, *status); michael@0: } michael@0: michael@0: U_CAPI const char * U_EXPORT2 michael@0: ucal_getTZDataVersion(UErrorCode* status) michael@0: { michael@0: return TimeZone::getTZDataVersion(*status); michael@0: } michael@0: michael@0: U_CAPI int32_t U_EXPORT2 michael@0: ucal_getCanonicalTimeZoneID(const UChar* id, int32_t len, michael@0: UChar* result, int32_t resultCapacity, UBool *isSystemID, UErrorCode* status) { michael@0: if(status == 0 || U_FAILURE(*status)) { michael@0: return 0; michael@0: } michael@0: if (isSystemID) { michael@0: *isSystemID = FALSE; michael@0: } michael@0: if (id == 0 || len == 0 || result == 0 || resultCapacity <= 0) { michael@0: *status = U_ILLEGAL_ARGUMENT_ERROR; michael@0: return 0; michael@0: } michael@0: int32_t reslen = 0; michael@0: UnicodeString canonical; michael@0: UBool systemID = FALSE; michael@0: TimeZone::getCanonicalID(UnicodeString(id, len), canonical, systemID, *status); michael@0: if (U_SUCCESS(*status)) { michael@0: if (isSystemID) { michael@0: *isSystemID = systemID; michael@0: } michael@0: reslen = canonical.extract(result, resultCapacity, *status); michael@0: } michael@0: return reslen; michael@0: } michael@0: michael@0: U_CAPI const char * U_EXPORT2 michael@0: ucal_getType(const UCalendar *cal, UErrorCode* status) michael@0: { michael@0: if (U_FAILURE(*status)) { michael@0: return NULL; michael@0: } michael@0: return ((Calendar*)cal)->getType(); michael@0: } michael@0: michael@0: U_CAPI UCalendarWeekdayType U_EXPORT2 michael@0: ucal_getDayOfWeekType(const UCalendar *cal, UCalendarDaysOfWeek dayOfWeek, UErrorCode* status) michael@0: { michael@0: if (U_FAILURE(*status)) { michael@0: return UCAL_WEEKDAY; michael@0: } michael@0: return ((Calendar*)cal)->getDayOfWeekType(dayOfWeek, *status); michael@0: } michael@0: michael@0: U_CAPI int32_t U_EXPORT2 michael@0: ucal_getWeekendTransition(const UCalendar *cal, UCalendarDaysOfWeek dayOfWeek, UErrorCode *status) michael@0: { michael@0: if (U_FAILURE(*status)) { michael@0: return 0; michael@0: } michael@0: return ((Calendar*)cal)->getWeekendTransition(dayOfWeek, *status); michael@0: } michael@0: michael@0: U_CAPI UBool U_EXPORT2 michael@0: ucal_isWeekend(const UCalendar *cal, UDate date, UErrorCode *status) michael@0: { michael@0: if (U_FAILURE(*status)) { michael@0: return FALSE; michael@0: } michael@0: return ((Calendar*)cal)->isWeekend(date, *status); michael@0: } michael@0: michael@0: U_CAPI int32_t U_EXPORT2 michael@0: ucal_getFieldDifference(UCalendar* cal, UDate target, michael@0: UCalendarDateFields field, michael@0: UErrorCode* status ) michael@0: { michael@0: if (U_FAILURE(*status)) { michael@0: return 0; michael@0: } michael@0: return ((Calendar*)cal)->fieldDifference(target, field, *status); michael@0: } michael@0: michael@0: michael@0: static const UEnumeration defaultKeywordValues = { michael@0: NULL, michael@0: NULL, michael@0: ulist_close_keyword_values_iterator, michael@0: ulist_count_keyword_values, michael@0: uenum_unextDefault, michael@0: ulist_next_keyword_value, michael@0: ulist_reset_keyword_values_iterator michael@0: }; michael@0: michael@0: static const char * const CAL_TYPES[] = { michael@0: "gregorian", michael@0: "japanese", michael@0: "buddhist", michael@0: "roc", michael@0: "persian", michael@0: "islamic-civil", michael@0: "islamic", michael@0: "hebrew", michael@0: "chinese", michael@0: "indian", michael@0: "coptic", michael@0: "ethiopic", michael@0: "ethiopic-amete-alem", michael@0: "iso8601", michael@0: "dangi", michael@0: "islamic-umalqura", michael@0: "islamic-tbla", michael@0: "islamic-rgsa", michael@0: NULL michael@0: }; michael@0: michael@0: U_CAPI UEnumeration* U_EXPORT2 michael@0: ucal_getKeywordValuesForLocale(const char * /* key */, const char* locale, UBool commonlyUsed, UErrorCode *status) { michael@0: // Resolve region michael@0: char prefRegion[ULOC_FULLNAME_CAPACITY] = ""; michael@0: int32_t prefRegionLength = 0; michael@0: prefRegionLength = uloc_getCountry(locale, prefRegion, sizeof(prefRegion), status); michael@0: if (prefRegionLength == 0) { michael@0: char loc[ULOC_FULLNAME_CAPACITY] = ""; michael@0: uloc_addLikelySubtags(locale, loc, sizeof(loc), status); michael@0: michael@0: prefRegionLength = uloc_getCountry(loc, prefRegion, sizeof(prefRegion), status); michael@0: } michael@0: michael@0: // Read preferred calendar values from supplementalData calendarPreference michael@0: UResourceBundle *rb = ures_openDirect(NULL, "supplementalData", status); michael@0: ures_getByKey(rb, "calendarPreferenceData", rb, status); michael@0: UResourceBundle *order = ures_getByKey(rb, prefRegion, NULL, status); michael@0: if (*status == U_MISSING_RESOURCE_ERROR && rb != NULL) { michael@0: *status = U_ZERO_ERROR; michael@0: order = ures_getByKey(rb, "001", NULL, status); michael@0: } michael@0: michael@0: // Create a list of calendar type strings michael@0: UList *values = NULL; michael@0: if (U_SUCCESS(*status)) { michael@0: values = ulist_createEmptyList(status); michael@0: if (U_SUCCESS(*status)) { michael@0: for (int i = 0; i < ures_getSize(order); i++) { michael@0: int32_t len; michael@0: const UChar *type = ures_getStringByIndex(order, i, &len, status); michael@0: char *caltype = (char*)uprv_malloc(len + 1); michael@0: if (caltype == NULL) { michael@0: *status = U_MEMORY_ALLOCATION_ERROR; michael@0: break; michael@0: } michael@0: u_UCharsToChars(type, caltype, len); michael@0: *(caltype + len) = 0; michael@0: michael@0: ulist_addItemEndList(values, caltype, TRUE, status); michael@0: if (U_FAILURE(*status)) { michael@0: break; michael@0: } michael@0: } michael@0: michael@0: if (U_SUCCESS(*status) && !commonlyUsed) { michael@0: // If not commonlyUsed, add other available values michael@0: for (int32_t i = 0; CAL_TYPES[i] != NULL; i++) { michael@0: if (!ulist_containsString(values, CAL_TYPES[i], (int32_t)uprv_strlen(CAL_TYPES[i]))) { michael@0: ulist_addItemEndList(values, CAL_TYPES[i], FALSE, status); michael@0: if (U_FAILURE(*status)) { michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: if (U_FAILURE(*status)) { michael@0: ulist_deleteList(values); michael@0: values = NULL; michael@0: } michael@0: } michael@0: } michael@0: michael@0: ures_close(order); michael@0: ures_close(rb); michael@0: michael@0: if (U_FAILURE(*status) || values == NULL) { michael@0: return NULL; michael@0: } michael@0: michael@0: // Create string enumeration michael@0: UEnumeration *en = (UEnumeration*)uprv_malloc(sizeof(UEnumeration)); michael@0: if (en == NULL) { michael@0: *status = U_MEMORY_ALLOCATION_ERROR; michael@0: ulist_deleteList(values); michael@0: return NULL; michael@0: } michael@0: ulist_resetList(values); michael@0: memcpy(en, &defaultKeywordValues, sizeof(UEnumeration)); michael@0: en->context = values; michael@0: return en; michael@0: } michael@0: michael@0: U_CAPI UBool U_EXPORT2 michael@0: ucal_getTimeZoneTransitionDate(const UCalendar* cal, UTimeZoneTransitionType type, michael@0: UDate* transition, UErrorCode* status) michael@0: { michael@0: if (U_FAILURE(*status)) { michael@0: return FALSE; michael@0: } michael@0: UDate base = ((Calendar*)cal)->getTime(*status); michael@0: const TimeZone& tz = ((Calendar*)cal)->getTimeZone(); michael@0: const BasicTimeZone * btz = dynamic_cast(&tz); michael@0: if (btz != NULL && U_SUCCESS(*status)) { michael@0: TimeZoneTransition tzt; michael@0: UBool inclusive = (type == UCAL_TZ_TRANSITION_NEXT_INCLUSIVE || type == UCAL_TZ_TRANSITION_PREVIOUS_INCLUSIVE); michael@0: UBool result = (type == UCAL_TZ_TRANSITION_NEXT || type == UCAL_TZ_TRANSITION_NEXT_INCLUSIVE)? michael@0: btz->getNextTransition(base, inclusive, tzt): michael@0: btz->getPreviousTransition(base, inclusive, tzt); michael@0: if (result) { michael@0: *transition = tzt.getTime(); michael@0: return TRUE; michael@0: } michael@0: } michael@0: return FALSE; michael@0: } michael@0: michael@0: #ifndef U_HIDE_DRAFT_API michael@0: U_CAPI int32_t U_EXPORT2 michael@0: ucal_getWindowsTimeZoneID(const UChar* id, int32_t len, UChar* winid, int32_t winidCapacity, UErrorCode* status) { michael@0: if (U_FAILURE(*status)) { michael@0: return 0; michael@0: } michael@0: michael@0: int32_t resultLen = 0; michael@0: UnicodeString resultWinID; michael@0: michael@0: TimeZone::getWindowsID(UnicodeString(id, len), resultWinID, *status); michael@0: if (U_SUCCESS(*status) && resultWinID.length() > 0) { michael@0: resultLen = resultWinID.length(); michael@0: resultWinID.extract(winid, winidCapacity, *status); michael@0: } michael@0: michael@0: return resultLen; michael@0: } michael@0: michael@0: U_CAPI int32_t U_EXPORT2 michael@0: ucal_getTimeZoneIDForWindowsID(const UChar* winid, int32_t len, const char* region, UChar* id, int32_t idCapacity, UErrorCode* status) { michael@0: if (U_FAILURE(*status)) { michael@0: return 0; michael@0: } michael@0: michael@0: int32_t resultLen = 0; michael@0: UnicodeString resultID; michael@0: michael@0: TimeZone::getIDForWindowsID(UnicodeString(winid, len), region, resultID, *status); michael@0: if (U_SUCCESS(*status) && resultID.length() > 0) { michael@0: resultLen = resultID.length(); michael@0: resultID.extract(id, idCapacity, *status); michael@0: } michael@0: michael@0: return resultLen; michael@0: } michael@0: michael@0: #endif /* U_HIDE_DRAFT_API */ michael@0: michael@0: #endif /* #if !UCONFIG_NO_FORMATTING */