2 *******************************************************************************
3 * Copyright (C) 1996-2004, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 *******************************************************************************
8 #include "unicode/utypes.h"
10 #if !UCONFIG_NO_FORMATTING
12 #include "unicode/ucal.h"
13 #include "unicode/uloc.h"
14 #include "unicode/calendar.h"
15 #include "unicode/timezone.h"
16 #include "unicode/simpletz.h"
17 #include "unicode/ustring.h"
18 #include "unicode/strenum.h"
25 _createTimeZone(const UChar
* zoneID
, int32_t len
, UErrorCode
* ec
) {
26 TimeZone
* zone
= NULL
;
27 if (ec
!=NULL
&& U_SUCCESS(*ec
)) {
28 // Note that if zoneID is invalid, we get back GMT. This odd
29 // behavior is by design and goes back to the JDK. The only
30 // failure we will see is a memory allocation failure.
31 int32_t l
= (len
<0 ? u_strlen(zoneID
) : len
);
32 UnicodeString zoneStrID
;
33 zoneStrID
.setTo((UBool
)(len
< 0), zoneID
, l
); /* temporary read-only alias */
34 zone
= TimeZone::createTimeZone(zoneStrID
);
36 *ec
= U_MEMORY_ALLOCATION_ERROR
;
42 U_CAPI UEnumeration
* U_EXPORT2
43 ucal_openTimeZones(UErrorCode
* ec
) {
44 return uenum_openStringEnumeration(TimeZone::createEnumeration(), ec
);
47 U_CAPI UEnumeration
* U_EXPORT2
48 ucal_openCountryTimeZones(const char* country
, UErrorCode
* ec
) {
49 return uenum_openStringEnumeration(TimeZone::createEnumeration(country
), ec
);
52 U_CAPI
int32_t U_EXPORT2
53 ucal_getDefaultTimeZone(UChar
* result
, int32_t resultCapacity
, UErrorCode
* ec
) {
55 if (ec
!=NULL
&& U_SUCCESS(*ec
)) {
56 TimeZone
* zone
= TimeZone::createDefault();
58 *ec
= U_MEMORY_ALLOCATION_ERROR
;
63 len
= id
.extract(result
, resultCapacity
, *ec
);
70 ucal_setDefaultTimeZone(const UChar
* zoneID
, UErrorCode
* ec
) {
71 TimeZone
* zone
= _createTimeZone(zoneID
, -1, ec
);
73 TimeZone::adoptDefault(zone
);
77 U_CAPI
int32_t U_EXPORT2
78 ucal_getDSTSavings(const UChar
* zoneID
, UErrorCode
* ec
) {
80 TimeZone
* zone
= _createTimeZone(zoneID
, -1, ec
);
82 if (zone
->getDynamicClassID() == SimpleTimeZone::getStaticClassID()) {
83 result
= ((SimpleTimeZone
*) zone
)->getDSTSavings();
85 // Since there is no getDSTSavings on TimeZone, we use a
86 // heuristic: Starting with the current time, march
87 // forwards for one year, looking for DST savings.
88 // Stepping by weeks is sufficient.
89 UDate d
= Calendar::getNow();
90 for (int32_t i
=0; i
<53; ++i
, d
+=U_MILLIS_PER_DAY
*7.0) {
92 zone
->getOffset(d
, FALSE
, raw
, dst
, *ec
);
95 } else if (dst
!= 0) {
106 #ifdef U_USE_UCAL_OBSOLETE_2_8
107 U_CAPI
const UChar
* U_EXPORT2
108 ucal_getAvailableTZIDs( int32_t rawOffset
,
113 if(U_FAILURE(*status
)) return 0;
116 const UChar
*retVal
= 0;
118 const UnicodeString
** tzs
= TimeZone::createAvailableIDs(rawOffset
,
122 *status
= U_MEMORY_ALLOCATION_ERROR
;
127 retVal
= tzs
[index
]->getBuffer();
129 *status
= U_INDEX_OUTOFBOUNDS_ERROR
;
135 U_CAPI
int32_t U_EXPORT2
136 ucal_countAvailableTZIDs(int32_t rawOffset
)
141 const UnicodeString
** tzs
= TimeZone::createAvailableIDs(rawOffset
,
145 // TBD: U_MEMORY_ALLOCATION_ERROR
154 U_CAPI UDate U_EXPORT2
158 return Calendar::getNow();
161 // ignore type until we add more subclasses
162 U_CAPI UCalendar
* U_EXPORT2
163 ucal_open( const UChar
* zoneID
,
166 UCalendarType
/*type*/,
170 if(U_FAILURE(*status
)) return 0;
172 TimeZone
* zone
= (zoneID
==NULL
) ? TimeZone::createDefault()
173 : _createTimeZone(zoneID
, len
, status
);
175 if (U_FAILURE(*status
)) {
179 return (UCalendar
*)Calendar::createInstance(zone
, Locale(locale
), *status
);
182 U_CAPI
void U_EXPORT2
183 ucal_close(UCalendar
*cal
)
186 delete (Calendar
*) cal
;
189 U_CAPI
void U_EXPORT2
190 ucal_setTimeZone( UCalendar
* cal
,
196 if(U_FAILURE(*status
))
199 TimeZone
* zone
= (zoneID
==NULL
) ? TimeZone::createDefault()
200 : _createTimeZone(zoneID
, len
, status
);
203 ((Calendar
*)cal
)->adoptTimeZone(zone
);
207 U_CAPI
int32_t U_EXPORT2
208 ucal_getTimeZoneDisplayName(const UCalendar
* cal
,
209 UCalendarDisplayNameType type
,
212 int32_t resultLength
,
216 if(U_FAILURE(*status
)) return -1;
218 const TimeZone
& tz
= ((Calendar
*)cal
)->getTimeZone();
220 if(!(result
==NULL
&& resultLength
==0)) {
221 // NULL destination for pure preflighting: empty dummy string
222 // otherwise, alias the destination buffer
223 id
.setTo(result
, 0, resultLength
);
228 tz
.getDisplayName(FALSE
, TimeZone::LONG
, Locale(locale
), id
);
231 case UCAL_SHORT_STANDARD
:
232 tz
.getDisplayName(FALSE
, TimeZone::SHORT
, Locale(locale
), id
);
236 tz
.getDisplayName(TRUE
, TimeZone::LONG
, Locale(locale
), id
);
240 tz
.getDisplayName(TRUE
, TimeZone::SHORT
, Locale(locale
), id
);
244 return id
.extract(result
, resultLength
, *status
);
247 U_CAPI UBool U_EXPORT2
248 ucal_inDaylightTime( const UCalendar
* cal
,
252 if(U_FAILURE(*status
)) return (UBool
) -1;
253 return ((Calendar
*)cal
)->inDaylightTime(*status
);
256 U_CAPI
int32_t U_EXPORT2
257 ucal_getAttribute( const UCalendar
* cal
,
258 UCalendarAttribute attr
)
263 return ((Calendar
*)cal
)->isLenient();
265 case UCAL_FIRST_DAY_OF_WEEK
:
266 return ((Calendar
*)cal
)->getFirstDayOfWeek();
268 case UCAL_MINIMAL_DAYS_IN_FIRST_WEEK
:
269 return ((Calendar
*)cal
)->getMinimalDaysInFirstWeek();
277 U_CAPI
void U_EXPORT2
278 ucal_setAttribute( UCalendar
* cal
,
279 UCalendarAttribute attr
,
285 ((Calendar
*)cal
)->setLenient((UBool
)newValue
);
288 case UCAL_FIRST_DAY_OF_WEEK
:
289 ((Calendar
*)cal
)->setFirstDayOfWeek((UCalendarDaysOfWeek
)newValue
);
292 case UCAL_MINIMAL_DAYS_IN_FIRST_WEEK
:
293 ((Calendar
*)cal
)->setMinimalDaysInFirstWeek((uint8_t)newValue
);
298 U_CAPI
const char* U_EXPORT2
299 ucal_getAvailable(int32_t index
)
302 return uloc_getAvailable(index
);
305 U_CAPI
int32_t U_EXPORT2
306 ucal_countAvailable()
309 return uloc_countAvailable();
312 U_CAPI UDate U_EXPORT2
313 ucal_getMillis( const UCalendar
* cal
,
317 if(U_FAILURE(*status
)) return (UDate
) 0;
319 return ((Calendar
*)cal
)->getTime(*status
);
322 U_CAPI
void U_EXPORT2
323 ucal_setMillis( UCalendar
* cal
,
327 if(U_FAILURE(*status
)) return;
329 ((Calendar
*)cal
)->setTime(dateTime
, *status
);
332 // TBD: why does this take an UErrorCode?
333 U_CAPI
void U_EXPORT2
334 ucal_setDate( UCalendar
* cal
,
341 if(U_FAILURE(*status
)) return;
343 ((Calendar
*)cal
)->set(year
, month
, date
);
346 // TBD: why does this take an UErrorCode?
347 U_CAPI
void U_EXPORT2
348 ucal_setDateTime( UCalendar
* cal
,
357 if(U_FAILURE(*status
)) return;
359 ((Calendar
*)cal
)->set(year
, month
, date
, hour
, minute
, second
);
362 U_CAPI UBool U_EXPORT2
363 ucal_equivalentTo( const UCalendar
* cal1
,
364 const UCalendar
* cal2
)
367 return ((Calendar
*)cal1
)->isEquivalentTo(*((Calendar
*)cal2
));
370 U_CAPI
void U_EXPORT2
371 ucal_add( UCalendar
* cal
,
372 UCalendarDateFields field
,
377 if(U_FAILURE(*status
)) return;
379 ((Calendar
*)cal
)->add(field
, amount
, *status
);
382 U_CAPI
void U_EXPORT2
383 ucal_roll( UCalendar
* cal
,
384 UCalendarDateFields field
,
389 if(U_FAILURE(*status
)) return;
391 ((Calendar
*)cal
)->roll(field
, amount
, *status
);
394 U_CAPI
int32_t U_EXPORT2
395 ucal_get( const UCalendar
* cal
,
396 UCalendarDateFields field
,
400 if(U_FAILURE(*status
)) return -1;
402 return ((Calendar
*)cal
)->get(field
, *status
);
405 U_CAPI
void U_EXPORT2
406 ucal_set( UCalendar
* cal
,
407 UCalendarDateFields field
,
411 ((Calendar
*)cal
)->set(field
, value
);
414 U_CAPI UBool U_EXPORT2
415 ucal_isSet( const UCalendar
* cal
,
416 UCalendarDateFields field
)
419 return ((Calendar
*)cal
)->isSet(field
);
422 U_CAPI
void U_EXPORT2
423 ucal_clearField( UCalendar
* cal
,
424 UCalendarDateFields field
)
427 ((Calendar
*)cal
)->clear(field
);
430 U_CAPI
void U_EXPORT2
431 ucal_clear(UCalendar
* calendar
)
434 ((Calendar
*)calendar
)->clear();
437 U_CAPI
int32_t U_EXPORT2
438 ucal_getLimit( const UCalendar
* cal
,
439 UCalendarDateFields field
,
440 UCalendarLimitType type
,
444 if(status
==0 || U_FAILURE(*status
)) {
450 return ((Calendar
*)cal
)->getMinimum(field
);
453 return ((Calendar
*)cal
)->getMaximum(field
);
455 case UCAL_GREATEST_MINIMUM
:
456 return ((Calendar
*)cal
)->getGreatestMinimum(field
);
458 case UCAL_LEAST_MAXIMUM
:
459 return ((Calendar
*)cal
)->getLeastMaximum(field
);
461 case UCAL_ACTUAL_MINIMUM
:
462 return ((Calendar
*)cal
)->getActualMinimum(field
,
465 case UCAL_ACTUAL_MAXIMUM
:
466 return ((Calendar
*)cal
)->getActualMaximum(field
,
475 U_CAPI
const char * U_EXPORT2
476 ucal_getLocaleByType(const UCalendar
*cal
, ULocDataLocaleType type
, UErrorCode
* status
)
479 if (U_SUCCESS(*status
)) {
480 *status
= U_ILLEGAL_ARGUMENT_ERROR
;
484 return ((Calendar
*)cal
)->getLocaleID(type
, *status
);
487 #endif /* #if !UCONFIG_NO_FORMATTING */