|
| 1 | +// |
| 2 | +// Date.swift |
| 3 | +// Foundation |
| 4 | +// |
| 5 | +// Created by Philippe Hausler on 5/13/16. |
| 6 | +// Copyright © 2016 Apple. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import CoreFoundation |
| 10 | + |
| 11 | +/** |
| 12 | + `Date` structs represent a single point in time. |
| 13 | + */ |
| 14 | +public struct Date : Comparable, Equatable, CustomStringConvertible { |
| 15 | + public typealias ReferenceType = NSDate |
| 16 | + |
| 17 | + private var _time : NSTimeInterval |
| 18 | + |
| 19 | + /// The number of seconds from 1 January 1970 to the reference date, 1 January 2001. |
| 20 | + public static let timeIntervalBetween1970AndReferenceDate = 978307200.0 |
| 21 | + |
| 22 | + /// The interval between 00:00:00 UTC on 1 January 2001 and the current date and time. |
| 23 | + public static var timeIntervalSinceReferenceDate : NSTimeInterval { |
| 24 | + return CFAbsoluteTimeGetCurrent() |
| 25 | + } |
| 26 | + |
| 27 | + /// Returns a `Date` initialized to the current date and time. |
| 28 | + public init() { |
| 29 | + _time = CFAbsoluteTimeGetCurrent() |
| 30 | + } |
| 31 | + |
| 32 | + /// Returns a `Date` initialized relative to the current date and time by a given number of seconds. |
| 33 | + public init(timeIntervalSinceNow: NSTimeInterval) { |
| 34 | + self.init(timeIntervalSinceReferenceDate: timeIntervalSinceNow + CFAbsoluteTimeGetCurrent()) |
| 35 | + } |
| 36 | + |
| 37 | + /// Returns a `Date` initialized relative to 00:00:00 UTC on 1 January 1970 by a given number of seconds. |
| 38 | + public init(timeIntervalSince1970: NSTimeInterval) { |
| 39 | + self.init(timeIntervalSinceReferenceDate: timeIntervalSince1970 - Date.timeIntervalBetween1970AndReferenceDate) |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + Returns a `Date` initialized relative to another given date by a given number of seconds. |
| 44 | + |
| 45 | + - Parameter timeInterval: The number of seconds to add to `date`. A negative value means the receiver will be earlier than `date`. |
| 46 | + - Parameter date: The reference date. |
| 47 | + */ |
| 48 | + public init(timeInterval: NSTimeInterval, since date: Date) { |
| 49 | + self.init(timeIntervalSinceReferenceDate: date.timeIntervalSinceReferenceDate + timeInterval) |
| 50 | + } |
| 51 | + |
| 52 | + /// Returns an `Date` initialized relative to 00:00:00 UTC on 1 January 2001 by a given number of seconds. |
| 53 | + public init(timeIntervalSinceReferenceDate ti: NSTimeInterval) { |
| 54 | + _time = ti |
| 55 | + } |
| 56 | + |
| 57 | + /// Returns a `Date` initialized with the value from a `NSDate`. `Date` does not store the reference after initialization. |
| 58 | + private init(reference: NSDate) { |
| 59 | + self.init(timeIntervalSinceReferenceDate: reference.timeIntervalSinceReferenceDate) |
| 60 | + } |
| 61 | + |
| 62 | + private var reference : NSDate { |
| 63 | + return NSDate(timeIntervalSinceReferenceDate: _time) |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + Returns the interval between the date object and 00:00:00 UTC on 1 January 2001. |
| 68 | + |
| 69 | + 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). |
| 70 | + */ |
| 71 | + public var timeIntervalSinceReferenceDate: NSTimeInterval { |
| 72 | + return _time |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + Returns the interval between the receiver and another given date. |
| 77 | + |
| 78 | + - Parameter another: The date with which to compare the receiver. |
| 79 | + |
| 80 | + - 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. |
| 81 | + |
| 82 | + - SeeAlso: `timeIntervalSince1970` |
| 83 | + - SeeAlso: `timeIntervalSinceNow` |
| 84 | + - SeeAlso: `timeIntervalSinceReferenceDate` |
| 85 | + */ |
| 86 | + public func timeIntervalSince(_ date: Date) -> NSTimeInterval { |
| 87 | + return self.timeIntervalSinceReferenceDate - date.timeIntervalSinceReferenceDate |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + The time interval between the date and the current date and time. |
| 92 | + |
| 93 | + If the date is earlier than the current date and time, the this property’s value is negative. |
| 94 | + |
| 95 | + - SeeAlso: `timeIntervalSince(_:)` |
| 96 | + - SeeAlso: `timeIntervalSince1970` |
| 97 | + - SeeAlso: `timeIntervalSinceReferenceDate` |
| 98 | + */ |
| 99 | + public var timeIntervalSinceNow: NSTimeInterval { |
| 100 | + return self.timeIntervalSinceReferenceDate - CFAbsoluteTimeGetCurrent() |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + The interval between the date object and 00:00:00 UTC on 1 January 1970. |
| 105 | + |
| 106 | + This property’s value is negative if the date object is earlier than 00:00:00 UTC on 1 January 1970. |
| 107 | + |
| 108 | + - SeeAlso: `timeIntervalSince(_:)` |
| 109 | + - SeeAlso: `timeIntervalSinceNow` |
| 110 | + - SeeAlso: `timeIntervalSinceReferenceDate` |
| 111 | + */ |
| 112 | + public var timeIntervalSince1970: NSTimeInterval { |
| 113 | + return self.timeIntervalSinceReferenceDate + Date.timeIntervalBetween1970AndReferenceDate |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + Creates and returns a Date value representing a date in the distant future. |
| 118 | + |
| 119 | + The distant future is in terms of centuries. |
| 120 | + */ |
| 121 | + public static let distantFuture = Date(timeIntervalSinceReferenceDate: 63113904000.0) |
| 122 | + |
| 123 | + /** |
| 124 | + Creates and returns a Date value representing a date in the distant past. |
| 125 | + |
| 126 | + The distant past is in terms of centuries. |
| 127 | + */ |
| 128 | + public static let distantPast = Date(timeIntervalSinceReferenceDate: -63114076800.0) |
| 129 | + |
| 130 | + public var hashValue: Int { |
| 131 | + if #available(OSX 10.12, iOS 10.0, *) { |
| 132 | + return Int(bitPattern: __CFHashDouble(_time)) |
| 133 | + } else { // 10.11 and previous behavior fallback; this must allocate a date to reference the hash value and then throw away the reference |
| 134 | + return NSDate(timeIntervalSinceReferenceDate: _time).hash |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + public func compare(_ other: Date) -> NSComparisonResult { |
| 139 | + if _time < other.timeIntervalSinceReferenceDate { |
| 140 | + return .OrderedAscending |
| 141 | + } else if _time > other.timeIntervalSinceReferenceDate { |
| 142 | + return .OrderedDescending |
| 143 | + } else { |
| 144 | + return .OrderedSame |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + A string representation of the date object (read-only). |
| 150 | + |
| 151 | + The representation is useful for debugging only. |
| 152 | + |
| 153 | + There are a number of options to acquire a formatted string for a date including: date formatters (see |
| 154 | + [NSDateFormatter](//apple_ref/occ/cl/NSDateFormatter) and [Data Formatting Guide](//apple_ref/doc/uid/10000029i)), and the `Date` functions `description(locale:)`. |
| 155 | + */ |
| 156 | + public var description: String { |
| 157 | + // Defer to NSDate for description |
| 158 | + return NSDate(timeIntervalSinceReferenceDate: _time).description |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + Returns a string representation of the receiver using the given |
| 163 | + locale. |
| 164 | + |
| 165 | + - Parameter locale: A `Locale` object. If you pass `nil`, `NSDate` formats the date in the same way as the `description` property. |
| 166 | + |
| 167 | + - Returns: A string representation of the receiver, 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`”) |
| 168 | + */ |
| 169 | + public func description(with locale : NSLocale?) -> String { |
| 170 | + return NSDate(timeIntervalSinceReferenceDate: _time).descriptionWithLocale(locale) |
| 171 | + } |
| 172 | + |
| 173 | + public var debugDescription: String { return description } |
| 174 | +} |
| 175 | + |
| 176 | +/// Returns true if the two Date values are equal. |
| 177 | +public func ==(lhs: Date, rhs: Date) -> Bool { |
| 178 | + return lhs.timeIntervalSinceReferenceDate == rhs.timeIntervalSinceReferenceDate |
| 179 | +} |
| 180 | + |
| 181 | +/// Returns true if the left hand Date is less than the right hand Date. |
| 182 | +public func <(lhs: Date, rhs: Date) -> Bool { |
| 183 | + return lhs.timeIntervalSinceReferenceDate < rhs.timeIntervalSinceReferenceDate |
| 184 | +} |
| 185 | + |
| 186 | +/// Returns a Date with a specified amount of time added to it. |
| 187 | +public func +(lhs: Date, rhs: NSTimeInterval) -> Date { |
| 188 | + return Date(timeIntervalSinceReferenceDate: lhs.timeIntervalSinceReferenceDate + rhs) |
| 189 | +} |
| 190 | + |
| 191 | +/// Returns a Date with a specified amount of time subtracted from it. |
| 192 | +public func -(lhs: Date, rhs: NSTimeInterval) -> Date { |
| 193 | + return Date(timeIntervalSinceReferenceDate: lhs.timeIntervalSinceReferenceDate - rhs) |
| 194 | +} |
| 195 | + |
| 196 | +public func +=(lhs: inout Date, rhs: NSTimeInterval) { |
| 197 | + lhs = lhs + rhs |
| 198 | +} |
| 199 | + |
| 200 | +public func -=(lhs: inout Date, rhs: NSTimeInterval) { |
| 201 | + lhs = lhs - rhs |
| 202 | +} |
0 commit comments