-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathCFDateComponents.c
467 lines (431 loc) · 19.5 KB
/
CFDateComponents.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
/* CFDateComponents.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
Responsibility: Itai Ferber
*/
#include <assert.h>
#include "CFCalendar.h"
#include "CFString.h"
#include "CFDateComponents.h"
#include "CFInternal.h"
#include "CFCalendar_Internal.h"
#include "CFRuntime_Internal.h"
static Boolean __CFDateComponentsEqual(CFTypeRef cf1, CFTypeRef cf2) {
assert(NULL != cf1);
assert(NULL != cf2);
CFDateComponentsRef dc1 = (CFDateComponentsRef)cf1;
CFDateComponentsRef dc2 = (CFDateComponentsRef)cf2;
if (dc1->_era != dc2->_era) return false;
if (dc1->_year != dc2->_year) return false;
if (dc1->_quarter != dc2->_quarter) return false;
if (dc1->_month != dc2->_month) return false;
if (dc1->_day != dc2->_day) return false;
if (dc1->_hour != dc2->_hour) return false;
if (dc1->_minute != dc2->_minute) return false;
if (dc1->_second != dc2->_second) return false;
if (dc1->_nanosecond != dc2->_nanosecond) return false;
if (dc1->_week != dc2->_week) return false;
if (dc1->_weekOfYear != dc2->_weekOfYear) return false;
if (dc1->_weekOfMonth != dc2->_weekOfMonth) return false;
if (dc1->_yearForWeekOfYear != dc2->_yearForWeekOfYear) return false;
if (dc1->_weekday != dc2->_weekday) return false;
if (dc1->_weekdayOrdinal != dc2->_weekdayOrdinal) return false;
// TODO: NSDateComponents would compare leapMonth, not checking isLeapMonthSet first. 'isLeapMonth' returns NO in the case where 'isLeapMonthSet' returns NO.
// This also manifested as a bug where setting leapMonth -> NO meant that the 'isLeapMonthSet' was false after decoding the archive, because the value was only set on the decoded NSDateComponents if 'leapMonth' was YES.
// For now, we will use the same logic as before, and look into seeing if we can change that behavior for encoding later.
if (!((dc1->_leapMonth == 0 && dc2->_leapMonth == CFDateComponentUndefined) ||
(dc1->_leapMonth == CFDateComponentUndefined && dc2->_leapMonth == 0) ||
(dc1->_leapMonth == dc2->_leapMonth))) {
return false;
}
if ((dc1->_calendar && !dc2->_calendar) || (!dc1->_calendar && dc2->_calendar)) return false;
if (dc1->_calendar && dc2->_calendar && !CFEqual(dc1->_calendar, dc2->_calendar)) return false;
if ((dc1->_timeZone && !dc2->_timeZone) || (!dc1->_timeZone && dc2->_timeZone)) return false;
if (dc1->_timeZone && dc2->_timeZone && !CFEqual(dc1->_timeZone, dc2->_timeZone)) return false;
return true;
}
static CFHashCode __CFDateComponentsHash(CFTypeRef cf) {
assert(NULL != cf);
CFDateComponentsRef dc = (CFDateComponentsRef)cf;
CFIndex calHash = dc->_calendar ? CFHash(dc->_calendar) : 0;
CFIndex tzHash = dc->_timeZone ? CFHash(dc->_timeZone) : 0;
CFIndex calTzHash = calHash ^ tzHash;
CFIndex y = dc->_year; if (y == CFDateComponentUndefined) y = 0;
CFIndex m = dc->_month; if (m == CFDateComponentUndefined) m = 0;
CFIndex d = dc->_day; if (d == CFDateComponentUndefined) d = 0;
CFIndex h = dc->_hour; if (h == CFDateComponentUndefined) h = 0;
CFIndex mm = dc->_minute; if (mm == CFDateComponentUndefined) mm = 0;
CFIndex s = dc->_second; if (s == CFDateComponentUndefined) s = 0;
CFIndex yy = dc->_yearForWeekOfYear; if (yy == CFDateComponentUndefined) yy = 0;
CFIndex hash = calTzHash + (32832013 * (y + yy) + 2678437 * m + 86413 * d + 3607 * h + 61 * mm + s);
hash = hash + (41 * dc->_weekOfYear + 11 * dc->_weekOfMonth + 7 * dc->_weekday + 3 * dc->_weekdayOrdinal + dc->_quarter) * (1ULL << 5);
return hash;
}
Boolean CFDateComponentsIsLeapMonthSet(CFDateComponentsRef dc) {
return dc->_leapMonth != CFDateComponentUndefined;
}
Boolean CFDateComponentsIsLeapMonth(CFDateComponentsRef dc) {
return (CFDateComponentUndefined != dc->_leapMonth && dc->_leapMonth) ? true : false;
}
CFStringRef _CFDateComponentsCopyDescriptionInner(CFDateComponentsRef dc) {
CFMutableStringRef mstr = CFStringCreateMutable(kCFAllocatorSystemDefault, 0);
CFStringAppend(mstr, CFSTR("{"));
CFCalendarRef cal = dc->_calendar;
if (cal) CFStringAppendFormat(mstr, NULL, CFSTR("\n Calendar: %@"), cal);
CFTimeZoneRef tz = dc->_timeZone;
if (tz) CFStringAppendFormat(mstr, NULL, CFSTR("\n TimeZone: %@"), tz);
CFIndex val = dc->_era;
if (CFDateComponentUndefined != val) CFStringAppendFormat(mstr, NULL, CFSTR("\n Era: %ld"), (long)val);
val = dc->_year;
if (CFDateComponentUndefined != val) CFStringAppendFormat(mstr, NULL, CFSTR("\n Calendar Year: %ld"), (long)val);
val = dc->_month;
if (CFDateComponentUndefined != val) CFStringAppendFormat(mstr, NULL, CFSTR("\n Month: %ld"), (long)val);
val = dc->_leapMonth;
if (CFDateComponentUndefined != val) CFStringAppendFormat(mstr, NULL, CFSTR("\n Leap Month: %ld"), (long)val);
val = dc->_day;
if (CFDateComponentUndefined != val) CFStringAppendFormat(mstr, NULL, CFSTR("\n Day: %ld"), (long)val);
val = dc->_hour;
if (CFDateComponentUndefined != val) CFStringAppendFormat(mstr, NULL, CFSTR("\n Hour: %ld"), (long)val);
val = dc->_minute;
if (CFDateComponentUndefined != val) CFStringAppendFormat(mstr, NULL, CFSTR("\n Minute: %ld"), (long)val);
val = dc->_second;
if (CFDateComponentUndefined != val) CFStringAppendFormat(mstr, NULL, CFSTR("\n Second: %ld"), (long)val);
val = dc->_nanosecond;
if (CFDateComponentUndefined != val) CFStringAppendFormat(mstr, NULL, CFSTR("\n Nanosecond: %ld"), (long)val);
val = dc->_quarter;
if (CFDateComponentUndefined != val) CFStringAppendFormat(mstr, NULL, CFSTR("\n Quarter: %ld"), (long)val);
val = dc->_yearForWeekOfYear;
if (CFDateComponentUndefined != val) CFStringAppendFormat(mstr, NULL, CFSTR("\n Year for Week of Year: %ld"), (long)val);
val = dc->_weekOfYear;
if (CFDateComponentUndefined != val) CFStringAppendFormat(mstr, NULL, CFSTR("\n Week of Year: %ld"), (long)val);
val = dc->_weekOfMonth;
if (CFDateComponentUndefined != val) CFStringAppendFormat(mstr, NULL, CFSTR("\n Week of Month: %ld"), (long)val);
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated"
val = dc->_week;
if (CFDateComponentUndefined != val) CFStringAppendFormat(mstr, NULL, CFSTR("\n Week (obsolete): %ld"), (long)val);
#pragma GCC diagnostic pop
val = dc->_weekday;
if (CFDateComponentUndefined != val) CFStringAppendFormat(mstr, NULL, CFSTR("\n Weekday: %ld"), (long)val);
val = dc->_weekdayOrdinal;
if (CFDateComponentUndefined != val) CFStringAppendFormat(mstr, NULL, CFSTR("\n Weekday Ordinal: %ld"), (long)val);
return mstr;
}
static CFStringRef __CFDateComponentsCopyDescription(CFTypeRef cf) {
assert(NULL != cf);
CFDateComponentsRef dc = (CFDateComponentsRef)cf;
CFStringRef interiorDescription = _CFDateComponentsCopyDescriptionInner(dc);
CFStringRef result = CFStringCreateWithFormat(kCFAllocatorSystemDefault, NULL, CFSTR("<CFDateComponents %p [%p]>%@"), dc, CFGetAllocator(dc), interiorDescription);
CFRelease(interiorDescription);
return result;
}
static void __CFDateComponentsDeallocate(CFTypeRef cf) {
assert(NULL != cf);
CFDateComponentsRef dc = (CFDateComponentsRef)cf;
if (dc->_calendar) CFRelease(dc->_calendar);
if (dc->_timeZone) CFRelease(dc->_timeZone);
}
const CFRuntimeClass __CFDateComponentsClass = {
0,
"CFDateComponents",
NULL, // init
NULL, // copy
__CFDateComponentsDeallocate,
__CFDateComponentsEqual,
__CFDateComponentsHash,
NULL, //
__CFDateComponentsCopyDescription
};
CFTypeID CFDateComponentsGetTypeID(void) {
return _kCFRuntimeIDCFDateComponents;
}
/* End Runtime setup */
CFDateComponentsRef CFDateComponentsCreate(CFAllocatorRef allocator) {
if (!allocator) allocator = CFAllocatorGetDefault();
struct __CFDateComponents *dc = NULL;
uint32_t size = sizeof(struct __CFDateComponents) - sizeof(CFRuntimeBase);
dc = (struct __CFDateComponents *)_CFRuntimeCreateInstance(allocator, CFDateComponentsGetTypeID(), size, NULL);
if (NULL == dc) return NULL;
dc->_calendar = NULL;
dc->_timeZone = NULL;
dc->_era = CFDateComponentUndefined;
dc->_year = CFDateComponentUndefined;
dc->_month = CFDateComponentUndefined;
dc->_leapMonth = CFDateComponentUndefined;
dc->_day = CFDateComponentUndefined;
dc->_hour = CFDateComponentUndefined;
dc->_minute = CFDateComponentUndefined;
dc->_second = CFDateComponentUndefined;
dc->_week = CFDateComponentUndefined;
dc->_weekday = CFDateComponentUndefined;
dc->_weekdayOrdinal = CFDateComponentUndefined;
dc->_quarter = CFDateComponentUndefined;
dc->_weekOfMonth = CFDateComponentUndefined;
dc->_weekOfYear = CFDateComponentUndefined;
dc->_yearForWeekOfYear = CFDateComponentUndefined;
dc->_nanosecond = CFDateComponentUndefined;
return dc;
}
CFDateComponentsRef CFDateComponentsCreateCopy(CFAllocatorRef allocator, CFDateComponentsRef dc) {
CFDateComponentsRef result = CFDateComponentsCreate(allocator);
if (!result) HALT_MSG("Out of memory");
CFCalendarRef cal = CFDateComponentsCopyCalendar(dc);
if (cal) {
CFDateComponentsSetCalendar(result, cal);
CFRelease(cal);
}
CFTimeZoneRef tz = CFDateComponentsCopyTimeZone(dc);
if (tz) {
CFDateComponentsSetTimeZone(result, tz);
CFRelease(tz);
}
// Just reach in for the rest
result->_era = dc->_era;
result->_year = dc->_year;
result->_month = dc->_month;
result->_leapMonth = dc->_leapMonth;
result->_day = dc->_day;
result->_hour = dc->_hour;
result->_minute = dc->_minute;
result->_second = dc->_second;
result->_week = dc->_week;
result->_weekday = dc->_weekday;
result->_weekdayOrdinal = dc->_weekdayOrdinal;
result->_quarter = dc->_quarter;
result->_weekOfMonth = dc->_weekOfMonth;
result->_weekOfYear = dc->_weekOfYear;
result->_yearForWeekOfYear = dc->_yearForWeekOfYear;
result->_nanosecond = dc->_nanosecond;
return result;
}
CFIndex CFDateComponentsGetValue(CFDateComponentsRef dateComp, CFCalendarUnit unit) {
assert(NULL != dateComp);
CFIndex val = CFDateComponentUndefined;
switch (unit) {
case kCFCalendarUnitEra:
val = dateComp->_era;
break;
case kCFCalendarUnitYear:
val = dateComp->_year;
break;
case kCFCalendarUnitMonth:
val = dateComp->_month;
break;
case kCFCalendarUnitLeapMonth:
val = dateComp->_leapMonth;
break;
case kCFCalendarUnitDay:
val = dateComp->_day;
break;
case kCFCalendarUnitHour:
val = dateComp->_hour;
break;
case kCFCalendarUnitMinute:
val = dateComp->_minute;
break;
case kCFCalendarUnitSecond:
val = dateComp->_second;
break;
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated"
case kCFCalendarUnitWeek:
val = dateComp->_week;
break;
#pragma GCC diagnostic pop
case kCFCalendarUnitWeekday:
val = dateComp->_weekday;
break;
case kCFCalendarUnitWeekdayOrdinal:
val = dateComp->_weekdayOrdinal;
break;
case kCFCalendarUnitQuarter:
val = dateComp->_quarter;
break;
case kCFCalendarUnitWeekOfMonth:
val = dateComp->_weekOfMonth;
break;
case kCFCalendarUnitWeekOfYear:
val = dateComp->_weekOfYear;
break;
case kCFCalendarUnitYearForWeekOfYear:
val = dateComp->_yearForWeekOfYear;
break;
case kCFCalendarUnitNanosecond:
val = dateComp->_nanosecond;
break;
default:
// Unknown units are ignored for forwards compatibility
break;
}
return val;
}
void CFDateComponentsSetValue(CFDateComponentsRef dateComp, CFCalendarUnit unit, CFIndex value) {
assert(NULL != dateComp);
switch (unit) {
case kCFCalendarUnitEra:
dateComp->_era = value;
break;
case kCFCalendarUnitYear:
dateComp->_year = value;
break;
case kCFCalendarUnitMonth:
dateComp->_month = value;
break;
case kCFCalendarUnitLeapMonth:
dateComp->_leapMonth = value;
break;
case kCFCalendarUnitDay:
dateComp->_day = value;
break;
case kCFCalendarUnitHour:
dateComp->_hour = value;
break;
case kCFCalendarUnitMinute:
dateComp->_minute = value;
break;
case kCFCalendarUnitSecond:
dateComp->_second = value;
break;
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated"
case kCFCalendarUnitWeek:
dateComp->_week = value;
break;
#pragma GCC diagnostic pop
case kCFCalendarUnitWeekday:
dateComp->_weekday = value;
break;
case kCFCalendarUnitWeekdayOrdinal:
dateComp->_weekdayOrdinal = value;
break;
case kCFCalendarUnitQuarter:
dateComp->_quarter = value;
break;
case kCFCalendarUnitWeekOfMonth:
dateComp->_weekOfMonth = value;
break;
case kCFCalendarUnitWeekOfYear:
dateComp->_weekOfYear = value;
break;
case kCFCalendarUnitYearForWeekOfYear:
dateComp->_yearForWeekOfYear = value;
break;
case kCFCalendarUnitNanosecond:
dateComp->_nanosecond = value;
break;
default:
// Unknown units are ignored for forwards compatibility
break;
}
}
CFCalendarRef CFDateComponentsCopyCalendar(CFDateComponentsRef dateComp) {
assert(NULL != dateComp);
if (dateComp->_calendar) {
return (CFCalendarRef)CFRetain(dateComp->_calendar);
} else {
return NULL;
}
}
void CFDateComponentsSetCalendar(CFDateComponentsRef dateComp, CFCalendarRef calendar) {
assert(NULL != dateComp);
CFCalendarRef currCal = dateComp->_calendar;
if (calendar && currCal) {
if (CFEqual(currCal, calendar)) return;
}
if (currCal) {
CFRelease(dateComp->_calendar);
dateComp->_calendar = NULL;
}
if (calendar) {
// We copy the calendar, and set its time zone to match the one in the current date components if required
CFCalendarRef calCopy = _CFCalendarCreateCopy(kCFAllocatorSystemDefault, calendar);
if (dateComp->_timeZone) {
CFCalendarSetTimeZone(calCopy, dateComp->_timeZone);
}
dateComp->_calendar = calCopy;
}
}
CFTimeZoneRef CFDateComponentsCopyTimeZone(CFDateComponentsRef dateComp) {
assert(NULL != dateComp);
if (dateComp->_timeZone) {
return (CFTimeZoneRef)CFRetain(dateComp->_timeZone);
} else {
return NULL;
}
}
void CFDateComponentsSetTimeZone(CFDateComponentsRef dateComp, CFTimeZoneRef timeZone) {
assert(NULL != dateComp);
CFTimeZoneRef currTZ = dateComp->_timeZone;
if (timeZone && currTZ) {
if (CFEqual(currTZ, timeZone)) return;
}
if (currTZ) {
CFRelease(dateComp->_timeZone);
dateComp->_timeZone = NULL;
}
if (timeZone) {
dateComp->_timeZone = (CFTimeZoneRef)CFRetain(timeZone);
// Also set the tz on our calendar
CFCalendarRef cal = dateComp->_calendar;
if (cal) {
CFCalendarSetTimeZone(cal, timeZone);
}
}
}
Boolean CFDateComponentsIsValidDate(CFDateComponentsRef dateComp) {
CFCalendarRef cal = dateComp->_calendar;
if (!cal) {
return false;
} else {
return CFDateComponentsIsValidDateInCalendar(dateComp, cal);
}
}
Boolean CFDateComponentsIsValidDateInCalendar(CFDateComponentsRef dateComp, CFCalendarRef inCalendar) {
assert(NULL != dateComp);
assert(NULL != inCalendar);
CFIndex ns = dateComp->_nanosecond;
if (CFDateComponentUndefined != ns && 1000 * 1000 * 1000UL <= ns) {
return false;
}
CFCalendarRef calendar = _CFCalendarCreateCopy(kCFAllocatorSystemDefault, inCalendar);
// Clear nanoseconds temporarily
if (CFDateComponentUndefined != ns && 0 < ns) {
dateComp->_nanosecond = 0;
}
CFDateRef date = CFCalendarCreateDateFromComponents(kCFAllocatorSystemDefault, calendar, dateComp);
// Reset nanoseconds
if (CFDateComponentUndefined != ns && 0 < ns) {
dateComp->_nanosecond = ns;
}
Boolean result = true;
if (date) {
CFCalendarUnit all = kCFCalendarUnitEra | kCFCalendarUnitYear | kCFCalendarUnitMonth | kCFCalendarUnitDay | kCFCalendarUnitHour | kCFCalendarUnitMinute | kCFCalendarUnitSecond | kCFCalendarUnitWeekday | kCFCalendarUnitWeekdayOrdinal | kCFCalendarUnitQuarter | kCFCalendarUnitWeekOfMonth | kCFCalendarUnitWeekOfYear | kCFCalendarUnitYearForWeekOfYear;
CFDateComponentsRef newComps = CFCalendarCreateDateComponentsFromDate(kCFAllocatorSystemDefault, calendar, all, date);
if (result && CFDateComponentUndefined != dateComp->_era) if (newComps->_era != dateComp->_era) result = false;
if (result && CFDateComponentUndefined != dateComp->_year) if (newComps->_year != dateComp->_year) result = false;
if (result && CFDateComponentUndefined != dateComp->_month) if (newComps->_month != dateComp->_month) result = false;
if (result && CFDateComponentUndefined != dateComp->_leapMonth) if (newComps->_leapMonth != dateComp->_leapMonth) result = false;
if (result && CFDateComponentUndefined != dateComp->_day) if (newComps->_day != dateComp->_day) result = false;
if (result && CFDateComponentUndefined != dateComp->_hour) if (newComps->_hour != dateComp->_hour) result = false;
if (result && CFDateComponentUndefined != dateComp->_minute) if (newComps->_minute != dateComp->_minute) result = false;
if (result && CFDateComponentUndefined != dateComp->_second) if (newComps->_second != dateComp->_second) result = false;
if (result && CFDateComponentUndefined != dateComp->_weekday) if (newComps->_weekday != dateComp->_weekday) result = false;
if (result && CFDateComponentUndefined != dateComp->_weekdayOrdinal) if (newComps->_weekdayOrdinal != dateComp->_weekdayOrdinal) result = false;
if (result && CFDateComponentUndefined != dateComp->_quarter) if (newComps->_quarter != dateComp->_quarter) result = false;
if (result && CFDateComponentUndefined != dateComp->_weekOfMonth) if (newComps->_weekOfMonth != dateComp->_weekOfMonth) result = false;
if (result && CFDateComponentUndefined != dateComp->_weekOfYear) if (newComps->_weekOfYear != dateComp->_weekOfYear) result = false;
if (result && CFDateComponentUndefined != dateComp->_yearForWeekOfYear) if (newComps->_yearForWeekOfYear != dateComp->_yearForWeekOfYear) result = false;
CFRelease(date);
CFRelease(newComps);
}
CFRelease(calendar);
// NSDateComponents has a legacy behavior here to support an unset calendar that means if date is not set we return true
return result;
}
Boolean CFDateComponentsDateMatchesComponents(CFDateComponentsRef dateComp, CFCalendarRef calendar, CFDateRef date) {
// TODO
return false;
}