1 // © 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 <stdlib.h> // Apple addition for uacal_getDayPeriod
18 #include "unicode/ucal.h"
19 #include "unicode/uloc.h"
20 #include "unicode/calendar.h"
21 #include "unicode/timezone.h"
22 #include "unicode/gregocal.h"
23 #include "unicode/simpletz.h"
24 #include "unicode/ustring.h"
25 #include "unicode/strenum.h"
26 #include "unicode/localpointer.h"
37 _createTimeZone(const UChar
* zoneID
, int32_t len
, UErrorCode
* ec
) {
38 TimeZone
* zone
= NULL
;
39 if (ec
!=NULL
&& U_SUCCESS(*ec
)) {
40 // Note that if zoneID is invalid, we get back GMT. This odd
41 // behavior is by design and goes back to the JDK. The only
42 // failure we will see is a memory allocation failure.
43 int32_t l
= (len
<0 ? u_strlen(zoneID
) : len
);
44 UnicodeString zoneStrID
;
45 zoneStrID
.setTo((UBool
)(len
< 0), zoneID
, l
); /* temporary read-only alias */
46 zone
= TimeZone::createTimeZone(zoneStrID
);
48 *ec
= U_MEMORY_ALLOCATION_ERROR
;
54 U_CAPI UEnumeration
* U_EXPORT2
55 ucal_openTimeZoneIDEnumeration(USystemTimeZoneType zoneType
, const char* region
,
56 const int32_t* rawOffset
, UErrorCode
* ec
) {
57 return uenum_openFromStringEnumeration(TimeZone::createTimeZoneIDEnumeration(
58 zoneType
, region
, rawOffset
, *ec
), ec
);
61 U_CAPI UEnumeration
* U_EXPORT2
62 ucal_openTimeZones(UErrorCode
* ec
) {
63 return uenum_openFromStringEnumeration(TimeZone::createEnumeration(), ec
);
66 U_CAPI UEnumeration
* U_EXPORT2
67 ucal_openCountryTimeZones(const char* country
, UErrorCode
* ec
) {
68 return uenum_openFromStringEnumeration(TimeZone::createEnumeration(country
), ec
);
71 U_CAPI
int32_t U_EXPORT2
72 ucal_getDefaultTimeZone(UChar
* result
, int32_t resultCapacity
, UErrorCode
* ec
) {
74 if (ec
!=NULL
&& U_SUCCESS(*ec
)) {
75 TimeZone
* zone
= TimeZone::createDefault();
77 *ec
= U_MEMORY_ALLOCATION_ERROR
;
82 len
= id
.extract(result
, resultCapacity
, *ec
);
89 ucal_setDefaultTimeZone(const UChar
* zoneID
, UErrorCode
* ec
) {
90 TimeZone
* zone
= _createTimeZone(zoneID
, -1, ec
);
92 TimeZone::adoptDefault(zone
);
96 U_CAPI
int32_t U_EXPORT2
97 ucal_getDSTSavings(const UChar
* zoneID
, UErrorCode
* ec
) {
99 TimeZone
* zone
= _createTimeZone(zoneID
, -1, ec
);
100 if (U_SUCCESS(*ec
)) {
101 SimpleTimeZone
* stz
= dynamic_cast<SimpleTimeZone
*>(zone
);
103 result
= stz
->getDSTSavings();
105 // Since there is no getDSTSavings on TimeZone, we use a
106 // heuristic: Starting with the current time, march
107 // forwards for one year, looking for DST savings.
108 // Stepping by weeks is sufficient.
109 UDate d
= Calendar::getNow();
110 for (int32_t i
=0; i
<53; ++i
, d
+=U_MILLIS_PER_DAY
*7.0) {
112 zone
->getOffset(d
, FALSE
, raw
, dst
, *ec
);
113 if (U_FAILURE(*ec
)) {
115 } else if (dst
!= 0) {
126 U_CAPI UDate U_EXPORT2
130 return Calendar::getNow();
133 #define ULOC_LOCALE_IDENTIFIER_CAPACITY (ULOC_FULLNAME_CAPACITY + 1 + ULOC_KEYWORD_AND_VALUES_CAPACITY)
135 U_CAPI UCalendar
* U_EXPORT2
136 ucal_open( const UChar
* zoneID
,
139 UCalendarType caltype
,
143 if(U_FAILURE(*status
)) return 0;
145 TimeZone
* zone
= (zoneID
==NULL
) ? TimeZone::createDefault()
146 : _createTimeZone(zoneID
, len
, status
);
148 if (U_FAILURE(*status
)) {
152 if ( caltype
== UCAL_GREGORIAN
) {
153 char localeBuf
[ULOC_LOCALE_IDENTIFIER_CAPACITY
];
154 if ( locale
== NULL
) {
155 locale
= uloc_getDefault();
157 uprv_strncpy(localeBuf
, locale
, ULOC_LOCALE_IDENTIFIER_CAPACITY
);
158 uloc_setKeywordValue("calendar", "gregorian", localeBuf
, ULOC_LOCALE_IDENTIFIER_CAPACITY
, status
);
159 if (U_FAILURE(*status
)) {
162 return (UCalendar
*)Calendar::createInstance(zone
, Locale(localeBuf
), *status
);
164 return (UCalendar
*)Calendar::createInstance(zone
, Locale(locale
), *status
);
167 U_CAPI
void U_EXPORT2
168 ucal_close(UCalendar
*cal
)
171 delete (Calendar
*) cal
;
174 U_CAPI UCalendar
* U_EXPORT2
175 ucal_clone(const UCalendar
* cal
,
178 if(U_FAILURE(*status
)) return 0;
180 Calendar
* res
= ((Calendar
*)cal
)->clone();
183 *status
= U_MEMORY_ALLOCATION_ERROR
;
187 return (UCalendar
*) res
;
190 U_CAPI
void U_EXPORT2
191 ucal_setTimeZone( UCalendar
* cal
,
197 if(U_FAILURE(*status
))
200 TimeZone
* zone
= (zoneID
==NULL
) ? TimeZone::createDefault()
201 : _createTimeZone(zoneID
, len
, status
);
204 ((Calendar
*)cal
)->adoptTimeZone(zone
);
208 U_CAPI
int32_t U_EXPORT2
209 ucal_getTimeZoneID(const UCalendar
*cal
,
211 int32_t resultLength
,
214 if (U_FAILURE(*status
)) {
217 const TimeZone
& tz
= ((Calendar
*)cal
)->getTimeZone();
220 return id
.extract(result
, resultLength
, *status
);
223 U_CAPI
int32_t U_EXPORT2
224 ucal_getTimeZoneDisplayName(const UCalendar
* cal
,
225 UCalendarDisplayNameType type
,
228 int32_t resultLength
,
232 if(U_FAILURE(*status
)) return -1;
234 const TimeZone
& tz
= ((Calendar
*)cal
)->getTimeZone();
236 if(!(result
==NULL
&& resultLength
==0)) {
237 // NULL destination for pure preflighting: empty dummy string
238 // otherwise, alias the destination buffer
239 id
.setTo(result
, 0, resultLength
);
244 tz
.getDisplayName(FALSE
, TimeZone::LONG
, Locale(locale
), id
);
247 case UCAL_SHORT_STANDARD
:
248 tz
.getDisplayName(FALSE
, TimeZone::SHORT
, Locale(locale
), id
);
252 tz
.getDisplayName(TRUE
, TimeZone::LONG
, Locale(locale
), id
);
256 tz
.getDisplayName(TRUE
, TimeZone::SHORT
, Locale(locale
), id
);
260 return id
.extract(result
, resultLength
, *status
);
263 U_CAPI UBool U_EXPORT2
264 ucal_inDaylightTime( const UCalendar
* cal
,
268 if(U_FAILURE(*status
)) return (UBool
) -1;
269 return ((Calendar
*)cal
)->inDaylightTime(*status
);
272 U_CAPI
void U_EXPORT2
273 ucal_setGregorianChange(UCalendar
*cal
, UDate date
, UErrorCode
*pErrorCode
) {
274 if(U_FAILURE(*pErrorCode
)) {
277 Calendar
*cpp_cal
= (Calendar
*)cal
;
278 GregorianCalendar
*gregocal
= dynamic_cast<GregorianCalendar
*>(cpp_cal
);
279 // Not if(gregocal == NULL) {
280 // because we really want to work only with a GregorianCalendar, not with
281 // its subclasses like BuddhistCalendar.
282 if (cpp_cal
== NULL
) {
283 // We normally don't check "this" pointers for NULL, but this here avoids
284 // compiler-generated exception-throwing code in case cal == NULL.
285 *pErrorCode
= U_ILLEGAL_ARGUMENT_ERROR
;
288 if(typeid(*cpp_cal
) != typeid(GregorianCalendar
)) {
289 *pErrorCode
= U_UNSUPPORTED_ERROR
;
292 gregocal
->setGregorianChange(date
, *pErrorCode
);
295 U_CAPI UDate U_EXPORT2
296 ucal_getGregorianChange(const UCalendar
*cal
, UErrorCode
*pErrorCode
) {
297 if(U_FAILURE(*pErrorCode
)) {
300 const Calendar
*cpp_cal
= (const Calendar
*)cal
;
301 const GregorianCalendar
*gregocal
= dynamic_cast<const GregorianCalendar
*>(cpp_cal
);
302 // Not if(gregocal == NULL) {
303 // see comments in ucal_setGregorianChange().
304 if (cpp_cal
== NULL
) {
305 // We normally don't check "this" pointers for NULL, but this here avoids
306 // compiler-generated exception-throwing code in case cal == NULL.
307 *pErrorCode
= U_ILLEGAL_ARGUMENT_ERROR
;
310 if(typeid(*cpp_cal
) != typeid(GregorianCalendar
)) {
311 *pErrorCode
= U_UNSUPPORTED_ERROR
;
314 return gregocal
->getGregorianChange();
317 U_CAPI
int32_t U_EXPORT2
318 ucal_getAttribute( const UCalendar
* cal
,
319 UCalendarAttribute attr
)
324 return ((Calendar
*)cal
)->isLenient();
326 case UCAL_FIRST_DAY_OF_WEEK
:
327 return ((Calendar
*)cal
)->getFirstDayOfWeek();
329 case UCAL_MINIMAL_DAYS_IN_FIRST_WEEK
:
330 return ((Calendar
*)cal
)->getMinimalDaysInFirstWeek();
332 case UCAL_REPEATED_WALL_TIME
:
333 return ((Calendar
*)cal
)->getRepeatedWallTimeOption();
335 case UCAL_SKIPPED_WALL_TIME
:
336 return ((Calendar
*)cal
)->getSkippedWallTimeOption();
344 U_CAPI
void U_EXPORT2
345 ucal_setAttribute( UCalendar
* cal
,
346 UCalendarAttribute attr
,
352 ((Calendar
*)cal
)->setLenient((UBool
)newValue
);
355 case UCAL_FIRST_DAY_OF_WEEK
:
356 ((Calendar
*)cal
)->setFirstDayOfWeek((UCalendarDaysOfWeek
)newValue
);
359 case UCAL_MINIMAL_DAYS_IN_FIRST_WEEK
:
360 ((Calendar
*)cal
)->setMinimalDaysInFirstWeek((uint8_t)newValue
);
363 case UCAL_REPEATED_WALL_TIME
:
364 ((Calendar
*)cal
)->setRepeatedWallTimeOption((UCalendarWallTimeOption
)newValue
);
367 case UCAL_SKIPPED_WALL_TIME
:
368 ((Calendar
*)cal
)->setSkippedWallTimeOption((UCalendarWallTimeOption
)newValue
);
373 U_CAPI
const char* U_EXPORT2
374 ucal_getAvailable(int32_t index
)
377 return uloc_getAvailable(index
);
380 U_CAPI
int32_t U_EXPORT2
381 ucal_countAvailable()
384 return uloc_countAvailable();
387 U_CAPI UDate U_EXPORT2
388 ucal_getMillis( const UCalendar
* cal
,
392 if(U_FAILURE(*status
)) return (UDate
) 0;
394 return ((Calendar
*)cal
)->getTime(*status
);
397 U_CAPI
void U_EXPORT2
398 ucal_setMillis( UCalendar
* cal
,
402 if(U_FAILURE(*status
)) return;
404 ((Calendar
*)cal
)->setTime(dateTime
, *status
);
407 // TBD: why does this take an UErrorCode?
408 U_CAPI
void U_EXPORT2
409 ucal_setDate( UCalendar
* cal
,
416 if(U_FAILURE(*status
)) return;
418 ((Calendar
*)cal
)->set(year
, month
, date
);
421 // TBD: why does this take an UErrorCode?
422 U_CAPI
void U_EXPORT2
423 ucal_setDateTime( UCalendar
* cal
,
432 if(U_FAILURE(*status
)) return;
434 ((Calendar
*)cal
)->set(year
, month
, date
, hour
, minute
, second
);
437 U_CAPI UBool U_EXPORT2
438 ucal_equivalentTo( const UCalendar
* cal1
,
439 const UCalendar
* cal2
)
442 return ((Calendar
*)cal1
)->isEquivalentTo(*((Calendar
*)cal2
));
445 U_CAPI
void U_EXPORT2
446 ucal_add( UCalendar
* cal
,
447 UCalendarDateFields field
,
452 if(U_FAILURE(*status
)) return;
454 ((Calendar
*)cal
)->add(field
, amount
, *status
);
457 U_CAPI
void U_EXPORT2
458 ucal_roll( UCalendar
* cal
,
459 UCalendarDateFields field
,
464 if(U_FAILURE(*status
)) return;
466 ((Calendar
*)cal
)->roll(field
, amount
, *status
);
469 U_CAPI
int32_t U_EXPORT2
470 ucal_get( const UCalendar
* cal
,
471 UCalendarDateFields field
,
475 if(U_FAILURE(*status
)) return -1;
477 return ((Calendar
*)cal
)->get(field
, *status
);
480 U_CAPI
void U_EXPORT2
481 ucal_set( UCalendar
* cal
,
482 UCalendarDateFields field
,
486 ((Calendar
*)cal
)->set(field
, value
);
489 U_CAPI UBool U_EXPORT2
490 ucal_isSet( const UCalendar
* cal
,
491 UCalendarDateFields field
)
494 return ((Calendar
*)cal
)->isSet(field
);
497 U_CAPI
void U_EXPORT2
498 ucal_clearField( UCalendar
* cal
,
499 UCalendarDateFields field
)
502 ((Calendar
*)cal
)->clear(field
);
505 U_CAPI
void U_EXPORT2
506 ucal_clear(UCalendar
* calendar
)
509 ((Calendar
*)calendar
)->clear();
512 U_CAPI
int32_t U_EXPORT2
513 ucal_getLimit( const UCalendar
* cal
,
514 UCalendarDateFields field
,
515 UCalendarLimitType type
,
519 if(status
==0 || U_FAILURE(*status
)) {
525 return ((Calendar
*)cal
)->getMinimum(field
);
528 return ((Calendar
*)cal
)->getMaximum(field
);
530 case UCAL_GREATEST_MINIMUM
:
531 return ((Calendar
*)cal
)->getGreatestMinimum(field
);
533 case UCAL_LEAST_MAXIMUM
:
534 return ((Calendar
*)cal
)->getLeastMaximum(field
);
536 case UCAL_ACTUAL_MINIMUM
:
537 return ((Calendar
*)cal
)->getActualMinimum(field
,
540 case UCAL_ACTUAL_MAXIMUM
:
541 return ((Calendar
*)cal
)->getActualMaximum(field
,
550 U_CAPI
const char * U_EXPORT2
551 ucal_getLocaleByType(const UCalendar
*cal
, ULocDataLocaleType type
, UErrorCode
* status
)
554 if (U_SUCCESS(*status
)) {
555 *status
= U_ILLEGAL_ARGUMENT_ERROR
;
559 return ((Calendar
*)cal
)->getLocaleID(type
, *status
);
562 U_CAPI
const char * U_EXPORT2
563 ucal_getTZDataVersion(UErrorCode
* status
)
565 return TimeZone::getTZDataVersion(*status
);
568 U_CAPI
int32_t U_EXPORT2
569 ucal_getCanonicalTimeZoneID(const UChar
* id
, int32_t len
,
570 UChar
* result
, int32_t resultCapacity
, UBool
*isSystemID
, UErrorCode
* status
) {
571 if(status
== 0 || U_FAILURE(*status
)) {
577 if (id
== 0 || len
== 0 || result
== 0 || resultCapacity
<= 0) {
578 *status
= U_ILLEGAL_ARGUMENT_ERROR
;
582 UnicodeString canonical
;
583 UBool systemID
= FALSE
;
584 TimeZone::getCanonicalID(UnicodeString(id
, len
), canonical
, systemID
, *status
);
585 if (U_SUCCESS(*status
)) {
587 *isSystemID
= systemID
;
589 reslen
= canonical
.extract(result
, resultCapacity
, *status
);
594 U_CAPI
const char * U_EXPORT2
595 ucal_getType(const UCalendar
*cal
, UErrorCode
* status
)
597 if (U_FAILURE(*status
)) {
600 return ((Calendar
*)cal
)->getType();
603 U_CAPI UCalendarWeekdayType U_EXPORT2
604 ucal_getDayOfWeekType(const UCalendar
*cal
, UCalendarDaysOfWeek dayOfWeek
, UErrorCode
* status
)
606 if (U_FAILURE(*status
)) {
609 return ((Calendar
*)cal
)->getDayOfWeekType(dayOfWeek
, *status
);
612 U_CAPI
int32_t U_EXPORT2
613 ucal_getWeekendTransition(const UCalendar
*cal
, UCalendarDaysOfWeek dayOfWeek
, UErrorCode
*status
)
615 if (U_FAILURE(*status
)) {
618 return ((Calendar
*)cal
)->getWeekendTransition(dayOfWeek
, *status
);
621 U_CAPI UBool U_EXPORT2
622 ucal_isWeekend(const UCalendar
*cal
, UDate date
, UErrorCode
*status
)
624 if (U_FAILURE(*status
)) {
627 return ((Calendar
*)cal
)->isWeekend(date
, *status
);
630 U_CAPI
int32_t U_EXPORT2
631 ucal_getFieldDifference(UCalendar
* cal
, UDate target
,
632 UCalendarDateFields field
,
635 if (U_FAILURE(*status
)) {
638 return ((Calendar
*)cal
)->fieldDifference(target
, field
, *status
);
642 static const UEnumeration defaultKeywordValues
= {
645 ulist_close_keyword_values_iterator
,
646 ulist_count_keyword_values
,
648 ulist_next_keyword_value
,
649 ulist_reset_keyword_values_iterator
652 static const char * const CAL_TYPES
[] = {
665 "ethiopic-amete-alem",
674 U_CAPI UEnumeration
* U_EXPORT2
675 ucal_getKeywordValuesForLocale(const char * /* key */, const char* locale
, UBool commonlyUsed
, UErrorCode
*status
) {
677 char prefRegion
[ULOC_COUNTRY_CAPACITY
];
678 (void)ulocimp_getRegionForSupplementalData(locale
, TRUE
, prefRegion
, sizeof(prefRegion
), status
);
680 // Read preferred calendar values from supplementalData calendarPreference
681 UResourceBundle
*rb
= ures_openDirect(NULL
, "supplementalData", status
);
682 ures_getByKey(rb
, "calendarPreferenceData", rb
, status
);
683 UResourceBundle
*order
= ures_getByKey(rb
, prefRegion
, NULL
, status
);
684 if (*status
== U_MISSING_RESOURCE_ERROR
&& rb
!= NULL
) {
685 *status
= U_ZERO_ERROR
;
686 order
= ures_getByKey(rb
, "001", NULL
, status
);
689 // Create a list of calendar type strings
690 UList
*values
= NULL
;
691 if (U_SUCCESS(*status
)) {
692 values
= ulist_createEmptyList(status
);
693 if (U_SUCCESS(*status
)) {
694 for (int i
= 0; i
< ures_getSize(order
); i
++) {
696 const UChar
*type
= ures_getStringByIndex(order
, i
, &len
, status
);
697 char *caltype
= (char*)uprv_malloc(len
+ 1);
698 if (caltype
== NULL
) {
699 *status
= U_MEMORY_ALLOCATION_ERROR
;
702 u_UCharsToChars(type
, caltype
, len
);
703 *(caltype
+ len
) = 0;
705 ulist_addItemEndList(values
, caltype
, TRUE
, status
);
706 if (U_FAILURE(*status
)) {
711 if (U_SUCCESS(*status
) && !commonlyUsed
) {
712 // If not commonlyUsed, add other available values
713 for (int32_t i
= 0; CAL_TYPES
[i
] != NULL
; i
++) {
714 if (!ulist_containsString(values
, CAL_TYPES
[i
], (int32_t)uprv_strlen(CAL_TYPES
[i
]))) {
715 ulist_addItemEndList(values
, CAL_TYPES
[i
], FALSE
, status
);
716 if (U_FAILURE(*status
)) {
722 if (U_FAILURE(*status
)) {
723 ulist_deleteList(values
);
732 if (U_FAILURE(*status
) || values
== NULL
) {
736 // Create string enumeration
737 UEnumeration
*en
= (UEnumeration
*)uprv_malloc(sizeof(UEnumeration
));
739 *status
= U_MEMORY_ALLOCATION_ERROR
;
740 ulist_deleteList(values
);
743 ulist_resetList(values
);
744 memcpy(en
, &defaultKeywordValues
, sizeof(UEnumeration
));
745 en
->context
= values
;
749 U_CAPI UBool U_EXPORT2
750 ucal_getTimeZoneTransitionDate(const UCalendar
* cal
, UTimeZoneTransitionType type
,
751 UDate
* transition
, UErrorCode
* status
)
753 if (U_FAILURE(*status
)) {
756 UDate base
= ((Calendar
*)cal
)->getTime(*status
);
757 const TimeZone
& tz
= ((Calendar
*)cal
)->getTimeZone();
758 const BasicTimeZone
* btz
= dynamic_cast<const BasicTimeZone
*>(&tz
);
759 if (btz
!= NULL
&& U_SUCCESS(*status
)) {
760 TimeZoneTransition tzt
;
761 UBool inclusive
= (type
== UCAL_TZ_TRANSITION_NEXT_INCLUSIVE
|| type
== UCAL_TZ_TRANSITION_PREVIOUS_INCLUSIVE
);
762 UBool result
= (type
== UCAL_TZ_TRANSITION_NEXT
|| type
== UCAL_TZ_TRANSITION_NEXT_INCLUSIVE
)?
763 btz
->getNextTransition(base
, inclusive
, tzt
):
764 btz
->getPreviousTransition(base
, inclusive
, tzt
);
766 *transition
= tzt
.getTime();
773 U_CAPI
int32_t U_EXPORT2
774 ucal_getWindowsTimeZoneID(const UChar
* id
, int32_t len
, UChar
* winid
, int32_t winidCapacity
, UErrorCode
* status
) {
775 if (U_FAILURE(*status
)) {
779 int32_t resultLen
= 0;
780 UnicodeString resultWinID
;
782 TimeZone::getWindowsID(UnicodeString(id
, len
), resultWinID
, *status
);
783 if (U_SUCCESS(*status
) && resultWinID
.length() > 0) {
784 resultLen
= resultWinID
.length();
785 resultWinID
.extract(winid
, winidCapacity
, *status
);
791 U_CAPI
int32_t U_EXPORT2
792 ucal_getTimeZoneIDForWindowsID(const UChar
* winid
, int32_t len
, const char* region
, UChar
* id
, int32_t idCapacity
, UErrorCode
* status
) {
793 if (U_FAILURE(*status
)) {
797 int32_t resultLen
= 0;
798 UnicodeString resultID
;
800 TimeZone::getIDForWindowsID(UnicodeString(winid
, len
), region
, resultID
, *status
);
801 if (U_SUCCESS(*status
) && resultID
.length() > 0) {
802 resultLen
= resultID
.length();
803 resultID
.extract(id
, idCapacity
, *status
);
809 // Apple-specific function uacal_getDayPeriod and helper functions/data
813 } DayPeriodNameToValue
;
815 static const DayPeriodNameToValue dpNameToValue
[] = {
816 { "afternoon1", UADAYPERIOD_AFTERNOON1
},
817 { "afternoon2", UADAYPERIOD_AFTERNOON2
},
818 { "evening1", UADAYPERIOD_EVENING1
},
819 { "evening2", UADAYPERIOD_EVENING2
},
820 { "midnight", UADAYPERIOD_MIDNIGHT
},
821 { "morning1", UADAYPERIOD_MORNING1
},
822 { "morning2", UADAYPERIOD_MORNING2
},
823 { "night1", UADAYPERIOD_NIGHT1
},
824 { "night2", UADAYPERIOD_NIGHT2
},
825 { "noon", UADAYPERIOD_NOON
},
828 static UADayPeriod
dayPeriodFromName(const char* name
) {
829 const DayPeriodNameToValue
* dpNameToValuePtr
= dpNameToValue
;
830 const DayPeriodNameToValue
* dpNameToValueLim
= dpNameToValue
+ UPRV_LENGTHOF(dpNameToValue
);
831 // simple linear search, dpNameToValue is small enough
832 for (; dpNameToValuePtr
< dpNameToValueLim
; dpNameToValuePtr
++) {
833 if (uprv_strcmp(name
, dpNameToValuePtr
->name
) == 0) {
834 return dpNameToValuePtr
->value
;
837 return UADAYPERIOD_UNKNOWN
;
846 int CompareDayPeriodEntries(const void* entry1Ptr
, const void* entry2Ptr
) {
847 const DayPeriodEntry
* dpEntry1Ptr
= (const DayPeriodEntry
*)entry1Ptr
;
848 const DayPeriodEntry
* dpEntry2Ptr
= (const DayPeriodEntry
*)entry2Ptr
;
849 if (dpEntry1Ptr
->startHour
< dpEntry2Ptr
->startHour
) return -1;
850 if (dpEntry1Ptr
->startHour
> dpEntry2Ptr
->startHour
) return 1;
851 // here hours are equal
852 if (dpEntry1Ptr
->startMin
< dpEntry2Ptr
->startMin
) return -1;
853 if (dpEntry1Ptr
->startMin
> dpEntry2Ptr
->startMin
) return 1;
857 enum { kSetNameMaxLen
= 8, kBoundaryTimeMaxLen
= 6, kDayPeriodEntriesMax
= 12 };
859 U_CAPI UADayPeriod U_EXPORT2
860 uacal_getDayPeriod( const char* locale
,
864 UErrorCode
* status
) {
865 UADayPeriod dayPeriod
= UADAYPERIOD_UNKNOWN
;
866 DayPeriodEntry dpEntries
[kDayPeriodEntriesMax
];
867 int32_t dpEntriesCount
= 0;
869 if (U_FAILURE(*status
)) {
872 if (hour
< 0 || hour
> 23 || minute
< 0 || minute
> 59) {
873 *status
= U_ILLEGAL_ARGUMENT_ERROR
;
876 // get dayPeriods bundle
877 LocalUResourceBundlePointer
rb(ures_openDirect(NULL
, "dayPeriods", status
));
878 if (U_FAILURE(*status
)) {
881 // get locales/locales_selection subbundle
882 LocalUResourceBundlePointer
rbSub(ures_getByKey(rb
.getAlias(), formatStyle
? "locales": "locales_selection", NULL
, status
));
883 if (U_FAILURE(*status
)) {
886 // get bundle for language (maps to setName)
887 char lang
[ULOC_LANG_CAPACITY
] = {0};
888 if (locale
!= NULL
) {
889 UErrorCode tempStatus
= U_ZERO_ERROR
;
890 uloc_getLanguage(locale
, lang
, ULOC_LANG_CAPACITY
, &tempStatus
);
891 if (U_FAILURE(*status
) || *status
== U_STRING_NOT_TERMINATED_WARNING
) {
896 uprv_strcpy(lang
, "en"); // should be "root" but the data for root was missing
898 LocalUResourceBundlePointer
rbLang(ures_getByKey(rbSub
.getAlias(), lang
, NULL
, status
));
899 if (U_FAILURE(*status
)) {
900 // should only happen if lang was not [root] en
901 // fallback should be "root" but the data for root was missing, use "en"
902 *status
= U_ZERO_ERROR
;
903 rbLang
.adoptInstead(ures_getByKey(rbSub
.getAlias(), "en", rbLang
.orphan(), status
));
905 if (U_FAILURE(*status
)) {
908 // get setName from language bundle
909 char setName
[kSetNameMaxLen
] = {0};
910 int32_t setNameLen
= kSetNameMaxLen
;
911 ures_getUTF8String(rbLang
.getAlias(), setName
, &setNameLen
, TRUE
, status
);
912 if (U_FAILURE(*status
)) {
915 // get rules subbundle
916 rbSub
.adoptInstead(ures_getByKey(rb
.getAlias(), "rules", rbSub
.orphan(), status
));
917 if (U_FAILURE(*status
)) {
920 // get ruleset from rules subbundle
921 rb
.adoptInstead(ures_getByKey(rbSub
.getAlias(), setName
, rb
.orphan(), status
));
922 if (U_FAILURE(*status
)) {
925 // OK, we should finally have a ruleset (works to here).
926 // Iterate over it to collect entries
927 LocalUResourceBundlePointer rbBound
;
928 while (ures_hasNext(rb
.getAlias())) {
929 rbSub
.adoptInstead(ures_getNextResource(rb
.getAlias(), rbSub
.orphan(), status
));
930 if (U_FAILURE(*status
)) {
933 // rbSub now has the bundle for a particular dayPeriod such as morning1, afternoon2, noon
934 UADayPeriod dpForBundle
= dayPeriodFromName(ures_getKey(rbSub
.getAlias()));
935 int32_t dpLimit
= 24;
936 while (ures_hasNext(rbSub
.getAlias())) {
937 rbBound
.adoptInstead(ures_getNextResource(rbSub
.getAlias(), rbBound
.orphan(), status
));
938 if (U_FAILURE(*status
)) {
941 // rbBound now has the bundle for a particular time period boundary such as at, from, before.
942 // This is either of type URES_STRING (size=1) or of type URES_ARRAY (size > 1)
943 const char *boundaryType
= ures_getKey(rbBound
.getAlias());
944 char boundaryTimeStr
[kBoundaryTimeMaxLen
];
945 int32_t boundaryTimeStrLen
= kBoundaryTimeMaxLen
;
946 ures_getUTF8String(rbBound
.getAlias(), boundaryTimeStr
, &boundaryTimeStrLen
, TRUE
, status
);
947 if (U_FAILURE(*status
)) {
950 int32_t startHour
= atoi(boundaryTimeStr
); // can depend on POSIX locale (fortunately no decimal sep here)
951 if (uprv_strcmp(boundaryType
, "before") == 0) {
955 int32_t startMinute
= 0;
956 if (uprv_strcmp(boundaryType
, "from") == 0) {
958 if (startHour
> dpLimit
&& dpEntriesCount
< kDayPeriodEntriesMax
) {
959 dpEntries
[dpEntriesCount
].startHour
= 0;
960 dpEntries
[dpEntriesCount
].startMin
= startMinute
;
961 dpEntries
[dpEntriesCount
].value
= dpForBundle
;
965 if (dpEntriesCount
< kDayPeriodEntriesMax
) {
966 dpEntries
[dpEntriesCount
].startHour
= startHour
;
967 dpEntries
[dpEntriesCount
].startMin
= startMinute
;
968 dpEntries
[dpEntriesCount
].value
= dpForBundle
;
973 if (dpEntriesCount
< kDayPeriodEntriesMax
) {
974 dpEntries
[dpEntriesCount
].startHour
= 24;
975 dpEntries
[dpEntriesCount
].startMin
= 0;
976 dpEntries
[dpEntriesCount
].value
= UADAYPERIOD_UNKNOWN
;
979 // We have collected all of the rule data, now sort by time
980 qsort(dpEntries
, dpEntriesCount
, sizeof(DayPeriodEntry
), CompareDayPeriodEntries
);
982 // now fix start minute for the non-"at" entries
984 for (dpIndex
= 0; dpIndex
< dpEntriesCount
; dpIndex
++) {
985 if (dpIndex
== 0 || (dpEntries
[dpIndex
-1].value
!= UADAYPERIOD_MIDNIGHT
&& dpEntries
[dpIndex
-1].value
!= UADAYPERIOD_NOON
)) {
986 dpEntries
[dpIndex
].startMin
= 0;
990 // OK, all of the above is what we would do in an "open" function if we were using an
991 // open/use/close model for this; the object would just have the sorted array above.
993 // Now we use the sorted array to find the dayPeriod matching the supplied time.
994 // Only a few entries, linear search OK
995 DayPeriodEntry entryToMatch
= { hour
, minute
, UADAYPERIOD_UNKNOWN
};
997 while (dpIndex
< dpEntriesCount
- 1 && CompareDayPeriodEntries(&entryToMatch
, &dpEntries
[dpIndex
+ 1]) >= 0) {
1000 if (CompareDayPeriodEntries(&entryToMatch
, &dpEntries
[dpIndex
]) >= 0) {
1001 dayPeriod
= dpEntries
[dpIndex
].value
;
1009 #endif /* #if !UCONFIG_NO_FORMATTING */