Skip to content
This repository was archived by the owner on Dec 15, 2024. It is now read-only.

Commit 628daa9

Browse files
committed
Updated for Swift 5
1 parent 7ae773d commit 628daa9

File tree

2 files changed

+76
-24
lines changed

2 files changed

+76
-24
lines changed

Sources/SwiftFoundation/ComparisonResult.swift

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,12 @@ public extension Comparable {
1919

2020
/// Compares the reciever with another and returns their order.
2121
func compare(_ other: Self) -> ComparisonResult {
22-
2322
if self < other {
24-
2523
return .orderedAscending
2624
}
27-
2825
if self > other {
29-
3026
return .orderedDescending
3127
}
32-
3328
return .orderedSame
3429
}
3530
}

Sources/SwiftFoundation/Date.swift

Lines changed: 76 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -69,19 +69,16 @@ public struct Date: Equatable, Hashable {
6969

7070
/// Returns an `Date` initialized relative to 00:00:00 UTC on 1 January 2001 by a given number of seconds.
7171
public init(timeIntervalSinceReferenceDate timeInterval: TimeInterval) {
72-
7372
self.timeIntervalSinceReferenceDate = timeInterval
7473
}
7574

7675
/// Returns a `Date` initialized relative to the current date and time by a given number of seconds.
7776
public init(timeIntervalSinceNow: TimeInterval) {
78-
7977
self.timeIntervalSinceReferenceDate = timeIntervalSinceNow + Date.timeIntervalSinceReferenceDate
8078
}
8179

8280
/// Returns a `Date` initialized relative to 00:00:00 UTC on 1 January 1970 by a given number of seconds.
8381
public init(timeIntervalSince1970: TimeInterval) {
84-
8582
self.timeIntervalSinceReferenceDate = timeIntervalSince1970 - Date.timeIntervalBetween1970AndReferenceDate
8683
}
8784

@@ -92,7 +89,6 @@ public struct Date: Equatable, Hashable {
9289
- Parameter date: The reference date.
9390
*/
9491
public init(timeInterval: SwiftFoundation.TimeInterval, since date: SwiftFoundation.Date) {
95-
9692
self.timeIntervalSinceReferenceDate = date.timeIntervalSinceReferenceDate + timeInterval
9793
}
9894

@@ -112,6 +108,22 @@ public struct Date: Equatable, Hashable {
112108
public func timeIntervalSince(_ date: SwiftFoundation.Date) -> SwiftFoundation.TimeInterval {
113109
return timeIntervalSinceReferenceDate - date.timeIntervalSinceReferenceDate
114110
}
111+
112+
/// Return a new `Date` by adding a `TimeInterval` to this `Date`.
113+
///
114+
/// - parameter timeInterval: The value to add, in seconds.
115+
/// - 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.
116+
public func addingTimeInterval(_ timeInterval: TimeInterval) -> Date {
117+
return self + timeInterval
118+
}
119+
120+
/// Add a `TimeInterval` to this `Date`.
121+
///
122+
/// - parameter timeInterval: The value to add, in seconds.
123+
/// - 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.
124+
public mutating func addTimeInterval(_ timeInterval: TimeInterval) {
125+
self += timeInterval
126+
}
115127
}
116128

117129
// MARK: - CustomStringConvertible
@@ -124,6 +136,15 @@ extension SwiftFoundation.Date: CustomStringConvertible {
124136
}
125137
}
126138

139+
// MARK: - CustomDebugStringConvertible
140+
141+
extension SwiftFoundation.Date: CustomDebugStringConvertible {
142+
143+
public var debugDescription: String {
144+
return description
145+
}
146+
}
147+
127148
// MARK: - Comparable
128149

129150
extension SwiftFoundation.Date: Comparable {
@@ -139,26 +160,62 @@ extension SwiftFoundation.Date: Comparable {
139160

140161
// MARK: - Operators
141162

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 {
163+
public extension SwiftFoundation.Date {
147164

148-
return SwiftFoundation.Date(timeIntervalSinceReferenceDate: lhs.timeIntervalSinceReferenceDate + rhs)
149-
}
150-
151-
public func - (lhs: SwiftFoundation.Date, rhs: SwiftFoundation.TimeInterval) -> SwiftFoundation.Date {
165+
/// Returns a `Date` with a specified amount of time added to it.
166+
static func +(lhs: Date, rhs: TimeInterval) -> Date {
167+
return Date(timeIntervalSinceReferenceDate: lhs.timeIntervalSinceReferenceDate + rhs)
168+
}
152169

153-
return SwiftFoundation.Date(timeIntervalSinceReferenceDate: lhs.timeIntervalSinceReferenceDate - rhs)
154-
}
170+
/// Returns a `Date` with a specified amount of time subtracted from it.
171+
static func -(lhs: Date, rhs: TimeInterval) -> Date {
172+
return Date(timeIntervalSinceReferenceDate: lhs.timeIntervalSinceReferenceDate - rhs)
173+
}
174+
175+
/// Add a `TimeInterval` to a `Date`.
176+
///
177+
/// - 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.
178+
static func +=(lhs: inout Date, rhs: TimeInterval) {
179+
lhs = lhs + rhs
180+
}
181+
182+
/// Subtract a `TimeInterval` from a `Date`.
183+
///
184+
/// - 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.
185+
static func -=(lhs: inout Date, rhs: TimeInterval) {
186+
lhs = lhs - rhs
187+
}
155188

156-
public func += (lhs: inout SwiftFoundation.Date, rhs: SwiftFoundation.TimeInterval) {
157-
lhs = lhs + rhs
189+
typealias Stride = TimeInterval
190+
191+
/// Returns the `TimeInterval` between this `Date` and another given date.
192+
///
193+
/// - returns: The interval between the receiver and the another parameter. If the receiver is earlier than `other`, the return value is negative.
194+
func distance(to other: Date) -> TimeInterval {
195+
return other.timeIntervalSince(self)
196+
}
197+
198+
/// Creates a new date value by adding a `TimeInterval` to this `Date`.
199+
///
200+
/// - 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.
201+
func advanced(by n: TimeInterval) -> Date {
202+
return self.addingTimeInterval(n)
203+
}
158204
}
159205

160-
public func -= (lhs: inout SwiftFoundation.Date, rhs: SwiftFoundation.TimeInterval) {
161-
lhs = lhs - rhs
206+
// MARK: - Codable
207+
208+
extension Date: Codable {
209+
public init(from decoder: Decoder) throws {
210+
let container = try decoder.singleValueContainer()
211+
let timestamp = try container.decode(Double.self)
212+
self.init(timeIntervalSinceReferenceDate: timestamp)
213+
}
214+
215+
public func encode(to encoder: Encoder) throws {
216+
var container = encoder.singleValueContainer()
217+
try container.encode(self.timeIntervalSinceReferenceDate)
218+
}
162219
}
163220

164221
// MARK: - Supporting Types

0 commit comments

Comments
 (0)