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
))
202 zone
= TimeZone::createDefault();
204 UnicodeString zoneStrID
, id
;
205 zoneStrID
.setTo(len
< 0, zoneID
, len
); /* aliasing assignment, avoids copy */
206 ((Calendar
*)cal
)->getTimeZone().getID(id
);
207 if (id
== zoneStrID
) {
210 zone
= TimeZone::createTimeZone(zoneStrID
);
214 ((Calendar
*)cal
)->adoptTimeZone(zone
);
216 *status
= U_MEMORY_ALLOCATION_ERROR
;
220 U_CAPI
int32_t U_EXPORT2
221 ucal_getTimeZoneID(const UCalendar
*cal
,
223 int32_t resultLength
,
226 if (U_FAILURE(*status
)) {
229 const TimeZone
& tz
= ((Calendar
*)cal
)->getTimeZone();
232 return id
.extract(result
, resultLength
, *status
);
235 U_CAPI
int32_t U_EXPORT2
236 ucal_getTimeZoneDisplayName(const UCalendar
* cal
,
237 UCalendarDisplayNameType type
,
240 int32_t resultLength
,
244 if(U_FAILURE(*status
)) return -1;
246 const TimeZone
& tz
= ((Calendar
*)cal
)->getTimeZone();
248 if(!(result
==NULL
&& resultLength
==0)) {
249 // NULL destination for pure preflighting: empty dummy string
250 // otherwise, alias the destination buffer
251 id
.setTo(result
, 0, resultLength
);
256 tz
.getDisplayName(FALSE
, TimeZone::LONG
, Locale(locale
), id
);
259 case UCAL_SHORT_STANDARD
:
260 tz
.getDisplayName(FALSE
, TimeZone::SHORT
, Locale(locale
), id
);
264 tz
.getDisplayName(TRUE
, TimeZone::LONG
, Locale(locale
), id
);
268 tz
.getDisplayName(TRUE
, TimeZone::SHORT
, Locale(locale
), id
);
272 return id
.extract(result
, resultLength
, *status
);
275 U_CAPI UBool U_EXPORT2
276 ucal_inDaylightTime( const UCalendar
* cal
,
280 if(U_FAILURE(*status
)) return (UBool
) -1;
281 return ((Calendar
*)cal
)->inDaylightTime(*status
);
284 U_CAPI
void U_EXPORT2
285 ucal_setGregorianChange(UCalendar
*cal
, UDate date
, UErrorCode
*pErrorCode
) {
286 if(U_FAILURE(*pErrorCode
)) {
289 Calendar
*cpp_cal
= (Calendar
*)cal
;
290 GregorianCalendar
*gregocal
= dynamic_cast<GregorianCalendar
*>(cpp_cal
);
291 // Not if(gregocal == NULL) {
292 // because we really want to work only with a GregorianCalendar, not with
293 // its subclasses like BuddhistCalendar.
294 if (cpp_cal
== NULL
) {
295 // We normally don't check "this" pointers for NULL, but this here avoids
296 // compiler-generated exception-throwing code in case cal == NULL.
297 *pErrorCode
= U_ILLEGAL_ARGUMENT_ERROR
;
300 if(typeid(*cpp_cal
) != typeid(GregorianCalendar
)) {
301 *pErrorCode
= U_UNSUPPORTED_ERROR
;
304 gregocal
->setGregorianChange(date
, *pErrorCode
);
307 U_CAPI UDate U_EXPORT2
308 ucal_getGregorianChange(const UCalendar
*cal
, UErrorCode
*pErrorCode
) {
309 if(U_FAILURE(*pErrorCode
)) {
312 const Calendar
*cpp_cal
= (const Calendar
*)cal
;
313 const GregorianCalendar
*gregocal
= dynamic_cast<const GregorianCalendar
*>(cpp_cal
);
314 // Not if(gregocal == NULL) {
315 // see comments in ucal_setGregorianChange().
316 if (cpp_cal
== NULL
) {
317 // We normally don't check "this" pointers for NULL, but this here avoids
318 // compiler-generated exception-throwing code in case cal == NULL.
319 *pErrorCode
= U_ILLEGAL_ARGUMENT_ERROR
;
322 if(typeid(*cpp_cal
) != typeid(GregorianCalendar
)) {
323 *pErrorCode
= U_UNSUPPORTED_ERROR
;
326 return gregocal
->getGregorianChange();
329 U_CAPI
int32_t U_EXPORT2
330 ucal_getAttribute( const UCalendar
* cal
,
331 UCalendarAttribute attr
)
336 return ((Calendar
*)cal
)->isLenient();
338 case UCAL_FIRST_DAY_OF_WEEK
:
339 return ((Calendar
*)cal
)->getFirstDayOfWeek();
341 case UCAL_MINIMAL_DAYS_IN_FIRST_WEEK
:
342 return ((Calendar
*)cal
)->getMinimalDaysInFirstWeek();
344 case UCAL_REPEATED_WALL_TIME
:
345 return ((Calendar
*)cal
)->getRepeatedWallTimeOption();
347 case UCAL_SKIPPED_WALL_TIME
:
348 return ((Calendar
*)cal
)->getSkippedWallTimeOption();
356 U_CAPI
void U_EXPORT2
357 ucal_setAttribute( UCalendar
* cal
,
358 UCalendarAttribute attr
,
364 ((Calendar
*)cal
)->setLenient((UBool
)newValue
);
367 case UCAL_FIRST_DAY_OF_WEEK
:
368 ((Calendar
*)cal
)->setFirstDayOfWeek((UCalendarDaysOfWeek
)newValue
);
371 case UCAL_MINIMAL_DAYS_IN_FIRST_WEEK
:
372 ((Calendar
*)cal
)->setMinimalDaysInFirstWeek((uint8_t)newValue
);
375 case UCAL_REPEATED_WALL_TIME
:
376 ((Calendar
*)cal
)->setRepeatedWallTimeOption((UCalendarWallTimeOption
)newValue
);
379 case UCAL_SKIPPED_WALL_TIME
:
380 ((Calendar
*)cal
)->setSkippedWallTimeOption((UCalendarWallTimeOption
)newValue
);
385 U_CAPI
const char* U_EXPORT2
386 ucal_getAvailable(int32_t index
)
389 return uloc_getAvailable(index
);
392 U_CAPI
int32_t U_EXPORT2
393 ucal_countAvailable()
396 return uloc_countAvailable();
399 U_CAPI UDate U_EXPORT2
400 ucal_getMillis( const UCalendar
* cal
,
404 if(U_FAILURE(*status
)) return (UDate
) 0;
406 return ((Calendar
*)cal
)->getTime(*status
);
409 U_CAPI
void U_EXPORT2
410 ucal_setMillis( UCalendar
* cal
,
414 if(U_FAILURE(*status
)) return;
416 ((Calendar
*)cal
)->setTime(dateTime
, *status
);
419 // TBD: why does this take an UErrorCode?
420 U_CAPI
void U_EXPORT2
421 ucal_setDate( UCalendar
* cal
,
428 if(U_FAILURE(*status
)) return;
430 ((Calendar
*)cal
)->set(year
, month
, date
);
433 // TBD: why does this take an UErrorCode?
434 U_CAPI
void U_EXPORT2
435 ucal_setDateTime( UCalendar
* cal
,
444 if(U_FAILURE(*status
)) return;
446 ((Calendar
*)cal
)->set(year
, month
, date
, hour
, minute
, second
);
449 U_CAPI UBool U_EXPORT2
450 ucal_equivalentTo( const UCalendar
* cal1
,
451 const UCalendar
* cal2
)
454 return ((Calendar
*)cal1
)->isEquivalentTo(*((Calendar
*)cal2
));
457 U_CAPI
void U_EXPORT2
458 ucal_add( UCalendar
* cal
,
459 UCalendarDateFields field
,
464 if(U_FAILURE(*status
)) return;
466 ((Calendar
*)cal
)->add(field
, amount
, *status
);
469 U_CAPI
void U_EXPORT2
470 ucal_roll( UCalendar
* cal
,
471 UCalendarDateFields field
,
476 if(U_FAILURE(*status
)) return;
478 ((Calendar
*)cal
)->roll(field
, amount
, *status
);
481 U_CAPI
int32_t U_EXPORT2
482 ucal_get( const UCalendar
* cal
,
483 UCalendarDateFields field
,
487 if(U_FAILURE(*status
)) return -1;
489 return ((Calendar
*)cal
)->get(field
, *status
);
492 U_CAPI
void U_EXPORT2
493 ucal_set( UCalendar
* cal
,
494 UCalendarDateFields field
,
498 ((Calendar
*)cal
)->set(field
, value
);
501 U_CAPI UBool U_EXPORT2
502 ucal_isSet( const UCalendar
* cal
,
503 UCalendarDateFields field
)
506 return ((Calendar
*)cal
)->isSet(field
);
509 U_CAPI
void U_EXPORT2
510 ucal_clearField( UCalendar
* cal
,
511 UCalendarDateFields field
)
514 ((Calendar
*)cal
)->clear(field
);
517 U_CAPI
void U_EXPORT2
518 ucal_clear(UCalendar
* calendar
)
521 ((Calendar
*)calendar
)->clear();
524 U_CAPI
int32_t U_EXPORT2
525 ucal_getLimit( const UCalendar
* cal
,
526 UCalendarDateFields field
,
527 UCalendarLimitType type
,
531 if(status
==0 || U_FAILURE(*status
)) {
537 return ((Calendar
*)cal
)->getMinimum(field
);
540 return ((Calendar
*)cal
)->getMaximum(field
);
542 case UCAL_GREATEST_MINIMUM
:
543 return ((Calendar
*)cal
)->getGreatestMinimum(field
);
545 case UCAL_LEAST_MAXIMUM
:
546 return ((Calendar
*)cal
)->getLeastMaximum(field
);
548 case UCAL_ACTUAL_MINIMUM
:
549 return ((Calendar
*)cal
)->getActualMinimum(field
,
552 case UCAL_ACTUAL_MAXIMUM
:
553 return ((Calendar
*)cal
)->getActualMaximum(field
,
562 U_CAPI
const char * U_EXPORT2
563 ucal_getLocaleByType(const UCalendar
*cal
, ULocDataLocaleType type
, UErrorCode
* status
)
566 if (U_SUCCESS(*status
)) {
567 *status
= U_ILLEGAL_ARGUMENT_ERROR
;
571 return ((Calendar
*)cal
)->getLocaleID(type
, *status
);
574 U_CAPI
const char * U_EXPORT2
575 ucal_getTZDataVersion(UErrorCode
* status
)
577 return TimeZone::getTZDataVersion(*status
);
580 U_CAPI
int32_t U_EXPORT2
581 ucal_getCanonicalTimeZoneID(const UChar
* id
, int32_t len
,
582 UChar
* result
, int32_t resultCapacity
, UBool
*isSystemID
, UErrorCode
* status
) {
583 if(status
== 0 || U_FAILURE(*status
)) {
589 if (id
== 0 || len
== 0 || result
== 0 || resultCapacity
<= 0) {
590 *status
= U_ILLEGAL_ARGUMENT_ERROR
;
594 UnicodeString canonical
;
595 UBool systemID
= FALSE
;
596 TimeZone::getCanonicalID(UnicodeString(id
, len
), canonical
, systemID
, *status
);
597 if (U_SUCCESS(*status
)) {
599 *isSystemID
= systemID
;
601 reslen
= canonical
.extract(result
, resultCapacity
, *status
);
606 U_CAPI
const char * U_EXPORT2
607 ucal_getType(const UCalendar
*cal
, UErrorCode
* status
)
609 if (U_FAILURE(*status
)) {
612 return ((Calendar
*)cal
)->getType();
615 U_CAPI UCalendarWeekdayType U_EXPORT2
616 ucal_getDayOfWeekType(const UCalendar
*cal
, UCalendarDaysOfWeek dayOfWeek
, UErrorCode
* status
)
618 if (U_FAILURE(*status
)) {
621 return ((Calendar
*)cal
)->getDayOfWeekType(dayOfWeek
, *status
);
624 U_CAPI
int32_t U_EXPORT2
625 ucal_getWeekendTransition(const UCalendar
*cal
, UCalendarDaysOfWeek dayOfWeek
, UErrorCode
*status
)
627 if (U_FAILURE(*status
)) {
630 return ((Calendar
*)cal
)->getWeekendTransition(dayOfWeek
, *status
);
633 U_CAPI UBool U_EXPORT2
634 ucal_isWeekend(const UCalendar
*cal
, UDate date
, UErrorCode
*status
)
636 if (U_FAILURE(*status
)) {
639 return ((Calendar
*)cal
)->isWeekend(date
, *status
);
642 U_CAPI
int32_t U_EXPORT2
643 ucal_getFieldDifference(UCalendar
* cal
, UDate target
,
644 UCalendarDateFields field
,
647 if (U_FAILURE(*status
)) {
650 return ((Calendar
*)cal
)->fieldDifference(target
, field
, *status
);
654 static const UEnumeration defaultKeywordValues
= {
657 ulist_close_keyword_values_iterator
,
658 ulist_count_keyword_values
,
660 ulist_next_keyword_value
,
661 ulist_reset_keyword_values_iterator
664 static const char * const CAL_TYPES
[] = {
677 "ethiopic-amete-alem",
686 U_CAPI UEnumeration
* U_EXPORT2
687 ucal_getKeywordValuesForLocale(const char * /* key */, const char* locale
, UBool commonlyUsed
, UErrorCode
*status
) {
689 char prefRegion
[ULOC_COUNTRY_CAPACITY
];
690 (void)ulocimp_getRegionForSupplementalData(locale
, TRUE
, prefRegion
, sizeof(prefRegion
), status
);
692 // Read preferred calendar values from supplementalData calendarPreference
693 UResourceBundle
*rb
= ures_openDirect(NULL
, "supplementalData", status
);
694 ures_getByKey(rb
, "calendarPreferenceData", rb
, status
);
695 UResourceBundle
*order
= ures_getByKey(rb
, prefRegion
, NULL
, status
);
696 if (*status
== U_MISSING_RESOURCE_ERROR
&& rb
!= NULL
) {
697 *status
= U_ZERO_ERROR
;
698 order
= ures_getByKey(rb
, "001", NULL
, status
);
701 // Create a list of calendar type strings
702 UList
*values
= NULL
;
703 if (U_SUCCESS(*status
)) {
704 values
= ulist_createEmptyList(status
);
705 if (U_SUCCESS(*status
)) {
706 for (int i
= 0; i
< ures_getSize(order
); i
++) {
708 const UChar
*type
= ures_getStringByIndex(order
, i
, &len
, status
);
709 char *caltype
= (char*)uprv_malloc(len
+ 1);
710 if (caltype
== NULL
) {
711 *status
= U_MEMORY_ALLOCATION_ERROR
;
714 u_UCharsToChars(type
, caltype
, len
);
715 *(caltype
+ len
) = 0;
717 ulist_addItemEndList(values
, caltype
, TRUE
, status
);
718 if (U_FAILURE(*status
)) {
723 if (U_SUCCESS(*status
) && !commonlyUsed
) {
724 // If not commonlyUsed, add other available values
725 for (int32_t i
= 0; CAL_TYPES
[i
] != NULL
; i
++) {
726 if (!ulist_containsString(values
, CAL_TYPES
[i
], (int32_t)uprv_strlen(CAL_TYPES
[i
]))) {
727 ulist_addItemEndList(values
, CAL_TYPES
[i
], FALSE
, status
);
728 if (U_FAILURE(*status
)) {
734 if (U_FAILURE(*status
)) {
735 ulist_deleteList(values
);
744 if (U_FAILURE(*status
) || values
== NULL
) {
748 // Create string enumeration
749 UEnumeration
*en
= (UEnumeration
*)uprv_malloc(sizeof(UEnumeration
));
751 *status
= U_MEMORY_ALLOCATION_ERROR
;
752 ulist_deleteList(values
);
755 ulist_resetList(values
);
756 memcpy(en
, &defaultKeywordValues
, sizeof(UEnumeration
));
757 en
->context
= values
;
761 U_CAPI UBool U_EXPORT2
762 ucal_getTimeZoneTransitionDate(const UCalendar
* cal
, UTimeZoneTransitionType type
,
763 UDate
* transition
, UErrorCode
* status
)
765 if (U_FAILURE(*status
)) {
768 UDate base
= ((Calendar
*)cal
)->getTime(*status
);
769 const TimeZone
& tz
= ((Calendar
*)cal
)->getTimeZone();
770 const BasicTimeZone
* btz
= dynamic_cast<const BasicTimeZone
*>(&tz
);
771 if (btz
!= NULL
&& U_SUCCESS(*status
)) {
772 TimeZoneTransition tzt
;
773 UBool inclusive
= (type
== UCAL_TZ_TRANSITION_NEXT_INCLUSIVE
|| type
== UCAL_TZ_TRANSITION_PREVIOUS_INCLUSIVE
);
774 UBool result
= (type
== UCAL_TZ_TRANSITION_NEXT
|| type
== UCAL_TZ_TRANSITION_NEXT_INCLUSIVE
)?
775 btz
->getNextTransition(base
, inclusive
, tzt
):
776 btz
->getPreviousTransition(base
, inclusive
, tzt
);
778 *transition
= tzt
.getTime();
785 U_CAPI
int32_t U_EXPORT2
786 ucal_getWindowsTimeZoneID(const UChar
* id
, int32_t len
, UChar
* winid
, int32_t winidCapacity
, UErrorCode
* status
) {
787 if (U_FAILURE(*status
)) {
791 int32_t resultLen
= 0;
792 UnicodeString resultWinID
;
794 TimeZone::getWindowsID(UnicodeString(id
, len
), resultWinID
, *status
);
795 if (U_SUCCESS(*status
) && resultWinID
.length() > 0) {
796 resultLen
= resultWinID
.length();
797 resultWinID
.extract(winid
, winidCapacity
, *status
);
803 U_CAPI
int32_t U_EXPORT2
804 ucal_getTimeZoneIDForWindowsID(const UChar
* winid
, int32_t len
, const char* region
, UChar
* id
, int32_t idCapacity
, UErrorCode
* status
) {
805 if (U_FAILURE(*status
)) {
809 int32_t resultLen
= 0;
810 UnicodeString resultID
;
812 TimeZone::getIDForWindowsID(UnicodeString(winid
, len
), region
, resultID
, *status
);
813 if (U_SUCCESS(*status
) && resultID
.length() > 0) {
814 resultLen
= resultID
.length();
815 resultID
.extract(id
, idCapacity
, *status
);
821 // Apple-specific function uacal_getDayPeriod and helper functions/data
825 } DayPeriodNameToValue
;
827 static const DayPeriodNameToValue dpNameToValue
[] = {
828 { "afternoon1", UADAYPERIOD_AFTERNOON1
},
829 { "afternoon2", UADAYPERIOD_AFTERNOON2
},
830 { "evening1", UADAYPERIOD_EVENING1
},
831 { "evening2", UADAYPERIOD_EVENING2
},
832 { "midnight", UADAYPERIOD_MIDNIGHT
},
833 { "morning1", UADAYPERIOD_MORNING1
},
834 { "morning2", UADAYPERIOD_MORNING2
},
835 { "night1", UADAYPERIOD_NIGHT1
},
836 { "night2", UADAYPERIOD_NIGHT2
},
837 { "noon", UADAYPERIOD_NOON
},
840 static UADayPeriod
dayPeriodFromName(const char* name
) {
841 const DayPeriodNameToValue
* dpNameToValuePtr
= dpNameToValue
;
842 const DayPeriodNameToValue
* dpNameToValueLim
= dpNameToValue
+ UPRV_LENGTHOF(dpNameToValue
);
843 // simple linear search, dpNameToValue is small enough
844 for (; dpNameToValuePtr
< dpNameToValueLim
; dpNameToValuePtr
++) {
845 if (uprv_strcmp(name
, dpNameToValuePtr
->name
) == 0) {
846 return dpNameToValuePtr
->value
;
849 return UADAYPERIOD_UNKNOWN
;
858 int CompareDayPeriodEntries(const void* entry1Ptr
, const void* entry2Ptr
) {
859 const DayPeriodEntry
* dpEntry1Ptr
= (const DayPeriodEntry
*)entry1Ptr
;
860 const DayPeriodEntry
* dpEntry2Ptr
= (const DayPeriodEntry
*)entry2Ptr
;
861 if (dpEntry1Ptr
->startHour
< dpEntry2Ptr
->startHour
) return -1;
862 if (dpEntry1Ptr
->startHour
> dpEntry2Ptr
->startHour
) return 1;
863 // here hours are equal
864 if (dpEntry1Ptr
->startMin
< dpEntry2Ptr
->startMin
) return -1;
865 if (dpEntry1Ptr
->startMin
> dpEntry2Ptr
->startMin
) return 1;
869 enum { kSetNameMaxLen
= 8, kBoundaryTimeMaxLen
= 6, kDayPeriodEntriesMax
= 12 };
871 U_CAPI UADayPeriod U_EXPORT2
872 uacal_getDayPeriod( const char* locale
,
876 UErrorCode
* status
) {
877 UADayPeriod dayPeriod
= UADAYPERIOD_UNKNOWN
;
878 DayPeriodEntry dpEntries
[kDayPeriodEntriesMax
];
879 int32_t dpEntriesCount
= 0;
881 if (U_FAILURE(*status
)) {
884 if (hour
< 0 || hour
> 23 || minute
< 0 || minute
> 59) {
885 *status
= U_ILLEGAL_ARGUMENT_ERROR
;
888 // get dayPeriods bundle
889 LocalUResourceBundlePointer
rb(ures_openDirect(NULL
, "dayPeriods", status
));
890 if (U_FAILURE(*status
)) {
893 // get locales/locales_selection subbundle
894 LocalUResourceBundlePointer
rbSub(ures_getByKey(rb
.getAlias(), formatStyle
? "locales": "locales_selection", NULL
, status
));
895 if (U_FAILURE(*status
)) {
898 // get bundle for language (maps to setName)
899 char lang
[ULOC_LANG_CAPACITY
] = {0};
900 if (locale
!= NULL
) {
901 UErrorCode tempStatus
= U_ZERO_ERROR
;
902 uloc_getLanguage(locale
, lang
, ULOC_LANG_CAPACITY
, &tempStatus
);
903 if (U_FAILURE(*status
) || *status
== U_STRING_NOT_TERMINATED_WARNING
) {
908 uprv_strcpy(lang
, "en"); // should be "root" but the data for root was missing
910 LocalUResourceBundlePointer
rbLang(ures_getByKey(rbSub
.getAlias(), lang
, NULL
, status
));
911 if (U_FAILURE(*status
)) {
912 // should only happen if lang was not [root] en
913 // fallback should be "root" but the data for root was missing, use "en"
914 *status
= U_ZERO_ERROR
;
915 rbLang
.adoptInstead(ures_getByKey(rbSub
.getAlias(), "en", rbLang
.orphan(), status
));
917 if (U_FAILURE(*status
)) {
920 // get setName from language bundle
921 char setName
[kSetNameMaxLen
] = {0};
922 int32_t setNameLen
= kSetNameMaxLen
;
923 ures_getUTF8String(rbLang
.getAlias(), setName
, &setNameLen
, TRUE
, status
);
924 if (U_FAILURE(*status
)) {
927 // get rules subbundle
928 rbSub
.adoptInstead(ures_getByKey(rb
.getAlias(), "rules", rbSub
.orphan(), status
));
929 if (U_FAILURE(*status
)) {
932 // get ruleset from rules subbundle
933 rb
.adoptInstead(ures_getByKey(rbSub
.getAlias(), setName
, rb
.orphan(), status
));
934 if (U_FAILURE(*status
)) {
937 // OK, we should finally have a ruleset (works to here).
938 // Iterate over it to collect entries
939 LocalUResourceBundlePointer rbBound
;
940 while (ures_hasNext(rb
.getAlias())) {
941 rbSub
.adoptInstead(ures_getNextResource(rb
.getAlias(), rbSub
.orphan(), status
));
942 if (U_FAILURE(*status
)) {
945 // rbSub now has the bundle for a particular dayPeriod such as morning1, afternoon2, noon
946 UADayPeriod dpForBundle
= dayPeriodFromName(ures_getKey(rbSub
.getAlias()));
947 int32_t dpLimit
= 24;
948 while (ures_hasNext(rbSub
.getAlias())) {
949 rbBound
.adoptInstead(ures_getNextResource(rbSub
.getAlias(), rbBound
.orphan(), status
));
950 if (U_FAILURE(*status
)) {
953 // rbBound now has the bundle for a particular time period boundary such as at, from, before.
954 // This is either of type URES_STRING (size=1) or of type URES_ARRAY (size > 1)
955 const char *boundaryType
= ures_getKey(rbBound
.getAlias());
956 char boundaryTimeStr
[kBoundaryTimeMaxLen
];
957 int32_t boundaryTimeStrLen
= kBoundaryTimeMaxLen
;
958 ures_getUTF8String(rbBound
.getAlias(), boundaryTimeStr
, &boundaryTimeStrLen
, TRUE
, status
);
959 if (U_FAILURE(*status
)) {
962 int32_t startHour
= atoi(boundaryTimeStr
); // can depend on POSIX locale (fortunately no decimal sep here)
963 if (uprv_strcmp(boundaryType
, "before") == 0) {
967 int32_t startMinute
= 0;
968 if (uprv_strcmp(boundaryType
, "from") == 0) {
970 if (startHour
> dpLimit
&& dpEntriesCount
< kDayPeriodEntriesMax
) {
971 dpEntries
[dpEntriesCount
].startHour
= 0;
972 dpEntries
[dpEntriesCount
].startMin
= startMinute
;
973 dpEntries
[dpEntriesCount
].value
= dpForBundle
;
977 if (dpEntriesCount
< kDayPeriodEntriesMax
) {
978 dpEntries
[dpEntriesCount
].startHour
= startHour
;
979 dpEntries
[dpEntriesCount
].startMin
= startMinute
;
980 dpEntries
[dpEntriesCount
].value
= dpForBundle
;
985 if (dpEntriesCount
< kDayPeriodEntriesMax
) {
986 dpEntries
[dpEntriesCount
].startHour
= 24;
987 dpEntries
[dpEntriesCount
].startMin
= 0;
988 dpEntries
[dpEntriesCount
].value
= UADAYPERIOD_UNKNOWN
;
991 // We have collected all of the rule data, now sort by time
992 qsort(dpEntries
, dpEntriesCount
, sizeof(DayPeriodEntry
), CompareDayPeriodEntries
);
994 // now fix start minute for the non-"at" entries
996 for (dpIndex
= 0; dpIndex
< dpEntriesCount
; dpIndex
++) {
997 if (dpIndex
== 0 || (dpEntries
[dpIndex
-1].value
!= UADAYPERIOD_MIDNIGHT
&& dpEntries
[dpIndex
-1].value
!= UADAYPERIOD_NOON
)) {
998 dpEntries
[dpIndex
].startMin
= 0;
1002 // OK, all of the above is what we would do in an "open" function if we were using an
1003 // open/use/close model for this; the object would just have the sorted array above.
1005 // Now we use the sorted array to find the dayPeriod matching the supplied time.
1006 // Only a few entries, linear search OK
1007 DayPeriodEntry entryToMatch
= { hour
, minute
, UADAYPERIOD_UNKNOWN
};
1009 while (dpIndex
< dpEntriesCount
- 1 && CompareDayPeriodEntries(&entryToMatch
, &dpEntries
[dpIndex
+ 1]) >= 0) {
1012 if (CompareDayPeriodEntries(&entryToMatch
, &dpEntries
[dpIndex
]) >= 0) {
1013 dayPeriod
= dpEntries
[dpIndex
].value
;
1021 #endif /* #if !UCONFIG_NO_FORMATTING */