-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathCFCalendar.c
3012 lines (2850 loc) · 142 KB
/
CFCalendar.c
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
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* CFCalendar.c
Copyright (c) 2004-2019, Apple Inc. and the Swift project authors
Portions Copyright (c) 2014-2019, Apple Inc. and the Swift project authors
Licensed under Apache License v2.0 with Runtime Library Exception
See http://swift.org/LICENSE.txt for license information
See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
*/
#include "CFCalendar.h"
#include "CFCalendarPriv.h"
#include "CFRuntime.h"
#include "CFInternal.h"
#include "CFRuntime_Internal.h"
#include "CFPriv.h"
#include "CFCalendar_Internal.h"
#include "CFConstantKeys.h"
#include "CFICULogging.h"
#include "CFDateInterval.h"
#include <assert.h>
// This avoids having to use a deprecated enum value throughout the implementation
enum {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated"
kCFCalendarUnitWeek_Deprecated = kCFCalendarUnitWeek,
#pragma GCC diagnostic pop
};
// In Swift Foundation, nanoseconds are always available.
#define _CF_CALENDAR_NANOSECONDS_AVAILABLE 1
#define ICU_LOG(FMT, ...) do { } while (0)
#define MIN_CALENDAR_TIME -211845067200.0 // Julian day 0 (-4713-01-01 12:00:00 +0000) in CFAbsoluteTime.
#define MAX_CALENDAR_TIME 15927175497600.0 // 50000-01-01 00:00:00 +0000, smaller than the max time ICU supported.
#define __CFCalendarValidateAndCapTimeRange(at) do { \
if (at < MIN_CALENDAR_TIME || at > MAX_CALENDAR_TIME) { \
os_log_error(_CFOSLog(), "CFAbsoluteTime %lf exceeds calendar calculation range.", at); \
if (at < MIN_CALENDAR_TIME) { at = MIN_CALENDAR_TIME; } \
else { at = MAX_CALENDAR_TIME; } \
} \
} while (0)
extern CFDictionaryRef __CFLocaleGetPrefs(CFLocaleRef locale);
#define MIN_TIMEZONE_UDATE -2177452800000.0 // 1901-01-01 00:00:00 +0000
#define MAX_TIMEZONE_UDATE 4133980800000.0 // 2101-01-01 00:00:00 +0000
CF_PRIVATE Boolean __calcNextDaylightSavingTimeTransition(UCalendar *ucal, UDate start_udate, UDate limit, UDate *answer) {
if (start_udate < MIN_TIMEZONE_UDATE) start_udate = MIN_TIMEZONE_UDATE; // answer question as if this date for anything earlier
if (MAX_TIMEZONE_UDATE < limit) limit = MAX_TIMEZONE_UDATE; // limit gets the smaller
if (limit < start_udate) { // no transitions searched for after the limit arg, or the max time (for performance)
return false;
}
UErrorCode status = U_ZERO_ERROR;
ucal_setMillis(ucal, start_udate, &status);
#if TARGET_OS_WIN32 // TODO: Unify this and CFTimeZoneGetNextDaylightSavingTimeTransition.
UBool b = false;
#else
UBool b = ucal_getTimeZoneTransitionDate(ucal, UCAL_TZ_TRANSITION_NEXT, answer, &status);
#endif
if (!U_SUCCESS(status) || limit < *answer) {
b = false;
}
return b ? true : false;
}
// check if a given time is within the second repeated time frame of time zone transition
// for example, the midnight Oct 1 1978 to 1am in Rome has been repeated twice due to dst transition
// this function returns true for anytime in between the second midnight to the second 1am, excluding the second 1am
// if the function returns true, it will also return the transition point, and the length of the transition
static Boolean __CFCalendarGetTimeRangeOfTimeZoneTransition(CFCalendarRef cal, CFAbsoluteTime at, CFAbsoluteTime *transition, CFTimeInterval *length) {
// if the given time is before 1900, assume there is no dst transition yet
if (at < -3187299600.0) {
return false;
}
UDate ans = 0.0;
UDate start = (at - 48.0 * 60 * 60 + kCFAbsoluteTimeIntervalSince1970) * 1000.0; // start back 48 hours
UErrorCode status = U_ZERO_ERROR;
UDate orig_millis = __cficu_ucal_getMillis(cal->_cal, &status);
Boolean b = __calcNextDaylightSavingTimeTransition(cal->_cal, start, start + 4 * 86400 * 1000.0, &ans);
status = U_ZERO_ERROR;
__cficu_ucal_setMillis(cal->_cal, orig_millis, &status);
CFAbsoluteTime tran = (ans / 1000.0) - kCFAbsoluteTimeIntervalSince1970;
// the transition must be at or before "at" if "at" is within the repeated time frame
if (!b || tran > at) {
return false;
}
// gmt offset includes dst offset
CFTimeInterval preOffset = CFTimeZoneGetSecondsFromGMT(cal->_tz, tran - 1.0);
CFTimeInterval nextOffset = CFTimeZoneGetSecondsFromGMT(cal->_tz, tran + 1.0);
CFTimeInterval diff = preOffset - nextOffset;
// gmt offset before the transition > gmt offset after the transition => backward dst transition
if (diff > 0.0 && at >= tran && at < tran + diff) {
if (transition) *transition = tran;
if (length) *length = diff;
return true;
}
return false;
}
static void __CFCalendarSetToFirstInstant(CFCalendarRef calendar, CFCalendarUnit unit, CFAbsoluteTime at) {
// Set UCalendar to first instant of unit prior to 'at'
UErrorCode status = U_ZERO_ERROR;
UDate udate = floor((at + kCFAbsoluteTimeIntervalSince1970) * 1000.0);
__cficu_ucal_setMillis(calendar->_cal, udate, &status);
int32_t target_era = INT_MIN;
#pragma GCC diagnostic push // See 10693376
#pragma GCC diagnostic ignored "-Wswitch-enum"
switch (unit) { // largest to smallest, we set the fields to their minimum value
case kCFCalendarUnitQuarter: {
// #warning support UCAL_QUARTER better
int32_t month = __cficu_ucal_get(calendar->_cal, UCAL_MONTH, &status);
// int32_t leap = __cficu_ucal_get(calendar->_cal, UCAL_IS_LEAP_MONTH, &status);
if (kCFCalendarIdentifierHebrew == CFCalendarGetIdentifier(calendar)) {
int32_t qmonth[] = {0, 0, 0, 3, 3, 3, 3, 7, 7, 7, 10, 10, 10};
month = qmonth[month];
} else {
// A lunar leap month is considered to be in the same quarter
// that the base month number is in.
int32_t qmonth[] = {0, 0, 0, 3, 3, 3, 6, 6, 6, 9, 9, 9, 9};
month = qmonth[month];
}
// #warning if there is a lunar leap month of the same number *preceding* month N,
// then we should set the calendar to the leap month, not the regular month.
__cficu_ucal_set(calendar->_cal, UCAL_MONTH, month);
__cficu_ucal_set(calendar->_cal, UCAL_IS_LEAP_MONTH, 0);
goto month;
}
case kCFCalendarUnitYearForWeekOfYear:;
__cficu_ucal_set(calendar->_cal, UCAL_WEEK_OF_YEAR, __cficu_ucal_getLimit(calendar->_cal, UCAL_WEEK_OF_YEAR, UCAL_ACTUAL_MINIMUM, &status));
case kCFCalendarUnitWeek_Deprecated:;
case kCFCalendarUnitWeekOfMonth:;
case kCFCalendarUnitWeekOfYear:;
// reduce to first day of week, then reduce the rest of the day
int32_t goal; goal = calendar->_firstWeekday;
int32_t dow; dow = __cficu_ucal_get(calendar->_cal, UCAL_DAY_OF_WEEK, &status);
while (dow != goal) {
// 13161987: we may skip the first day of week if the midnight is missing
__cficu_ucal_add(calendar->_cal, UCAL_DAY_OF_MONTH, -3, &status);
__cficu_ucal_add(calendar->_cal, UCAL_DAY_OF_MONTH, 2, &status);
dow = __cficu_ucal_get(calendar->_cal, UCAL_DAY_OF_WEEK, &status);
}
goto day;
case kCFCalendarUnitEra:
target_era = __cficu_ucal_get(calendar->_cal, UCAL_ERA, &status);
__cficu_ucal_set(calendar->_cal, UCAL_YEAR, __cficu_ucal_getLimit(calendar->_cal, UCAL_YEAR, UCAL_ACTUAL_MINIMUM, &status));
case kCFCalendarUnitYear:
__cficu_ucal_set(calendar->_cal, UCAL_MONTH, __cficu_ucal_getLimit(calendar->_cal, UCAL_MONTH, UCAL_ACTUAL_MINIMUM, &status));
// #warning if there is a lunar leap month of the same number *preceeding* month N,
// then we should set the calendar to the leap month, not the regular month.
__cficu_ucal_set(calendar->_cal, UCAL_IS_LEAP_MONTH, 0);
case kCFCalendarUnitMonth:
month:;
__cficu_ucal_set(calendar->_cal, UCAL_DAY_OF_MONTH, __cficu_ucal_getLimit(calendar->_cal, UCAL_DAY_OF_MONTH, UCAL_ACTUAL_MINIMUM, &status));
case kCFCalendarUnitWeekdayOrdinal:
case kCFCalendarUnitWeekday:
case kCFCalendarUnitDay:
day:;
__cficu_ucal_set(calendar->_cal, UCAL_HOUR_OF_DAY, __cficu_ucal_getLimit(calendar->_cal, UCAL_HOUR_OF_DAY, UCAL_ACTUAL_MINIMUM, &status));
case kCFCalendarUnitHour:
__cficu_ucal_set(calendar->_cal, UCAL_MINUTE, __cficu_ucal_getLimit(calendar->_cal, UCAL_MINUTE, UCAL_ACTUAL_MINIMUM, &status));
case kCFCalendarUnitMinute:
__cficu_ucal_set(calendar->_cal, UCAL_SECOND, __cficu_ucal_getLimit(calendar->_cal, UCAL_SECOND, UCAL_ACTUAL_MINIMUM, &status));
case kCFCalendarUnitSecond:
__cficu_ucal_set(calendar->_cal, UCAL_MILLISECOND, 0);
}
#pragma GCC diagnostic pop // See 10693376
if (INT_MIN != target_era && __cficu_ucal_get(calendar->_cal, UCAL_ERA, &status) < target_era) {
// In the Japanese calendar, and possibly others, eras don't necessarily
// start on the first day of a year, so the previous code may have backed
// up into the previous era, and we have to correct forward.
UDate bad_udate = __cficu_ucal_getMillis(calendar->_cal, &status);
__cficu_ucal_add(calendar->_cal, UCAL_MONTH, 1, &status);
while (__cficu_ucal_get(calendar->_cal, UCAL_ERA, &status) < target_era) {
bad_udate = __cficu_ucal_getMillis(calendar->_cal, &status);
__cficu_ucal_add(calendar->_cal, UCAL_MONTH, 1, &status);
}
udate = __cficu_ucal_getMillis(calendar->_cal, &status);
// target date is between bad_udate and udate
for (;;) {
UDate test_udate = (udate + bad_udate) / 2;
__cficu_ucal_setMillis(calendar->_cal, test_udate, &status);
if (__cficu_ucal_get(calendar->_cal, UCAL_ERA, &status) < target_era) {
bad_udate = test_udate;
} else {
udate = test_udate;
}
if (fabs(udate - bad_udate) < 1000) break;
}
do {
bad_udate = floor((bad_udate + 1000) / 1000) * 1000;
__cficu_ucal_setMillis(calendar->_cal, bad_udate, &status);
} while (__cficu_ucal_get(calendar->_cal, UCAL_ERA, &status) < target_era);
}
if (unit == kCFCalendarUnitDay || unit == kCFCalendarUnitWeekday || unit == kCFCalendarUnitWeekdayOrdinal) {
status = U_ZERO_ERROR;
int32_t targetDay = __cficu_ucal_get(calendar->_cal, UCAL_DAY_OF_MONTH, &status);
int32_t currentDay = targetDay;
do {
udate = __cficu_ucal_getMillis(calendar->_cal, &status);
__cficu_ucal_add(calendar->_cal, UCAL_SECOND, -1, &status);
currentDay = __cficu_ucal_get(calendar->_cal, UCAL_DAY_OF_MONTH, &status);
} while (targetDay == currentDay);
__cficu_ucal_setMillis(calendar->_cal, udate, &status);
}
CFAbsoluteTime start, t;
CFTimeInterval length;
udate = __cficu_ucal_getMillis(calendar->_cal, &status);
start = udate / 1000.0 - kCFAbsoluteTimeIntervalSince1970;
if (__CFCalendarGetTimeRangeOfTimeZoneTransition(calendar, start, &t, &length)) {
udate = (start - length + kCFAbsoluteTimeIntervalSince1970) * 1000.0;
__cficu_ucal_setMillis(calendar->_cal, udate, &status);
}
}
// assume field is within millisecond to hour
static double __CFCalendarTotalSecondsInSmallUnits(CFCalendarRef calendar, UCalendarDateFields field, UErrorCode *status) {
double totalSecond = 0;
if (field == UCAL_MILLISECOND || field == UCAL_MILLISECONDS_IN_DAY) {
return totalSecond;
}
int32_t value = __cficu_ucal_get(calendar->_cal, UCAL_MILLISECOND, status);
totalSecond += (double)value/1000.0;
if (field == UCAL_SECOND) {
return totalSecond;
}
value = __cficu_ucal_get(calendar->_cal, UCAL_SECOND, status);
totalSecond += (double)value;
if (field == UCAL_MINUTE) {
return totalSecond;
}
value = __cficu_ucal_get(calendar->_cal, UCAL_MINUTE, status);
totalSecond += (double)value*60.0;
return totalSecond;
}
// assume the ICU calendar has been initialized
// we rely on ICU to add and roll units which are larger than or equal to DAYs
// we have an assumption which is we assume that there is no time zone with a backward repeated day
// at the time of writing this code, there is only one instance of DST that forwards a day
static UDate __CFCalendarAdd(CFCalendarRef calendar, UCalendarDateFields field, int32_t amount, CFOptionFlags options, UErrorCode *status){
UDate result;
CFOptionFlags roll = options & kCFCalendarComponentsWrap;
if (field == UCAL_MILLISECOND || field == UCAL_SECOND || field == UCAL_MINUTE || field == UCAL_HOUR_OF_DAY ||
field == UCAL_HOUR || field == UCAL_MILLISECONDS_IN_DAY || field == UCAL_AM_PM) {
double unitLength = 0.0;
BOOL keepHourInvariant = NO;
switch (field) {
case UCAL_MILLISECOND:
case UCAL_MILLISECONDS_IN_DAY:
unitLength = 1.0;
break;
case UCAL_MINUTE:
unitLength = 60000.0;
break;
case UCAL_SECOND:
unitLength = 1000.0;
break;
case UCAL_HOUR:
case UCAL_HOUR_OF_DAY:
unitLength = 3600000.0;
break;
case UCAL_AM_PM:
unitLength = 3600000.0 * 12.0;
keepHourInvariant = YES;
break;
default:
break;
}
double leftoverTime = 0.0;
if (roll) {
int32_t min = __cficu_ucal_getLimit(calendar->_cal, field, UCAL_ACTUAL_MINIMUM, status);
int32_t max = __cficu_ucal_getLimit(calendar->_cal, field, UCAL_ACTUAL_MAXIMUM, status);
int32_t gap = max - min + 1;
int32_t originalValue = __cficu_ucal_get(calendar->_cal, field, status);
int32_t finalValue = originalValue + amount;
finalValue = (finalValue - min) % gap;
if (finalValue < 0) {
finalValue += gap;
}
finalValue += min;
if (finalValue < originalValue && amount > 0) {
amount = finalValue;
CFAbsoluteTime at = __cficu_ucal_getMillis(calendar->_cal, status) / 1000.0 - kCFAbsoluteTimeIntervalSince1970;
CFCalendarUnit largeField;
switch (field) {
case UCAL_MILLISECOND:
case UCAL_MILLISECONDS_IN_DAY:
largeField = kCFCalendarUnitSecond;
break;
case UCAL_SECOND:
largeField = kCFCalendarUnitMinute;
break;
case UCAL_MINUTE:
largeField = kCFCalendarUnitHour;
break;
case UCAL_HOUR_OF_DAY:
case UCAL_HOUR:
largeField = kCFCalendarUnitDay;
break;
default:
largeField = kCFCalendarUnitSecond; // Just pick some value
break;
}
leftoverTime = __CFCalendarTotalSecondsInSmallUnits(calendar, field, status);
__CFCalendarSetToFirstInstant(calendar, largeField, at);
} else {
amount = finalValue - originalValue;
}
}
int32_t dst = 0;
int32_t hour = 0;
if (keepHourInvariant) {
dst = __cficu_ucal_get(calendar->_cal, UCAL_DST_OFFSET, status) + __cficu_ucal_get(calendar->_cal, UCAL_ZONE_OFFSET, status);
hour = __cficu_ucal_get(calendar->_cal, UCAL_HOUR_OF_DAY, status);
}
result = __cficu_ucal_getMillis(calendar->_cal, status);
result += (double)amount * unitLength;
result += leftoverTime * 1000.0;
__cficu_ucal_setMillis(calendar->_cal, result, status);
if (keepHourInvariant) {
dst -= __cficu_ucal_get(calendar->_cal, UCAL_DST_OFFSET, status) + __cficu_ucal_get(calendar->_cal, UCAL_ZONE_OFFSET, status);
if (dst != 0) {
result = __cficu_ucal_getMillis(calendar->_cal, status) + dst;
__cficu_ucal_setMillis(calendar->_cal, result, status);
if (__cficu_ucal_get(calendar->_cal, UCAL_HOUR_OF_DAY, status) != hour) {
result -= dst;
__cficu_ucal_setMillis(calendar->_cal, result, status);
}
}
}
} else {
if (roll) {
__cficu_ucal_roll(calendar->_cal, field, amount, status);
} else {
__cficu_ucal_add(calendar->_cal, field, amount, status);
}
CFAbsoluteTime t, start;
CFTimeInterval length;
result = __cficu_ucal_getMillis(calendar->_cal, status);
start = result / 1000.0 - kCFAbsoluteTimeIntervalSince1970;
if (amount > 0 && __CFCalendarGetTimeRangeOfTimeZoneTransition(calendar, start, &t, &length)) {
result = (start - length + kCFAbsoluteTimeIntervalSince1970) * 1000.0;
__cficu_ucal_setMillis(calendar->_cal, result, status);
}
}
return result;
}
static Boolean __CFCalendarEqual(CFTypeRef cf1, CFTypeRef cf2) {
CFCalendarRef calendar1 = (CFCalendarRef)cf1;
CFCalendarRef calendar2 = (CFCalendarRef)cf2;
return CFEqual(calendar1->_identifier, calendar2->_identifier);
}
static CFHashCode __CFCalendarHash(CFTypeRef cf) {
CFCalendarRef calendar = (CFCalendarRef)cf;
return CFHash(calendar->_identifier);
}
static CFStringRef __CFCalendarCopyDescription(CFTypeRef cf) {
CFCalendarRef calendar = (CFCalendarRef)cf;
return CFStringCreateWithFormat(CFGetAllocator(calendar), NULL, CFSTR("<CFCalendar %p [%p]>{identifier = '%@'}"), cf, CFGetAllocator(calendar), calendar->_identifier);
}
static void __CFCalendarDeallocate(CFTypeRef cf) {
CFCalendarRef calendar = (CFCalendarRef)cf;
if (calendar->_identifier) {
CFRelease(calendar->_identifier);
}
if (calendar->_locale) {
CFRelease(calendar->_locale);
}
if (calendar->_tz) {
CFRelease(calendar->_tz);
}
if (calendar->_gregorianStart) {
CFRelease(calendar->_gregorianStart);
}
if (calendar->_cal) {
__cficu_ucal_close(calendar->_cal);
}
}
const CFRuntimeClass __CFCalendarClass = {
0,
"CFCalendar",
NULL, // init
NULL, // copy
__CFCalendarDeallocate,
__CFCalendarEqual,
__CFCalendarHash,
NULL, //
__CFCalendarCopyDescription
};
CFTypeID CFCalendarGetTypeID(void) {
return _kCFRuntimeIDCFCalendar;
}
CF_PRIVATE UCalendar *__CFCalendarCreateUCalendar(CFStringRef calendarID, CFStringRef localeID, CFTimeZoneRef tz) {
if (calendarID) {
CFDictionaryRef components = CFLocaleCreateComponentsFromLocaleIdentifier(kCFAllocatorSystemDefault, localeID);
CFMutableDictionaryRef mcomponents = CFDictionaryCreateMutableCopy(kCFAllocatorSystemDefault, 0, components);
CFDictionarySetValue(mcomponents, kCFLocaleCalendarIdentifierKey, calendarID);
localeID = CFLocaleCreateLocaleIdentifierFromComponents(kCFAllocatorSystemDefault, mcomponents);
CFRelease(mcomponents);
CFRelease(components);
}
const size_t BUFFER_SIZE = 512;
char buffer[BUFFER_SIZE];
const char *cstr = CFStringGetCStringPtr(localeID, kCFStringEncodingASCII);
if (NULL == cstr) {
if (CFStringGetCString(localeID, buffer, BUFFER_SIZE, kCFStringEncodingASCII)) cstr = buffer;
}
if (NULL == cstr) {
if (calendarID) CFRelease(localeID);
return NULL;
}
UChar ubuffer[BUFFER_SIZE];
CFStringRef tznam = CFTimeZoneGetName(tz);
CFIndex cnt = CFStringGetLength(tznam);
if (BUFFER_SIZE < cnt) cnt = BUFFER_SIZE;
CFStringGetCharacters(tznam, CFRangeMake(0, cnt), (UniChar *)ubuffer);
UErrorCode status = U_ZERO_ERROR;
UCalendar *cal = __cficu_ucal_open(ubuffer, cnt, cstr, UCAL_DEFAULT, &status);
if (calendarID) CFRelease(localeID);
return cal;
}
CF_PRIVATE void __CFCalendarSetupCal(CFCalendarRef calendar) {
ICU_LOG(" // __CFCalendarSetupCal enter\n");
calendar->_cal = __CFCalendarCreateUCalendar(calendar->_identifier, CFLocaleGetIdentifier(calendar->_locale), calendar->_tz);
__cficu_ucal_setAttribute(calendar->_cal, UCAL_FIRST_DAY_OF_WEEK, calendar->_firstWeekday);
__cficu_ucal_setAttribute(calendar->_cal, UCAL_MINIMAL_DAYS_IN_FIRST_WEEK, calendar->_minDaysInFirstWeek);
ICU_LOG(" ucal_setAttribute(cal, UCAL_FIRST_DAY_OF_WEEK, %ld);\n", calendar->_firstWeekday);
ICU_LOG(" ucal_setAttribute(cal, UCAL_MINIMAL_DAYS_IN_FIRST_WEEK, %ld);\n", calendar->_minDaysInFirstWeek);
if (calendar->_gregorianStart) {
CFAbsoluteTime at = CFDateGetAbsoluteTime(calendar->_gregorianStart);
UDate udate = (at + kCFAbsoluteTimeIntervalSince1970) * 1000.0;
UErrorCode status = U_ZERO_ERROR;
__cficu_ucal_setGregorianChange(calendar->_cal, udate, &status);
ICU_LOG(" UErrorCode status = U_ZERO_ERROR;\n");
ICU_LOG(" UDate udate = %.06f;\n", udate);
ICU_LOG(" ucal_setGregorianChange(cal, udate, &status);\n");
}
ICU_LOG(" // __CFCalendarSetupCal exit\n");
}
CF_PRIVATE Boolean _CFCalendarIsDateInWeekend(CFCalendarRef calendar, CFDateRef date) {
if (calendar->_cal == NULL) {
__CFCalendarSetupCal(calendar);
}
CFAbsoluteTime at = CFDateGetAbsoluteTime(date);
UDate udate = (at + kCFAbsoluteTimeIntervalSince1970) * 1000.0;
UErrorCode status = U_ZERO_ERROR;
UBool result = __cficu_ucal_isWeekend(calendar->_cal, udate, &status);
return result;
}
CF_PRIVATE Boolean _CFCalendarGetNextWeekend(CFCalendarRef calendar, _CFCalendarWeekendRange *range) {
// TODO: Double check this logic vs that in _NSCFCalendar
CFIndex weekdaysIndex[7];
memset(weekdaysIndex, '\0', sizeof(CFIndex)*7);
CFIndex firstWeekday = CFCalendarGetFirstWeekday(calendar);
weekdaysIndex[0] = firstWeekday;
for (CFIndex i = 1; i < 7; i++) {
weekdaysIndex[i] = (weekdaysIndex[i-1] % 7) + 1;
}
UCalendarWeekdayType weekdayTypes[7];
CFIndex onset = kCFNotFound;
CFIndex cease = kCFNotFound;
if (!calendar->_cal) __CFCalendarSetupCal(calendar);
if (!calendar->_cal) {
return false;
}
for (CFIndex i = 0; i < 7; i++) {
UErrorCode status = U_ZERO_ERROR;
weekdayTypes[i] = ucal_getDayOfWeekType(calendar->_cal, (UCalendarDaysOfWeek)weekdaysIndex[i], &status);
if (weekdayTypes[i] == UCAL_WEEKEND_ONSET) {
onset = weekdaysIndex[i];
} else if (weekdayTypes[i] == UCAL_WEEKEND_CEASE) {
cease = weekdaysIndex[i];
}
}
BOOL hasWeekend = NO;
for (CFIndex i = 0; i < 7; i++) {
if (weekdayTypes[i] == UCAL_WEEKEND || weekdayTypes[i] == UCAL_WEEKEND_ONSET || weekdayTypes[i] == UCAL_WEEKEND_CEASE) {
hasWeekend = YES;
break;
}
}
if (!hasWeekend) {
return false;
}
int32_t onsetTime = 0;
int32_t ceaseTime = 0;
if (onset != kCFNotFound) {
UErrorCode status = U_ZERO_ERROR;
onsetTime = ucal_getWeekendTransition(calendar->_cal, (UCalendarDaysOfWeek)onset, &status);
}
if (cease != kCFNotFound) {
UErrorCode status = U_ZERO_ERROR;
ceaseTime = ucal_getWeekendTransition(calendar->_cal, (UCalendarDaysOfWeek)cease, &status);
}
CFIndex weekendStart = kCFNotFound;
CFIndex weekendEnd = kCFNotFound;
if (onset != kCFNotFound) {
weekendStart = onset;
} else {
if (weekdayTypes[0] == UCAL_WEEKEND && weekdayTypes[6] == UCAL_WEEKEND) {
for (CFIndex i = 5; i >= 0; i--) {
if (weekdayTypes[i] != UCAL_WEEKEND) {
weekendStart = weekdaysIndex[i + 1];
break;
}
}
} else {
for (CFIndex i = 0; i < 7; i++) {
if (weekdayTypes[i] == UCAL_WEEKEND) {
weekendStart = weekdaysIndex[i];
break;
}
}
}
}
if (cease != kCFNotFound) {
weekendEnd = cease;
} else {
if (weekdayTypes[0] == UCAL_WEEKEND && weekdayTypes[6] == UCAL_WEEKEND) {
for (CFIndex i = 1; i < 7; i++) {
if (weekdayTypes[i] != UCAL_WEEKEND) {
weekendEnd = weekdaysIndex[i - 1];
break;
}
}
} else {
for (CFIndex i = 6; i >= 0; i--) {
if (weekdayTypes[i] == UCAL_WEEKEND) {
weekendEnd = weekdaysIndex[i];
break;
}
}
}
}
range->onsetTime = onsetTime / 1000.0;
range->ceaseTime = ceaseTime / 1000.0;
range->start = weekendStart;
range->end = weekendEnd;
// TODO: NSCalendar.m has additional logic here but it depends on stuff not yet ported to this C implementation
return true;
}
CF_PRIVATE void __CFCalendarZapCal(CFCalendarRef calendar) {
if (calendar->_cal) __cficu_ucal_close(calendar->_cal);
calendar->_cal = NULL;
ICU_LOG(" if (cal) ucal_close(cal);\n");
}
// Applies user-editable settings (first weekday, minimum days in first week) from the given locale onto the given calendar without applying the locale onto the calendar.
static void __CFCalendarApplyUserSettingsFromLocale(CFCalendarRef calendar, CFLocaleRef locale) {
CFDictionaryRef prefs = __CFLocaleGetPrefs(locale);
if (prefs && !calendar->_userSet_firstWeekday) {
CFPropertyListRef metapref = (CFPropertyListRef)CFDictionaryGetValue(prefs, CFSTR("AppleFirstWeekday"));
if (NULL != metapref && CFGetTypeID(metapref) == CFDictionaryGetTypeID()) {
metapref = (CFNumberRef)CFDictionaryGetValue((CFDictionaryRef)metapref, calendar->_identifier);
}
if (NULL != metapref && CFGetTypeID(metapref) == CFNumberGetTypeID()) {
CFIndex wkdy;
if (CFNumberGetValue((CFNumberRef)metapref, kCFNumberCFIndexType, &wkdy)) {
calendar->_firstWeekday = wkdy;
if (calendar->_cal) {
__cficu_ucal_setAttribute(calendar->_cal, UCAL_FIRST_DAY_OF_WEEK, wkdy);
ICU_LOG(" ucal_setAttribute(cal, UCAL_FIRST_DAY_OF_WEEK, %ld);\n", wkdy);
}
}
}
}
if (prefs && !calendar->_userSet_minDaysInFirstWeek) {
CFPropertyListRef metapref = (CFPropertyListRef)CFDictionaryGetValue(prefs, CFSTR("AppleMinDaysInFirstWeek"));
if (NULL != metapref && CFGetTypeID(metapref) == CFDictionaryGetTypeID()) {
metapref = (CFNumberRef)CFDictionaryGetValue((CFDictionaryRef)metapref, calendar->_identifier);
}
if (NULL != metapref && CFGetTypeID(metapref) == CFNumberGetTypeID()) {
CFIndex mwd;
if (CFNumberGetValue((CFNumberRef)metapref, kCFNumberCFIndexType, &mwd)) {
calendar->_minDaysInFirstWeek = mwd;
if (calendar->_cal) {
__cficu_ucal_setAttribute(calendar->_cal, UCAL_MINIMAL_DAYS_IN_FIRST_WEEK, mwd);
ICU_LOG(" ucal_setAttribute(cal, UCAL_MINIMAL_DAYS_IN_FIRST_WEEK, %ld);\n", mwd);
}
}
}
}
}
static bool _CFCalendarInitialize(CFCalendarRef calendar, CFAllocatorRef allocator, CFStringRef identifier, CFTimeZoneRef tz, CFLocaleRef locale, CFIndex firstDayOfWeek, CFIndex minDaysInFirstWeek, CFDateRef gregorianStartDate) {
ICU_LOG(" // CFCalendarCreateWithIdentifier enter\n");
if (allocator == NULL) allocator = __CFGetDefaultAllocator();
__CFGenericValidateType(allocator, CFAllocatorGetTypeID());
__CFGenericValidateType(identifier, CFStringGetTypeID());
CFStringRef canonicalIdent = NULL;
if (CFEqual(kCFCalendarIdentifierGregorian, identifier)) canonicalIdent = kCFCalendarIdentifierGregorian;
else if (CFEqual(kCFCalendarIdentifierJapanese, identifier)) canonicalIdent = kCFCalendarIdentifierJapanese;
else if (CFEqual(kCFCalendarIdentifierBuddhist, identifier)) canonicalIdent = kCFCalendarIdentifierBuddhist;
else if (CFEqual(kCFCalendarIdentifierIslamic, identifier)) canonicalIdent = kCFCalendarIdentifierIslamic;
else if (CFEqual(kCFCalendarIdentifierIslamicCivil, identifier)) canonicalIdent = kCFCalendarIdentifierIslamicCivil;
else if (CFEqual(kCFCalendarIdentifierHebrew, identifier)) canonicalIdent = kCFCalendarIdentifierHebrew;
else if (CFEqual(kCFCalendarIdentifierRepublicOfChina, identifier)) canonicalIdent = kCFCalendarIdentifierRepublicOfChina;
else if (CFEqual(kCFCalendarIdentifierPersian, identifier)) canonicalIdent = kCFCalendarIdentifierPersian;
else if (CFEqual(kCFCalendarIdentifierIndian, identifier)) canonicalIdent = kCFCalendarIdentifierIndian;
else if (CFEqual(kCFCalendarIdentifierCoptic, identifier)) canonicalIdent = kCFCalendarIdentifierCoptic;
else if (CFEqual(kCFCalendarIdentifierEthiopicAmeteMihret, identifier)) canonicalIdent = kCFCalendarIdentifierEthiopicAmeteMihret;
else if (CFEqual(kCFCalendarIdentifierEthiopicAmeteAlem, identifier)) canonicalIdent = kCFCalendarIdentifierEthiopicAmeteAlem;
else if (CFEqual(kCFCalendarIdentifierChinese, identifier)) canonicalIdent = kCFCalendarIdentifierChinese;
else if (CFEqual(kCFCalendarIdentifierISO8601, identifier)) canonicalIdent = kCFCalendarIdentifierISO8601;
else if (CFEqual(kCFCalendarIdentifierIslamicTabular, identifier)) canonicalIdent = kCFCalendarIdentifierIslamicTabular;
else if (CFEqual(kCFCalendarIdentifierIslamicUmmAlQura, identifier)) canonicalIdent = kCFCalendarIdentifierIslamicUmmAlQura;
if (!canonicalIdent) ICU_LOG(" // CFCalendarCreateWithIdentifier exit NULL 1\n");
if (!canonicalIdent) return false;
calendar->_identifier = (CFStringRef)CFRetain(canonicalIdent);
calendar->_locale = locale ? CFLocaleCreateCopy(allocator, locale) : CFRetain(CFLocaleGetSystem());
calendar->_tz = tz ? CFRetain(tz) : CFTimeZoneCopyDefault();
calendar->_cal = __CFCalendarCreateUCalendar(calendar->_identifier, CFLocaleGetIdentifier(calendar->_locale), calendar->_tz);
if (!calendar->_cal) {
ICU_LOG(" // CFCalendarCreateWithIdentifier exit NULL 3\n");
return false;
}
calendar->_firstWeekday = firstDayOfWeek == kCFNotFound ? __cficu_ucal_getAttribute(calendar->_cal, UCAL_FIRST_DAY_OF_WEEK) : firstDayOfWeek;
calendar->_minDaysInFirstWeek = minDaysInFirstWeek == kCFNotFound ? __cficu_ucal_getAttribute(calendar->_cal, UCAL_MINIMAL_DAYS_IN_FIRST_WEEK) : minDaysInFirstWeek;
ICU_LOG(" int32_t firstWeekday = ucal_getAttribute(cal, UCAL_FIRST_DAY_OF_WEEK);\n");
ICU_LOG(" int32_t minDaysInFirstWeek = ucal_getAttribute(cal, UCAL_MINIMAL_DAYS_IN_FIRST_WEEK);\n");
if (kCFCalendarIdentifierGregorian == calendar->_identifier) {
ICU_LOG(" UErrorCode status = U_ZERO_ERROR;\n");
ICU_LOG(" UDate udate = ucal_getGregorianChange(cal, &status);\n");
ICU_LOG(" CFAbsoluteTime gregorianStart = U_SUCCESS(status) ? (udate / 1000.0 - kCFAbsoluteTimeIntervalSince1970) : -13197600000.0;\n");
CFAbsoluteTime at = 0;
if (gregorianStartDate) {
at = CFDateGetAbsoluteTime(gregorianStartDate);
calendar->_gregorianStart = CFRetain(gregorianStartDate);
} else {
UErrorCode status = U_ZERO_ERROR;
UDate udate = __cficu_ucal_getGregorianChange(calendar->_cal, &status);
at = U_SUCCESS(status) ? (udate / 1000.0 - kCFAbsoluteTimeIntervalSince1970) : -13197600000.0; // Oct 15, 1582
calendar->_gregorianStart = CFDateCreate(CFGetAllocator(calendar), at);
}
UErrorCode status = U_ZERO_ERROR;
UDate udate = (at + kCFAbsoluteTimeIntervalSince1970) * 1000.0;
__cficu_ucal_setGregorianChange(calendar->_cal, udate, &status);
}
calendar->_userSet_firstWeekday = false;
calendar->_userSet_minDaysInFirstWeek = false;
calendar->_userSet_gregorianStart = false;
ICU_LOG(" // CFCalendarCreateWithIdentifier exit\n");
return true;
}
static CFCalendarRef _CFCalendarCreate(CFAllocatorRef allocator, CFStringRef identifier, CFTimeZoneRef tz, CFLocaleRef locale, CFIndex firstDayOfWeek, CFIndex minDaysInFirstWeek, CFDateRef gregorianStartDate) {
if (allocator == NULL) allocator = __CFGetDefaultAllocator();
__CFGenericValidateType(allocator, CFAllocatorGetTypeID());
struct __CFCalendar *calendar = NULL;
size_t size = sizeof(struct __CFCalendar) - sizeof(CFRuntimeBase);
calendar = (struct __CFCalendar *)_CFRuntimeCreateInstance(allocator, CFCalendarGetTypeID(), size, NULL);
if (NULL == calendar) {
ICU_LOG(" // CFCalendarCreateWithIdentifier exit NULL 2\n");
return NULL;
}
if (!_CFCalendarInitialize(calendar, allocator, identifier, tz, locale, firstDayOfWeek, minDaysInFirstWeek, gregorianStartDate)) {
// _CFCalendarInitialize will have already logged which exit path was taken.
CFRelease(calendar);
return NULL;
}
return calendar;
}
CFCalendarRef CFCalendarCopyCurrent(void) {
CFLocaleRef locale = CFLocaleCopyCurrent();
CFStringRef calID = (CFStringRef)CFLocaleGetValue(locale, kCFLocaleCalendarIdentifierKey);
if (calID) {
CFCalendarRef calendar = _CFCalendarCreate(kCFAllocatorSystemDefault, calID, NULL, locale, kCFNotFound, kCFNotFound, NULL);
// `calendar` has the default first weekday and other locale settings set on it.
// We need to explicitly apply the settings from the locale without creating a new copy of it (via `CFCalendarSetLocale`).
__CFCalendarApplyUserSettingsFromLocale(calendar, locale);
CFRelease(locale);
return calendar;
} else if(locale) {
CFRelease(locale);
}
return NULL;
}
CFCalendarRef CFCalendarCreateWithIdentifier(CFAllocatorRef allocator, CFStringRef identifier) {
return _CFCalendarCreate(allocator, identifier, NULL, NULL, kCFNotFound, kCFNotFound, NULL);
}
CF_EXPORT Boolean _CFCalendarInitWithIdentifier(CFCalendarRef calendar, CFStringRef identifier) {
return _CFCalendarInitialize(calendar, kCFAllocatorSystemDefault, identifier, NULL, NULL, kCFNotFound, kCFNotFound, NULL) ? true : false;
}
CFCalendarRef _CFCalendarCreateCopy(CFAllocatorRef allocator, CFCalendarRef calendar) {
//We should probably just conditionally call -copyWithZone: here but I'm concerned that it could expose incorrect third party subclasses that have just happened to get away with it until now
Boolean isObjC = CF_IS_OBJC(_kCFRuntimeIDCFCalendar, calendar);
CFCalendarRef result = NULL;
if (isObjC) {
CFTimeZoneRef tz = CFCalendarCopyTimeZone(calendar);
CFLocaleRef locale = CFCalendarCopyLocale(calendar);
CFDateRef gsd = CFCalendarCopyGregorianStartDate(calendar);
CFIndex firstWeekday = CFCalendarGetFirstWeekday(calendar);
CFIndex minDaysInFirstWeek = CFCalendarGetMinimumDaysInFirstWeek(calendar);
//Do not attempt to refactor _CFCalendarCreate to take fewer arguments with the idea of using the setter functions instead, the setters also set the _userSet_* flags
result = _CFCalendarCreate(allocator, CFCalendarGetIdentifier(calendar), tz, locale, firstWeekday, minDaysInFirstWeek, gsd);
if (tz) CFRelease(tz);
if (locale) CFRelease(locale);
if (gsd) CFRelease(gsd);
} else {
result = _CFCalendarCreate(allocator, calendar->_identifier, calendar->_tz, calendar->_locale, calendar->_firstWeekday, calendar->_minDaysInFirstWeek, calendar->_gregorianStart);
}
return result;
}
CFStringRef CFCalendarGetIdentifier(CFCalendarRef calendar) {
CF_OBJC_FUNCDISPATCHV(_kCFRuntimeIDCFCalendar, CFStringRef, (NSCalendar *)calendar, calendarIdentifier);
__CFGenericValidateType(calendar, CFCalendarGetTypeID());
return calendar->_identifier;
}
CFLocaleRef CFCalendarCopyLocale(CFCalendarRef calendar) {
CF_OBJC_FUNCDISPATCHV(_kCFRuntimeIDCFCalendar, CFLocaleRef, (NSCalendar *)calendar, _copyLocale);
__CFGenericValidateType(calendar, CFCalendarGetTypeID());
return CFLocaleCreateCopy(CFGetAllocator(calendar->_locale), calendar->_locale);
}
void CFCalendarSetLocale(CFCalendarRef calendar, CFLocaleRef locale) {
ICU_LOG(" // CFCalendarSetLocale enter\n");
CF_OBJC_FUNCDISPATCHV(_kCFRuntimeIDCFCalendar, void, (NSCalendar *)calendar, setLocale:(NSLocale *)locale);
__CFGenericValidateType(calendar, CFCalendarGetTypeID());
__CFGenericValidateType(locale, CFLocaleGetTypeID());
if (locale && (locale != calendar->_locale)) {
CFRelease(calendar->_locale);
calendar->_locale = CFLocaleCreateCopy(CFGetAllocator(calendar), locale);
if (calendar->_cal) __cficu_ucal_close(calendar->_cal);
ICU_LOG(" if (cal) ucal_close(cal);\n");
// do NOT use __CFCalendarSetupCal here
calendar->_cal = __CFCalendarCreateUCalendar(calendar->_identifier, CFLocaleGetIdentifier(calendar->_locale), calendar->_tz);
if (!calendar->_cal) HALT;
if (!calendar->_userSet_firstWeekday) {
calendar->_firstWeekday = __cficu_ucal_getAttribute(calendar->_cal, UCAL_FIRST_DAY_OF_WEEK);
ICU_LOG(" int32_t firstWeekday = ucal_getAttribute(cal, UCAL_FIRST_DAY_OF_WEEK);\n");
__cficu_ucal_setAttribute(calendar->_cal, UCAL_FIRST_DAY_OF_WEEK, calendar->_firstWeekday);
} else {
__cficu_ucal_setAttribute(calendar->_cal, UCAL_FIRST_DAY_OF_WEEK, calendar->_firstWeekday);
ICU_LOG(" ucal_setAttribute(cal, UCAL_FIRST_DAY_OF_WEEK, %ld);\n", calendar->_firstWeekday);
}
if (!calendar->_userSet_minDaysInFirstWeek) {
calendar->_minDaysInFirstWeek = __cficu_ucal_getAttribute(calendar->_cal, UCAL_MINIMAL_DAYS_IN_FIRST_WEEK);
ICU_LOG(" int32_t minDaysInFirstWeek = ucal_getAttribute(cal, UCAL_MINIMAL_DAYS_IN_FIRST_WEEK);\n");
__cficu_ucal_setAttribute(calendar->_cal, UCAL_MINIMAL_DAYS_IN_FIRST_WEEK, calendar->_minDaysInFirstWeek);
} else {
__cficu_ucal_setAttribute(calendar->_cal, UCAL_MINIMAL_DAYS_IN_FIRST_WEEK, calendar->_minDaysInFirstWeek);
ICU_LOG(" ucal_setAttribute(cal, UCAL_MINIMAL_DAYS_IN_FIRST_WEEK, %ld);\n", calendar->_minDaysInFirstWeek);
}
if (!calendar->_userSet_gregorianStart && calendar->_gregorianStart) {
CFRelease(calendar->_gregorianStart);
UErrorCode status = U_ZERO_ERROR;
UDate udate = __cficu_ucal_getGregorianChange(calendar->_cal, &status);
CFAbsoluteTime at = U_SUCCESS(status) ? (udate / 1000.0 - kCFAbsoluteTimeIntervalSince1970) : -13197600000.0; // Oct 15, 1582
calendar->_gregorianStart = CFDateCreate(CFGetAllocator(calendar), at);
udate = (at + kCFAbsoluteTimeIntervalSince1970) * 1000.0;
status = U_ZERO_ERROR;
__cficu_ucal_setGregorianChange(calendar->_cal, udate, &status);
ICU_LOG(" UErrorCode status = U_ZERO_ERROR;\n");
ICU_LOG(" UDate udate = ucal_getGregorianChange(cal, &status);\n");
ICU_LOG(" CFAbsoluteTime gregorianStart = U_SUCCESS(status) ? (udate / 1000.0 - kCFAbsoluteTimeIntervalSince1970) : -13197600000.0;\n");
} else if (calendar->_gregorianStart) {
CFAbsoluteTime at = CFDateGetAbsoluteTime(calendar->_gregorianStart);
UDate udate = (at + kCFAbsoluteTimeIntervalSince1970) * 1000.0;
UErrorCode status = U_ZERO_ERROR;
if (calendar->_cal) __cficu_ucal_setGregorianChange(calendar->_cal, udate, &status);
ICU_LOG(" CFAbsoluteTime at = %.06f;\n", at);
ICU_LOG(" UDate udate = (at + kCFAbsoluteTimeIntervalSince1970) * 1000.0;\n");
ICU_LOG(" UErrorCode status = U_ZERO_ERROR;\n");
ICU_LOG(" if (cal) ucal_setGregorianChange(cal, udate, &status);\n");
}
__CFCalendarApplyUserSettingsFromLocale(calendar, locale);
}
ICU_LOG(" // CFCalendarSetLocale exit\n");
}
CFTimeZoneRef CFCalendarCopyTimeZone(CFCalendarRef calendar) {
CF_OBJC_FUNCDISPATCHV(_kCFRuntimeIDCFCalendar, CFTimeZoneRef, (NSCalendar *)calendar, _copyTimeZone);
__CFGenericValidateType(calendar, CFCalendarGetTypeID());
return (CFTimeZoneRef)CFRetain(calendar->_tz);
}
void CFCalendarSetTimeZone(CFCalendarRef calendar, CFTimeZoneRef tz) {
ICU_LOG(" // CFCalendarSetTimeZone enter\n");
CF_OBJC_FUNCDISPATCHV(_kCFRuntimeIDCFCalendar, void, (NSCalendar *)calendar, setTimeZone:(NSTimeZone *)tz);
__CFGenericValidateType(calendar, CFCalendarGetTypeID());
if (tz) __CFGenericValidateType(tz, CFTimeZoneGetTypeID());
if (tz != calendar->_tz) {
if (calendar->_tz) CFRelease(calendar->_tz);
calendar->_tz = tz ? (CFTimeZoneRef)CFRetain(tz) : CFTimeZoneCopyDefault();
if (calendar->_cal) __CFCalendarZapCal(calendar);
}
ICU_LOG(" // CFCalendarSetTimeZone exit\n");
}
CFIndex CFCalendarGetFirstWeekday(CFCalendarRef calendar) {
CF_OBJC_FUNCDISPATCHV(_kCFRuntimeIDCFCalendar, CFIndex, (NSCalendar *)calendar, firstWeekday);
__CFGenericValidateType(calendar, CFCalendarGetTypeID());
return calendar->_firstWeekday;
}
void CFCalendarSetFirstWeekday(CFCalendarRef calendar, CFIndex wkdy) {
ICU_LOG(" // CFCalendarSetFirstWeekday enter\n");
CF_OBJC_FUNCDISPATCHV(_kCFRuntimeIDCFCalendar, void, (NSCalendar *)calendar, setFirstWeekday:(NSUInteger)wkdy);
__CFGenericValidateType(calendar, CFCalendarGetTypeID());
calendar->_firstWeekday = wkdy;
if (calendar->_cal) {
__cficu_ucal_setAttribute(calendar->_cal, UCAL_FIRST_DAY_OF_WEEK, wkdy);
ICU_LOG(" ucal_setAttribute(cal, UCAL_FIRST_DAY_OF_WEEK, %ld);\n", wkdy);
}
calendar->_userSet_firstWeekday = true;
ICU_LOG(" // CFCalendarSetFirstWeekday exit\n");
}
CFIndex CFCalendarGetMinimumDaysInFirstWeek(CFCalendarRef calendar) {
CF_OBJC_FUNCDISPATCHV(_kCFRuntimeIDCFCalendar, CFIndex, (NSCalendar *)calendar, minimumDaysInFirstWeek);
__CFGenericValidateType(calendar, CFCalendarGetTypeID());
return calendar->_minDaysInFirstWeek;
}
void CFCalendarSetMinimumDaysInFirstWeek(CFCalendarRef calendar, CFIndex mwd) {
ICU_LOG(" // CFCalendarSetMinimumDaysInFirstWeek enter\n");
CF_OBJC_FUNCDISPATCHV(_kCFRuntimeIDCFCalendar, void, (NSCalendar *)calendar, setMinimumDaysInFirstWeek:(NSUInteger)mwd);
__CFGenericValidateType(calendar, CFCalendarGetTypeID());
calendar->_minDaysInFirstWeek = mwd;
if (calendar->_cal) {
__cficu_ucal_setAttribute(calendar->_cal, UCAL_MINIMAL_DAYS_IN_FIRST_WEEK, mwd);
ICU_LOG(" ucal_setAttribute(cal, UCAL_MINIMAL_DAYS_IN_FIRST_WEEK, %ld);\n", mwd);
}
calendar->_userSet_minDaysInFirstWeek = true;
ICU_LOG(" // CFCalendarSetMinimumDaysInFirstWeek exit\n");
}
CFDateRef CFCalendarCopyGregorianStartDate(CFCalendarRef calendar) {
CF_OBJC_FUNCDISPATCHV(_kCFRuntimeIDCFCalendar, CFDateRef, (NSCalendar *)calendar, _copyGregorianStartDate);
__CFGenericValidateType(calendar, CFCalendarGetTypeID());
return calendar->_gregorianStart ? (CFDateRef)CFRetain(calendar->_gregorianStart) : NULL;
}
void CFCalendarSetGregorianStartDate(CFCalendarRef calendar, CFDateRef _Nullable date) {
ICU_LOG(" // CFCalendarSetGregorianStartDate enter\n");
CF_OBJC_FUNCDISPATCHV(_kCFRuntimeIDCFCalendar, void, (NSCalendar *)calendar, _setGregorianStartDate:(NSDate *)date);
__CFGenericValidateType(calendar, CFCalendarGetTypeID());
if (date) __CFGenericValidateType(date, CFDateGetTypeID());
if (calendar->_gregorianStart) CFRelease(calendar->_gregorianStart);
calendar->_gregorianStart = NULL;
if (!date && (kCFCalendarIdentifierGregorian == calendar->_identifier)) {
UErrorCode status = U_ZERO_ERROR;
ICU_LOG(" UErrorCode status = U_ZERO_ERROR;\n");
UCalendar *cal = __CFCalendarCreateUCalendar(calendar->_identifier, CFLocaleGetIdentifier(calendar->_locale), calendar->_tz);
UDate udate = cal ? __cficu_ucal_getGregorianChange(cal, &status) : 0;
CFAbsoluteTime at;
if (cal && U_SUCCESS(status)) {
at = udate / 1000.0 - kCFAbsoluteTimeIntervalSince1970;
} else {
at = -13197600000.0; // Oct 15, 1582
udate = (at + kCFAbsoluteTimeIntervalSince1970) * 1000.0;
}
ICU_LOG(" UDate udate = cal ? ucal_getGregorianChange(cal, &status) : 0;\n");
ICU_LOG(" CFAbsoluteTime gregorianStart = (cal && U_SUCCESS(status)) ? udate / 1000.0 - kCFAbsoluteTimeIntervalSince1970 : -13197600000.0;\n");
ICU_LOG(" if (!(cal && U_SUCCESS(status))) { udate = (at + kCFAbsoluteTimeIntervalSince1970) * 1000.0; }\n");
calendar->_gregorianStart = CFDateCreate(CFGetAllocator(calendar), at);
status = U_ZERO_ERROR;
if (calendar->_cal) __cficu_ucal_setGregorianChange(calendar->_cal, udate, &status);
if (cal) __cficu_ucal_close(cal);
ICU_LOG(" status = U_ZERO_ERROR;\n");
ICU_LOG(" if (cal) ucal_setGregorianChange(cal, udate, &status);\n");
ICU_LOG(" if (cal) ucal_close(cal);\n");
calendar->_userSet_gregorianStart = false;
} else if (kCFCalendarIdentifierGregorian == calendar->_identifier) {
calendar->_gregorianStart = (CFDateRef)CFRetain(date);
CFAbsoluteTime at = CFDateGetAbsoluteTime(date);
UDate udate = (at + kCFAbsoluteTimeIntervalSince1970) * 1000.0;
ICU_LOG(" CFAbsoluteTime at = %.06f;\n", at);
ICU_LOG(" UDate udate = (at + kCFAbsoluteTimeIntervalSince1970) * 1000.0;\n");
UErrorCode status = U_ZERO_ERROR;
if (calendar->_cal) __cficu_ucal_setGregorianChange(calendar->_cal, udate, &status);
ICU_LOG(" UErrorCode status = U_ZERO_ERROR;\n");
ICU_LOG(" if (cal) ucal_setGregorianChange(cal, udate, &status);\n");
calendar->_userSet_gregorianStart = true;
}
ICU_LOG(" // CFCalendarSetGregorianStartDate exit\n");
}
// #warning workaround for 5718919
#define UCAL_QUARTER 4444
#define UCAL_FIELD_ERROR 9999
static UCalendarDateFields __CFCalendarGetICUFieldCode(CFCalendarUnit unit) {
switch (unit) {
case kCFCalendarUnitEra: return UCAL_ERA;
case kCFCalendarUnitYear: return UCAL_YEAR;
case kCFCalendarUnitQuarter: return (UCalendarDateFields)UCAL_QUARTER;
case kCFCalendarUnitMonth: return UCAL_MONTH;
case kCFCalendarUnitDay: return UCAL_DAY_OF_MONTH;
case kCFCalendarUnitHour: return UCAL_HOUR_OF_DAY;
case kCFCalendarUnitMinute: return UCAL_MINUTE;
case kCFCalendarUnitSecond: return UCAL_SECOND;
case kCFCalendarUnitWeek_Deprecated: return UCAL_WEEK_OF_YEAR;
case kCFCalendarUnitWeekOfYear: return UCAL_WEEK_OF_YEAR;
case kCFCalendarUnitWeekOfMonth: return UCAL_WEEK_OF_MONTH;
case kCFCalendarUnitYearForWeekOfYear: return UCAL_YEAR_WOY;
case kCFCalendarUnitWeekday: return UCAL_DAY_OF_WEEK;
case kCFCalendarUnitWeekdayOrdinal: return UCAL_DAY_OF_WEEK_IN_MONTH;
}
return (UCalendarDateFields)UCAL_FIELD_ERROR;
}
static CFCalendarUnit __CFCalendarUnitFromICUDateFields(UCalendarDateFields field) {
switch (field) {
case UCAL_ERA: return kCFCalendarUnitEra;
case UCAL_YEAR: return kCFCalendarUnitYear;
case UCAL_MONTH: return kCFCalendarUnitMonth;
case UCAL_WEEK_OF_YEAR: return kCFCalendarUnitWeekOfYear;
case UCAL_WEEK_OF_MONTH: return kCFCalendarUnitWeekOfMonth;
case UCAL_DATE: return kCFCalendarUnitDay; // same value as UCAL_DAY_OF_MONTH
case UCAL_DAY_OF_YEAR: return kCFCalendarUnitDay;
case UCAL_DAY_OF_WEEK: return kCFCalendarUnitWeekday;
case UCAL_DAY_OF_WEEK_IN_MONTH: return kCFCalendarUnitWeekdayOrdinal;
case UCAL_HOUR: return kCFCalendarUnitHour;
case UCAL_HOUR_OF_DAY: return kCFCalendarUnitHour;
case UCAL_MINUTE: return kCFCalendarUnitMinute;
case UCAL_SECOND: return kCFCalendarUnitSecond;
case UCAL_ZONE_OFFSET: return kCFCalendarUnitTimeZone;
case UCAL_YEAR_WOY: return kCFCalendarUnitYearForWeekOfYear;
case UCAL_IS_LEAP_MONTH: return kCFCalendarUnitLeapMonth;
case UCAL_EXTENDED_YEAR: return kCFCalendarUnitYear;
}
return 0;
}
static UCalendarDateFields __CFCalendarGetICUFieldCodeFromChar(char ch) {
switch (ch) {
case 'G': return UCAL_ERA;
case 'y': return UCAL_YEAR;
case 'U': return UCAL_YEAR;
case 'r': return UCAL_YEAR;
case 'M': return UCAL_MONTH;
case 'L': return UCAL_MONTH;
case 'l': return UCAL_IS_LEAP_MONTH;
case 'd': return UCAL_DAY_OF_MONTH;
case 'h': return UCAL_HOUR;
case 'H': return UCAL_HOUR_OF_DAY;
case 'm': return UCAL_MINUTE;
case 's': return UCAL_SECOND;
case 'S': return UCAL_MILLISECOND;
case '^': return UCAL_WEEK_OF_YEAR;
case 'w': return UCAL_WEEK_OF_YEAR;
case 'W': return UCAL_WEEK_OF_MONTH;
case 'Y': return UCAL_YEAR_WOY;
case 'E': return UCAL_DAY_OF_WEEK;
case 'c': return UCAL_DAY_OF_WEEK;
case 'D': return UCAL_DAY_OF_YEAR;