]> git.saurik.com Git - apple/libdispatch.git/blob - src/swift/Time.swift
libdispatch-703.1.4.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 {
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 private init(rawValue: dispatch_time_t) {
30 self.rawValue = rawValue
31 }
32 }
33
34 public struct DispatchWallTime {
35 public let rawValue: dispatch_time_t
36
37 public static func now() -> DispatchWallTime {
38 return DispatchWallTime(rawValue: CDispatch.dispatch_walltime(nil, 0))
39 }
40
41 public static let distantFuture = DispatchWallTime(rawValue: ~0)
42
43 private init(rawValue: dispatch_time_t) {
44 self.rawValue = rawValue
45 }
46
47 public init(time: timespec) {
48 var t = time
49 self.rawValue = CDispatch.dispatch_walltime(&t, 0)
50 }
51 }
52
53 @available(*, deprecated, renamed: "DispatchWallTime")
54 public typealias DispatchWalltime = DispatchWallTime
55
56 public enum DispatchTimeInterval {
57 case seconds(Int)
58 case milliseconds(Int)
59 case microseconds(Int)
60 case nanoseconds(Int)
61
62 internal var rawValue: UInt64 {
63 switch self {
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)
68 }
69 }
70 }
71
72 public func +(time: DispatchTime, interval: DispatchTimeInterval) -> DispatchTime {
73 let t = CDispatch.dispatch_time(time.rawValue, Int64(interval.rawValue))
74 return DispatchTime(rawValue: t)
75 }
76
77 public func -(time: DispatchTime, interval: DispatchTimeInterval) -> DispatchTime {
78 let t = CDispatch.dispatch_time(time.rawValue, -Int64(interval.rawValue))
79 return DispatchTime(rawValue: t)
80 }
81
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)
85 }
86
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)
90 }
91
92 public func +(time: DispatchWallTime, interval: DispatchTimeInterval) -> DispatchWallTime {
93 let t = CDispatch.dispatch_time(time.rawValue, Int64(interval.rawValue))
94 return DispatchWallTime(rawValue: t)
95 }
96
97 public func -(time: DispatchWallTime, interval: DispatchTimeInterval) -> DispatchWallTime {
98 let t = CDispatch.dispatch_time(time.rawValue, -Int64(interval.rawValue))
99 return DispatchWallTime(rawValue: t)
100 }
101
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)
105 }
106
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)
110 }