1 //===----------------------------------------------------------------------===//
3 // This source file is part of the Swift.org open source project
5 // Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
6 // Licensed under Apache License v2.0 with Runtime Library Exception
8 // See http://swift.org/LICENSE.txt for license information
9 // See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
11 //===----------------------------------------------------------------------===//
14 // DISPATCH_TIME_NOW: ok
15 // DISPATCH_TIME_FOREVER: ok
19 public struct DispatchTime : Comparable {
20 public let rawValue: dispatch_time_t
22 public static func now() -> DispatchTime {
23 let t = CDispatch.dispatch_time(0, 0)
24 return DispatchTime(rawValue: t)
27 public static let distantFuture = DispatchTime(rawValue: ~0)
29 fileprivate init(rawValue: dispatch_time_t) {
30 self.rawValue = rawValue
33 /// Creates a `DispatchTime` relative to the system clock that
37 /// - uptimeNanoseconds: The number of nanoseconds since boot, excluding
38 /// time the system spent asleep
39 /// - Returns: A new `DispatchTime`
40 public init(uptimeNanoseconds: UInt64) {
41 self.rawValue = dispatch_time_t(uptimeNanoseconds)
44 public var uptimeNanoseconds: UInt64 {
45 return UInt64(self.rawValue)
49 public func <(a: DispatchTime, b: DispatchTime) -> Bool {
50 if a.rawValue == ~0 || b.rawValue == ~0 { return false }
51 return a.rawValue < b.rawValue
54 public func ==(a: DispatchTime, b: DispatchTime) -> Bool {
55 return a.rawValue == b.rawValue
58 public struct DispatchWallTime : Comparable {
59 public let rawValue: dispatch_time_t
61 public static func now() -> DispatchWallTime {
62 return DispatchWallTime(rawValue: CDispatch.dispatch_walltime(nil, 0))
65 public static let distantFuture = DispatchWallTime(rawValue: ~0)
67 fileprivate init(rawValue: dispatch_time_t) {
68 self.rawValue = rawValue
71 public init(timespec: timespec) {
73 self.rawValue = CDispatch.dispatch_walltime(&t, 0)
77 public func <(a: DispatchWallTime, b: DispatchWallTime) -> Bool {
78 if a.rawValue == ~0 || b.rawValue == ~0 { return false }
79 return -Int64(a.rawValue) < -Int64(b.rawValue)
82 public func ==(a: DispatchWallTime, b: DispatchWallTime) -> Bool {
83 return a.rawValue == b.rawValue
86 public enum DispatchTimeInterval {
88 case milliseconds(Int)
89 case microseconds(Int)
92 internal var rawValue: Int64 {
94 case .seconds(let s): return Int64(s) * Int64(NSEC_PER_SEC)
95 case .milliseconds(let ms): return Int64(ms) * Int64(NSEC_PER_MSEC)
96 case .microseconds(let us): return Int64(us) * Int64(NSEC_PER_USEC)
97 case .nanoseconds(let ns): return Int64(ns)
102 public func +(time: DispatchTime, interval: DispatchTimeInterval) -> DispatchTime {
103 let t = CDispatch.dispatch_time(time.rawValue, interval.rawValue)
104 return DispatchTime(rawValue: t)
107 public func -(time: DispatchTime, interval: DispatchTimeInterval) -> DispatchTime {
108 let t = CDispatch.dispatch_time(time.rawValue, -interval.rawValue)
109 return DispatchTime(rawValue: t)
112 public func +(time: DispatchTime, seconds: Double) -> DispatchTime {
113 let t = CDispatch.dispatch_time(time.rawValue, Int64(seconds * Double(NSEC_PER_SEC)))
114 return DispatchTime(rawValue: t)
117 public func -(time: DispatchTime, seconds: Double) -> DispatchTime {
118 let t = CDispatch.dispatch_time(time.rawValue, Int64(-seconds * Double(NSEC_PER_SEC)))
119 return DispatchTime(rawValue: t)
122 public func +(time: DispatchWallTime, interval: DispatchTimeInterval) -> DispatchWallTime {
123 let t = CDispatch.dispatch_time(time.rawValue, interval.rawValue)
124 return DispatchWallTime(rawValue: t)
127 public func -(time: DispatchWallTime, interval: DispatchTimeInterval) -> DispatchWallTime {
128 let t = CDispatch.dispatch_time(time.rawValue, -interval.rawValue)
129 return DispatchWallTime(rawValue: t)
132 public func +(time: DispatchWallTime, seconds: Double) -> DispatchWallTime {
133 let t = CDispatch.dispatch_time(time.rawValue, Int64(seconds * Double(NSEC_PER_SEC)))
134 return DispatchWallTime(rawValue: t)
137 public func -(time: DispatchWallTime, seconds: Double) -> DispatchWallTime {
138 let t = CDispatch.dispatch_time(time.rawValue, Int64(-seconds * Double(NSEC_PER_SEC)))
139 return DispatchWallTime(rawValue: t)