]> git.saurik.com Git - apple/libdispatch.git/blob - src/swift/Time.swift
libdispatch-703.50.37.tar.gz
[apple/libdispatch.git] / src / swift / Time.swift
1 //===----------------------------------------------------------------------===//
2 //
3 // This source file is part of the Swift.org open source project
4 //
5 // Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
6 // Licensed under Apache License v2.0 with Runtime Library Exception
7 //
8 // See http://swift.org/LICENSE.txt for license information
9 // See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10 //
11 //===----------------------------------------------------------------------===//
12
13 // dispatch/time.h
14 // DISPATCH_TIME_NOW: ok
15 // DISPATCH_TIME_FOREVER: ok
16
17 import CDispatch
18
19 public struct DispatchTime : Comparable {
20 public let rawValue: dispatch_time_t
21
22 public static func now() -> DispatchTime {
23 let t = CDispatch.dispatch_time(0, 0)
24 return DispatchTime(rawValue: t)
25 }
26
27 public static let distantFuture = DispatchTime(rawValue: ~0)
28
29 fileprivate init(rawValue: dispatch_time_t) {
30 self.rawValue = rawValue
31 }
32
33 /// Creates a `DispatchTime` relative to the system clock that
34 /// ticks since boot.
35 ///
36 /// - Parameters:
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)
42 }
43
44 public var uptimeNanoseconds: UInt64 {
45 return UInt64(self.rawValue)
46 }
47 }
48
49 public func <(a: DispatchTime, b: DispatchTime) -> Bool {
50 if a.rawValue == ~0 || b.rawValue == ~0 { return false }
51 return a.rawValue < b.rawValue
52 }
53
54 public func ==(a: DispatchTime, b: DispatchTime) -> Bool {
55 return a.rawValue == b.rawValue
56 }
57
58 public struct DispatchWallTime : Comparable {
59 public let rawValue: dispatch_time_t
60
61 public static func now() -> DispatchWallTime {
62 return DispatchWallTime(rawValue: CDispatch.dispatch_walltime(nil, 0))
63 }
64
65 public static let distantFuture = DispatchWallTime(rawValue: ~0)
66
67 fileprivate init(rawValue: dispatch_time_t) {
68 self.rawValue = rawValue
69 }
70
71 public init(timespec: timespec) {
72 var t = timespec
73 self.rawValue = CDispatch.dispatch_walltime(&t, 0)
74 }
75 }
76
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)
80 }
81
82 public func ==(a: DispatchWallTime, b: DispatchWallTime) -> Bool {
83 return a.rawValue == b.rawValue
84 }
85
86 public enum DispatchTimeInterval {
87 case seconds(Int)
88 case milliseconds(Int)
89 case microseconds(Int)
90 case nanoseconds(Int)
91
92 internal var rawValue: Int64 {
93 switch self {
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)
98 }
99 }
100 }
101
102 public func +(time: DispatchTime, interval: DispatchTimeInterval) -> DispatchTime {
103 let t = CDispatch.dispatch_time(time.rawValue, interval.rawValue)
104 return DispatchTime(rawValue: t)
105 }
106
107 public func -(time: DispatchTime, interval: DispatchTimeInterval) -> DispatchTime {
108 let t = CDispatch.dispatch_time(time.rawValue, -interval.rawValue)
109 return DispatchTime(rawValue: t)
110 }
111
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)
115 }
116
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)
120 }
121
122 public func +(time: DispatchWallTime, interval: DispatchTimeInterval) -> DispatchWallTime {
123 let t = CDispatch.dispatch_time(time.rawValue, interval.rawValue)
124 return DispatchWallTime(rawValue: t)
125 }
126
127 public func -(time: DispatchWallTime, interval: DispatchTimeInterval) -> DispatchWallTime {
128 let t = CDispatch.dispatch_time(time.rawValue, -interval.rawValue)
129 return DispatchWallTime(rawValue: t)
130 }
131
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)
135 }
136
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)
140 }