2 *******************************************************************************
3 * Copyright (C) 1996-2016, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 *******************************************************************************
8 #include "utypeinfo.h" // for 'typeid' to work
10 #include "unicode/utypes.h"
12 #if !UCONFIG_NO_FORMATTING
14 #include <stdlib.h> // Apple addition for uacal_getDayPeriod
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 // Apple-specific function uacal_getDayPeriod and helper functions/data
811 } DayPeriodNameToValue
;
813 static const DayPeriodNameToValue dpNameToValue
[] = {
814 { "afternoon1", UADAYPERIOD_AFTERNOON1
},
815 { "afternoon2", UADAYPERIOD_AFTERNOON2
},
816 { "evening1", UADAYPERIOD_EVENING1
},
817 { "evening2", UADAYPERIOD_EVENING2
},
818 { "midnight", UADAYPERIOD_MIDNIGHT
},
819 { "morning1", UADAYPERIOD_MORNING1
},
820 { "morning2", UADAYPERIOD_MORNING2
},
821 { "night1", UADAYPERIOD_NIGHT1
},
822 { "night2", UADAYPERIOD_NIGHT2
},
823 { "noon", UADAYPERIOD_NOON
},
826 static UADayPeriod
dayPeriodFromName(const char* name
) {
827 const DayPeriodNameToValue
* dpNameToValuePtr
= dpNameToValue
;
828 const DayPeriodNameToValue
* dpNameToValueLim
= dpNameToValue
+ UPRV_LENGTHOF(dpNameToValue
);
829 // simple linear search, dpNameToValue is small enough
830 for (; dpNameToValuePtr
< dpNameToValueLim
; dpNameToValuePtr
++) {
831 if (uprv_strcmp(name
, dpNameToValuePtr
->name
) == 0) {
832 return dpNameToValuePtr
->value
;
835 return UADAYPERIOD_UNKNOWN
;
844 int CompareDayPeriodEntries(const void* entry1Ptr
, const void* entry2Ptr
) {
845 const DayPeriodEntry
* dpEntry1Ptr
= (const DayPeriodEntry
*)entry1Ptr
;
846 const DayPeriodEntry
* dpEntry2Ptr
= (const DayPeriodEntry
*)entry2Ptr
;
847 if (dpEntry1Ptr
->startHour
< dpEntry2Ptr
->startHour
) return -1;
848 if (dpEntry1Ptr
->startHour
> dpEntry2Ptr
->startHour
) return 1;
849 // here hours are equal
850 if (dpEntry1Ptr
->startMin
< dpEntry2Ptr
->startMin
) return -1;
851 if (dpEntry1Ptr
->startMin
> dpEntry2Ptr
->startMin
) return 1;
855 enum { kSetNameMaxLen
= 8, kBoundaryTimeMaxLen
= 6, kDayPeriodEntriesMax
= 12 };
857 U_CAPI UADayPeriod U_EXPORT2
858 uacal_getDayPeriod( const char* locale
,
862 UErrorCode
* status
) {
863 UADayPeriod dayPeriod
= UADAYPERIOD_UNKNOWN
;
864 DayPeriodEntry dpEntries
[kDayPeriodEntriesMax
];
865 int32_t dpEntriesCount
= 0;
867 if (U_FAILURE(*status
)) {
870 if (hour
< 0 || hour
> 23 || minute
< 0 || minute
> 59) {
871 *status
= U_ILLEGAL_ARGUMENT_ERROR
;
874 // get dayPeriods bundle
875 LocalUResourceBundlePointer
rb(ures_openDirect(NULL
, "dayPeriods", status
));
876 if (U_FAILURE(*status
)) {
879 // get locales/locales_selection subbundle
880 LocalUResourceBundlePointer
rbSub(ures_getByKey(rb
.getAlias(), formatStyle
? "locales": "locales_selection", NULL
, status
));
881 if (U_FAILURE(*status
)) {
884 // get bundle for language (maps to setName)
885 char lang
[ULOC_LANG_CAPACITY
] = {0};
886 if (locale
!= NULL
) {
887 UErrorCode tempStatus
= U_ZERO_ERROR
;
888 uloc_getLanguage(locale
, lang
, ULOC_LANG_CAPACITY
, &tempStatus
);
889 if (U_FAILURE(*status
) || *status
== U_STRING_NOT_TERMINATED_WARNING
) {
894 uprv_strcpy(lang
, "en"); // should be "root" but the data for root was missing
896 LocalUResourceBundlePointer
rbLang(ures_getByKey(rbSub
.getAlias(), lang
, NULL
, status
));
897 if (U_FAILURE(*status
)) {
898 // should only happen if lang was not [root] en
899 // fallback should be "root" but the data for root was missing, use "en"
900 *status
= U_ZERO_ERROR
;
901 rbLang
.adoptInstead(ures_getByKey(rbSub
.getAlias(), "en", rbLang
.orphan(), status
));
903 if (U_FAILURE(*status
)) {
906 // get setName from language bundle
907 char setName
[kSetNameMaxLen
] = {0};
908 int32_t setNameLen
= kSetNameMaxLen
;
909 ures_getUTF8String(rbLang
.getAlias(), setName
, &setNameLen
, TRUE
, status
);
910 if (U_FAILURE(*status
)) {
913 // get rules subbundle
914 rbSub
.adoptInstead(ures_getByKey(rb
.getAlias(), "rules", rbSub
.orphan(), status
));
915 if (U_FAILURE(*status
)) {
918 // get ruleset from rules subbundle
919 rb
.adoptInstead(ures_getByKey(rbSub
.getAlias(), setName
, rb
.orphan(), status
));
920 if (U_FAILURE(*status
)) {
923 // OK, we should finally have a ruleset (works to here).
924 // Iterate over it to collect entries
925 LocalUResourceBundlePointer rbBound
;
926 while (ures_hasNext(rb
.getAlias())) {
927 rbSub
.adoptInstead(ures_getNextResource(rb
.getAlias(), rbSub
.orphan(), status
));
928 if (U_FAILURE(*status
)) {
931 // rbSub now has the bundle for a particular dayPeriod such as morning1, afternoon2, noon
932 UADayPeriod dpForBundle
= dayPeriodFromName(ures_getKey(rbSub
.getAlias()));
933 int32_t dpLimit
= 24;
934 while (ures_hasNext(rbSub
.getAlias())) {
935 rbBound
.adoptInstead(ures_getNextResource(rbSub
.getAlias(), rbBound
.orphan(), status
));
936 if (U_FAILURE(*status
)) {
939 // rbBound now has the bundle for a particular time period boundary such as at, from, before.
940 // This is either of type URES_STRING (size=1) or of type URES_ARRAY (size > 1)
941 const char *boundaryType
= ures_getKey(rbBound
.getAlias());
942 char boundaryTimeStr
[kBoundaryTimeMaxLen
];
943 int32_t boundaryTimeStrLen
= kBoundaryTimeMaxLen
;
944 ures_getUTF8String(rbBound
.getAlias(), boundaryTimeStr
, &boundaryTimeStrLen
, TRUE
, status
);
945 if (U_FAILURE(*status
)) {
948 int32_t startHour
= atoi(boundaryTimeStr
); // can depend on POSIX locale (fortunately no decimal sep here)
949 if (uprv_strcmp(boundaryType
, "before") == 0) {
953 int32_t startMinute
= 0;
954 if (uprv_strcmp(boundaryType
, "from") == 0) {
956 if (startHour
> dpLimit
&& dpEntriesCount
< kDayPeriodEntriesMax
) {
957 dpEntries
[dpEntriesCount
].startHour
= 0;
958 dpEntries
[dpEntriesCount
].startMin
= startMinute
;
959 dpEntries
[dpEntriesCount
].value
= dpForBundle
;
963 if (dpEntriesCount
< kDayPeriodEntriesMax
) {
964 dpEntries
[dpEntriesCount
].startHour
= startHour
;
965 dpEntries
[dpEntriesCount
].startMin
= startMinute
;
966 dpEntries
[dpEntriesCount
].value
= dpForBundle
;
971 if (dpEntriesCount
< kDayPeriodEntriesMax
) {
972 dpEntries
[dpEntriesCount
].startHour
= 24;
973 dpEntries
[dpEntriesCount
].startMin
= 0;
974 dpEntries
[dpEntriesCount
].value
= UADAYPERIOD_UNKNOWN
;
977 // We have collected all of the rule data, now sort by time
978 qsort(dpEntries
, dpEntriesCount
, sizeof(DayPeriodEntry
), CompareDayPeriodEntries
);
980 // now fix start minute for the non-"at" entries
982 for (dpIndex
= 0; dpIndex
< dpEntriesCount
; dpIndex
++) {
983 if (dpIndex
== 0 || (dpEntries
[dpIndex
-1].value
!= UADAYPERIOD_MIDNIGHT
&& dpEntries
[dpIndex
-1].value
!= UADAYPERIOD_NOON
)) {
984 dpEntries
[dpIndex
].startMin
= 0;
988 // OK, all of the above is what we would do in an "open" function if we were using an
989 // open/use/close model for this; the object would just have the sorted array above.
991 // Now we use the sorted array to find the dayPeriod matching the supplied time.
992 // Only a few entries, linear search OK
993 DayPeriodEntry entryToMatch
= { hour
, minute
, UADAYPERIOD_UNKNOWN
};
995 while (dpIndex
< dpEntriesCount
- 1 && CompareDayPeriodEntries(&entryToMatch
, &dpEntries
[dpIndex
+ 1]) >= 0) {
998 if (CompareDayPeriodEntries(&entryToMatch
, &dpEntries
[dpIndex
]) >= 0) {
999 dayPeriod
= dpEntries
[dpIndex
].value
;
1007 #endif /* #if !UCONFIG_NO_FORMATTING */