1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /*
4 *******************************************************************************
5 * Copyright (C) 1996-2016, International Business Machines
6 * Corporation and others. All Rights Reserved.
7 *******************************************************************************
8 */
9
10 #include "utypeinfo.h" // for 'typeid' to work
11
12 #include "unicode/utypes.h"
13
14 #if !UCONFIG_NO_FORMATTING
15
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"
25 #include "cmemory.h"
26 #include "cstring.h"
27 #include "ustrenum.h"
28 #include "uenumimp.h"
29 #include "ulist.h"
30 #include "ulocimp.h"
31
32 U_NAMESPACE_USE
33
34 static TimeZone*
_createTimeZone(const UChar * zoneID,int32_t len,UErrorCode * ec)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);
45 if (zone == NULL) {
46 *ec = U_MEMORY_ALLOCATION_ERROR;
47 }
48 }
49 return zone;
50 }
51
52 U_CAPI UEnumeration* U_EXPORT2
ucal_openTimeZoneIDEnumeration(USystemTimeZoneType zoneType,const char * region,const int32_t * rawOffset,UErrorCode * ec)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);
57 }
58
59 U_CAPI UEnumeration* U_EXPORT2
ucal_openTimeZones(UErrorCode * ec)60 ucal_openTimeZones(UErrorCode* ec) {
61 return uenum_openFromStringEnumeration(TimeZone::createEnumeration(), ec);
62 }
63
64 U_CAPI UEnumeration* U_EXPORT2
ucal_openCountryTimeZones(const char * country,UErrorCode * ec)65 ucal_openCountryTimeZones(const char* country, UErrorCode* ec) {
66 return uenum_openFromStringEnumeration(TimeZone::createEnumeration(country), ec);
67 }
68
69 U_CAPI int32_t U_EXPORT2
ucal_getDefaultTimeZone(UChar * result,int32_t resultCapacity,UErrorCode * ec)70 ucal_getDefaultTimeZone(UChar* result, int32_t resultCapacity, UErrorCode* ec) {
71 int32_t len = 0;
72 if (ec!=NULL && U_SUCCESS(*ec)) {
73 TimeZone* zone = TimeZone::createDefault();
74 if (zone == NULL) {
75 *ec = U_MEMORY_ALLOCATION_ERROR;
76 } else {
77 UnicodeString id;
78 zone->getID(id);
79 delete zone;
80 len = id.extract(result, resultCapacity, *ec);
81 }
82 }
83 return len;
84 }
85
86 U_CAPI void U_EXPORT2
ucal_setDefaultTimeZone(const UChar * zoneID,UErrorCode * ec)87 ucal_setDefaultTimeZone(const UChar* zoneID, UErrorCode* ec) {
88 TimeZone* zone = _createTimeZone(zoneID, -1, ec);
89 if (zone != NULL) {
90 TimeZone::adoptDefault(zone);
91 }
92 }
93
94 U_CAPI int32_t U_EXPORT2
ucal_getDSTSavings(const UChar * zoneID,UErrorCode * ec)95 ucal_getDSTSavings(const UChar* zoneID, UErrorCode* ec) {
96 int32_t result = 0;
97 TimeZone* zone = _createTimeZone(zoneID, -1, ec);
98 if (U_SUCCESS(*ec)) {
99 SimpleTimeZone* stz = dynamic_cast<SimpleTimeZone*>(zone);
100 if (stz != NULL) {
101 result = stz->getDSTSavings();
102 } else {
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) {
109 int32_t raw, dst;
110 zone->getOffset(d, FALSE, raw, dst, *ec);
111 if (U_FAILURE(*ec)) {
112 break;
113 } else if (dst != 0) {
114 result = dst;
115 break;
116 }
117 }
118 }
119 }
120 delete zone;
121 return result;
122 }
123
124 U_CAPI UDate U_EXPORT2
ucal_getNow()125 ucal_getNow()
126 {
127
128 return Calendar::getNow();
129 }
130
131 #define ULOC_LOCALE_IDENTIFIER_CAPACITY (ULOC_FULLNAME_CAPACITY + 1 + ULOC_KEYWORD_AND_VALUES_CAPACITY)
132
133 U_CAPI UCalendar* U_EXPORT2
ucal_open(const UChar * zoneID,int32_t len,const char * locale,UCalendarType caltype,UErrorCode * status)134 ucal_open( const UChar* zoneID,
135 int32_t len,
136 const char* locale,
137 UCalendarType caltype,
138 UErrorCode* status)
139 {
140
141 if(U_FAILURE(*status)) return 0;
142
143 TimeZone* zone = (zoneID==NULL) ? TimeZone::createDefault()
144 : _createTimeZone(zoneID, len, status);
145
146 if (U_FAILURE(*status)) {
147 return NULL;
148 }
149
150 if ( caltype == UCAL_GREGORIAN ) {
151 char localeBuf[ULOC_LOCALE_IDENTIFIER_CAPACITY];
152 if ( locale == NULL ) {
153 locale = uloc_getDefault();
154 }
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)) {
158 return NULL;
159 }
160 return (UCalendar*)Calendar::createInstance(zone, Locale(localeBuf), *status);
161 }
162 return (UCalendar*)Calendar::createInstance(zone, Locale(locale), *status);
163 }
164
165 U_CAPI void U_EXPORT2
ucal_close(UCalendar * cal)166 ucal_close(UCalendar *cal)
167 {
168
169 delete (Calendar*) cal;
170 }
171
172 U_CAPI UCalendar* U_EXPORT2
ucal_clone(const UCalendar * cal,UErrorCode * status)173 ucal_clone(const UCalendar* cal,
174 UErrorCode* status)
175 {
176 if(U_FAILURE(*status)) return 0;
177
178 Calendar* res = ((Calendar*)cal)->clone();
179
180 if(res == 0) {
181 *status = U_MEMORY_ALLOCATION_ERROR;
182 return 0;
183 }
184
185 return (UCalendar*) res;
186 }
187
188 U_CAPI void U_EXPORT2
ucal_setTimeZone(UCalendar * cal,const UChar * zoneID,int32_t len,UErrorCode * status)189 ucal_setTimeZone( UCalendar* cal,
190 const UChar* zoneID,
191 int32_t len,
192 UErrorCode *status)
193 {
194
195 if(U_FAILURE(*status))
196 return;
197
198 TimeZone* zone = (zoneID==NULL) ? TimeZone::createDefault()
199 : _createTimeZone(zoneID, len, status);
200
201 if (zone != NULL) {
202 ((Calendar*)cal)->adoptTimeZone(zone);
203 }
204 }
205
206 U_CAPI int32_t U_EXPORT2
ucal_getTimeZoneID(const UCalendar * cal,UChar * result,int32_t resultLength,UErrorCode * status)207 ucal_getTimeZoneID(const UCalendar *cal,
208 UChar *result,
209 int32_t resultLength,
210 UErrorCode *status)
211 {
212 if (U_FAILURE(*status)) {
213 return 0;
214 }
215 const TimeZone& tz = ((Calendar*)cal)->getTimeZone();
216 UnicodeString id;
217 tz.getID(id);
218 return id.extract(result, resultLength, *status);
219 }
220
221 U_CAPI int32_t U_EXPORT2
ucal_getTimeZoneDisplayName(const UCalendar * cal,UCalendarDisplayNameType type,const char * locale,UChar * result,int32_t resultLength,UErrorCode * status)222 ucal_getTimeZoneDisplayName(const UCalendar* cal,
223 UCalendarDisplayNameType type,
224 const char *locale,
225 UChar* result,
226 int32_t resultLength,
227 UErrorCode* status)
228 {
229
230 if(U_FAILURE(*status)) return -1;
231
232 const TimeZone& tz = ((Calendar*)cal)->getTimeZone();
233 UnicodeString id;
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);
238 }
239
240 switch(type) {
241 case UCAL_STANDARD:
242 tz.getDisplayName(FALSE, TimeZone::LONG, Locale(locale), id);
243 break;
244
245 case UCAL_SHORT_STANDARD:
246 tz.getDisplayName(FALSE, TimeZone::SHORT, Locale(locale), id);
247 break;
248
249 case UCAL_DST:
250 tz.getDisplayName(TRUE, TimeZone::LONG, Locale(locale), id);
251 break;
252
253 case UCAL_SHORT_DST:
254 tz.getDisplayName(TRUE, TimeZone::SHORT, Locale(locale), id);
255 break;
256 }
257
258 return id.extract(result, resultLength, *status);
259 }
260
261 U_CAPI UBool U_EXPORT2
ucal_inDaylightTime(const UCalendar * cal,UErrorCode * status)262 ucal_inDaylightTime( const UCalendar* cal,
263 UErrorCode* status )
264 {
265
266 if(U_FAILURE(*status)) return (UBool) -1;
267 return ((Calendar*)cal)->inDaylightTime(*status);
268 }
269
270 U_CAPI void U_EXPORT2
ucal_setGregorianChange(UCalendar * cal,UDate date,UErrorCode * pErrorCode)271 ucal_setGregorianChange(UCalendar *cal, UDate date, UErrorCode *pErrorCode) {
272 if(U_FAILURE(*pErrorCode)) {
273 return;
274 }
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;
284 return;
285 }
286 if(typeid(*cpp_cal) != typeid(GregorianCalendar)) {
287 *pErrorCode = U_UNSUPPORTED_ERROR;
288 return;
289 }
290 gregocal->setGregorianChange(date, *pErrorCode);
291 }
292
293 U_CAPI UDate U_EXPORT2
ucal_getGregorianChange(const UCalendar * cal,UErrorCode * pErrorCode)294 ucal_getGregorianChange(const UCalendar *cal, UErrorCode *pErrorCode) {
295 if(U_FAILURE(*pErrorCode)) {
296 return (UDate)0;
297 }
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;
306 return (UDate)0;
307 }
308 if(typeid(*cpp_cal) != typeid(GregorianCalendar)) {
309 *pErrorCode = U_UNSUPPORTED_ERROR;
310 return (UDate)0;
311 }
312 return gregocal->getGregorianChange();
313 }
314
315 U_CAPI int32_t U_EXPORT2
ucal_getAttribute(const UCalendar * cal,UCalendarAttribute attr)316 ucal_getAttribute( const UCalendar* cal,
317 UCalendarAttribute attr)
318 {
319
320 switch(attr) {
321 case UCAL_LENIENT:
322 return ((Calendar*)cal)->isLenient();
323
324 case UCAL_FIRST_DAY_OF_WEEK:
325 return ((Calendar*)cal)->getFirstDayOfWeek();
326
327 case UCAL_MINIMAL_DAYS_IN_FIRST_WEEK:
328 return ((Calendar*)cal)->getMinimalDaysInFirstWeek();
329
330 case UCAL_REPEATED_WALL_TIME:
331 return ((Calendar*)cal)->getRepeatedWallTimeOption();
332
333 case UCAL_SKIPPED_WALL_TIME:
334 return ((Calendar*)cal)->getSkippedWallTimeOption();
335
336 default:
337 break;
338 }
339 return -1;
340 }
341
342 U_CAPI void U_EXPORT2
ucal_setAttribute(UCalendar * cal,UCalendarAttribute attr,int32_t newValue)343 ucal_setAttribute( UCalendar* cal,
344 UCalendarAttribute attr,
345 int32_t newValue)
346 {
347
348 switch(attr) {
349 case UCAL_LENIENT:
350 ((Calendar*)cal)->setLenient((UBool)newValue);
351 break;
352
353 case UCAL_FIRST_DAY_OF_WEEK:
354 ((Calendar*)cal)->setFirstDayOfWeek((UCalendarDaysOfWeek)newValue);
355 break;
356
357 case UCAL_MINIMAL_DAYS_IN_FIRST_WEEK:
358 ((Calendar*)cal)->setMinimalDaysInFirstWeek((uint8_t)newValue);
359 break;
360
361 case UCAL_REPEATED_WALL_TIME:
362 ((Calendar*)cal)->setRepeatedWallTimeOption((UCalendarWallTimeOption)newValue);
363 break;
364
365 case UCAL_SKIPPED_WALL_TIME:
366 ((Calendar*)cal)->setSkippedWallTimeOption((UCalendarWallTimeOption)newValue);
367 break;
368 }
369 }
370
371 U_CAPI const char* U_EXPORT2
ucal_getAvailable(int32_t index)372 ucal_getAvailable(int32_t index)
373 {
374
375 return uloc_getAvailable(index);
376 }
377
378 U_CAPI int32_t U_EXPORT2
ucal_countAvailable()379 ucal_countAvailable()
380 {
381
382 return uloc_countAvailable();
383 }
384
385 U_CAPI UDate U_EXPORT2
ucal_getMillis(const UCalendar * cal,UErrorCode * status)386 ucal_getMillis( const UCalendar* cal,
387 UErrorCode* status)
388 {
389
390 if(U_FAILURE(*status)) return (UDate) 0;
391
392 return ((Calendar*)cal)->getTime(*status);
393 }
394
395 U_CAPI void U_EXPORT2
ucal_setMillis(UCalendar * cal,UDate dateTime,UErrorCode * status)396 ucal_setMillis( UCalendar* cal,
397 UDate dateTime,
398 UErrorCode* status )
399 {
400 if(U_FAILURE(*status)) return;
401
402 ((Calendar*)cal)->setTime(dateTime, *status);
403 }
404
405 // TBD: why does this take an UErrorCode?
406 U_CAPI void U_EXPORT2
ucal_setDate(UCalendar * cal,int32_t year,int32_t month,int32_t date,UErrorCode * status)407 ucal_setDate( UCalendar* cal,
408 int32_t year,
409 int32_t month,
410 int32_t date,
411 UErrorCode *status)
412 {
413
414 if(U_FAILURE(*status)) return;
415
416 ((Calendar*)cal)->set(year, month, date);
417 }
418
419 // TBD: why does this take an UErrorCode?
420 U_CAPI void U_EXPORT2
ucal_setDateTime(UCalendar * cal,int32_t year,int32_t month,int32_t date,int32_t hour,int32_t minute,int32_t second,UErrorCode * status)421 ucal_setDateTime( UCalendar* cal,
422 int32_t year,
423 int32_t month,
424 int32_t date,
425 int32_t hour,
426 int32_t minute,
427 int32_t second,
428 UErrorCode *status)
429 {
430 if(U_FAILURE(*status)) return;
431
432 ((Calendar*)cal)->set(year, month, date, hour, minute, second);
433 }
434
435 U_CAPI UBool U_EXPORT2
ucal_equivalentTo(const UCalendar * cal1,const UCalendar * cal2)436 ucal_equivalentTo( const UCalendar* cal1,
437 const UCalendar* cal2)
438 {
439
440 return ((Calendar*)cal1)->isEquivalentTo(*((Calendar*)cal2));
441 }
442
443 U_CAPI void U_EXPORT2
ucal_add(UCalendar * cal,UCalendarDateFields field,int32_t amount,UErrorCode * status)444 ucal_add( UCalendar* cal,
445 UCalendarDateFields field,
446 int32_t amount,
447 UErrorCode* status)
448 {
449
450 if(U_FAILURE(*status)) return;
451
452 ((Calendar*)cal)->add(field, amount, *status);
453 }
454
455 U_CAPI void U_EXPORT2
ucal_roll(UCalendar * cal,UCalendarDateFields field,int32_t amount,UErrorCode * status)456 ucal_roll( UCalendar* cal,
457 UCalendarDateFields field,
458 int32_t amount,
459 UErrorCode* status)
460 {
461
462 if(U_FAILURE(*status)) return;
463
464 ((Calendar*)cal)->roll(field, amount, *status);
465 }
466
467 U_CAPI int32_t U_EXPORT2
ucal_get(const UCalendar * cal,UCalendarDateFields field,UErrorCode * status)468 ucal_get( const UCalendar* cal,
469 UCalendarDateFields field,
470 UErrorCode* status )
471 {
472
473 if(U_FAILURE(*status)) return -1;
474
475 return ((Calendar*)cal)->get(field, *status);
476 }
477
478 U_CAPI void U_EXPORT2
ucal_set(UCalendar * cal,UCalendarDateFields field,int32_t value)479 ucal_set( UCalendar* cal,
480 UCalendarDateFields field,
481 int32_t value)
482 {
483
484 ((Calendar*)cal)->set(field, value);
485 }
486
487 U_CAPI UBool U_EXPORT2
ucal_isSet(const UCalendar * cal,UCalendarDateFields field)488 ucal_isSet( const UCalendar* cal,
489 UCalendarDateFields field)
490 {
491
492 return ((Calendar*)cal)->isSet(field);
493 }
494
495 U_CAPI void U_EXPORT2
ucal_clearField(UCalendar * cal,UCalendarDateFields field)496 ucal_clearField( UCalendar* cal,
497 UCalendarDateFields field)
498 {
499
500 ((Calendar*)cal)->clear(field);
501 }
502
503 U_CAPI void U_EXPORT2
ucal_clear(UCalendar * calendar)504 ucal_clear(UCalendar* calendar)
505 {
506
507 ((Calendar*)calendar)->clear();
508 }
509
510 U_CAPI int32_t U_EXPORT2
ucal_getLimit(const UCalendar * cal,UCalendarDateFields field,UCalendarLimitType type,UErrorCode * status)511 ucal_getLimit( const UCalendar* cal,
512 UCalendarDateFields field,
513 UCalendarLimitType type,
514 UErrorCode *status)
515 {
516
517 if(status==0 || U_FAILURE(*status)) {
518 return -1;
519 }
520
521 switch(type) {
522 case UCAL_MINIMUM:
523 return ((Calendar*)cal)->getMinimum(field);
524
525 case UCAL_MAXIMUM:
526 return ((Calendar*)cal)->getMaximum(field);
527
528 case UCAL_GREATEST_MINIMUM:
529 return ((Calendar*)cal)->getGreatestMinimum(field);
530
531 case UCAL_LEAST_MAXIMUM:
532 return ((Calendar*)cal)->getLeastMaximum(field);
533
534 case UCAL_ACTUAL_MINIMUM:
535 return ((Calendar*)cal)->getActualMinimum(field,
536 *status);
537
538 case UCAL_ACTUAL_MAXIMUM:
539 return ((Calendar*)cal)->getActualMaximum(field,
540 *status);
541
542 default:
543 break;
544 }
545 return -1;
546 }
547
548 U_CAPI const char * U_EXPORT2
ucal_getLocaleByType(const UCalendar * cal,ULocDataLocaleType type,UErrorCode * status)549 ucal_getLocaleByType(const UCalendar *cal, ULocDataLocaleType type, UErrorCode* status)
550 {
551 if (cal == NULL) {
552 if (U_SUCCESS(*status)) {
553 *status = U_ILLEGAL_ARGUMENT_ERROR;
554 }
555 return NULL;
556 }
557 return ((Calendar*)cal)->getLocaleID(type, *status);
558 }
559
560 U_CAPI const char * U_EXPORT2
ucal_getTZDataVersion(UErrorCode * status)561 ucal_getTZDataVersion(UErrorCode* status)
562 {
563 return TimeZone::getTZDataVersion(*status);
564 }
565
566 U_CAPI int32_t U_EXPORT2
ucal_getCanonicalTimeZoneID(const UChar * id,int32_t len,UChar * result,int32_t resultCapacity,UBool * isSystemID,UErrorCode * status)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)) {
570 return 0;
571 }
572 if (isSystemID) {
573 *isSystemID = FALSE;
574 }
575 if (id == 0 || len == 0 || result == 0 || resultCapacity <= 0) {
576 *status = U_ILLEGAL_ARGUMENT_ERROR;
577 return 0;
578 }
579 int32_t reslen = 0;
580 UnicodeString canonical;
581 UBool systemID = FALSE;
582 TimeZone::getCanonicalID(UnicodeString(id, len), canonical, systemID, *status);
583 if (U_SUCCESS(*status)) {
584 if (isSystemID) {
585 *isSystemID = systemID;
586 }
587 reslen = canonical.extract(result, resultCapacity, *status);
588 }
589 return reslen;
590 }
591
592 U_CAPI const char * U_EXPORT2
ucal_getType(const UCalendar * cal,UErrorCode * status)593 ucal_getType(const UCalendar *cal, UErrorCode* status)
594 {
595 if (U_FAILURE(*status)) {
596 return NULL;
597 }
598 return ((Calendar*)cal)->getType();
599 }
600
601 U_CAPI UCalendarWeekdayType U_EXPORT2
ucal_getDayOfWeekType(const UCalendar * cal,UCalendarDaysOfWeek dayOfWeek,UErrorCode * status)602 ucal_getDayOfWeekType(const UCalendar *cal, UCalendarDaysOfWeek dayOfWeek, UErrorCode* status)
603 {
604 if (U_FAILURE(*status)) {
605 return UCAL_WEEKDAY;
606 }
607 return ((Calendar*)cal)->getDayOfWeekType(dayOfWeek, *status);
608 }
609
610 U_CAPI int32_t U_EXPORT2
ucal_getWeekendTransition(const UCalendar * cal,UCalendarDaysOfWeek dayOfWeek,UErrorCode * status)611 ucal_getWeekendTransition(const UCalendar *cal, UCalendarDaysOfWeek dayOfWeek, UErrorCode *status)
612 {
613 if (U_FAILURE(*status)) {
614 return 0;
615 }
616 return ((Calendar*)cal)->getWeekendTransition(dayOfWeek, *status);
617 }
618
619 U_CAPI UBool U_EXPORT2
ucal_isWeekend(const UCalendar * cal,UDate date,UErrorCode * status)620 ucal_isWeekend(const UCalendar *cal, UDate date, UErrorCode *status)
621 {
622 if (U_FAILURE(*status)) {
623 return FALSE;
624 }
625 return ((Calendar*)cal)->isWeekend(date, *status);
626 }
627
628 U_CAPI int32_t U_EXPORT2
ucal_getFieldDifference(UCalendar * cal,UDate target,UCalendarDateFields field,UErrorCode * status)629 ucal_getFieldDifference(UCalendar* cal, UDate target,
630 UCalendarDateFields field,
631 UErrorCode* status )
632 {
633 if (U_FAILURE(*status)) {
634 return 0;
635 }
636 return ((Calendar*)cal)->fieldDifference(target, field, *status);
637 }
638
639
640 static const UEnumeration defaultKeywordValues = {
641 NULL,
642 NULL,
643 ulist_close_keyword_values_iterator,
644 ulist_count_keyword_values,
645 uenum_unextDefault,
646 ulist_next_keyword_value,
647 ulist_reset_keyword_values_iterator
648 };
649
650 static const char * const CAL_TYPES[] = {
651 "gregorian",
652 "japanese",
653 "buddhist",
654 "roc",
655 "persian",
656 "islamic-civil",
657 "islamic",
658 "hebrew",
659 "chinese",
660 "indian",
661 "coptic",
662 "ethiopic",
663 "ethiopic-amete-alem",
664 "iso8601",
665 "dangi",
666 "islamic-umalqura",
667 "islamic-tbla",
668 "islamic-rgsa",
669 NULL
670 };
671
672 U_CAPI UEnumeration* U_EXPORT2
ucal_getKeywordValuesForLocale(const char *,const char * locale,UBool commonlyUsed,UErrorCode * status)673 ucal_getKeywordValuesForLocale(const char * /* key */, const char* locale, UBool commonlyUsed, UErrorCode *status) {
674 // Resolve region
675 char prefRegion[ULOC_COUNTRY_CAPACITY];
676 (void)ulocimp_getRegionForSupplementalData(locale, TRUE, prefRegion, sizeof(prefRegion), status);
677
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);
685 }
686
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++) {
693 int32_t len;
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;
698 break;
699 }
700 u_UCharsToChars(type, caltype, len);
701 *(caltype + len) = 0;
702
703 ulist_addItemEndList(values, caltype, TRUE, status);
704 if (U_FAILURE(*status)) {
705 break;
706 }
707 }
708
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)) {
715 break;
716 }
717 }
718 }
719 }
720 if (U_FAILURE(*status)) {
721 ulist_deleteList(values);
722 values = NULL;
723 }
724 }
725 }
726
727 ures_close(order);
728 ures_close(rb);
729
730 if (U_FAILURE(*status) || values == NULL) {
731 return NULL;
732 }
733
734 // Create string enumeration
735 UEnumeration *en = (UEnumeration*)uprv_malloc(sizeof(UEnumeration));
736 if (en == NULL) {
737 *status = U_MEMORY_ALLOCATION_ERROR;
738 ulist_deleteList(values);
739 return NULL;
740 }
741 ulist_resetList(values);
742 memcpy(en, &defaultKeywordValues, sizeof(UEnumeration));
743 en->context = values;
744 return en;
745 }
746
747 U_CAPI UBool U_EXPORT2
ucal_getTimeZoneTransitionDate(const UCalendar * cal,UTimeZoneTransitionType type,UDate * transition,UErrorCode * status)748 ucal_getTimeZoneTransitionDate(const UCalendar* cal, UTimeZoneTransitionType type,
749 UDate* transition, UErrorCode* status)
750 {
751 if (U_FAILURE(*status)) {
752 return FALSE;
753 }
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);
763 if (result) {
764 *transition = tzt.getTime();
765 return TRUE;
766 }
767 }
768 return FALSE;
769 }
770
771 U_CAPI int32_t U_EXPORT2
ucal_getWindowsTimeZoneID(const UChar * id,int32_t len,UChar * winid,int32_t winidCapacity,UErrorCode * status)772 ucal_getWindowsTimeZoneID(const UChar* id, int32_t len, UChar* winid, int32_t winidCapacity, UErrorCode* status) {
773 if (U_FAILURE(*status)) {
774 return 0;
775 }
776
777 int32_t resultLen = 0;
778 UnicodeString resultWinID;
779
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);
784 }
785
786 return resultLen;
787 }
788
789 U_CAPI int32_t U_EXPORT2
ucal_getTimeZoneIDForWindowsID(const UChar * winid,int32_t len,const char * region,UChar * id,int32_t idCapacity,UErrorCode * status)790 ucal_getTimeZoneIDForWindowsID(const UChar* winid, int32_t len, const char* region, UChar* id, int32_t idCapacity, UErrorCode* status) {
791 if (U_FAILURE(*status)) {
792 return 0;
793 }
794
795 int32_t resultLen = 0;
796 UnicodeString resultID;
797
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);
802 }
803
804 return resultLen;
805 }
806
807 #endif /* #if !UCONFIG_NO_FORMATTING */
808