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 {
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 private init(rawValue: dispatch_time_t) {
30 self.rawValue = rawValue
34 public struct DispatchWallTime {
35 public let rawValue: dispatch_time_t
37 public static func now() -> DispatchWallTime {
38 return DispatchWallTime(rawValue: CDispatch.dispatch_walltime(nil, 0))
41 public static let distantFuture = DispatchWallTime(rawValue: ~0)
43 private init(rawValue: dispatch_time_t) {
44 self.rawValue = rawValue
47 public init(time: timespec) {
49 self.rawValue = CDispatch.dispatch_walltime(&t, 0)
53 @available(*, deprecated, renamed: "DispatchWallTime")
54 public typealias DispatchWalltime = DispatchWallTime
56 public enum DispatchTimeInterval {
58 case milliseconds(Int)
59 case microseconds(Int)
62 internal var rawValue: UInt64 {
64 case .seconds(let s): return UInt64(s) * NSEC_PER_SEC
65 case .milliseconds(let ms): return UInt64(ms) * NSEC_PER_MSEC
66 case .microseconds(let us): return UInt64(us) * NSEC_PER_USEC
67 case .nanoseconds(let ns): return UInt64(ns)
72 public func +(time: DispatchTime, interval: DispatchTimeInterval) -> DispatchTime {
73 let t = CDispatch.dispatch_time(time.rawValue, Int64(interval.rawValue))
74 return DispatchTime(rawValue: t)
77 public func -(time: DispatchTime, interval: DispatchTimeInterval) -> DispatchTime {
78 let t = CDispatch.dispatch_time(time.rawValue, -Int64(interval.rawValue))
79 return DispatchTime(rawValue: t)
82 public func +(time: DispatchTime, seconds: Double) -> DispatchTime {
83 let t = CDispatch.dispatch_time(time.rawValue, Int64(seconds * Double(NSEC_PER_SEC)))
84 return DispatchTime(rawValue: t)
87 public func -(time: DispatchTime, seconds: Double) -> DispatchTime {
88 let t = CDispatch.dispatch_time(time.rawValue, Int64(-seconds * Double(NSEC_PER_SEC)))
89 return DispatchTime(rawValue: t)
92 public func +(time: DispatchWallTime, interval: DispatchTimeInterval) -> DispatchWallTime {
93 let t = CDispatch.dispatch_time(time.rawValue, Int64(interval.rawValue))
94 return DispatchWallTime(rawValue: t)
97 public func -(time: DispatchWallTime, interval: DispatchTimeInterval) -> DispatchWallTime {
98 let t = CDispatch.dispatch_time(time.rawValue, -Int64(interval.rawValue))
99 return DispatchWallTime(rawValue: t)
102 public func +(time: DispatchWallTime, seconds: Double) -> DispatchWallTime {
103 let t = CDispatch.dispatch_time(time.rawValue, Int64(seconds * Double(NSEC_PER_SEC)))
104 return DispatchWallTime(rawValue: t)
107 public func -(time: DispatchWallTime, seconds: Double) -> DispatchWallTime {
108 let t = CDispatch.dispatch_time(time.rawValue, Int64(-seconds * Double(NSEC_PER_SEC)))
109 return DispatchWallTime(rawValue: t)