forked from swiftlang/swift-corelibs-foundation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNSDateComponents.swift
633 lines (591 loc) · 20.3 KB
/
NSDateComponents.swift
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
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2021 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
@_implementationOnly import CoreFoundation
// This is a just used as an extensible struct, basically;
// note that there are two uses: one for specifying a date
// via components (some components may be missing, making the
// specific date ambiguous), and the other for specifying a
// set of component quantities (like, 3 months and 5 hours).
// Undefined fields have (or fields can be set to) the value
// NSDateComponentUndefined.
// NSDateComponents is not responsible for answering questions
// about a date beyond the information it has been initialized
// with; for example, if you initialize one with May 6, 2004,
// and then ask for the weekday, you'll get Undefined, not Thurs.
// A NSDateComponents is meaningless in itself, because you need
// to know what calendar it is interpreted against, and you need
// to know whether the values are absolute values of the units,
// or quantities of the units.
// When you create a new one of these, all values begin Undefined.
public var NSDateComponentUndefined: Int = Int.max
open class NSDateComponents: NSObject, NSCopying, NSSecureCoding {
internal var _calendar: Calendar?
internal var _timeZone: TimeZone?
internal var _values = [Int](repeating: NSDateComponentUndefined, count: 19)
public override init() {
super.init()
}
open override var hash: Int {
var hasher = Hasher()
var mask = 0
// The list of fields fed to the hasher here must be exactly
// the same as the ones compared in isEqual(_:) (modulo
// ordering).
//
// Given that NSDateComponents instances usually only have a
// few fields present, it makes sense to only hash those, as
// an optimization. We keep track of the fields hashed in the
// mask value, which we also feed to the hasher to make sure
// any two unequal values produce different hash encodings.
//
// FIXME: Why not just feed _values, calendar & timeZone to
// the hasher?
if let calendar = calendar {
hasher.combine(calendar)
mask |= 1 << 0
}
if let timeZone = timeZone {
hasher.combine(timeZone)
mask |= 1 << 1
}
if era != NSDateComponentUndefined {
hasher.combine(era)
mask |= 1 << 2
}
if year != NSDateComponentUndefined {
hasher.combine(year)
mask |= 1 << 3
}
if quarter != NSDateComponentUndefined {
hasher.combine(quarter)
mask |= 1 << 4
}
if month != NSDateComponentUndefined {
hasher.combine(month)
mask |= 1 << 5
}
if day != NSDateComponentUndefined {
hasher.combine(day)
mask |= 1 << 6
}
if hour != NSDateComponentUndefined {
hasher.combine(hour)
mask |= 1 << 7
}
if minute != NSDateComponentUndefined {
hasher.combine(minute)
mask |= 1 << 8
}
if second != NSDateComponentUndefined {
hasher.combine(second)
mask |= 1 << 9
}
if nanosecond != NSDateComponentUndefined {
hasher.combine(nanosecond)
mask |= 1 << 10
}
if weekOfYear != NSDateComponentUndefined {
hasher.combine(weekOfYear)
mask |= 1 << 11
}
if weekOfMonth != NSDateComponentUndefined {
hasher.combine(weekOfMonth)
mask |= 1 << 12
}
if yearForWeekOfYear != NSDateComponentUndefined {
hasher.combine(yearForWeekOfYear)
mask |= 1 << 13
}
if weekday != NSDateComponentUndefined {
hasher.combine(weekday)
mask |= 1 << 14
}
if weekdayOrdinal != NSDateComponentUndefined {
hasher.combine(weekdayOrdinal)
mask |= 1 << 15
}
hasher.combine(isLeapMonth)
hasher.combine(mask)
return hasher.finalize()
}
open override func isEqual(_ object: Any?) -> Bool {
guard let other = object as? NSDateComponents else { return false }
// FIXME: Why not just compare _values, calendar & timeZone?
return self === other
|| (era == other.era
&& year == other.year
&& quarter == other.quarter
&& month == other.month
&& day == other.day
&& hour == other.hour
&& minute == other.minute
&& second == other.second
&& nanosecond == other.nanosecond
&& weekOfYear == other.weekOfYear
&& weekOfMonth == other.weekOfMonth
&& yearForWeekOfYear == other.yearForWeekOfYear
&& weekday == other.weekday
&& weekdayOrdinal == other.weekdayOrdinal
&& isLeapMonth == other.isLeapMonth
&& calendar == other.calendar
&& timeZone == other.timeZone)
}
public convenience required init?(coder aDecoder: NSCoder) {
guard aDecoder.allowsKeyedCoding else {
preconditionFailure("Unkeyed coding is unsupported.")
}
self.init()
self.era = aDecoder.decodeInteger(forKey: "NS.era")
self.year = aDecoder.decodeInteger(forKey: "NS.year")
self.quarter = aDecoder.decodeInteger(forKey: "NS.quarter")
self.month = aDecoder.decodeInteger(forKey: "NS.month")
self.day = aDecoder.decodeInteger(forKey: "NS.day")
self.hour = aDecoder.decodeInteger(forKey: "NS.hour")
self.minute = aDecoder.decodeInteger(forKey: "NS.minute")
self.second = aDecoder.decodeInteger(forKey: "NS.second")
self.nanosecond = aDecoder.decodeInteger(forKey: "NS.nanosec")
self.weekOfYear = aDecoder.decodeInteger(forKey: "NS.weekOfYear")
self.weekOfMonth = aDecoder.decodeInteger(forKey: "NS.weekOfMonth")
self.yearForWeekOfYear = aDecoder.decodeInteger(forKey: "NS.yearForWOY")
self.weekday = aDecoder.decodeInteger(forKey: "NS.weekday")
self.weekdayOrdinal = aDecoder.decodeInteger(forKey: "NS.weekdayOrdinal")
self.isLeapMonth = aDecoder.decodeBool(forKey: "NS.isLeapMonth")
self.calendar = aDecoder.decodeObject(of: NSCalendar.self, forKey: "NS.calendar")?._swiftObject
self.timeZone = aDecoder.decodeObject(of: NSTimeZone.self, forKey: "NS.timezone")?._swiftObject
}
open func encode(with aCoder: NSCoder) {
guard aCoder.allowsKeyedCoding else {
preconditionFailure("Unkeyed coding is unsupported.")
}
aCoder.encode(self.era, forKey: "NS.era")
aCoder.encode(self.year, forKey: "NS.year")
aCoder.encode(self.quarter, forKey: "NS.quarter")
aCoder.encode(self.month, forKey: "NS.month")
aCoder.encode(self.day, forKey: "NS.day")
aCoder.encode(self.hour, forKey: "NS.hour")
aCoder.encode(self.minute, forKey: "NS.minute")
aCoder.encode(self.second, forKey: "NS.second")
aCoder.encode(self.nanosecond, forKey: "NS.nanosec")
aCoder.encode(self.weekOfYear, forKey: "NS.weekOfYear")
aCoder.encode(self.weekOfMonth, forKey: "NS.weekOfMonth")
aCoder.encode(self.yearForWeekOfYear, forKey: "NS.yearForWOY")
aCoder.encode(self.weekday, forKey: "NS.weekday")
aCoder.encode(self.weekdayOrdinal, forKey: "NS.weekdayOrdinal")
aCoder.encode(self.isLeapMonth, forKey: "NS.isLeapMonth")
aCoder.encode(self.calendar?._nsObject, forKey: "NS.calendar")
aCoder.encode(self.timeZone?._nsObject, forKey: "NS.timezone")
}
static public var supportsSecureCoding: Bool {
return true
}
open override func copy() -> Any {
return copy(with: nil)
}
open func copy(with zone: NSZone? = nil) -> Any {
let newObj = NSDateComponents()
newObj.calendar = calendar
newObj.timeZone = timeZone
newObj.era = era
newObj.year = year
newObj.month = month
newObj.day = day
newObj.hour = hour
newObj.minute = minute
newObj.second = second
newObj.nanosecond = nanosecond
newObj.weekOfYear = weekOfYear
newObj.weekOfMonth = weekOfMonth
newObj.yearForWeekOfYear = yearForWeekOfYear
newObj.weekday = weekday
newObj.weekdayOrdinal = weekdayOrdinal
newObj.quarter = quarter
if leapMonthSet {
newObj.isLeapMonth = isLeapMonth
}
return newObj
}
/*@NSCopying*/ open var calendar: Calendar? {
get {
return _calendar
}
set {
if let val = newValue {
_calendar = val
} else {
_calendar = nil
}
}
}
/*@NSCopying*/ open var timeZone: TimeZone?
open var era: Int {
get {
return _values[0]
}
set {
_values[0] = newValue
}
}
open var year: Int {
get {
return _values[1]
}
set {
_values[1] = newValue
}
}
open var month: Int {
get {
return _values[2]
}
set {
_values[2] = newValue
}
}
open var day: Int {
get {
return _values[3]
}
set {
_values[3] = newValue
}
}
open var hour: Int {
get {
return _values[4]
}
set {
_values[4] = newValue
}
}
open var minute: Int {
get {
return _values[5]
}
set {
_values[5] = newValue
}
}
open var second: Int {
get {
return _values[6]
}
set {
_values[6] = newValue
}
}
open var weekday: Int {
get {
return _values[8]
}
set {
_values[8] = newValue
}
}
open var weekdayOrdinal: Int {
get {
return _values[9]
}
set {
_values[9] = newValue
}
}
open var quarter: Int {
get {
return _values[10]
}
set {
_values[10] = newValue
}
}
open var nanosecond: Int {
get {
return _values[11]
}
set {
_values[11] = newValue
}
}
open var weekOfYear: Int {
get {
return _values[12]
}
set {
_values[12] = newValue
}
}
open var weekOfMonth: Int {
get {
return _values[13]
}
set {
_values[13] = newValue
}
}
open var yearForWeekOfYear: Int {
get {
return _values[14]
}
set {
_values[14] = newValue
}
}
open var isLeapMonth: Bool {
get {
return _values[15] == 1
}
set {
_values[15] = newValue ? 1 : 0
}
}
internal var leapMonthSet: Bool {
return _values[15] != NSDateComponentUndefined
}
/*@NSCopying*/ open var date: Date? {
if let tz = timeZone {
calendar?.timeZone = tz
}
return calendar?.date(from: self._swiftObject)
}
/*
This API allows one to set a specific component of NSDateComponents, by enum constant value rather than property name.
The calendar and timeZone and isLeapMonth properties cannot be set by this method.
*/
open func setValue(_ value: Int, forComponent unit: NSCalendar.Unit) {
switch unit {
case .era:
era = value
case .year:
year = value
case .month:
month = value
case .day:
day = value
case .hour:
hour = value
case .minute:
minute = value
case .second:
second = value
case .nanosecond:
nanosecond = value
case .weekday:
weekday = value
case .weekdayOrdinal:
weekdayOrdinal = value
case .quarter:
quarter = value
case .weekOfMonth:
weekOfMonth = value
case .weekOfYear:
weekOfYear = value
case .yearForWeekOfYear:
yearForWeekOfYear = value
case .calendar:
print(".Calendar cannot be set via \(#function)")
case .timeZone:
print(".TimeZone cannot be set via \(#function)")
default:
break
}
}
/*
This API allows one to get the value of a specific component of NSDateComponents, by enum constant value rather than property name.
The calendar and timeZone and isLeapMonth property values cannot be gotten by this method.
*/
open func value(forComponent unit: NSCalendar.Unit) -> Int {
switch unit {
case .era:
return era
case .year:
return year
case .month:
return month
case .day:
return day
case .hour:
return hour
case .minute:
return minute
case .second:
return second
case .nanosecond:
return nanosecond
case .weekday:
return weekday
case .weekdayOrdinal:
return weekdayOrdinal
case .quarter:
return quarter
case .weekOfMonth:
return weekOfMonth
case .weekOfYear:
return weekOfYear
case .yearForWeekOfYear:
return yearForWeekOfYear
default:
break
}
return NSDateComponentUndefined
}
/*
Reports whether or not the combination of properties which have been set in the receiver is a date which exists in the calendar.
This method is not appropriate for use on NSDateComponents objects which are specifying relative quantities of calendar components.
Except for some trivial cases (e.g., 'seconds' should be 0 - 59 in any calendar), this method is not necessarily cheap.
If the time zone property is set in the NSDateComponents object, it is used.
The calendar property must be set, or NO is returned.
*/
open var isValidDate: Bool {
if let cal = calendar {
return isValidDate(in: cal)
}
return false
}
/*
Reports whether or not the combination of properties which have been set in the receiver is a date which exists in the calendar.
This method is not appropriate for use on NSDateComponents objects which are specifying relative quantities of calendar components.
Except for some trivial cases (e.g., 'seconds' should be 0 - 59 in any calendar), this method is not necessarily cheap.
If the time zone property is set in the NSDateComponents object, it is used.
*/
open func isValidDate(in calendar: Calendar) -> Bool {
var cal = calendar
if let tz = timeZone {
cal.timeZone = tz
}
let ns = nanosecond
if ns != NSDateComponentUndefined && 1000 * 1000 * 1000 <= ns {
return false
}
if ns != NSDateComponentUndefined && 0 < ns {
nanosecond = 0
}
let d = calendar.date(from: self._swiftObject)
if ns != NSDateComponentUndefined && 0 < ns {
nanosecond = ns
}
if let date = d {
let all: NSCalendar.Unit = [.era, .year, .month, .day, .hour, .minute, .second, .weekday, .weekdayOrdinal, .quarter, .weekOfMonth, .weekOfYear, .yearForWeekOfYear]
let comps = cal._bridgeToObjectiveC().components(all, from: date)
var val = era
if val != NSDateComponentUndefined {
if comps.era != val {
return false
}
}
val = year
if val != NSDateComponentUndefined {
if comps.year != val {
return false
}
}
val = month
if val != NSDateComponentUndefined {
if comps.month != val {
return false
}
}
if leapMonthSet {
if comps.isLeapMonth != isLeapMonth {
return false
}
}
val = day
if val != NSDateComponentUndefined {
if comps.day != val {
return false
}
}
val = hour
if val != NSDateComponentUndefined {
if comps.hour != val {
return false
}
}
val = minute
if val != NSDateComponentUndefined {
if comps.minute != val {
return false
}
}
val = second
if val != NSDateComponentUndefined {
if comps.second != val {
return false
}
}
val = weekday
if val != NSDateComponentUndefined {
if comps.weekday != val {
return false
}
}
val = weekdayOrdinal
if val != NSDateComponentUndefined {
if comps.weekdayOrdinal != val {
return false
}
}
val = quarter
if val != NSDateComponentUndefined {
if comps.quarter != val {
return false
}
}
val = weekOfMonth
if val != NSDateComponentUndefined {
if comps.weekOfMonth != val {
return false
}
}
val = weekOfYear
if val != NSDateComponentUndefined {
if comps.weekOfYear != val {
return false
}
}
val = yearForWeekOfYear
if val != NSDateComponentUndefined {
if comps.yearForWeekOfYear != val {
return false
}
}
return true
}
return false
}
}
extension NSDateComponents: _SwiftBridgeable {
typealias SwiftType = DateComponents
var _swiftObject: SwiftType { return DateComponents(reference: self) }
}
extension NSDateComponents: _StructTypeBridgeable {
public typealias _StructType = DateComponents
public func _bridgeToSwift() -> DateComponents {
return DateComponents._unconditionallyBridgeFromObjectiveC(self)
}
}
extension NSDateComponents {
func _createCFDateComponents() -> CFDateComponents {
let components = CFDateComponentsCreate(kCFAllocatorSystemDefault)!
CFDateComponentsSetValue(components, kCFCalendarUnitEra, era)
CFDateComponentsSetValue(components, kCFCalendarUnitYear, year)
CFDateComponentsSetValue(components, kCFCalendarUnitMonth, month)
CFDateComponentsSetValue(components, kCFCalendarUnitDay, day)
CFDateComponentsSetValue(components, kCFCalendarUnitHour, hour)
CFDateComponentsSetValue(components, kCFCalendarUnitMinute, minute)
CFDateComponentsSetValue(components, kCFCalendarUnitSecond, second)
CFDateComponentsSetValue(components, kCFCalendarUnitWeekday, weekday)
CFDateComponentsSetValue(components, kCFCalendarUnitWeekdayOrdinal, weekdayOrdinal)
CFDateComponentsSetValue(components, kCFCalendarUnitQuarter, quarter)
CFDateComponentsSetValue(components, kCFCalendarUnitWeekOfMonth, weekOfMonth)
CFDateComponentsSetValue(components, kCFCalendarUnitWeekOfYear, weekOfYear)
CFDateComponentsSetValue(components, kCFCalendarUnitYearForWeekOfYear, yearForWeekOfYear)
CFDateComponentsSetValue(components, kCFCalendarUnitNanosecond, nanosecond)
return components
}
}