1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
|
// © 2016 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html
/*
*******************************************************************************
* Copyright (C) 1996-2016, International Business Machines
* Corporation and others. All Rights Reserved.
*******************************************************************************
*/
#include "utypeinfo.h" // for 'typeid' to work
#include "unicode/utypes.h"
#if !UCONFIG_NO_FORMATTING
#include "unicode/ucal.h"
#include "unicode/uloc.h"
#include "unicode/calendar.h"
#include "unicode/timezone.h"
#include "unicode/gregocal.h"
#include "unicode/simpletz.h"
#include "unicode/ustring.h"
#include "unicode/strenum.h"
#include "unicode/localpointer.h"
#include "charstr.h"
#include "cmemory.h"
#include "cstring.h"
#include "iso8601cal.h"
#include "ustrenum.h"
#include "uenumimp.h"
#include "ulist.h"
#include "ulocimp.h"
U_NAMESPACE_USE
static TimeZone*
_createTimeZone(const char16_t* zoneID, int32_t len, UErrorCode* ec) {
TimeZone* zone = nullptr;
if (ec != nullptr && U_SUCCESS(*ec)) {
// Note that if zoneID is invalid, we get back GMT. This odd
// behavior is by design and goes back to the JDK. The only
// failure we will see is a memory allocation failure.
int32_t l = (len<0 ? u_strlen(zoneID) : len);
UnicodeString zoneStrID;
zoneStrID.setTo(static_cast<UBool>(len < 0), zoneID, l); /* temporary read-only alias */
zone = TimeZone::createTimeZone(zoneStrID);
if (zone == nullptr) {
*ec = U_MEMORY_ALLOCATION_ERROR;
}
}
return zone;
}
U_CAPI UEnumeration* U_EXPORT2
ucal_openTimeZoneIDEnumeration(USystemTimeZoneType zoneType, const char* region,
const int32_t* rawOffset, UErrorCode* ec) {
return uenum_openFromStringEnumeration(TimeZone::createTimeZoneIDEnumeration(
zoneType, region, rawOffset, *ec), ec);
}
U_CAPI UEnumeration* U_EXPORT2
ucal_openTimeZones(UErrorCode* ec) {
return ucal_openTimeZoneIDEnumeration(UCAL_ZONE_TYPE_ANY, nullptr, nullptr, ec);
}
U_CAPI UEnumeration* U_EXPORT2
ucal_openCountryTimeZones(const char* country, UErrorCode* ec) {
return ucal_openTimeZoneIDEnumeration(UCAL_ZONE_TYPE_ANY, country, nullptr, ec);
}
U_CAPI int32_t U_EXPORT2
ucal_getDefaultTimeZone(char16_t* result, int32_t resultCapacity, UErrorCode* ec) {
int32_t len = 0;
if (ec != nullptr && U_SUCCESS(*ec)) {
TimeZone* zone = TimeZone::createDefault();
if (zone == nullptr) {
*ec = U_MEMORY_ALLOCATION_ERROR;
} else {
UnicodeString id;
zone->getID(id);
delete zone;
len = id.extract(result, resultCapacity, *ec);
}
}
return len;
}
U_CAPI void U_EXPORT2
ucal_setDefaultTimeZone(const char16_t* zoneID, UErrorCode* ec) {
TimeZone* zone = _createTimeZone(zoneID, -1, ec);
if (zone != nullptr) {
TimeZone::adoptDefault(zone);
}
}
U_CAPI int32_t U_EXPORT2
ucal_getHostTimeZone(char16_t* result, int32_t resultCapacity, UErrorCode* ec) {
int32_t len = 0;
if (ec != nullptr && U_SUCCESS(*ec)) {
TimeZone *zone = TimeZone::detectHostTimeZone();
if (zone == nullptr) {
*ec = U_MEMORY_ALLOCATION_ERROR;
} else {
UnicodeString id;
zone->getID(id);
delete zone;
len = id.extract(result, resultCapacity, *ec);
}
}
return len;
}
U_CAPI int32_t U_EXPORT2
ucal_getDSTSavings(const char16_t* zoneID, UErrorCode* ec) {
int32_t result = 0;
TimeZone* zone = _createTimeZone(zoneID, -1, ec);
if (U_SUCCESS(*ec)) {
SimpleTimeZone* stz = dynamic_cast<SimpleTimeZone*>(zone);
if (stz != nullptr) {
result = stz->getDSTSavings();
} else {
// Since there is no getDSTSavings on TimeZone, we use a
// heuristic: Starting with the current time, march
// forwards for one year, looking for DST savings.
// Stepping by weeks is sufficient.
UDate d = Calendar::getNow();
for (int32_t i=0; i<53; ++i, d+=U_MILLIS_PER_DAY*7.0) {
int32_t raw, dst;
zone->getOffset(d, false, raw, dst, *ec);
if (U_FAILURE(*ec)) {
break;
} else if (dst != 0) {
result = dst;
break;
}
}
}
}
delete zone;
return result;
}
U_CAPI UDate U_EXPORT2
ucal_getNow()
{
return Calendar::getNow();
}
#define ULOC_LOCALE_IDENTIFIER_CAPACITY (ULOC_FULLNAME_CAPACITY + 1 + ULOC_KEYWORD_AND_VALUES_CAPACITY)
U_CAPI UCalendar* U_EXPORT2
ucal_open( const char16_t* zoneID,
int32_t len,
const char* locale,
UCalendarType caltype,
UErrorCode* status)
{
if (U_FAILURE(*status)) {
return nullptr;
}
LocalPointer<TimeZone> zone( (zoneID==nullptr) ? TimeZone::createDefault()
: _createTimeZone(zoneID, len, status), *status);
if (U_FAILURE(*status)) {
return nullptr;
}
if ( caltype == UCAL_GREGORIAN ) {
if ( locale == nullptr ) {
locale = uloc_getDefault();
}
CharString localeBuf(locale, *status);
ulocimp_setKeywordValue("calendar", "gregorian", localeBuf, *status);
if (U_FAILURE(*status)) {
return nullptr;
}
return (UCalendar*)Calendar::createInstance(zone.orphan(), Locale(localeBuf.data()), *status);
}
return (UCalendar*)Calendar::createInstance(zone.orphan(), Locale(locale), *status);
}
U_CAPI void U_EXPORT2
ucal_close(UCalendar *cal)
{
if (cal != nullptr) {
delete (Calendar*) cal;
}
}
U_CAPI UCalendar* U_EXPORT2
ucal_clone(const UCalendar* cal,
UErrorCode* status)
{
if (U_FAILURE(*status)) return nullptr;
Calendar* res = ((Calendar*)cal)->clone();
if (res == nullptr) {
*status = U_MEMORY_ALLOCATION_ERROR;
return nullptr;
}
return (UCalendar*) res;
}
U_CAPI void U_EXPORT2
ucal_setTimeZone( UCalendar* cal,
const char16_t* zoneID,
int32_t len,
UErrorCode *status)
{
if(U_FAILURE(*status))
return;
TimeZone* zone = (zoneID==nullptr) ? TimeZone::createDefault()
: _createTimeZone(zoneID, len, status);
if (zone != nullptr) {
((Calendar*)cal)->adoptTimeZone(zone);
}
}
U_CAPI int32_t U_EXPORT2
ucal_getTimeZoneID(const UCalendar *cal,
char16_t *result,
int32_t resultLength,
UErrorCode *status)
{
if (U_FAILURE(*status)) {
return 0;
}
const TimeZone& tz = ((Calendar*)cal)->getTimeZone();
UnicodeString id;
tz.getID(id);
return id.extract(result, resultLength, *status);
}
U_CAPI int32_t U_EXPORT2
ucal_getTimeZoneDisplayName(const UCalendar* cal,
UCalendarDisplayNameType type,
const char *locale,
char16_t* result,
int32_t resultLength,
UErrorCode* status)
{
if(U_FAILURE(*status)) return -1;
const TimeZone& tz = ((Calendar*)cal)->getTimeZone();
UnicodeString id;
if (!(result == nullptr && resultLength == 0)) {
// Null destination for pure preflighting: empty dummy string
// otherwise, alias the destination buffer
id.setTo(result, 0, resultLength);
}
switch(type) {
case UCAL_STANDARD:
tz.getDisplayName(false, TimeZone::LONG, Locale(locale), id);
break;
case UCAL_SHORT_STANDARD:
tz.getDisplayName(false, TimeZone::SHORT, Locale(locale), id);
break;
case UCAL_DST:
tz.getDisplayName(true, TimeZone::LONG, Locale(locale), id);
break;
case UCAL_SHORT_DST:
tz.getDisplayName(true, TimeZone::SHORT, Locale(locale), id);
break;
}
return id.extract(result, resultLength, *status);
}
U_CAPI UBool U_EXPORT2
ucal_inDaylightTime( const UCalendar* cal,
UErrorCode* status )
{
if(U_FAILURE(*status)) return (UBool) -1;
return ((Calendar*)cal)->inDaylightTime(*status);
}
U_CAPI void U_EXPORT2
ucal_setGregorianChange(UCalendar *cal, UDate date, UErrorCode *pErrorCode) {
if(U_FAILURE(*pErrorCode)) {
return;
}
Calendar *cpp_cal = (Calendar *)cal;
GregorianCalendar *gregocal = dynamic_cast<GregorianCalendar *>(cpp_cal);
// Not if(gregocal == nullptr) {
// because we really want to work only with a GregorianCalendar, not with
// its subclasses like BuddhistCalendar.
if (cpp_cal == nullptr) {
// We normally don't check "this" pointers for nullptr, but this here avoids
// compiler-generated exception-throwing code in case cal == nullptr.
*pErrorCode = U_ILLEGAL_ARGUMENT_ERROR;
return;
}
if(typeid(*cpp_cal) != typeid(GregorianCalendar) &&
typeid(*cpp_cal) != typeid(ISO8601Calendar)) {
*pErrorCode = U_UNSUPPORTED_ERROR;
return;
}
gregocal->setGregorianChange(date, *pErrorCode);
}
U_CAPI UDate U_EXPORT2
ucal_getGregorianChange(const UCalendar *cal, UErrorCode *pErrorCode) {
if(U_FAILURE(*pErrorCode)) {
return (UDate)0;
}
const Calendar *cpp_cal = (const Calendar *)cal;
const GregorianCalendar *gregocal = dynamic_cast<const GregorianCalendar *>(cpp_cal);
// Not if(gregocal == nullptr) {
// see comments in ucal_setGregorianChange().
if (cpp_cal == nullptr) {
// We normally don't check "this" pointers for nullptr, but this here avoids
// compiler-generated exception-throwing code in case cal == nullptr.
*pErrorCode = U_ILLEGAL_ARGUMENT_ERROR;
return (UDate)0;
}
if(typeid(*cpp_cal) != typeid(GregorianCalendar) &&
typeid(*cpp_cal) != typeid(ISO8601Calendar)) {
*pErrorCode = U_UNSUPPORTED_ERROR;
return (UDate)0;
}
return gregocal->getGregorianChange();
}
U_CAPI int32_t U_EXPORT2
ucal_getAttribute( const UCalendar* cal,
UCalendarAttribute attr) UPRV_NO_SANITIZE_UNDEFINED {
switch(attr) {
case UCAL_LENIENT:
return ((Calendar*)cal)->isLenient();
case UCAL_FIRST_DAY_OF_WEEK:
return ((Calendar*)cal)->getFirstDayOfWeek();
case UCAL_MINIMAL_DAYS_IN_FIRST_WEEK:
return ((Calendar*)cal)->getMinimalDaysInFirstWeek();
case UCAL_REPEATED_WALL_TIME:
return ((Calendar*)cal)->getRepeatedWallTimeOption();
case UCAL_SKIPPED_WALL_TIME:
return ((Calendar*)cal)->getSkippedWallTimeOption();
default:
break;
}
return -1;
}
U_CAPI void U_EXPORT2
ucal_setAttribute( UCalendar* cal,
UCalendarAttribute attr,
int32_t newValue)
{
switch(attr) {
case UCAL_LENIENT:
((Calendar*)cal)->setLenient((UBool)newValue);
break;
case UCAL_FIRST_DAY_OF_WEEK:
((Calendar*)cal)->setFirstDayOfWeek((UCalendarDaysOfWeek)newValue);
break;
case UCAL_MINIMAL_DAYS_IN_FIRST_WEEK:
((Calendar*)cal)->setMinimalDaysInFirstWeek((uint8_t)newValue);
break;
case UCAL_REPEATED_WALL_TIME:
((Calendar*)cal)->setRepeatedWallTimeOption((UCalendarWallTimeOption)newValue);
break;
case UCAL_SKIPPED_WALL_TIME:
((Calendar*)cal)->setSkippedWallTimeOption((UCalendarWallTimeOption)newValue);
break;
}
}
U_CAPI const char* U_EXPORT2
ucal_getAvailable(int32_t index)
{
return uloc_getAvailable(index);
}
U_CAPI int32_t U_EXPORT2
ucal_countAvailable()
{
return uloc_countAvailable();
}
U_CAPI UDate U_EXPORT2
ucal_getMillis( const UCalendar* cal,
UErrorCode* status)
{
if(U_FAILURE(*status)) return (UDate) 0;
return ((Calendar*)cal)->getTime(*status);
}
U_CAPI void U_EXPORT2
ucal_setMillis( UCalendar* cal,
UDate dateTime,
UErrorCode* status )
{
if(U_FAILURE(*status)) return;
((Calendar*)cal)->setTime(dateTime, *status);
}
// TBD: why does this take an UErrorCode?
U_CAPI void U_EXPORT2
ucal_setDate( UCalendar* cal,
int32_t year,
int32_t month,
int32_t date,
UErrorCode *status)
{
if(U_FAILURE(*status)) return;
((Calendar*)cal)->set(year, month, date);
}
// TBD: why does this take an UErrorCode?
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)
{
if(U_FAILURE(*status)) return;
((Calendar*)cal)->set(year, month, date, hour, minute, second);
}
U_CAPI UBool U_EXPORT2
ucal_equivalentTo( const UCalendar* cal1,
const UCalendar* cal2)
{
return ((Calendar*)cal1)->isEquivalentTo(*((Calendar*)cal2));
}
U_CAPI void U_EXPORT2
ucal_add( UCalendar* cal,
UCalendarDateFields field,
int32_t amount,
UErrorCode* status) UPRV_NO_SANITIZE_UNDEFINED {
if(U_FAILURE(*status)) return;
if (field < 0 || UCAL_FIELD_COUNT <= field) {
*status = U_ILLEGAL_ARGUMENT_ERROR;
return;
}
((Calendar*)cal)->add(field, amount, *status);
}
U_CAPI void U_EXPORT2
ucal_roll( UCalendar* cal,
UCalendarDateFields field,
int32_t amount,
UErrorCode* status) UPRV_NO_SANITIZE_UNDEFINED {
if(U_FAILURE(*status)) return;
if (field < 0 || UCAL_FIELD_COUNT <= field) {
*status = U_ILLEGAL_ARGUMENT_ERROR;
return;
}
((Calendar*)cal)->roll(field, amount, *status);
}
U_CAPI int32_t U_EXPORT2
ucal_get( const UCalendar* cal,
UCalendarDateFields field,
UErrorCode* status ) UPRV_NO_SANITIZE_UNDEFINED {
if(U_FAILURE(*status)) return -1;
if (field < 0 || UCAL_FIELD_COUNT <= field) {
*status = U_ILLEGAL_ARGUMENT_ERROR;
return -1;
}
return ((Calendar*)cal)->get(field, *status);
}
U_CAPI void U_EXPORT2
ucal_set( UCalendar* cal,
UCalendarDateFields field,
int32_t value) UPRV_NO_SANITIZE_UNDEFINED {
if (field < 0 || UCAL_FIELD_COUNT <= field) {
return;
}
((Calendar*)cal)->set(field, value);
}
U_CAPI UBool U_EXPORT2
ucal_isSet( const UCalendar* cal,
UCalendarDateFields field) UPRV_NO_SANITIZE_UNDEFINED {
if (field < 0 || UCAL_FIELD_COUNT <= field) {
return false;
}
return ((Calendar*)cal)->isSet(field);
}
U_CAPI void U_EXPORT2
ucal_clearField( UCalendar* cal,
UCalendarDateFields field) UPRV_NO_SANITIZE_UNDEFINED {
if (field < 0 || UCAL_FIELD_COUNT <= field) {
return;
}
((Calendar*)cal)->clear(field);
}
U_CAPI void U_EXPORT2
ucal_clear(UCalendar* calendar)
{
((Calendar*)calendar)->clear();
}
U_CAPI int32_t U_EXPORT2
ucal_getLimit( const UCalendar* cal,
UCalendarDateFields field,
UCalendarLimitType type,
UErrorCode *status) UPRV_NO_SANITIZE_UNDEFINED {
if (status == nullptr || U_FAILURE(*status)) {
return -1;
}
if (field < 0 || UCAL_FIELD_COUNT <= field) {
*status = U_ILLEGAL_ARGUMENT_ERROR;
return -1;
}
switch(type) {
case UCAL_MINIMUM:
return ((Calendar*)cal)->getMinimum(field);
case UCAL_MAXIMUM:
return ((Calendar*)cal)->getMaximum(field);
case UCAL_GREATEST_MINIMUM:
return ((Calendar*)cal)->getGreatestMinimum(field);
case UCAL_LEAST_MAXIMUM:
return ((Calendar*)cal)->getLeastMaximum(field);
case UCAL_ACTUAL_MINIMUM:
return ((Calendar*)cal)->getActualMinimum(field,
*status);
case UCAL_ACTUAL_MAXIMUM:
return ((Calendar*)cal)->getActualMaximum(field,
*status);
default:
break;
}
return -1;
}
U_CAPI const char * U_EXPORT2
ucal_getLocaleByType(const UCalendar *cal, ULocDataLocaleType type, UErrorCode* status)
{
if (cal == nullptr) {
if (U_SUCCESS(*status)) {
*status = U_ILLEGAL_ARGUMENT_ERROR;
}
return nullptr;
}
return ((Calendar*)cal)->getLocaleID(type, *status);
}
U_CAPI const char * U_EXPORT2
ucal_getTZDataVersion(UErrorCode* status)
{
return TimeZone::getTZDataVersion(*status);
}
U_CAPI int32_t U_EXPORT2
ucal_getCanonicalTimeZoneID(const char16_t* id, int32_t len,
char16_t* result, int32_t resultCapacity, UBool *isSystemID, UErrorCode* status) {
if (status == nullptr || U_FAILURE(*status)) {
return 0;
}
if (isSystemID) {
*isSystemID = false;
}
if (id == nullptr || len == 0 || result == nullptr || resultCapacity <= 0) {
*status = U_ILLEGAL_ARGUMENT_ERROR;
return 0;
}
int32_t reslen = 0;
UnicodeString canonical;
UBool systemID = false;
TimeZone::getCanonicalID(UnicodeString(id, len), canonical, systemID, *status);
if (U_SUCCESS(*status)) {
if (isSystemID) {
*isSystemID = systemID;
}
reslen = canonical.extract(result, resultCapacity, *status);
}
return reslen;
}
U_DRAFT int32_t U_EXPORT2
ucal_getIanaTimeZoneID(const char16_t* id, int32_t len,
char16_t* result, int32_t resultCapacity, UErrorCode* status)
{
UnicodeString ianaID;
TimeZone::getIanaID(UnicodeString(id, len), ianaID, *status);
return ianaID.extract(result, resultCapacity, *status);
}
U_CAPI const char * U_EXPORT2
ucal_getType(const UCalendar *cal, UErrorCode* status)
{
if (U_FAILURE(*status)) {
return nullptr;
}
return ((Calendar*)cal)->getType();
}
U_CAPI UCalendarWeekdayType U_EXPORT2
ucal_getDayOfWeekType(const UCalendar *cal, UCalendarDaysOfWeek dayOfWeek, UErrorCode* status)
{
if (U_FAILURE(*status)) {
return UCAL_WEEKDAY;
}
return ((Calendar*)cal)->getDayOfWeekType(dayOfWeek, *status);
}
U_CAPI int32_t U_EXPORT2
ucal_getWeekendTransition(const UCalendar *cal, UCalendarDaysOfWeek dayOfWeek, UErrorCode *status)
{
if (U_FAILURE(*status)) {
return 0;
}
return ((Calendar*)cal)->getWeekendTransition(dayOfWeek, *status);
}
U_CAPI UBool U_EXPORT2
ucal_isWeekend(const UCalendar *cal, UDate date, UErrorCode *status)
{
if (U_FAILURE(*status)) {
return false;
}
return ((Calendar*)cal)->isWeekend(date, *status);
}
U_CAPI int32_t U_EXPORT2
ucal_getFieldDifference(UCalendar* cal, UDate target,
UCalendarDateFields field,
UErrorCode* status )
{
if (U_FAILURE(*status)) {
return 0;
}
return ((Calendar*)cal)->fieldDifference(target, field, *status);
}
static const UEnumeration defaultKeywordValues = {
nullptr,
nullptr,
ulist_close_keyword_values_iterator,
ulist_count_keyword_values,
uenum_unextDefault,
ulist_next_keyword_value,
ulist_reset_keyword_values_iterator
};
static const char * const CAL_TYPES[] = {
"gregorian",
"japanese",
"buddhist",
"roc",
"persian",
"islamic-civil",
"islamic",
"hebrew",
"chinese",
"indian",
"coptic",
"ethiopic",
"ethiopic-amete-alem",
"iso8601",
"dangi",
"islamic-umalqura",
"islamic-tbla",
"islamic-rgsa",
nullptr
};
U_CAPI UEnumeration* U_EXPORT2
ucal_getKeywordValuesForLocale(const char * /* key */, const char* locale, UBool commonlyUsed, UErrorCode *status) {
// Resolve region
CharString prefRegion = ulocimp_getRegionForSupplementalData(locale, true, *status);
// Read preferred calendar values from supplementalData calendarPreference
UResourceBundle *rb = ures_openDirect(nullptr, "supplementalData", status);
ures_getByKey(rb, "calendarPreferenceData", rb, status);
UResourceBundle *order = ures_getByKey(rb, prefRegion.data(), nullptr, status);
if (*status == U_MISSING_RESOURCE_ERROR && rb != nullptr) {
*status = U_ZERO_ERROR;
order = ures_getByKey(rb, "001", nullptr, status);
}
// Create a list of calendar type strings
UList *values = nullptr;
if (U_SUCCESS(*status)) {
values = ulist_createEmptyList(status);
if (U_SUCCESS(*status)) {
for (int i = 0; i < ures_getSize(order); i++) {
int32_t len;
const char16_t *type = ures_getStringByIndex(order, i, &len, status);
char *caltype = (char*)uprv_malloc(len + 1);
if (caltype == nullptr) {
*status = U_MEMORY_ALLOCATION_ERROR;
break;
}
u_UCharsToChars(type, caltype, len);
*(caltype + len) = 0;
ulist_addItemEndList(values, caltype, true, status);
if (U_FAILURE(*status)) {
break;
}
}
if (U_SUCCESS(*status) && !commonlyUsed) {
// If not commonlyUsed, add other available values
for (int32_t i = 0; CAL_TYPES[i] != nullptr; i++) {
if (!ulist_containsString(values, CAL_TYPES[i], (int32_t)uprv_strlen(CAL_TYPES[i]))) {
ulist_addItemEndList(values, CAL_TYPES[i], false, status);
if (U_FAILURE(*status)) {
break;
}
}
}
}
if (U_FAILURE(*status)) {
ulist_deleteList(values);
values = nullptr;
}
}
}
ures_close(order);
ures_close(rb);
if (U_FAILURE(*status) || values == nullptr) {
return nullptr;
}
// Create string enumeration
UEnumeration *en = (UEnumeration*)uprv_malloc(sizeof(UEnumeration));
if (en == nullptr) {
*status = U_MEMORY_ALLOCATION_ERROR;
ulist_deleteList(values);
return nullptr;
}
ulist_resetList(values);
memcpy(en, &defaultKeywordValues, sizeof(UEnumeration));
en->context = values;
return en;
}
U_CAPI UBool U_EXPORT2
ucal_getTimeZoneTransitionDate(const UCalendar* cal, UTimeZoneTransitionType type,
UDate* transition, UErrorCode* status)
{
if (U_FAILURE(*status)) {
return false;
}
UDate base = ((Calendar*)cal)->getTime(*status);
const TimeZone& tz = ((Calendar*)cal)->getTimeZone();
const BasicTimeZone * btz = dynamic_cast<const BasicTimeZone *>(&tz);
if (btz != nullptr && U_SUCCESS(*status)) {
TimeZoneTransition tzt;
UBool inclusive = (type == UCAL_TZ_TRANSITION_NEXT_INCLUSIVE || type == UCAL_TZ_TRANSITION_PREVIOUS_INCLUSIVE);
UBool result = (type == UCAL_TZ_TRANSITION_NEXT || type == UCAL_TZ_TRANSITION_NEXT_INCLUSIVE)?
btz->getNextTransition(base, inclusive, tzt):
btz->getPreviousTransition(base, inclusive, tzt);
if (result) {
*transition = tzt.getTime();
return true;
}
}
return false;
}
U_CAPI int32_t U_EXPORT2
ucal_getWindowsTimeZoneID(const char16_t* id, int32_t len, char16_t* winid, int32_t winidCapacity, UErrorCode* status) {
if (U_FAILURE(*status)) {
return 0;
}
int32_t resultLen = 0;
UnicodeString resultWinID;
TimeZone::getWindowsID(UnicodeString(id, len), resultWinID, *status);
if (U_SUCCESS(*status) && resultWinID.length() > 0) {
resultLen = resultWinID.length();
resultWinID.extract(winid, winidCapacity, *status);
}
return resultLen;
}
U_CAPI int32_t U_EXPORT2
ucal_getTimeZoneIDForWindowsID(const char16_t* winid, int32_t len, const char* region, char16_t* id, int32_t idCapacity, UErrorCode* status) {
if (U_FAILURE(*status)) {
return 0;
}
int32_t resultLen = 0;
UnicodeString resultID;
TimeZone::getIDForWindowsID(UnicodeString(winid, len), region, resultID, *status);
if (U_SUCCESS(*status) && resultID.length() > 0) {
resultLen = resultID.length();
resultID.extract(id, idCapacity, *status);
}
return resultLen;
}
U_CAPI void U_EXPORT2 ucal_getTimeZoneOffsetFromLocal(
const UCalendar* cal,
UTimeZoneLocalOption nonExistingTimeOpt,
UTimeZoneLocalOption duplicatedTimeOpt,
int32_t* rawOffset, int32_t* dstOffset, UErrorCode* status)
{
if (U_FAILURE(*status)) {
return;
}
UDate date = ((Calendar*)cal)->getTime(*status);
if (U_FAILURE(*status)) {
return;
}
const TimeZone& tz = ((Calendar*)cal)->getTimeZone();
const BasicTimeZone* btz = dynamic_cast<const BasicTimeZone *>(&tz);
if (btz == nullptr) {
*status = U_ILLEGAL_ARGUMENT_ERROR;
return;
}
btz->getOffsetFromLocal(
date, nonExistingTimeOpt, duplicatedTimeOpt,
*rawOffset, *dstOffset, *status);
}
#endif /* #if !UCONFIG_NO_FORMATTING */
|