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

Commit c9eb4fa

Browse files
committed
Updated for Swift 5
1 parent 9b2b384 commit c9eb4fa

File tree

4 files changed

+227
-463
lines changed

4 files changed

+227
-463
lines changed

Sources/SwiftFoundation/Date.swift

Lines changed: 131 additions & 170 deletions
Original file line numberDiff line numberDiff line change
@@ -6,201 +6,162 @@
66
// Copyright © 2015 PureSwift. All rights reserved.
77
//
88

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 {
1711

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
15013

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 }
15216

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 }
15640
}
15741

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
16153
}
16254

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
16666
}
16767

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) {
16972

170-
return Date(timeIntervalSinceReferenceDate: lhs.timeIntervalSinceReferenceDate + rhs)
73+
self.timeIntervalSinceReferenceDate = timeInterval
17174
}
17275

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) {
17478

175-
return Date(timeIntervalSinceReferenceDate: lhs.timeIntervalSinceReferenceDate - rhs)
79+
self.timeIntervalSinceReferenceDate = timeIntervalSinceNow + Date.timeIntervalSinceReferenceDate
17680
}
17781

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) {
17984

180-
lhs = lhs + rhs
85+
self.timeIntervalSinceReferenceDate = timeIntervalSince1970 - Date.timeIntervalBetween1970AndReferenceDate
18186
}
18287

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
18697
}
18798

188-
// MARK: - Supporting Types
99+
// MARK: - Methods
189100

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
192118

193-
#endif
119+
extension SwiftFoundation.Date: CustomStringConvertible {
120+
121+
public var description: String {
122+
// TODO: Custom date printing
123+
return timeIntervalSinceReferenceDate.description
124+
}
125+
}
194126

195-
// MARK: - Darwin
127+
// MARK: - Comparable
196128

197-
#if (os(macOS) || os(iOS) || os(watchOS) || os(tvOS)) && !XcodeLinux
129+
extension SwiftFoundation.Date: Comparable {
198130

199-
public typealias Date = Foundation.Date
131+
public static func < (lhs: SwiftFoundation.Date, rhs: SwiftFoundation.Date) -> Bool {
132+
return lhs.timeIntervalSinceReferenceDate < rhs.timeIntervalSinceReferenceDate
133+
}
200134

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
204137
}
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 {
205147

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

Comments
 (0)