scuffed-code/icu4c/source/i18n/ucal.cpp

480 lines
12 KiB
C++
Raw Normal View History

1999-08-16 21:50:52 +00:00
/*
*******************************************************************************
* Copyright (C) 1996-2003, International Business Machines
* Corporation and others. All Rights Reserved.
1999-08-16 21:50:52 +00:00
*******************************************************************************
*/
#include "unicode/utypes.h"
#if !UCONFIG_NO_FORMATTING
1999-08-16 21:50:52 +00:00
#include "unicode/ucal.h"
#include "unicode/uloc.h"
#include "unicode/calendar.h"
#include "unicode/timezone.h"
#include "unicode/simpletz.h"
#include "unicode/ustring.h"
#include "unicode/strenum.h"
#include "cmemory.h"
#include "ustrenum.h"
1999-08-16 21:50:52 +00:00
U_NAMESPACE_USE
static TimeZone*
_createTimeZone(const UChar* zoneID, int32_t len, UErrorCode* ec) {
TimeZone* zone = NULL;
if (ec!=NULL && U_SUCCESS(*ec)) {
// Note that if zoneID is invalid, we get back GMT. This odd
// behavior is by design and goes back to the JDK. The only
// failure we will see is a memory allocation failure.
int32_t l = (len<0 ? u_strlen(zoneID) : len);
zone = TimeZone::createTimeZone(UnicodeString(zoneID, l));
if (zone == NULL) {
*ec = U_MEMORY_ALLOCATION_ERROR;
}
}
return zone;
}
U_CAPI UEnumeration* U_EXPORT2
ucal_openTimeZones(UErrorCode* ec) {
return uenum_openStringEnumeration(TimeZone::createEnumeration(), ec);
}
U_CAPI UEnumeration* U_EXPORT2
ucal_openCountryTimeZones(const char* country, UErrorCode* ec) {
return uenum_openStringEnumeration(TimeZone::createEnumeration(country), ec);
}
U_CAPI int32_t U_EXPORT2
ucal_getDefaultTimeZone(UChar* result, int32_t resultCapacity, UErrorCode* ec) {
int32_t len = 0;
if (ec!=NULL && U_SUCCESS(*ec)) {
TimeZone* zone = TimeZone::createDefault();
if (zone == NULL) {
*ec = U_MEMORY_ALLOCATION_ERROR;
} else {
UnicodeString id;
zone->getID(id);
delete zone;
len = id.extract(result, resultCapacity, *ec);
}
}
return len;
}
U_CAPI void U_EXPORT2
ucal_setDefaultTimeZone(const UChar* zoneID, UErrorCode* ec) {
TimeZone* zone = _createTimeZone(zoneID, -1, ec);
if (zone != NULL) {
TimeZone::adoptDefault(zone);
}
}
U_CAPI int32_t U_EXPORT2
ucal_getDSTSavings(const UChar* zoneID, UErrorCode* ec) {
int32_t result = 0;
TimeZone* zone = _createTimeZone(zoneID, -1, ec);
if (U_SUCCESS(*ec)) {
if (zone->getDynamicClassID() == SimpleTimeZone::getStaticClassID()) {
result = ((SimpleTimeZone*) zone)->getDSTSavings();
} else {
// Since there is no getDSTSavings on TimeZone, we use a
// heuristic: Starting with the current time, march
// forwards for one year, looking for DST savings.
// Stepping by weeks is sufficient.
UDate d = Calendar::getNow();
for (int32_t i=0; i<53; ++i, d+=U_MILLIS_PER_DAY*7.0) {
int32_t raw, dst;
zone->getOffset(d, FALSE, raw, dst, *ec);
if (U_FAILURE(*ec)) {
break;
} else if (dst != 0) {
result = dst;
break;
}
}
}
}
delete zone;
return result;
}
#ifdef U_USE_UCAL_OBSOLETE_2_8
U_CAPI const UChar* U_EXPORT2
1999-08-16 21:50:52 +00:00
ucal_getAvailableTZIDs( int32_t rawOffset,
int32_t index,
UErrorCode* status)
{
if(U_FAILURE(*status)) return 0;
1999-08-16 21:50:52 +00:00
int32_t count = 0;
1999-08-16 21:50:52 +00:00
const UChar *retVal = 0;
const UnicodeString** tzs = TimeZone::createAvailableIDs(rawOffset,
count);
1999-08-16 21:50:52 +00:00
if(tzs == 0) {
*status = U_MEMORY_ALLOCATION_ERROR;
1999-08-16 21:50:52 +00:00
return 0;
}
if(index < count)
retVal = tzs[index]->getBuffer();
else
*status = U_INDEX_OUTOFBOUNDS_ERROR;
uprv_free(tzs);
1999-08-16 21:50:52 +00:00
return retVal;
}
U_CAPI int32_t U_EXPORT2
1999-08-16 21:50:52 +00:00
ucal_countAvailableTZIDs(int32_t rawOffset)
{
int32_t count = 0;
const UnicodeString** tzs = TimeZone::createAvailableIDs(rawOffset,
count);
1999-08-16 21:50:52 +00:00
if(tzs == 0) {
// TBD: U_MEMORY_ALLOCATION_ERROR
1999-08-16 21:50:52 +00:00
return 0;
}
uprv_free(tzs);
1999-08-16 21:50:52 +00:00
return count;
}
#endif
1999-08-16 21:50:52 +00:00
U_CAPI UDate U_EXPORT2
1999-08-16 21:50:52 +00:00
ucal_getNow()
{
1999-08-16 21:50:52 +00:00
return Calendar::getNow();
}
// ignore type until we add more subclasses
U_CAPI UCalendar* U_EXPORT2
1999-08-16 21:50:52 +00:00
ucal_open( const UChar* zoneID,
int32_t len,
const char* locale,
UCalendarType /*type*/,
1999-08-16 21:50:52 +00:00
UErrorCode* status)
{
if(U_FAILURE(*status)) return 0;
1999-08-16 21:50:52 +00:00
TimeZone* zone = (zoneID==NULL) ? TimeZone::createDefault()
: _createTimeZone(zoneID, len, status);
1999-08-16 21:50:52 +00:00
if (U_FAILURE(*status)) {
return NULL;
1999-08-16 21:50:52 +00:00
}
return (UCalendar*)Calendar::createInstance(zone, Locale(locale), *status);
1999-08-16 21:50:52 +00:00
}
U_CAPI void U_EXPORT2
1999-08-16 21:50:52 +00:00
ucal_close(UCalendar *cal)
{
1999-08-16 21:50:52 +00:00
delete (Calendar*) cal;
}
U_CAPI void U_EXPORT2
1999-08-16 21:50:52 +00:00
ucal_setTimeZone( UCalendar* cal,
const UChar* zoneID,
int32_t len,
UErrorCode *status)
{
if(U_FAILURE(*status))
return;
1999-08-16 21:50:52 +00:00
TimeZone* zone = (zoneID==NULL) ? TimeZone::createDefault()
: _createTimeZone(zoneID, len, status);
1999-08-16 21:50:52 +00:00
if (zone != NULL) {
((Calendar*)cal)->adoptTimeZone(zone);
}
1999-08-16 21:50:52 +00:00
}
U_CAPI int32_t U_EXPORT2
ucal_getTimeZoneDisplayName(const UCalendar* cal,
1999-08-16 21:50:52 +00:00
UCalendarDisplayNameType type,
const char *locale,
1999-08-16 21:50:52 +00:00
UChar* result,
int32_t resultLength,
UErrorCode* status)
{
if(U_FAILURE(*status)) return -1;
1999-08-16 21:50:52 +00:00
const TimeZone& tz = ((Calendar*)cal)->getTimeZone();
UnicodeString id;
if(!(result==NULL && resultLength==0)) {
// NULL destination for pure preflighting: empty dummy string
// otherwise, alias the destination buffer
id.setTo(result, 0, resultLength);
}
1999-08-16 21:50:52 +00:00
switch(type) {
case UCAL_STANDARD:
tz.getDisplayName(FALSE, TimeZone::LONG, Locale(locale), id);
1999-08-16 21:50:52 +00:00
break;
case UCAL_SHORT_STANDARD:
tz.getDisplayName(FALSE, TimeZone::SHORT, Locale(locale), id);
1999-08-16 21:50:52 +00:00
break;
case UCAL_DST:
tz.getDisplayName(TRUE, TimeZone::LONG, Locale(locale), id);
1999-08-16 21:50:52 +00:00
break;
case UCAL_SHORT_DST:
tz.getDisplayName(TRUE, TimeZone::SHORT, Locale(locale), id);
1999-08-16 21:50:52 +00:00
break;
}
return id.extract(result, resultLength, *status);
1999-08-16 21:50:52 +00:00
}
U_CAPI UBool U_EXPORT2
1999-08-16 21:50:52 +00:00
ucal_inDaylightTime( const UCalendar* cal,
UErrorCode* status )
{
if(U_FAILURE(*status)) return (UBool) -1;
1999-08-16 21:50:52 +00:00
return ((Calendar*)cal)->inDaylightTime(*status);
}
U_CAPI int32_t U_EXPORT2
1999-08-16 21:50:52 +00:00
ucal_getAttribute( const UCalendar* cal,
UCalendarAttribute attr)
{
1999-08-16 21:50:52 +00:00
switch(attr) {
case UCAL_LENIENT:
return ((Calendar*)cal)->isLenient();
case UCAL_FIRST_DAY_OF_WEEK:
return ((Calendar*)cal)->getFirstDayOfWeek();
case UCAL_MINIMAL_DAYS_IN_FIRST_WEEK:
return ((Calendar*)cal)->getMinimalDaysInFirstWeek();
default:
break;
}
return -1;
1999-08-16 21:50:52 +00:00
}
U_CAPI void U_EXPORT2
1999-08-16 21:50:52 +00:00
ucal_setAttribute( UCalendar* cal,
UCalendarAttribute attr,
int32_t newValue)
{
1999-08-16 21:50:52 +00:00
switch(attr) {
case UCAL_LENIENT:
((Calendar*)cal)->setLenient((UBool)newValue);
1999-08-16 21:50:52 +00:00
break;
case UCAL_FIRST_DAY_OF_WEEK:
((Calendar*)cal)->setFirstDayOfWeek((UCalendarDaysOfWeek)newValue);
1999-08-16 21:50:52 +00:00
break;
case UCAL_MINIMAL_DAYS_IN_FIRST_WEEK:
((Calendar*)cal)->setMinimalDaysInFirstWeek((uint8_t)newValue);
break;
}
}
U_CAPI const char* U_EXPORT2
1999-08-16 21:50:52 +00:00
ucal_getAvailable(int32_t index)
{
1999-08-16 21:50:52 +00:00
return uloc_getAvailable(index);
}
U_CAPI int32_t U_EXPORT2
1999-08-16 21:50:52 +00:00
ucal_countAvailable()
{
1999-08-16 21:50:52 +00:00
return uloc_countAvailable();
}
U_CAPI UDate U_EXPORT2
1999-08-16 21:50:52 +00:00
ucal_getMillis( const UCalendar* cal,
UErrorCode* status)
{
if(U_FAILURE(*status)) return (UDate) 0;
1999-08-16 21:50:52 +00:00
return ((Calendar*)cal)->getTime(*status);
}
U_CAPI void U_EXPORT2
1999-08-16 21:50:52 +00:00
ucal_setMillis( UCalendar* cal,
UDate dateTime,
UErrorCode* status )
{
if(U_FAILURE(*status)) return;
1999-08-16 21:50:52 +00:00
((Calendar*)cal)->setTime(dateTime, *status);
}
// TBD: why does this take an UErrorCode?
U_CAPI void U_EXPORT2
1999-08-16 21:50:52 +00:00
ucal_setDate( UCalendar* cal,
int32_t year,
int32_t month,
int32_t date,
UErrorCode *status)
{
if(U_FAILURE(*status)) return;
1999-08-16 21:50:52 +00:00
((Calendar*)cal)->set(year, month, date);
}
// TBD: why does this take an UErrorCode?
U_CAPI void U_EXPORT2
1999-08-16 21:50:52 +00:00
ucal_setDateTime( UCalendar* cal,
int32_t year,
int32_t month,
int32_t date,
int32_t hour,
int32_t minute,
int32_t second,
UErrorCode *status)
{
if(U_FAILURE(*status)) return;
1999-08-16 21:50:52 +00:00
((Calendar*)cal)->set(year, month, date, hour, minute, second);
}
U_CAPI UBool U_EXPORT2
1999-08-16 21:50:52 +00:00
ucal_equivalentTo( const UCalendar* cal1,
const UCalendar* cal2)
{
return ((Calendar*)cal1)->isEquivalentTo(*((Calendar*)cal2));
1999-08-16 21:50:52 +00:00
}
U_CAPI void U_EXPORT2
1999-08-16 21:50:52 +00:00
ucal_add( UCalendar* cal,
UCalendarDateFields field,
int32_t amount,
UErrorCode* status)
{
if(U_FAILURE(*status)) return;
1999-08-16 21:50:52 +00:00
((Calendar*)cal)->add(field, amount, *status);
1999-08-16 21:50:52 +00:00
}
U_CAPI void U_EXPORT2
1999-08-16 21:50:52 +00:00
ucal_roll( UCalendar* cal,
UCalendarDateFields field,
int32_t amount,
UErrorCode* status)
{
if(U_FAILURE(*status)) return;
1999-08-16 21:50:52 +00:00
((Calendar*)cal)->roll(field, amount, *status);
1999-08-16 21:50:52 +00:00
}
U_CAPI int32_t U_EXPORT2
1999-08-16 21:50:52 +00:00
ucal_get( const UCalendar* cal,
UCalendarDateFields field,
UErrorCode* status )
{
if(U_FAILURE(*status)) return -1;
1999-08-16 21:50:52 +00:00
return ((Calendar*)cal)->get(field, *status);
1999-08-16 21:50:52 +00:00
}
U_CAPI void U_EXPORT2
1999-08-16 21:50:52 +00:00
ucal_set( UCalendar* cal,
UCalendarDateFields field,
int32_t value)
{
((Calendar*)cal)->set(field, value);
1999-08-16 21:50:52 +00:00
}
U_CAPI UBool U_EXPORT2
1999-08-16 21:50:52 +00:00
ucal_isSet( const UCalendar* cal,
UCalendarDateFields field)
{
return ((Calendar*)cal)->isSet(field);
1999-08-16 21:50:52 +00:00
}
U_CAPI void U_EXPORT2
1999-08-16 21:50:52 +00:00
ucal_clearField( UCalendar* cal,
UCalendarDateFields field)
{
((Calendar*)cal)->clear(field);
1999-08-16 21:50:52 +00:00
}
U_CAPI void U_EXPORT2
1999-08-16 21:50:52 +00:00
ucal_clear(UCalendar* calendar)
{
1999-08-16 21:50:52 +00:00
((Calendar*)calendar)->clear();
}
U_CAPI int32_t U_EXPORT2
1999-08-16 21:50:52 +00:00
ucal_getLimit( const UCalendar* cal,
UCalendarDateFields field,
UCalendarLimitType type,
UErrorCode *status)
{
if(status==0 || U_FAILURE(*status)) {
return -1;
}
1999-08-16 21:50:52 +00:00
switch(type) {
case UCAL_MINIMUM:
return ((Calendar*)cal)->getMinimum(field);
1999-08-16 21:50:52 +00:00
case UCAL_MAXIMUM:
return ((Calendar*)cal)->getMaximum(field);
1999-08-16 21:50:52 +00:00
case UCAL_GREATEST_MINIMUM:
return ((Calendar*)cal)->getGreatestMinimum(field);
1999-08-16 21:50:52 +00:00
case UCAL_LEAST_MAXIMUM:
return ((Calendar*)cal)->getLeastMaximum(field);
1999-08-16 21:50:52 +00:00
case UCAL_ACTUAL_MINIMUM:
return ((Calendar*)cal)->getActualMinimum(field,
1999-08-16 21:50:52 +00:00
*status);
case UCAL_ACTUAL_MAXIMUM:
return ((Calendar*)cal)->getActualMaximum(field,
1999-08-16 21:50:52 +00:00
*status);
default:
break;
}
return -1;
1999-08-16 21:50:52 +00:00
}
U_CAPI const char * U_EXPORT2
ucal_getLocaleByType(const UCalendar *cal, ULocDataLocaleType type, UErrorCode* status)
{
return ((Calendar*)cal)->getLocaleInternal(type, *status);
}
#endif /* #if !UCONFIG_NO_FORMATTING */