forked from opensource-apple/CF
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CFDateFormatter.c
2001 lines (1882 loc) · 104 KB
/
CFDateFormatter.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
/*
* Copyright (c) 2015 Apple Inc. All rights reserved.
*
* @APPLE_LICENSE_HEADER_START@
*
* This file contains Original Code and/or Modifications of Original Code
* as defined in and that are subject to the Apple Public Source License
* Version 2.0 (the 'License'). You may not use this file except in
* compliance with the License. Please obtain a copy of the License at
* http://www.opensource.apple.com/apsl/ and read it before using this
* file.
*
* The Original Code and all software distributed under the License are
* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
* Please see the License for the specific language governing rights and
* limitations under the License.
*
* @APPLE_LICENSE_HEADER_END@
*/
/* CFDateFormatter.c
Copyright (c) 2002-2014, Apple Inc. All rights reserved.
Responsibility: David Smith
*/
#define U_SHOW_INTERNAL_API 1
#include <CoreFoundation/CFDateFormatter.h>
#include <CoreFoundation/CFDate.h>
#include <CoreFoundation/CFTimeZone.h>
#include <CoreFoundation/CFCalendar.h>
#include <CoreFoundation/CFNumber.h>
#include "CFPriv.h"
#include "CFInternal.h"
#include "CFLocaleInternal.h"
#include "CFICULogging.h"
#include <math.h>
#include <float.h>
typedef CF_ENUM(CFIndex, CFDateFormatterAmbiguousYearHandling) {
kCFDateFormatterAmbiguousYearFailToParse = 0, // fail the parse; the default formatter behavior
kCFDateFormatterAmbiguousYearAssumeToNone = 1, // default to assuming era 1, or the year 0-99
kCFDateFormatterAmbiguousYearAssumeToCurrent = 2, // default to assuming the current century or era
kCFDateFormatterAmbiguousYearAssumeToCenteredAroundCurrentDate = 3,
kCFDateFormatterAmbiguousYearAssumeToFuture = 4,
kCFDateFormatterAmbiguousYearAssumeToPast = 5,
kCFDateFormatterAmbiguousYearAssumeToLikelyFuture = 6,
kCFDateFormatterAmbiguousYearAssumeToLikelyPast = 7
};
extern UCalendar *__CFCalendarCreateUCalendar(CFStringRef calendarID, CFStringRef localeID, CFTimeZoneRef tz);
static CONST_STRING_DECL(kCFDateFormatterFormattingContextKey, "kCFDateFormatterFormattingContextKey");
CF_EXPORT const CFStringRef kCFDateFormatterCalendarIdentifierKey;
#undef CFReleaseIfNotNull
#define CFReleaseIfNotNull(X) if (X) CFRelease(X)
#define BUFFER_SIZE 768
static CFStringRef __CFDateFormatterCreateForcedTemplate(CFLocaleRef locale, CFStringRef inString, Boolean stripAMPM);
// If you pass in a string in tmplate, you get back NULL (failure) or a CFStringRef.
// If you pass in an array in tmplate, you get back NULL (global failure) or a CFArrayRef with CFStringRefs or kCFNulls (per-template failure) at each corresponding index.
CFArrayRef CFDateFormatterCreateDateFormatsFromTemplates(CFAllocatorRef allocator, CFArrayRef tmplates, CFOptionFlags options, CFLocaleRef locale) {
return (CFArrayRef)CFDateFormatterCreateDateFormatFromTemplate(allocator, (CFStringRef)tmplates, options, locale);
}
static Boolean useTemplatePatternGenerator(CFLocaleRef locale, void(^work)(UDateTimePatternGenerator *ptg)) {
static UDateTimePatternGenerator *ptg;
static pthread_mutex_t ptgLock = PTHREAD_MUTEX_INITIALIZER;
static const char *ptgLocaleName;
CFStringRef ln = locale ? CFLocaleGetIdentifier(locale) : CFSTR("");
char buffer[BUFFER_SIZE];
const char *localeName = CFStringGetCStringPtr(ln, kCFStringEncodingASCII);
if (NULL == localeName) {
if (CFStringGetCString(ln, buffer, BUFFER_SIZE, kCFStringEncodingASCII)) localeName = buffer;
}
static void (^flushCache)() = ^{
__cficu_udatpg_close(ptg);
ptg = NULL;
free((void *)ptgLocaleName);
ptgLocaleName = NULL;
};
pthread_mutex_lock(&ptgLock);
if (ptgLocaleName && strcmp(ptgLocaleName, localeName) != 0) {
flushCache();
}
UErrorCode status = U_ZERO_ERROR;
if (!ptg) {
ptg = __cficu_udatpg_open(localeName, &status);
if (ptg && !U_FAILURE(status)) {
ptgLocaleName = strdup(localeName);
}
}
Boolean result = (NULL != ptg && !U_FAILURE(status));
if (result && work) {
work(ptg);
}
pthread_mutex_unlock(&ptgLock);
return result;
}
/*
1) Scan the string for an AM/PM indicator
2) Back up past any spaces in front of the AM/PM indicator
3) As long as the current character is whitespace, or an 'a', remove it and shift everything past it down
*/
static void _CFDateFormatterStripAMPMIndicators(UniChar **bpat, int32_t *bpatlen, CFIndex bufferSize) {
//scan
for (CFIndex idx = 0; idx < *bpatlen; idx++) {
if ((*bpat)[idx] == 'a') {
//back up
while ((*bpat)[idx - 1] == ' ') {
idx--;
}
//shift
for (; (*bpat)[idx] == ' ' || (*bpat)[idx] == 'a'; idx++) {
for (CFIndex shiftIdx = idx; shiftIdx < *bpatlen && shiftIdx + 1 < bufferSize; shiftIdx++) {
(*bpat)[shiftIdx] = (*bpat)[shiftIdx + 1];
}
//compensate for the character we just removed
(*bpatlen)--;
idx--;
}
}
}
}
CFStringRef CFDateFormatterCreateDateFormatFromTemplate(CFAllocatorRef allocator, CFStringRef tmplate, CFOptionFlags options, CFLocaleRef locale) {
if (allocator) __CFGenericValidateType(allocator, CFAllocatorGetTypeID());
if (locale) __CFGenericValidateType(locale, CFLocaleGetTypeID());
Boolean tmplateIsString = (CFStringGetTypeID() == CFGetTypeID(tmplate));
if (!tmplateIsString) {
__CFGenericValidateType(tmplate, CFArrayGetTypeID());
}
__block CFTypeRef result = tmplateIsString ? NULL : (CFTypeRef)CFArrayCreateMutable(allocator, 0, &kCFTypeArrayCallBacks);
Boolean success = useTemplatePatternGenerator(locale, ^(UDateTimePatternGenerator *ptg) {
for (CFIndex idx = 0, cnt = tmplateIsString ? 1 : CFArrayGetCount((CFArrayRef)tmplate); idx < cnt; idx++) {
CFStringRef tmplateString = tmplateIsString ? (CFStringRef)tmplate : (CFStringRef)CFArrayGetValueAtIndex((CFArrayRef)tmplate, idx);
CFStringRef resultString = NULL;
Boolean stripAMPM = CFStringFind(tmplateString, CFSTR("J"), 0).location != kCFNotFound;
tmplateString = __CFDateFormatterCreateForcedTemplate(locale ? locale : CFLocaleGetSystem(), tmplateString, stripAMPM);
CFIndex jCount = 0; // the only interesting cases are 0, 1, and 2 (adjacent)
UniChar adjacentJs[2] = {-1, -1};
CFRange r = CFStringFind(tmplateString, CFSTR("j"), kCFCompareCaseInsensitive);
if (kCFNotFound != r.location) {
adjacentJs[0] = CFStringGetCharacterAtIndex(tmplateString, r.location);
jCount++;
if ((r.location + 1 < CFStringGetLength(tmplateString)) && ('j' == CFStringGetCharacterAtIndex(tmplateString, r.location + 1) || 'J' == CFStringGetCharacterAtIndex(tmplateString, r.location + 1))) {
jCount++;
adjacentJs[1] = CFStringGetCharacterAtIndex(tmplateString, r.location + 1);
}
}
UChar pattern[BUFFER_SIZE] = {0}, skel[BUFFER_SIZE] = {0}, bpat[BUFFER_SIZE] = {0};
CFIndex tmpltLen = CFStringGetLength(tmplateString);
if (BUFFER_SIZE < tmpltLen) tmpltLen = BUFFER_SIZE;
CFStringGetCharacters(tmplateString, CFRangeMake(0, tmpltLen), (UniChar *)pattern);
CFRelease(tmplateString);
int32_t patlen = tmpltLen;
UErrorCode status = U_ZERO_ERROR;
int32_t skellen = __cficu_udatpg_getSkeleton(ptg, pattern, patlen, skel, sizeof(skel) / sizeof(skel[0]), &status);
if (!U_FAILURE(status)) {
if ((0 < jCount) && (skellen + jCount < (sizeof(skel) / sizeof(skel[0])))) {
skel[skellen++] = 'j'; //adjacentJs[0];
if (1 < jCount) skel[skellen++] = 'j'; //adjacentJs[1];
//stripAMPM = false; //'J' will take care of it. We only need to do it manually if we stripped the Js out ourselves while forcing 12/24 hour time
}
status = U_ZERO_ERROR;
int32_t bpatlen = __cficu_udatpg_getBestPattern(ptg, skel, skellen, bpat, sizeof(bpat) / sizeof(bpat[0]), &status);
if (!U_FAILURE(status)) {
if (stripAMPM) {
UniChar *bpatptr = (UniChar *)bpat;
_CFDateFormatterStripAMPMIndicators(&bpatptr, &bpatlen, BUFFER_SIZE);
}
resultString = CFStringCreateWithCharacters(allocator, (const UniChar *)bpat, bpatlen);
}
}
if (tmplateIsString) {
result = (CFTypeRef)resultString;
} else {
CFArrayAppendValue((CFMutableArrayRef)result, resultString ? (CFTypeRef)resultString : (CFTypeRef)kCFNull);
if (resultString) CFRelease(resultString);
}
}
});
if (!success) {
if (result) CFRelease(result);
result = NULL;
}
return (CFStringRef)result;
}
struct __CFDateFormatter {
CFRuntimeBase _base;
UDateFormat *_df;
CFLocaleRef _locale;
CFDateFormatterStyle _timeStyle;
CFDateFormatterStyle _dateStyle;
CFStringRef _format;
CFStringRef _defformat;
struct {
CFBooleanRef _IsLenient;
CFBooleanRef _DoesRelativeDateFormatting;
CFBooleanRef _HasCustomFormat;
CFTimeZoneRef _TimeZone;
CFCalendarRef _Calendar;
CFStringRef _CalendarName;
CFDateRef _TwoDigitStartDate;
CFDateRef _DefaultDate;
CFDateRef _GregorianStartDate;
CFArrayRef _EraSymbols;
CFArrayRef _LongEraSymbols;
CFArrayRef _MonthSymbols;
CFArrayRef _ShortMonthSymbols;
CFArrayRef _VeryShortMonthSymbols;
CFArrayRef _StandaloneMonthSymbols;
CFArrayRef _ShortStandaloneMonthSymbols;
CFArrayRef _VeryShortStandaloneMonthSymbols;
CFArrayRef _WeekdaySymbols;
CFArrayRef _ShortWeekdaySymbols;
CFArrayRef _VeryShortWeekdaySymbols;
CFArrayRef _StandaloneWeekdaySymbols;
CFArrayRef _ShortStandaloneWeekdaySymbols;
CFArrayRef _VeryShortStandaloneWeekdaySymbols;
CFArrayRef _QuarterSymbols;
CFArrayRef _ShortQuarterSymbols;
CFArrayRef _StandaloneQuarterSymbols;
CFArrayRef _ShortStandaloneQuarterSymbols;
CFStringRef _AMSymbol;
CFStringRef _PMSymbol;
CFNumberRef _AmbiguousYearStrategy;
CFBooleanRef _UsesCharacterDirection;
CFNumberRef _FormattingContext;
// the following are from preferences
CFArrayRef _CustomEraSymbols;
CFArrayRef _CustomLongEraSymbols;
CFArrayRef _CustomMonthSymbols;
CFArrayRef _CustomShortMonthSymbols;
CFArrayRef _CustomVeryShortMonthSymbols;
CFArrayRef _CustomStandaloneMonthSymbols;
CFArrayRef _CustomShortStandaloneMonthSymbols;
CFArrayRef _CustomVeryShortStandaloneMonthSymbols;
CFArrayRef _CustomWeekdaySymbols;
CFArrayRef _CustomShortWeekdaySymbols;
CFArrayRef _CustomVeryShortWeekdaySymbols;
CFArrayRef _CustomStandaloneWeekdaySymbols;
CFArrayRef _CustomShortStandaloneWeekdaySymbols;
CFArrayRef _CustomVeryShortStandaloneWeekdaySymbols;
CFArrayRef _CustomQuarterSymbols;
CFArrayRef _CustomShortQuarterSymbols;
CFArrayRef _CustomStandaloneQuarterSymbols;
CFArrayRef _CustomShortStandaloneQuarterSymbols;
CFStringRef _CustomDateFormat;
CFStringRef _CustomTimeFormat;
CFBooleanRef _Custom24Hour;
CFBooleanRef _Custom12Hour;
CFStringRef _CustomAMSymbol;
CFStringRef _CustomPMSymbol;
CFDictionaryRef _CustomFirstWeekday;
CFDictionaryRef _CustomMinDaysInFirstWeek;
} _property;
};
static CFStringRef __CFDateFormatterCopyDescription(CFTypeRef cf) {
CFDateFormatterRef formatter = (CFDateFormatterRef)cf;
return CFStringCreateWithFormat(CFGetAllocator(formatter), NULL, CFSTR("<CFDateFormatter %p [%p]>"), cf, CFGetAllocator(formatter));
}
static void __CFDateFormatterDeallocate(CFTypeRef cf) {
CFDateFormatterRef formatter = (CFDateFormatterRef)cf;
if (formatter->_df) __cficu_udat_close(formatter->_df);
if (formatter->_locale) CFRelease(formatter->_locale);
if (formatter->_format) CFRelease(formatter->_format);
if (formatter->_defformat) CFRelease(formatter->_defformat);
CFReleaseIfNotNull(formatter->_property._IsLenient);
CFReleaseIfNotNull(formatter->_property._DoesRelativeDateFormatting);
CFReleaseIfNotNull(formatter->_property._TimeZone);
CFReleaseIfNotNull(formatter->_property._Calendar);
CFReleaseIfNotNull(formatter->_property._CalendarName);
CFReleaseIfNotNull(formatter->_property._TwoDigitStartDate);
CFReleaseIfNotNull(formatter->_property._DefaultDate);
CFReleaseIfNotNull(formatter->_property._GregorianStartDate);
CFReleaseIfNotNull(formatter->_property._EraSymbols);
CFReleaseIfNotNull(formatter->_property._LongEraSymbols);
CFReleaseIfNotNull(formatter->_property._MonthSymbols);
CFReleaseIfNotNull(formatter->_property._ShortMonthSymbols);
CFReleaseIfNotNull(formatter->_property._VeryShortMonthSymbols);
CFReleaseIfNotNull(formatter->_property._StandaloneMonthSymbols);
CFReleaseIfNotNull(formatter->_property._ShortStandaloneMonthSymbols);
CFReleaseIfNotNull(formatter->_property._VeryShortStandaloneMonthSymbols);
CFReleaseIfNotNull(formatter->_property._WeekdaySymbols);
CFReleaseIfNotNull(formatter->_property._ShortWeekdaySymbols);
CFReleaseIfNotNull(formatter->_property._VeryShortWeekdaySymbols);
CFReleaseIfNotNull(formatter->_property._StandaloneWeekdaySymbols);
CFReleaseIfNotNull(formatter->_property._ShortStandaloneWeekdaySymbols);
CFReleaseIfNotNull(formatter->_property._VeryShortStandaloneWeekdaySymbols);
CFReleaseIfNotNull(formatter->_property._QuarterSymbols);
CFReleaseIfNotNull(formatter->_property._ShortQuarterSymbols);
CFReleaseIfNotNull(formatter->_property._StandaloneQuarterSymbols);
CFReleaseIfNotNull(formatter->_property._ShortStandaloneQuarterSymbols);
CFReleaseIfNotNull(formatter->_property._AMSymbol);
CFReleaseIfNotNull(formatter->_property._PMSymbol);
CFReleaseIfNotNull(formatter->_property._AmbiguousYearStrategy);
CFReleaseIfNotNull(formatter->_property._UsesCharacterDirection);
CFReleaseIfNotNull(formatter->_property._FormattingContext);
CFReleaseIfNotNull(formatter->_property._CustomEraSymbols);
CFReleaseIfNotNull(formatter->_property._CustomMonthSymbols);
CFReleaseIfNotNull(formatter->_property._CustomShortMonthSymbols);
CFReleaseIfNotNull(formatter->_property._CustomWeekdaySymbols);
CFReleaseIfNotNull(formatter->_property._CustomShortWeekdaySymbols);
CFReleaseIfNotNull(formatter->_property._CustomLongEraSymbols);
CFReleaseIfNotNull(formatter->_property._CustomVeryShortMonthSymbols);
CFReleaseIfNotNull(formatter->_property._CustomVeryShortWeekdaySymbols);
CFReleaseIfNotNull(formatter->_property._CustomStandaloneMonthSymbols);
CFReleaseIfNotNull(formatter->_property._CustomShortStandaloneMonthSymbols);
CFReleaseIfNotNull(formatter->_property._CustomVeryShortStandaloneMonthSymbols);
CFReleaseIfNotNull(formatter->_property._CustomStandaloneWeekdaySymbols);
CFReleaseIfNotNull(formatter->_property._CustomShortStandaloneWeekdaySymbols);
CFReleaseIfNotNull(formatter->_property._CustomVeryShortStandaloneWeekdaySymbols);
CFReleaseIfNotNull(formatter->_property._CustomQuarterSymbols);
CFReleaseIfNotNull(formatter->_property._CustomShortQuarterSymbols);
CFReleaseIfNotNull(formatter->_property._CustomShortStandaloneQuarterSymbols);
CFReleaseIfNotNull(formatter->_property._CustomDateFormat);
CFReleaseIfNotNull(formatter->_property._CustomTimeFormat);
CFReleaseIfNotNull(formatter->_property._Custom24Hour);
CFReleaseIfNotNull(formatter->_property._Custom12Hour);
CFReleaseIfNotNull(formatter->_property._CustomAMSymbol);
CFReleaseIfNotNull(formatter->_property._CustomPMSymbol);
CFReleaseIfNotNull(formatter->_property._CustomFirstWeekday);
CFReleaseIfNotNull(formatter->_property._CustomMinDaysInFirstWeek);
}
static CFStringRef __CFDateFormatterCreateForcedString(CFDateFormatterRef formatter, CFStringRef inString);
static void __CFDateFormatterSetProperty(CFDateFormatterRef formatter, CFStringRef key, CFTypeRef value, Boolean directToICU);
static void __CFDateFormatterStoreSymbolPrefs(const void *key, const void *value, void *context);
extern CFDictionaryRef __CFLocaleGetPrefs(CFLocaleRef locale);
static void __substituteFormatStringFromPrefsDFRelative(CFDateFormatterRef formatter);
static void __substituteFormatStringFromPrefsDF(CFDateFormatterRef formatter, bool doTime);
static void __CFDateFormatterSetSymbolsArray(UDateFormat *icudf, int32_t icucode, int index_base, CFTypeRef value);
static void __ReadCustomUDateFormatProperty(CFDateFormatterRef formatter) {
CFDictionaryRef prefs = __CFLocaleGetPrefs(formatter->_locale);
CFPropertyListRef metapref = prefs ? CFDictionaryGetValue(prefs, CFSTR("AppleICUDateTimeSymbols")) : NULL;
if (NULL != metapref && CFGetTypeID(metapref) == CFDictionaryGetTypeID()) {
CFDictionaryApplyFunction((CFDictionaryRef)metapref, __CFDateFormatterStoreSymbolPrefs, formatter);
}
metapref = prefs ? CFDictionaryGetValue(prefs, CFSTR("AppleFirstWeekday")) : NULL;
if (NULL != metapref && CFGetTypeID(metapref) == CFDictionaryGetTypeID()) {
formatter->_property._CustomFirstWeekday = (CFDictionaryRef)CFRetain(metapref);
}
metapref = prefs ? CFDictionaryGetValue(prefs, CFSTR("AppleMinDaysInFirstWeek")) : NULL;
if (NULL != metapref && CFGetTypeID(metapref) == CFDictionaryGetTypeID()) {
formatter->_property._CustomMinDaysInFirstWeek = (CFDictionaryRef)CFRetain(metapref);
}
metapref = prefs ? CFDictionaryGetValue(prefs, CFSTR("AppleICUForce24HourTime")) : NULL;
if (NULL != metapref && CFGetTypeID(metapref) == CFBooleanGetTypeID()) {
formatter->_property._Custom24Hour = (CFBooleanRef)CFRetain(metapref);
}
metapref = prefs ? CFDictionaryGetValue(prefs, CFSTR("AppleICUForce12HourTime")) : NULL;
if (NULL != metapref && CFGetTypeID(metapref) == CFBooleanGetTypeID()) {
formatter->_property._Custom12Hour = (CFBooleanRef)CFRetain(metapref);
}
metapref = prefs ? CFDictionaryGetValue(prefs, CFSTR("AppleICUDateFormatStrings")) : NULL;
if (NULL != metapref && CFGetTypeID(metapref) == CFDictionaryGetTypeID()) {
CFStringRef key;
switch (formatter->_dateStyle) {
case kCFDateFormatterShortStyle: key = CFSTR("1"); break;
case kCFDateFormatterMediumStyle: key = CFSTR("2"); break;
case kCFDateFormatterLongStyle: key = CFSTR("3"); break;
case kCFDateFormatterFullStyle: key = CFSTR("4"); break;
default: key = CFSTR("0"); break;
}
CFStringRef dateFormat = (CFStringRef)CFDictionaryGetValue((CFDictionaryRef)metapref, key);
if (NULL != dateFormat && CFGetTypeID(dateFormat) == CFStringGetTypeID()) {
formatter->_property._CustomDateFormat = (CFStringRef)CFRetain(dateFormat);
}
}
metapref = prefs ? CFDictionaryGetValue(prefs, CFSTR("AppleICUTimeFormatStrings")) : NULL;
if (NULL != metapref && CFGetTypeID(metapref) == CFDictionaryGetTypeID()) {
CFStringRef key;
switch (formatter->_timeStyle) {
case kCFDateFormatterShortStyle: key = CFSTR("1"); break;
case kCFDateFormatterMediumStyle: key = CFSTR("2"); break;
case kCFDateFormatterLongStyle: key = CFSTR("3"); break;
case kCFDateFormatterFullStyle: key = CFSTR("4"); break;
default: key = CFSTR("0"); break;
}
CFStringRef timeFormat = (CFStringRef)CFDictionaryGetValue((CFDictionaryRef)metapref, key);
if (NULL != timeFormat && CFGetTypeID(timeFormat) == CFStringGetTypeID()) {
formatter->_property._CustomTimeFormat = (CFStringRef)CFRetain(timeFormat);
}
}
}
static void __ApplyUDateFormatSymbol(CFDateFormatterRef formatter) {
UDateFormatSymbolType types[18] = {UDAT_ERAS,
UDAT_ERA_NAMES,
UDAT_MONTHS,
UDAT_SHORT_MONTHS,
UDAT_NARROW_MONTHS,
UDAT_STANDALONE_MONTHS,
UDAT_STANDALONE_SHORT_MONTHS,
UDAT_STANDALONE_NARROW_MONTHS,
UDAT_WEEKDAYS,
UDAT_SHORT_WEEKDAYS,
UDAT_NARROW_WEEKDAYS,
UDAT_STANDALONE_WEEKDAYS,
UDAT_STANDALONE_SHORT_WEEKDAYS,
UDAT_STANDALONE_NARROW_WEEKDAYS,
UDAT_QUARTERS,
UDAT_SHORT_QUARTERS,
UDAT_STANDALONE_QUARTERS,
UDAT_STANDALONE_SHORT_QUARTERS};
int offsets[18] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0};
CFArrayRef symbols[18] = {formatter->_property._EraSymbols,
formatter->_property._LongEraSymbols,
formatter->_property._MonthSymbols,
formatter->_property._ShortMonthSymbols,
formatter->_property._VeryShortMonthSymbols,
formatter->_property._StandaloneMonthSymbols,
formatter->_property._ShortStandaloneMonthSymbols,
formatter->_property._VeryShortStandaloneMonthSymbols,
formatter->_property._WeekdaySymbols,
formatter->_property._ShortWeekdaySymbols,
formatter->_property._VeryShortWeekdaySymbols,
formatter->_property._StandaloneWeekdaySymbols,
formatter->_property._ShortStandaloneWeekdaySymbols,
formatter->_property._VeryShortStandaloneWeekdaySymbols,
formatter->_property._QuarterSymbols,
formatter->_property._ShortQuarterSymbols,
formatter->_property._StandaloneQuarterSymbols,
formatter->_property._ShortStandaloneQuarterSymbols
};
CFArrayRef customSymbols[18] = {formatter->_property._CustomEraSymbols,
formatter->_property._CustomLongEraSymbols,
formatter->_property._CustomMonthSymbols,
formatter->_property._CustomShortMonthSymbols,
formatter->_property._CustomVeryShortMonthSymbols,
formatter->_property._CustomStandaloneMonthSymbols,
formatter->_property._CustomShortStandaloneMonthSymbols,
formatter->_property._CustomVeryShortStandaloneMonthSymbols,
formatter->_property._CustomWeekdaySymbols,
formatter->_property._CustomShortWeekdaySymbols,
formatter->_property._CustomVeryShortWeekdaySymbols,
formatter->_property._CustomStandaloneWeekdaySymbols,
formatter->_property._CustomShortStandaloneWeekdaySymbols,
formatter->_property._CustomVeryShortStandaloneWeekdaySymbols,
formatter->_property._CustomQuarterSymbols,
formatter->_property._CustomShortQuarterSymbols,
formatter->_property._CustomStandaloneQuarterSymbols,
formatter->_property._CustomShortStandaloneQuarterSymbols
};
for (CFIndex i = 0; i < 18; i++) {
if (symbols[i] != NULL) {
__CFDateFormatterSetSymbolsArray(formatter->_df, types[i], offsets[i], symbols[i]);
} else if (customSymbols[i] != NULL) {
__CFDateFormatterSetSymbolsArray(formatter->_df, types[i], offsets[i], customSymbols[i]);
}
}
CFStringRef ampm[2];
ampm[0] = NULL;
ampm[1] = NULL;
if (formatter->_property._AMSymbol != NULL) {
ampm[0] = formatter->_property._AMSymbol;
} else if (formatter->_property._CustomAMSymbol != NULL) {
ampm[0] = formatter->_property._CustomAMSymbol;
}
if (formatter->_property._PMSymbol != NULL) {
ampm[1] = formatter->_property._PMSymbol;
} else if (formatter->_property._CustomPMSymbol != NULL) {
ampm[1] = formatter->_property._CustomPMSymbol;
}
for (CFIndex i = 0; i < 2; i++) {
CFStringRef sym = ampm[i];
if (sym != NULL) {
CFIndex item_cnt = CFStringGetLength(sym);
STACK_BUFFER_DECL(UChar, item_buffer, __CFMin(BUFFER_SIZE, item_cnt));
UChar *item_ustr = (UChar *)CFStringGetCharactersPtr(sym);
if (NULL == item_ustr) {
item_cnt = __CFMin(BUFFER_SIZE, item_cnt);
CFStringGetCharacters(sym, CFRangeMake(0, item_cnt), (UniChar *)item_buffer);
item_ustr = item_buffer;
}
UErrorCode status = U_ZERO_ERROR;
__cficu_udat_setSymbols(formatter->_df, UDAT_AM_PMS, i, item_ustr, item_cnt, &status);
}
}
}
static void __SetCalendarProperties(CFDateFormatterRef df) {
CFStringRef calName = df->_property._CalendarName ? (df->_property._CalendarName) : NULL;
if (!calName) {
calName = (CFStringRef)CFLocaleGetValue(df->_locale, kCFLocaleCalendarIdentifierKey);
}
UErrorCode status = U_ZERO_ERROR;
const UCalendar *cal = __cficu_udat_getCalendar(df->_df);
UCalendar *new_cal = NULL;
if (df->_property._Calendar != NULL || df->_property._CalendarName != NULL) {
UCalendar *caltmp = __CFCalendarCreateUCalendar(NULL, CFLocaleGetIdentifier(df->_locale), df->_property._TimeZone);
if (caltmp) {
new_cal = caltmp;
}
}
if (new_cal == NULL) {
new_cal = __cficu_ucal_clone(cal, &status);
}
if (df->_property._IsLenient != NULL) {
status = U_ZERO_ERROR;
CFBooleanRef value = df->_property._IsLenient;
__cficu_ucal_setAttribute(new_cal, UCAL_LENIENT, (kCFBooleanTrue == value));
}
if (df->_property._TimeZone != NULL) {
status = U_ZERO_ERROR;
UChar ubuffer[BUFFER_SIZE];
CFStringRef tznam = CFTimeZoneGetName(df->_property._TimeZone);
CFIndex ucnt = CFStringGetLength(tznam);
if (BUFFER_SIZE < ucnt) ucnt = BUFFER_SIZE;
CFStringGetCharacters(tznam, CFRangeMake(0, ucnt), (UniChar *)ubuffer);
__cficu_ucal_setTimeZone(new_cal, ubuffer, ucnt, &status);
}
if (df->_property._GregorianStartDate != NULL) {
status = U_ZERO_ERROR;
CFAbsoluteTime at = CFDateGetAbsoluteTime((CFDateRef)df->_property._GregorianStartDate);
UDate udate = (at + kCFAbsoluteTimeIntervalSince1970) * 1000.0;
__cficu_ucal_setGregorianChange(new_cal, udate, &status);
} else if (calName && CFEqual(calName, kCFCalendarIdentifierGregorian)) {
status = U_ZERO_ERROR;
UDate udate = __cficu_ucal_getGregorianChange(cal, &status);
CFAbsoluteTime at = U_SUCCESS(status) ? (udate / 1000.0 - kCFAbsoluteTimeIntervalSince1970) : -13197600000.0; // Oct 15, 1582
udate = (at + kCFAbsoluteTimeIntervalSince1970) * 1000.0;
status = U_ZERO_ERROR;
__cficu_ucal_setGregorianChange(new_cal, udate, &status);
}
if (df->_property._Calendar != NULL) {
__cficu_ucal_setAttribute(new_cal, UCAL_FIRST_DAY_OF_WEEK, CFCalendarGetFirstWeekday((CFCalendarRef)df->_property._Calendar));
} else if (df->_property._CustomFirstWeekday != NULL) {
CFNumberRef firstWeekday = (CFNumberRef)CFDictionaryGetValue(df->_property._CustomFirstWeekday, calName);
if (NULL != firstWeekday && CFGetTypeID(firstWeekday) == CFNumberGetTypeID()) {
CFIndex wkdy;
if (CFNumberGetValue((CFNumberRef)firstWeekday, kCFNumberCFIndexType, &wkdy)) {
status = U_ZERO_ERROR;
__cficu_ucal_setAttribute(new_cal, UCAL_FIRST_DAY_OF_WEEK, wkdy);
}
}
}
if (df->_property._Calendar != NULL) {
__cficu_ucal_setAttribute(new_cal, UCAL_MINIMAL_DAYS_IN_FIRST_WEEK, CFCalendarGetMinimumDaysInFirstWeek((CFCalendarRef)df->_property._Calendar));
} else if (df->_property._CustomMinDaysInFirstWeek != NULL) {
CFNumberRef minDays = (CFNumberRef)CFDictionaryGetValue(df->_property._CustomMinDaysInFirstWeek, calName);
if (NULL != minDays && CFGetTypeID(minDays) == CFNumberGetTypeID()) {
CFIndex mwd;
if (CFNumberGetValue((CFNumberRef)minDays, kCFNumberCFIndexType, &mwd)) {
status = U_ZERO_ERROR;
__cficu_ucal_setAttribute(new_cal, UCAL_MINIMAL_DAYS_IN_FIRST_WEEK, mwd);
}
}
}
__cficu_udat_setCalendar(df->_df, new_cal);
__cficu_ucal_close(new_cal);
}
#define RESET_PROPERTY(C, K) \
if (df->_property. C) __CFDateFormatterSetProperty(df, K, df->_property. C, true);
static void __ResetUDateFormat(CFDateFormatterRef df, Boolean goingToHaveCustomFormat) {
if (df->_df) __cficu_udat_close(df->_df);
df->_df = NULL;
// uses _timeStyle, _dateStyle, _locale, _property._TimeZone; sets _df, _format, _defformat
char loc_buffer[BUFFER_SIZE];
loc_buffer[0] = 0;
CFStringRef tmpLocName = df->_locale ? CFLocaleGetIdentifier(df->_locale) : CFSTR("");
CFStringGetCString(tmpLocName, loc_buffer, BUFFER_SIZE, kCFStringEncodingASCII);
UChar tz_buffer[BUFFER_SIZE];
tz_buffer[0] = 0;
CFStringRef tmpTZName = df->_property._TimeZone ? CFTimeZoneGetName(df->_property._TimeZone) : CFSTR("GMT");
CFStringGetCharacters(tmpTZName, CFRangeMake(0, CFStringGetLength(tmpTZName)), (UniChar *)tz_buffer);
int32_t udstyle = 0, utstyle = 0; // effectively this makes UDAT_FULL the default for unknown dateStyle/timeStyle values
switch (df->_dateStyle) {
case kCFDateFormatterNoStyle: udstyle = UDAT_NONE; break;
case kCFDateFormatterShortStyle: udstyle = UDAT_SHORT; break;
case kCFDateFormatterMediumStyle: udstyle = UDAT_MEDIUM; break;
case kCFDateFormatterLongStyle: udstyle = UDAT_LONG; break;
case kCFDateFormatterFullStyle: udstyle = UDAT_FULL; break;
}
switch (df->_timeStyle) {
case kCFDateFormatterNoStyle: utstyle = UDAT_NONE; break;
case kCFDateFormatterShortStyle: utstyle = UDAT_SHORT; break;
case kCFDateFormatterMediumStyle: utstyle = UDAT_MEDIUM; break;
case kCFDateFormatterLongStyle: utstyle = UDAT_LONG; break;
case kCFDateFormatterFullStyle: utstyle = UDAT_FULL; break;
}
Boolean wantRelative = (NULL != df->_property._DoesRelativeDateFormatting && df->_property._DoesRelativeDateFormatting == kCFBooleanTrue);
Boolean hasFormat = (NULL != df->_property._HasCustomFormat && df->_property._HasCustomFormat == kCFBooleanTrue) || goingToHaveCustomFormat;
if (wantRelative && !hasFormat && kCFDateFormatterNoStyle != df->_dateStyle) {
udstyle |= UDAT_RELATIVE;
}
UErrorCode status = U_ZERO_ERROR;
UDateFormat *icudf = __cficu_udat_open((UDateFormatStyle)utstyle, (UDateFormatStyle)udstyle, loc_buffer, tz_buffer, CFStringGetLength(tmpTZName), NULL, 0, &status);
if (NULL == icudf || U_FAILURE(status)) {
return;
}
// <rdar://problem/15420462> "Yesterday" and "Today" now appear in lower case
// ICU uses middle of sentence context for relative days by default. We need to have relative dates to be captalized by default for backward compatibility
if (wantRelative) {
__cficu_udat_setContext(icudf, UDISPCTX_CAPITALIZATION_FOR_UI_LIST_OR_MENU, &status);
}
if (df->_property._IsLenient != NULL) {
__cficu_udat_setLenient(icudf, (kCFBooleanTrue == df->_property._IsLenient));
} else {
__cficu_udat_setLenient(icudf, 0);
}
if (kCFDateFormatterNoStyle == df->_dateStyle && kCFDateFormatterNoStyle == df->_timeStyle) {
if (wantRelative && !hasFormat && kCFDateFormatterNoStyle != df->_dateStyle) {
UErrorCode s = U_ZERO_ERROR;
__cficu_udat_applyPatternRelative(icudf, NULL, 0, NULL, 0, &s);
} else {
__cficu_udat_applyPattern(icudf, false, NULL, 0);
}
}
if (!wantRelative && df->_property._HasCustomFormat == kCFBooleanTrue) {
CFIndex cnt = CFStringGetLength(df->_format);
STACK_BUFFER_DECL(UChar, ubuffer, cnt);
const UChar *ustr = (UChar *)CFStringGetCharactersPtr((CFStringRef)df->_format);
if (NULL == ustr) {
CFStringGetCharacters(df->_format, CFRangeMake(0, cnt), (UniChar *)ubuffer);
ustr = ubuffer;
}
__cficu_udat_applyPattern(icudf, false, ustr, cnt);
}
CFStringRef calident = (CFStringRef)CFLocaleGetValue(df->_locale, kCFLocaleCalendarIdentifierKey);
if (calident && CFEqual(calident, kCFCalendarIdentifierGregorian)) {
status = U_ZERO_ERROR;
__cficu_udat_set2DigitYearStart(icudf, -631152000000.0, &status); // 1950-01-01 00:00:00 GMT
}
df->_df = icudf;
__ReadCustomUDateFormatProperty(df);
__SetCalendarProperties(df);
if (wantRelative && !hasFormat && kCFDateFormatterNoStyle != df->_dateStyle) {
__substituteFormatStringFromPrefsDFRelative(df);
} else {
__substituteFormatStringFromPrefsDF(df, false);
__substituteFormatStringFromPrefsDF(df, true);
}
__ApplyUDateFormatSymbol(df);
if (wantRelative && !hasFormat && kCFDateFormatterNoStyle != df->_dateStyle) {
UChar dateBuffer[BUFFER_SIZE];
UChar timeBuffer[BUFFER_SIZE];
status = U_ZERO_ERROR;
CFIndex dateLen = __cficu_udat_toPatternRelativeDate(icudf, dateBuffer, BUFFER_SIZE, &status);
CFIndex timeLen = (utstyle != UDAT_NONE) ? __cficu_udat_toPatternRelativeTime(icudf, timeBuffer, BUFFER_SIZE, &status) : 0;
if (U_SUCCESS(status) && dateLen <= BUFFER_SIZE && timeLen <= BUFFER_SIZE) {
// We assume that the 12/24-hour forcing preferences only affect the Time component
CFStringRef newFormat = CFStringCreateWithCharacters(kCFAllocatorSystemDefault, (const UniChar *)timeBuffer, timeLen);
CFStringRef formatString = __CFDateFormatterCreateForcedString(df, newFormat);
CFIndex cnt = CFStringGetLength(formatString);
CFAssert1(cnt <= BUFFER_SIZE, __kCFLogAssertion, "%s(): time format string too long", __PRETTY_FUNCTION__);
if (cnt <= BUFFER_SIZE) {
CFStringGetCharacters(formatString, CFRangeMake(0, cnt), (UniChar *)timeBuffer);
timeLen = cnt;
status = U_ZERO_ERROR;
__cficu_udat_applyPatternRelative(icudf, dateBuffer, dateLen, timeBuffer, timeLen, &status);
// ignore error and proceed anyway, what else can be done?
UChar ubuffer[BUFFER_SIZE];
status = U_ZERO_ERROR;
int32_t ret = __cficu_udat_toPattern(icudf, false, ubuffer, BUFFER_SIZE, &status); // read out current pattern
if (U_SUCCESS(status) && ret <= BUFFER_SIZE) {
if (df->_format) CFRelease(df->_format);
df->_format = CFStringCreateWithCharacters(kCFAllocatorSystemDefault, (const UniChar *)ubuffer, ret);
}
}
CFRelease(formatString);
CFRelease(newFormat);
}
} else {
UChar ubuffer[BUFFER_SIZE];
status = U_ZERO_ERROR;
int32_t ret = __cficu_udat_toPattern(icudf, false, ubuffer, BUFFER_SIZE, &status);
if (U_SUCCESS(status) && ret <= BUFFER_SIZE) {
CFStringRef newFormat = CFStringCreateWithCharacters(kCFAllocatorSystemDefault, (const UniChar *)ubuffer, ret);
CFStringRef formatString = __CFDateFormatterCreateForcedString(df, newFormat);
CFIndex cnt = CFStringGetLength(formatString);
CFAssert1(cnt <= 1024, __kCFLogAssertion, "%s(): format string too long", __PRETTY_FUNCTION__);
if (df->_format != formatString && cnt <= 1024) {
STACK_BUFFER_DECL(UChar, ubuffer, cnt);
const UChar *ustr = (UChar *)CFStringGetCharactersPtr((CFStringRef)formatString);
if (NULL == ustr) {
CFStringGetCharacters(formatString, CFRangeMake(0, cnt), (UniChar *)ubuffer);
ustr = ubuffer;
}
UErrorCode status = U_ZERO_ERROR;
// __cficu_udat_applyPattern(df->_df, false, ustr, cnt, &status);
__cficu_udat_applyPattern(df->_df, false, ustr, cnt);
if (U_SUCCESS(status)) {
if (df->_format) CFRelease(df->_format);
df->_format = (CFStringRef)CFStringCreateCopy(CFGetAllocator(df), formatString);
}
}
CFRelease(formatString);
CFRelease(newFormat);
}
}
if (df->_defformat) CFRelease(df->_defformat);
df->_defformat = df->_format ? (CFStringRef)CFRetain(df->_format) : NULL;
RESET_PROPERTY(_IsLenient, kCFDateFormatterIsLenientKey);
RESET_PROPERTY(_DoesRelativeDateFormatting, kCFDateFormatterDoesRelativeDateFormattingKey);
RESET_PROPERTY(_Calendar, kCFDateFormatterCalendarKey);
RESET_PROPERTY(_CalendarName, kCFDateFormatterCalendarIdentifierKey);
RESET_PROPERTY(_TimeZone, kCFDateFormatterTimeZoneKey);
RESET_PROPERTY(_TwoDigitStartDate, kCFDateFormatterTwoDigitStartDateKey);
RESET_PROPERTY(_DefaultDate, kCFDateFormatterDefaultDateKey);
RESET_PROPERTY(_GregorianStartDate, kCFDateFormatterGregorianStartDateKey);
RESET_PROPERTY(_AmbiguousYearStrategy, kCFDateFormatterAmbiguousYearStrategyKey);
RESET_PROPERTY(_UsesCharacterDirection, kCFDateFormatterUsesCharacterDirectionKey);
RESET_PROPERTY(_FormattingContext, kCFDateFormatterFormattingContextKey);
}
static CFTypeID __kCFDateFormatterTypeID = _kCFRuntimeNotATypeID;
static const CFRuntimeClass __CFDateFormatterClass = {
0,
"CFDateFormatter",
NULL, // init
NULL, // copy
__CFDateFormatterDeallocate,
NULL,
NULL,
NULL, //
__CFDateFormatterCopyDescription
};
CFTypeID CFDateFormatterGetTypeID(void) {
static dispatch_once_t initOnce;
dispatch_once(&initOnce, ^{ __kCFDateFormatterTypeID = _CFRuntimeRegisterClass(&__CFDateFormatterClass); });
return __kCFDateFormatterTypeID;
}
CFDateFormatterRef CFDateFormatterCreate(CFAllocatorRef allocator, CFLocaleRef locale, CFDateFormatterStyle dateStyle, CFDateFormatterStyle timeStyle) {
struct __CFDateFormatter *memory;
uint32_t size = sizeof(struct __CFDateFormatter) - sizeof(CFRuntimeBase);
if (allocator == NULL) allocator = __CFGetDefaultAllocator();
__CFGenericValidateType(allocator, CFAllocatorGetTypeID());
if (locale) __CFGenericValidateType(locale, CFLocaleGetTypeID());
memory = (struct __CFDateFormatter *)_CFRuntimeCreateInstance(allocator, CFDateFormatterGetTypeID(), size, NULL);
if (NULL == memory) {
return NULL;
}
memory->_df = NULL;
memory->_locale = NULL;
memory->_format = NULL;
memory->_defformat = NULL;
memory->_dateStyle = dateStyle;
memory->_timeStyle = timeStyle;
memory->_property._IsLenient = NULL;
memory->_property._DoesRelativeDateFormatting = NULL;
memory->_property._HasCustomFormat = NULL;
memory->_property._TimeZone = NULL;
memory->_property._Calendar = NULL;
memory->_property._CalendarName = NULL;
memory->_property._TwoDigitStartDate = NULL;
memory->_property._DefaultDate = NULL;
memory->_property._GregorianStartDate = NULL;
memory->_property._EraSymbols = NULL;
memory->_property._LongEraSymbols = NULL;
memory->_property._MonthSymbols = NULL;
memory->_property._ShortMonthSymbols = NULL;
memory->_property._VeryShortMonthSymbols = NULL;
memory->_property._StandaloneMonthSymbols = NULL;
memory->_property._ShortStandaloneMonthSymbols = NULL;
memory->_property._VeryShortStandaloneMonthSymbols = NULL;
memory->_property._WeekdaySymbols = NULL;
memory->_property._ShortWeekdaySymbols = NULL;
memory->_property._VeryShortWeekdaySymbols = NULL;
memory->_property._StandaloneWeekdaySymbols = NULL;
memory->_property._ShortStandaloneWeekdaySymbols = NULL;
memory->_property._VeryShortStandaloneWeekdaySymbols = NULL;
memory->_property._QuarterSymbols = NULL;
memory->_property._ShortQuarterSymbols = NULL;
memory->_property._StandaloneQuarterSymbols = NULL;
memory->_property._ShortStandaloneQuarterSymbols = NULL;
memory->_property._AMSymbol = NULL;
memory->_property._PMSymbol = NULL;
memory->_property._AmbiguousYearStrategy = NULL;
memory->_property._UsesCharacterDirection = NULL;
memory->_property._FormattingContext = NULL;
memory->_property._CustomEraSymbols = NULL;
memory->_property._CustomMonthSymbols = NULL;
memory->_property._CustomShortMonthSymbols = NULL;
memory->_property._CustomWeekdaySymbols = NULL;
memory->_property._CustomShortWeekdaySymbols = NULL;
memory->_property._CustomLongEraSymbols = NULL;
memory->_property._CustomVeryShortMonthSymbols = NULL;
memory->_property._CustomVeryShortWeekdaySymbols = NULL;
memory->_property._CustomStandaloneMonthSymbols = NULL;
memory->_property._CustomShortStandaloneMonthSymbols = NULL;
memory->_property._CustomVeryShortStandaloneMonthSymbols = NULL;
memory->_property._CustomStandaloneWeekdaySymbols = NULL;
memory->_property._CustomShortStandaloneWeekdaySymbols = NULL;
memory->_property._CustomVeryShortStandaloneWeekdaySymbols = NULL;
memory->_property._CustomQuarterSymbols = NULL;
memory->_property._CustomShortQuarterSymbols = NULL;
memory->_property._CustomStandaloneQuarterSymbols = NULL;
memory->_property._CustomShortStandaloneQuarterSymbols = NULL;
memory->_property._CustomDateFormat = NULL;
memory->_property._CustomTimeFormat = NULL;
memory->_property._Custom24Hour = NULL;
memory->_property._Custom12Hour = NULL;
memory->_property._CustomAMSymbol = NULL;
memory->_property._CustomPMSymbol = NULL;
memory->_property._CustomFirstWeekday = NULL;
memory->_property._CustomMinDaysInFirstWeek = NULL;
switch (dateStyle) {
case kCFDateFormatterNoStyle:
case kCFDateFormatterShortStyle:
case kCFDateFormatterMediumStyle:
case kCFDateFormatterLongStyle:
case kCFDateFormatterFullStyle: break;
default:
CFAssert2(0, __kCFLogAssertion, "%s(): unknown date style %d", __PRETTY_FUNCTION__, dateStyle);
memory->_dateStyle = kCFDateFormatterMediumStyle;
break;
}
switch (timeStyle) {
case kCFDateFormatterNoStyle:
case kCFDateFormatterShortStyle:
case kCFDateFormatterMediumStyle:
case kCFDateFormatterLongStyle:
case kCFDateFormatterFullStyle: break;
default:
CFAssert2(0, __kCFLogAssertion, "%s(): unknown time style %d", __PRETTY_FUNCTION__, timeStyle);
memory->_timeStyle = kCFDateFormatterMediumStyle;
break;
}
memory->_locale = locale ? CFLocaleCreateCopy(allocator, locale) : (CFLocaleRef)CFRetain(CFLocaleGetSystem());
memory->_property._TimeZone = CFTimeZoneCopyDefault();
CFStringRef calident = (CFStringRef)CFLocaleGetValue(memory->_locale, kCFLocaleCalendarIdentifierKey);
if (calident && CFEqual(calident, kCFCalendarIdentifierGregorian)) {
memory->_property._TwoDigitStartDate = CFDateCreate(kCFAllocatorSystemDefault, -1609459200.0); // 1950-01-01 00:00:00 +0000
}
__ResetUDateFormat(memory, false);
if (!memory->_df) {
CFRelease(memory);
return NULL;
}
return (CFDateFormatterRef)memory;
}
static void __substituteFormatStringFromPrefsDFRelative(CFDateFormatterRef formatter) {
CFIndex dateLen = -1;
UChar dateBuffer[BUFFER_SIZE];
if (kCFDateFormatterNoStyle != formatter->_dateStyle) {
if (formatter->_property._CustomDateFormat != NULL) {
dateLen = __CFMin(CFStringGetLength(formatter->_property._CustomDateFormat), BUFFER_SIZE);
CFStringGetCharacters(formatter->_property._CustomDateFormat, CFRangeMake(0, dateLen), (UniChar *)dateBuffer);
}
}
if (-1 == dateLen) {
UErrorCode status = U_ZERO_ERROR;
int32_t ret = __cficu_udat_toPatternRelativeDate(formatter->_df, dateBuffer, BUFFER_SIZE, &status);
if (!U_FAILURE(status)) {
dateLen = ret;
}
}
CFIndex timeLen = -1;
UChar timeBuffer[BUFFER_SIZE];
if (kCFDateFormatterNoStyle != formatter->_timeStyle) {
if (formatter->_property._CustomTimeFormat != NULL) {
timeLen = __CFMin(CFStringGetLength(formatter->_property._CustomTimeFormat), BUFFER_SIZE);
CFStringGetCharacters(formatter->_property._CustomTimeFormat, CFRangeMake(0, timeLen), (UniChar *)timeBuffer);
}
}
if (-1 == timeLen) {
UErrorCode status = U_ZERO_ERROR;
int32_t ret = __cficu_udat_toPatternRelativeTime(formatter->_df, timeBuffer, BUFFER_SIZE, &status);
if (!U_FAILURE(status)) {
timeLen = ret;
}
}
UErrorCode status = U_ZERO_ERROR;
__cficu_udat_applyPatternRelative(formatter->_df, (0 <= dateLen) ? dateBuffer : NULL, (0 <= dateLen) ? dateLen : 0, (0 <= timeLen) ? timeBuffer : NULL, (0 <= timeLen) ? timeLen : 0, &status);
}
static void __substituteFormatStringFromPrefsDF(CFDateFormatterRef formatter, bool doTime) {
CFIndex formatStyle = doTime ? formatter->_timeStyle : formatter->_dateStyle;
CFStringRef pref = doTime ? formatter->_property._CustomTimeFormat : formatter->_property._CustomDateFormat;
if (kCFDateFormatterNoStyle != formatStyle) {
if (NULL != pref) {
int32_t icustyle = UDAT_NONE;
switch (formatStyle) {
case kCFDateFormatterShortStyle: icustyle = UDAT_SHORT; break;
case kCFDateFormatterMediumStyle: icustyle = UDAT_MEDIUM; break;
case kCFDateFormatterLongStyle: icustyle = UDAT_LONG; break;
case kCFDateFormatterFullStyle: icustyle = UDAT_FULL; break;
}
CFStringRef localeName = CFLocaleGetIdentifier(formatter->_locale);
char buffer[BUFFER_SIZE];
const char *cstr = CFStringGetCStringPtr(localeName, kCFStringEncodingASCII);
if (NULL == cstr) {
if (CFStringGetCString(localeName, buffer, BUFFER_SIZE, kCFStringEncodingASCII)) cstr = buffer;
}
UErrorCode status = U_ZERO_ERROR;
UDateFormat *df = __cficu_udat_open((UDateFormatStyle)(doTime ? icustyle : UDAT_NONE), (UDateFormatStyle)(doTime ? UDAT_NONE : icustyle), cstr, NULL, 0, NULL, 0, &status);
if (NULL != df) {
UChar ubuffer[BUFFER_SIZE];
status = U_ZERO_ERROR;
int32_t date_len = __cficu_udat_toPattern(df, false, ubuffer, BUFFER_SIZE, &status);
if (U_SUCCESS(status) && date_len <= BUFFER_SIZE) {
CFStringRef dateString = CFStringCreateWithCharacters(kCFAllocatorSystemDefault, (UniChar *)ubuffer, date_len);
status = U_ZERO_ERROR;
int32_t formatter_len = __cficu_udat_toPattern(formatter->_df, false, ubuffer, BUFFER_SIZE, &status);
if (U_SUCCESS(status) && formatter_len <= BUFFER_SIZE) {
CFMutableStringRef formatString = CFStringCreateMutable(kCFAllocatorSystemDefault, 0);
CFStringAppendCharacters(formatString, (UniChar *)ubuffer, formatter_len);
// find dateString inside formatString, substitute the pref in that range
CFRange result;
if (CFStringFindWithOptions(formatString, dateString, CFRangeMake(0, formatter_len), 0, &result)) {
CFStringReplace(formatString, result, pref);
int32_t new_len = CFStringGetLength(formatString);
STACK_BUFFER_DECL(UChar, new_buffer, new_len);
const UChar *new_ustr = (UChar *)CFStringGetCharactersPtr(formatString);
if (NULL == new_ustr) {
CFStringGetCharacters(formatString, CFRangeMake(0, new_len), (UniChar *)new_buffer);
new_ustr = new_buffer;
}
status = U_ZERO_ERROR;
// __cficu_udat_applyPattern(formatter->_df, false, new_ustr, new_len, &status);
__cficu_udat_applyPattern(formatter->_df, false, new_ustr, new_len);
}
CFRelease(formatString);
}
CFRelease(dateString);
}
__cficu_udat_close(df);
}
}
}
}
static void __CFDateFormatterStoreSymbolPrefs(const void *key, const void *value, void *context) {
if (CFGetTypeID(key) == CFStringGetTypeID() && CFGetTypeID(value) == CFArrayGetTypeID()) {
CFDateFormatterRef formatter = (CFDateFormatterRef)context;
UDateFormatSymbolType sym = (UDateFormatSymbolType)CFStringGetIntValue((CFStringRef)key);
CFArrayRef array = (CFArrayRef)value;
CFIndex idx, cnt = CFArrayGetCount(array);
switch (sym) {