|
9 | 9 |
|
10 | 10 | @_implementationOnly import _CoreFoundation |
11 | 11 |
|
12 | | -/** |
13 | | - `Date` represents a single point in time. |
14 | | - |
15 | | - A `Date` is independent of a particular calendar or time zone. To represent a `Date` to a user, you must interpret it in the context of a `Calendar`. |
16 | | - */ |
17 | | -public struct Date : ReferenceConvertible, Comparable, Equatable, Sendable { |
18 | | - public typealias ReferenceType = NSDate |
19 | | - |
20 | | - fileprivate var _time: TimeInterval |
21 | | - |
22 | | - /// The number of seconds from 1 January 1970 to the reference date, 1 January 2001. |
23 | | - public static let timeIntervalBetween1970AndReferenceDate: TimeInterval = 978307200.0 |
24 | | - |
25 | | - /// The interval between 00:00:00 UTC on 1 January 2001 and the current date and time. |
26 | | - public static var timeIntervalSinceReferenceDate: TimeInterval { |
27 | | - return CFAbsoluteTimeGetCurrent() |
28 | | - } |
29 | | - |
30 | | - /// Returns a `Date` initialized to the current date and time. |
31 | | - public static var now: Date { Date() } |
32 | | - |
33 | | - /// Returns a `Date` initialized to the current date and time. |
34 | | - public init() { |
35 | | - _time = CFAbsoluteTimeGetCurrent() |
36 | | - } |
37 | | - |
38 | | - /// Returns a `Date` initialized relative to the current date and time by a given number of seconds. |
39 | | - public init(timeIntervalSinceNow: TimeInterval) { |
40 | | - self.init(timeIntervalSinceReferenceDate: timeIntervalSinceNow + CFAbsoluteTimeGetCurrent()) |
41 | | - } |
42 | | - |
43 | | - /// Returns a `Date` initialized relative to 00:00:00 UTC on 1 January 1970 by a given number of seconds. |
44 | | - public init(timeIntervalSince1970: TimeInterval) { |
45 | | - self.init(timeIntervalSinceReferenceDate: timeIntervalSince1970 - Date.timeIntervalBetween1970AndReferenceDate) |
46 | | - } |
47 | | - |
48 | | - /** |
49 | | - Returns a `Date` initialized relative to another given date by a given number of seconds. |
50 | | - |
51 | | - - Parameter timeInterval: The number of seconds to add to `date`. A negative value means the receiver will be earlier than `date`. |
52 | | - - Parameter date: The reference date. |
53 | | - */ |
54 | | - public init(timeInterval: TimeInterval, since date: Date) { |
55 | | - self.init(timeIntervalSinceReferenceDate: date.timeIntervalSinceReferenceDate + timeInterval) |
56 | | - } |
57 | | - |
58 | | - /// Returns a `Date` initialized relative to 00:00:00 UTC on 1 January 2001 by a given number of seconds. |
59 | | - public init(timeIntervalSinceReferenceDate ti: TimeInterval) { |
60 | | - _time = ti |
61 | | - } |
62 | | - |
63 | | - /** |
64 | | - Returns the interval between the date object and 00:00:00 UTC on 1 January 2001. |
65 | | - |
66 | | - This property's value is negative if the date object is earlier than the system's absolute reference date (00:00:00 UTC on 1 January 2001). |
67 | | - */ |
68 | | - public var timeIntervalSinceReferenceDate: TimeInterval { |
69 | | - return _time |
70 | | - } |
71 | | - |
72 | | - /** |
73 | | - Returns the interval between the receiver and another given date. |
74 | | - |
75 | | - - Parameter another: The date with which to compare the receiver. |
76 | | - |
77 | | - - Returns: The interval between the receiver and the `another` parameter. If the receiver is earlier than `anotherDate`, the return value is negative. If `anotherDate` is `nil`, the results are undefined. |
78 | | - |
79 | | - - SeeAlso: `timeIntervalSince1970` |
80 | | - - SeeAlso: `timeIntervalSinceNow` |
81 | | - - SeeAlso: `timeIntervalSinceReferenceDate` |
82 | | - */ |
83 | | - public func timeIntervalSince(_ date: Date) -> TimeInterval { |
84 | | - return self.timeIntervalSinceReferenceDate - date.timeIntervalSinceReferenceDate |
85 | | - } |
86 | | - |
87 | | - /** |
88 | | - The time interval between the date and the current date and time. |
89 | | - |
90 | | - If the date is earlier than the current date and time, this property's value is negative. |
91 | | - |
92 | | - - SeeAlso: `timeIntervalSince(_:)` |
93 | | - - SeeAlso: `timeIntervalSince1970` |
94 | | - - SeeAlso: `timeIntervalSinceReferenceDate` |
95 | | - */ |
96 | | - public var timeIntervalSinceNow: TimeInterval { |
97 | | - return self.timeIntervalSinceReferenceDate - CFAbsoluteTimeGetCurrent() |
98 | | - } |
99 | | - |
100 | | - /** |
101 | | - The interval between the date object and 00:00:00 UTC on 1 January 1970. |
102 | | - |
103 | | - This property's value is negative if the date object is earlier than 00:00:00 UTC on 1 January 1970. |
104 | | - |
105 | | - - SeeAlso: `timeIntervalSince(_:)` |
106 | | - - SeeAlso: `timeIntervalSinceNow` |
107 | | - - SeeAlso: `timeIntervalSinceReferenceDate` |
108 | | - */ |
109 | | - public var timeIntervalSince1970: TimeInterval { |
110 | | - return self.timeIntervalSinceReferenceDate + Date.timeIntervalBetween1970AndReferenceDate |
111 | | - } |
112 | | - |
113 | | - /// Return a new `Date` by adding a `TimeInterval` to this `Date`. |
114 | | - /// |
115 | | - /// - parameter timeInterval: The value to add, in seconds. |
116 | | - /// - warning: This only adjusts an absolute value. If you wish to add calendrical concepts like hours, days, months then you must use a `Calendar`. That will take into account complexities like daylight saving time, months with different numbers of days, and more. |
117 | | - public func addingTimeInterval(_ timeInterval: TimeInterval) -> Date { |
118 | | - return self + timeInterval |
119 | | - } |
120 | | - |
121 | | - /// Add a `TimeInterval` to this `Date`. |
122 | | - /// |
123 | | - /// - parameter timeInterval: The value to add, in seconds. |
124 | | - /// - warning: This only adjusts an absolute value. If you wish to add calendrical concepts like hours, days, months then you must use a `Calendar`. That will take into account complexities like daylight saving time, months with different numbers of days, and more. |
125 | | - public mutating func addTimeInterval(_ timeInterval: TimeInterval) { |
126 | | - self += timeInterval |
127 | | - } |
128 | | - |
129 | | - /** |
130 | | - Creates and returns a Date value representing a date in the distant future. |
131 | | - |
132 | | - The distant future is in terms of centuries. |
133 | | - */ |
134 | | - public static let distantFuture = Date(timeIntervalSinceReferenceDate: 63113904000.0) |
135 | | - |
136 | | - /** |
137 | | - Creates and returns a Date value representing a date in the distant past. |
138 | | - |
139 | | - The distant past is in terms of centuries. |
140 | | - */ |
141 | | - public static let distantPast = Date(timeIntervalSinceReferenceDate: -63114076800.0) |
142 | | - |
143 | | - public func hash(into hasher: inout Hasher) { |
144 | | - hasher.combine(_time) |
145 | | - } |
146 | | - |
147 | | - /// Compare two `Date` values. |
148 | | - public func compare(_ other: Date) -> ComparisonResult { |
149 | | - if _time < other.timeIntervalSinceReferenceDate { |
150 | | - return .orderedAscending |
151 | | - } else if _time > other.timeIntervalSinceReferenceDate { |
152 | | - return .orderedDescending |
153 | | - } else { |
154 | | - return .orderedSame |
155 | | - } |
156 | | - } |
157 | | - |
158 | | - /// Returns true if the two `Date` values represent the same point in time. |
159 | | - public static func ==(lhs: Date, rhs: Date) -> Bool { |
160 | | - return lhs.timeIntervalSinceReferenceDate == rhs.timeIntervalSinceReferenceDate |
161 | | - } |
162 | | - |
163 | | - /// Returns true if the left hand `Date` is earlier in time than the right hand `Date`. |
164 | | - public static func <(lhs: Date, rhs: Date) -> Bool { |
165 | | - return lhs.timeIntervalSinceReferenceDate < rhs.timeIntervalSinceReferenceDate |
166 | | - } |
167 | | - |
168 | | - /// Returns true if the left hand `Date` is later in time than the right hand `Date`. |
169 | | - public static func >(lhs: Date, rhs: Date) -> Bool { |
170 | | - return lhs.timeIntervalSinceReferenceDate > rhs.timeIntervalSinceReferenceDate |
171 | | - } |
172 | | - |
173 | | - /// Returns a `Date` with a specified amount of time added to it. |
174 | | - public static func +(lhs: Date, rhs: TimeInterval) -> Date { |
175 | | - return Date(timeIntervalSinceReferenceDate: lhs.timeIntervalSinceReferenceDate + rhs) |
176 | | - } |
177 | | - |
178 | | - /// Returns a `Date` with a specified amount of time subtracted from it. |
179 | | - public static func -(lhs: Date, rhs: TimeInterval) -> Date { |
180 | | - return Date(timeIntervalSinceReferenceDate: lhs.timeIntervalSinceReferenceDate - rhs) |
181 | | - } |
182 | | - |
183 | | - /// Add a `TimeInterval` to a `Date`. |
184 | | - /// |
185 | | - /// - warning: This only adjusts an absolute value. If you wish to add calendrical concepts like hours, days, months then you must use a `Calendar`. That will take into account complexities like daylight saving time, months with different numbers of days, and more. |
186 | | - public static func +=(lhs: inout Date, rhs: TimeInterval) { |
187 | | - lhs = lhs + rhs |
188 | | - } |
189 | | - |
190 | | - /// Subtract a `TimeInterval` from a `Date`. |
191 | | - /// |
192 | | - /// - warning: This only adjusts an absolute value. If you wish to add calendrical concepts like hours, days, months then you must use a `Calendar`. That will take into account complexities like daylight saving time, months with different numbers of days, and more. |
193 | | - public static func -=(lhs: inout Date, rhs: TimeInterval) { |
194 | | - lhs = lhs - rhs |
195 | | - } |
196 | | - |
197 | | - public typealias Stride = TimeInterval |
198 | | - |
199 | | - /// Returns the `TimeInterval` between this `Date` and another given date. |
200 | | - /// |
201 | | - /// - returns: The interval between the receiver and the another parameter. If the receiver is earlier than `other`, the return value is negative. |
202 | | - public func distance(to other: Date) -> TimeInterval { |
203 | | - return other.timeIntervalSince(self) |
204 | | - } |
205 | | - |
206 | | - /// Creates a new date value by adding a `TimeInterval` to this `Date`. |
207 | | - /// |
208 | | - /// - warning: This only adjusts an absolute value. If you wish to add calendrical concepts like hours, days, months then you must use a `Calendar`. That will take into account complexities like daylight saving time, months with different numbers of days, and more. |
209 | | - public func advanced(by n: TimeInterval) -> Date { |
210 | | - return self.addingTimeInterval(n) |
211 | | - } |
212 | | -} |
213 | | - |
214 | | -extension Date : CustomDebugStringConvertible, CustomStringConvertible, CustomReflectable { |
215 | | - /** |
216 | | - A string representation of the date object (read-only). |
217 | | - |
218 | | - The representation is useful for debugging only. |
219 | | - |
220 | | - There are a number of options to acquire a formatted string for a date including: date formatters (see |
221 | | - [NSDateFormatter](//apple_ref/occ/cl/NSDateFormatter) and [Data Formatting Guide](//apple_ref/doc/uid/10000029i)), and the `Date` function `description(locale:)`. |
222 | | - */ |
223 | | - public var description: String { |
224 | | - // Defer to NSDate for description |
225 | | - return NSDate(timeIntervalSinceReferenceDate: _time).description |
226 | | - } |
227 | | - |
228 | | - /** |
229 | | - Returns a string representation of the receiver using the given |
230 | | - locale. |
231 | | - |
232 | | - - Parameter locale: A `Locale`. If you pass `nil`, `Date` formats the date in the same way as the `description` property. |
233 | | - |
234 | | - - Returns: A string representation of the `Date`, using the given locale, or if the locale argument is `nil`, in the international format `YYYY-MM-DD HH:MM:SS ±HHMM`, where `±HHMM` represents the time zone offset in hours and minutes from UTC (for example, "`2001-03-24 10:45:32 +0600`"). |
235 | | - */ |
236 | | - public func description(with locale: Locale?) -> String { |
237 | | - return NSDate(timeIntervalSinceReferenceDate: _time).description(with: locale) |
238 | | - } |
239 | | - |
240 | | - public var debugDescription: String { |
241 | | - return description |
242 | | - } |
243 | | - |
244 | | - public var customMirror: Mirror { |
245 | | - var c: [(label: String?, value: Any)] = [] |
246 | | - c.append((label: "timeIntervalSinceReferenceDate", value: timeIntervalSinceReferenceDate)) |
247 | | - return Mirror(self, children: c, displayStyle: .struct) |
248 | | - } |
249 | | -} |
250 | | - |
251 | 12 | extension Date : _ObjectiveCBridgeable { |
252 | 13 | @_semantics("convertToObjectiveC") |
253 | 14 | public func _bridgeToObjectiveC() -> NSDate { |
254 | | - return NSDate(timeIntervalSinceReferenceDate: _time) |
| 15 | + return NSDate(timeIntervalSinceReferenceDate: timeIntervalSinceReferenceDate) |
255 | 16 | } |
256 | 17 |
|
257 | 18 | public static func _forceBridgeFromObjectiveC(_ x: NSDate, result: inout Date?) { |
@@ -280,16 +41,3 @@ extension Date : CustomPlaygroundDisplayConvertible { |
280 | 41 | return df.string(from: self) |
281 | 42 | } |
282 | 43 | } |
283 | | - |
284 | | -extension Date : Codable { |
285 | | - public init(from decoder: Decoder) throws { |
286 | | - let container = try decoder.singleValueContainer() |
287 | | - let timestamp = try container.decode(Double.self) |
288 | | - self.init(timeIntervalSinceReferenceDate: timestamp) |
289 | | - } |
290 | | - |
291 | | - public func encode(to encoder: Encoder) throws { |
292 | | - var container = encoder.singleValueContainer() |
293 | | - try container.encode(self.timeIntervalSinceReferenceDate) |
294 | | - } |
295 | | -} |
0 commit comments