|
6 | 6 | // Copyright © 2015 PureSwift. All rights reserved. |
7 | 7 | // |
8 | 8 |
|
9 | | -#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS) |
10 | | - import Darwin.C |
11 | | - import struct Foundation.Date |
12 | | -#elseif os(Linux) |
13 | | - import Glibc |
14 | | -#endif |
15 | | - |
16 | | -#if os(Linux) || XcodeLinux |
| 9 | +/// `Date` structs represent a single point in time. |
| 10 | +public struct Date: Equatable, Hashable { |
17 | 11 |
|
18 | | - /// `Date` structs represent a single point in time. |
19 | | - public struct Date: Equatable, Hashable, Comparable, CustomStringConvertible { |
20 | | - |
21 | | - // MARK: - Static Properties and Methods |
22 | | - |
23 | | - /// The number of seconds from 1 January 1970 to the reference date, 1 January 2001. |
24 | | - public static let timeIntervalBetween1970AndReferenceDate = 978307200.0 |
25 | | - |
26 | | - /** |
27 | | - Creates and returns a Date value representing a date in the distant future. |
28 | | - |
29 | | - The distant future is in terms of centuries. |
30 | | - */ |
31 | | - public static let distantFuture = Date(timeIntervalSinceReferenceDate: 63113904000.0) |
32 | | - |
33 | | - /** |
34 | | - Creates and returns a Date value representing a date in the distant past. |
35 | | - |
36 | | - The distant past is in terms of centuries. |
37 | | - */ |
38 | | - public static let distantPast = Date(timeIntervalSinceReferenceDate: -63114076800.0) |
39 | | - |
40 | | - /// The interval between 00:00:00 UTC on 1 January 2001 and the current date and time. |
41 | | - public static var timeIntervalSinceReferenceDate: TimeInterval { |
42 | | - |
43 | | - return try! timeval.timeOfDay().timeInterval - Date.timeIntervalBetween1970AndReferenceDate |
44 | | - } |
45 | | - |
46 | | - // MARK: - Properties |
47 | | - |
48 | | - /// The time interval between the date and the reference date (1 January 2001, GMT). |
49 | | - public var timeIntervalSinceReferenceDate: TimeInterval |
50 | | - |
51 | | - /// The time interval between the current date and 1 January 1970, GMT. |
52 | | - public var timeIntervalsince1970: TimeInterval { |
53 | | - |
54 | | - get { return timeIntervalSinceReferenceDate + Date.timeIntervalBetween1970AndReferenceDate } |
55 | | - |
56 | | - set { timeIntervalSinceReferenceDate = timeIntervalsince1970 - Date.timeIntervalBetween1970AndReferenceDate } |
57 | | - } |
58 | | - |
59 | | - /** |
60 | | - The time interval between the date and the current date and time. |
61 | | - |
62 | | - If the date is earlier than the current date and time, the this property’s value is negative. |
63 | | - |
64 | | - - SeeAlso: `timeIntervalSince(_:)` |
65 | | - - SeeAlso: `timeIntervalSince1970` |
66 | | - - SeeAlso: `timeIntervalSinceReferenceDate` |
67 | | - */ |
68 | | - public var timeIntervalSinceNow: TimeInterval { |
69 | | - return timeIntervalSinceReferenceDate - Date.timeIntervalSinceReferenceDate |
70 | | - } |
71 | | - |
72 | | - /** |
73 | | - The interval between the date object and 00:00:00 UTC on 1 January 1970. |
74 | | - |
75 | | - This property’s value is negative if the date object is earlier than 00:00:00 UTC on 1 January 1970. |
76 | | - |
77 | | - - SeeAlso: `timeIntervalSince(_:)` |
78 | | - - SeeAlso: `timeIntervalSinceNow` |
79 | | - - SeeAlso: `timeIntervalSinceReferenceDate` |
80 | | - */ |
81 | | - public var timeIntervalSince1970: TimeInterval { |
82 | | - return timeIntervalSinceReferenceDate + Date.timeIntervalBetween1970AndReferenceDate |
83 | | - } |
84 | | - |
85 | | - public var description: String { |
86 | | - |
87 | | - return "\(timeIntervalSinceReferenceDate)" |
88 | | - } |
89 | | - |
90 | | - public var hashValue: Int { |
91 | | - |
92 | | - return timeIntervalSinceReferenceDate.hashValue |
93 | | - } |
94 | | - |
95 | | - // MARK: - Initialization |
96 | | - |
97 | | - /// Returns a `Date` initialized to the current date and time. |
98 | | - public init() { |
99 | | - |
100 | | - self.timeIntervalSinceReferenceDate = Date.timeIntervalSinceReferenceDate |
101 | | - } |
102 | | - |
103 | | - /// Returns an `Date` initialized relative to 00:00:00 UTC on 1 January 2001 by a given number of seconds. |
104 | | - public init(timeIntervalSinceReferenceDate timeInterval: TimeInterval) { |
105 | | - |
106 | | - self.timeIntervalSinceReferenceDate = timeInterval |
107 | | - } |
108 | | - |
109 | | - /// Returns a `Date` initialized relative to the current date and time by a given number of seconds. |
110 | | - public init(timeIntervalSinceNow: TimeInterval) { |
111 | | - |
112 | | - self.timeIntervalSinceReferenceDate = timeIntervalSinceNow + Date.timeIntervalSinceReferenceDate |
113 | | - } |
114 | | - |
115 | | - /// Returns a `Date` initialized relative to 00:00:00 UTC on 1 January 1970 by a given number of seconds. |
116 | | - public init(timeIntervalSince1970: TimeInterval) { |
117 | | - |
118 | | - self.timeIntervalSinceReferenceDate = timeIntervalSince1970 - Date.timeIntervalBetween1970AndReferenceDate |
119 | | - } |
120 | | - |
121 | | - /** |
122 | | - Returns a `Date` initialized relative to another given date by a given number of seconds. |
123 | | - |
124 | | - - Parameter timeInterval: The number of seconds to add to `date`. A negative value means the receiver will be earlier than `date`. |
125 | | - - Parameter date: The reference date. |
126 | | - */ |
127 | | - public init(timeInterval: TimeInterval, since date: Date) { |
128 | | - |
129 | | - self.timeIntervalSinceReferenceDate = date.timeIntervalSinceReferenceDate + timeInterval |
130 | | - } |
131 | | - |
132 | | - // MARK: - Methods |
133 | | - |
134 | | - /** |
135 | | - Returns the interval between the receiver and another given date. |
136 | | - |
137 | | - - Parameter another: The date with which to compare the receiver. |
138 | | - |
139 | | - - 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. |
140 | | - |
141 | | - - SeeAlso: `timeIntervalSince1970` |
142 | | - - SeeAlso: `timeIntervalSinceNow` |
143 | | - - SeeAlso: `timeIntervalSinceReferenceDate` |
144 | | - */ |
145 | | - public func timeIntervalSince(_ date: Date) -> TimeInterval { |
146 | | - |
147 | | - return timeIntervalSinceReferenceDate - date.timeIntervalSinceReferenceDate |
148 | | - } |
149 | | - } |
| 12 | + // MARK: - Static Properties and Methods |
150 | 13 |
|
151 | | - // MARK: - Operators |
| 14 | + /// The number of seconds from 1 January 1970 to the reference date, 1 January 2001. |
| 15 | + public static var timeIntervalBetween1970AndReferenceDate: SwiftFoundation.TimeInterval { return 978307200.0 } |
152 | 16 |
|
153 | | - public func == (lhs: Date, rhs: Date) -> Bool { |
154 | | - |
155 | | - return lhs.timeIntervalSinceReferenceDate == rhs.timeIntervalSinceReferenceDate |
| 17 | + /** |
| 18 | + Creates and returns a Date value representing a date in the distant future. |
| 19 | + |
| 20 | + The distant future is in terms of centuries. |
| 21 | + */ |
| 22 | + public static var distantFuture: SwiftFoundation.Date { return Date(timeIntervalSinceReferenceDate: 63113904000.0) } |
| 23 | + |
| 24 | + /** |
| 25 | + Creates and returns a Date value representing a date in the distant past. |
| 26 | + |
| 27 | + The distant past is in terms of centuries. |
| 28 | + */ |
| 29 | + public static var distantPast: SwiftFoundation.Date { return Date(timeIntervalSinceReferenceDate: -63114076800.0) } |
| 30 | + |
| 31 | + // MARK: - Properties |
| 32 | + |
| 33 | + /// The time interval between the date and the reference date (1 January 2001, GMT). |
| 34 | + public var timeIntervalSinceReferenceDate: TimeInterval |
| 35 | + |
| 36 | + /// The time interval between the current date and 1 January 1970, GMT. |
| 37 | + public var timeIntervalsince1970: TimeInterval { |
| 38 | + get { return timeIntervalSinceReferenceDate + Date.timeIntervalBetween1970AndReferenceDate } |
| 39 | + set { timeIntervalSinceReferenceDate = newValue - Date.timeIntervalBetween1970AndReferenceDate } |
156 | 40 | } |
157 | 41 |
|
158 | | - public func < (lhs: Date, rhs: Date) -> Bool { |
159 | | - |
160 | | - return lhs.timeIntervalSinceReferenceDate < rhs.timeIntervalSinceReferenceDate |
| 42 | + /** |
| 43 | + The time interval between the date and the current date and time. |
| 44 | + |
| 45 | + If the date is earlier than the current date and time, the this property’s value is negative. |
| 46 | + |
| 47 | + - SeeAlso: `timeIntervalSince(_:)` |
| 48 | + - SeeAlso: `timeIntervalSince1970` |
| 49 | + - SeeAlso: `timeIntervalSinceReferenceDate` |
| 50 | + */ |
| 51 | + public var timeIntervalSinceNow: TimeInterval { |
| 52 | + return timeIntervalSinceReferenceDate - Date.timeIntervalSinceReferenceDate |
161 | 53 | } |
162 | 54 |
|
163 | | - public func - (lhs: Date, rhs: Date) -> TimeInterval { |
164 | | - |
165 | | - return lhs.timeIntervalSinceReferenceDate - rhs.timeIntervalSinceReferenceDate |
| 55 | + /** |
| 56 | + The interval between the date object and 00:00:00 UTC on 1 January 1970. |
| 57 | + |
| 58 | + This property’s value is negative if the date object is earlier than 00:00:00 UTC on 1 January 1970. |
| 59 | + |
| 60 | + - SeeAlso: `timeIntervalSince(_:)` |
| 61 | + - SeeAlso: `timeIntervalSinceNow` |
| 62 | + - SeeAlso: `timeIntervalSinceReferenceDate` |
| 63 | + */ |
| 64 | + public var timeIntervalSince1970: TimeInterval { |
| 65 | + return timeIntervalSinceReferenceDate + Date.timeIntervalBetween1970AndReferenceDate |
166 | 66 | } |
167 | 67 |
|
168 | | - public func + (lhs: Date, rhs: TimeInterval) -> Date { |
| 68 | + // MARK: - Initialization |
| 69 | + |
| 70 | + /// Returns an `Date` initialized relative to 00:00:00 UTC on 1 January 2001 by a given number of seconds. |
| 71 | + public init(timeIntervalSinceReferenceDate timeInterval: TimeInterval) { |
169 | 72 |
|
170 | | - return Date(timeIntervalSinceReferenceDate: lhs.timeIntervalSinceReferenceDate + rhs) |
| 73 | + self.timeIntervalSinceReferenceDate = timeInterval |
171 | 74 | } |
172 | 75 |
|
173 | | - public func - (lhs: Date, rhs: TimeInterval) -> Date { |
| 76 | + /// Returns a `Date` initialized relative to the current date and time by a given number of seconds. |
| 77 | + public init(timeIntervalSinceNow: TimeInterval) { |
174 | 78 |
|
175 | | - return Date(timeIntervalSinceReferenceDate: lhs.timeIntervalSinceReferenceDate - rhs) |
| 79 | + self.timeIntervalSinceReferenceDate = timeIntervalSinceNow + Date.timeIntervalSinceReferenceDate |
176 | 80 | } |
177 | 81 |
|
178 | | - public func += (lhs: inout Date, rhs: TimeInterval) { |
| 82 | + /// Returns a `Date` initialized relative to 00:00:00 UTC on 1 January 1970 by a given number of seconds. |
| 83 | + public init(timeIntervalSince1970: TimeInterval) { |
179 | 84 |
|
180 | | - lhs = lhs + rhs |
| 85 | + self.timeIntervalSinceReferenceDate = timeIntervalSince1970 - Date.timeIntervalBetween1970AndReferenceDate |
181 | 86 | } |
182 | 87 |
|
183 | | - public func -= (lhs: inout Date, rhs: TimeInterval) { |
184 | | - |
185 | | - lhs = lhs - rhs |
| 88 | + /** |
| 89 | + Returns a `Date` initialized relative to another given date by a given number of seconds. |
| 90 | + |
| 91 | + - Parameter timeInterval: The number of seconds to add to `date`. A negative value means the receiver will be earlier than `date`. |
| 92 | + - Parameter date: The reference date. |
| 93 | + */ |
| 94 | + public init(timeInterval: SwiftFoundation.TimeInterval, since date: SwiftFoundation.Date) { |
| 95 | + |
| 96 | + self.timeIntervalSinceReferenceDate = date.timeIntervalSinceReferenceDate + timeInterval |
186 | 97 | } |
187 | 98 |
|
188 | | - // MARK: - Supporting Types |
| 99 | + // MARK: - Methods |
189 | 100 |
|
190 | | - /// Time interval difference between two dates, in seconds. |
191 | | - public typealias TimeInterval = Double |
| 101 | + /** |
| 102 | + Returns the interval between the receiver and another given date. |
| 103 | + |
| 104 | + - Parameter another: The date with which to compare the receiver. |
| 105 | + |
| 106 | + - 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. |
| 107 | + |
| 108 | + - SeeAlso: `timeIntervalSince1970` |
| 109 | + - SeeAlso: `timeIntervalSinceNow` |
| 110 | + - SeeAlso: `timeIntervalSinceReferenceDate` |
| 111 | + */ |
| 112 | + public func timeIntervalSince(_ date: SwiftFoundation.Date) -> SwiftFoundation.TimeInterval { |
| 113 | + return timeIntervalSinceReferenceDate - date.timeIntervalSinceReferenceDate |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +// MARK: - CustomStringConvertible |
192 | 118 |
|
193 | | -#endif |
| 119 | +extension SwiftFoundation.Date: CustomStringConvertible { |
| 120 | + |
| 121 | + public var description: String { |
| 122 | + // TODO: Custom date printing |
| 123 | + return timeIntervalSinceReferenceDate.description |
| 124 | + } |
| 125 | +} |
194 | 126 |
|
195 | | -// MARK: - Darwin |
| 127 | +// MARK: - Comparable |
196 | 128 |
|
197 | | -#if (os(macOS) || os(iOS) || os(watchOS) || os(tvOS)) && !XcodeLinux |
| 129 | +extension SwiftFoundation.Date: Comparable { |
198 | 130 |
|
199 | | - public typealias Date = Foundation.Date |
| 131 | + public static func < (lhs: SwiftFoundation.Date, rhs: SwiftFoundation.Date) -> Bool { |
| 132 | + return lhs.timeIntervalSinceReferenceDate < rhs.timeIntervalSinceReferenceDate |
| 133 | + } |
200 | 134 |
|
201 | | - public func - (lhs: Date, rhs: Date) -> TimeInterval { |
202 | | - |
203 | | - return lhs.timeIntervalSinceReferenceDate - rhs.timeIntervalSinceReferenceDate |
| 135 | + public static func > (lhs: SwiftFoundation.Date, rhs: SwiftFoundation.Date) -> Bool { |
| 136 | + return lhs.timeIntervalSinceReferenceDate > rhs.timeIntervalSinceReferenceDate |
204 | 137 | } |
| 138 | +} |
| 139 | + |
| 140 | +// MARK: - Operators |
| 141 | + |
| 142 | +public func - (lhs: SwiftFoundation.Date, rhs: SwiftFoundation.Date) -> SwiftFoundation.TimeInterval { |
| 143 | + return lhs.timeIntervalSinceReferenceDate - rhs.timeIntervalSinceReferenceDate |
| 144 | +} |
| 145 | + |
| 146 | +public func + (lhs: SwiftFoundation.Date, rhs: SwiftFoundation.TimeInterval) -> SwiftFoundation.Date { |
205 | 147 |
|
206 | | -#endif |
| 148 | + return SwiftFoundation.Date(timeIntervalSinceReferenceDate: lhs.timeIntervalSinceReferenceDate + rhs) |
| 149 | +} |
| 150 | + |
| 151 | +public func - (lhs: SwiftFoundation.Date, rhs: SwiftFoundation.TimeInterval) -> SwiftFoundation.Date { |
| 152 | + |
| 153 | + return SwiftFoundation.Date(timeIntervalSinceReferenceDate: lhs.timeIntervalSinceReferenceDate - rhs) |
| 154 | +} |
| 155 | + |
| 156 | +public func += (lhs: inout SwiftFoundation.Date, rhs: SwiftFoundation.TimeInterval) { |
| 157 | + lhs = lhs + rhs |
| 158 | +} |
| 159 | + |
| 160 | +public func -= (lhs: inout SwiftFoundation.Date, rhs: SwiftFoundation.TimeInterval) { |
| 161 | + lhs = lhs - rhs |
| 162 | +} |
| 163 | + |
| 164 | +// MARK: - Supporting Types |
| 165 | + |
| 166 | +/// Time interval difference between two dates, in seconds. |
| 167 | +public typealias TimeInterval = Double |
0 commit comments