17
17
import CDispatch
18
18
19
19
public struct DispatchTime : Comparable {
20
+ #if HAVE_MACH
21
+ private static let timebaseInfo : mach_timebase_info_data_t = {
22
+ var info = mach_timebase_info_data_t ( numer: 1 , denom: 1 )
23
+ mach_timebase_info ( & info)
24
+ return info
25
+ } ( )
26
+ #endif
27
+
20
28
public let rawValue : dispatch_time_t
21
29
22
30
public static func now( ) -> DispatchTime {
@@ -39,16 +47,34 @@ public struct DispatchTime : Comparable {
39
47
/// - Returns: A new `DispatchTime`
40
48
/// - Discussion: This clock is the same as the value returned by
41
49
/// `mach_absolute_time` when converted into nanoseconds.
50
+ /// On some platforms, the nanosecond value is rounded up to a
51
+ /// multiple of the Mach timebase, using the conversion factors
52
+ /// returned by `mach_timebase_info()`. The nanosecond equivalent
53
+ /// of the rounded result can be obtained by reading the
54
+ /// `uptimeNanoseconds` property.
42
55
/// Note that `DispatchTime(uptimeNanoseconds: 0)` is
43
56
/// equivalent to `DispatchTime.now()`, that is, its value
44
57
/// represents the number of nanoseconds since boot (excluding
45
58
/// system sleep time), not zero nanoseconds since boot.
46
59
public init ( uptimeNanoseconds: UInt64 ) {
47
- self . rawValue = dispatch_time_t ( uptimeNanoseconds)
60
+ var rawValue = uptimeNanoseconds
61
+ #if HAVE_MACH
62
+ if ( DispatchTime . timebaseInfo. numer != DispatchTime . timebaseInfo. denom) {
63
+ rawValue = ( rawValue * UInt64( DispatchTime . timebaseInfo. denom)
64
+ + UInt64( DispatchTime . timebaseInfo. numer - 1 ) ) / UInt64( DispatchTime . timebaseInfo. numer)
65
+ }
66
+ #endif
67
+ self . rawValue = dispatch_time_t ( rawValue)
48
68
}
49
69
50
70
public var uptimeNanoseconds : UInt64 {
51
- return UInt64 ( self . rawValue)
71
+ var result = self . rawValue
72
+ #if HAVE_MACH
73
+ if ( DispatchTime . timebaseInfo. numer != DispatchTime . timebaseInfo. denom) {
74
+ result = result * UInt64( DispatchTime . timebaseInfo. numer) / UInt64( DispatchTime . timebaseInfo. denom)
75
+ }
76
+ #endif
77
+ return result
52
78
}
53
79
}
54
80
@@ -81,8 +107,12 @@ public struct DispatchWallTime : Comparable {
81
107
}
82
108
83
109
public func < ( a: DispatchWallTime , b: DispatchWallTime ) -> Bool {
84
- if a. rawValue == ~ 0 || b. rawValue == ~ 0 { return false }
85
- return - Int64( a. rawValue) < - Int64( b. rawValue)
110
+ if b. rawValue == ~ 0 {
111
+ return a. rawValue != ~ 0
112
+ } else if a. rawValue == ~ 0 {
113
+ return false
114
+ }
115
+ return - Int64( bitPattern: a. rawValue) < - Int64( bitPattern: b. rawValue)
86
116
}
87
117
88
118
public func == ( a: DispatchWallTime , b: DispatchWallTime ) -> Bool {
@@ -147,7 +177,7 @@ public func +(time: DispatchWallTime, seconds: Double) -> DispatchWallTime {
147
177
}
148
178
149
179
public func - ( time: DispatchWallTime , seconds: Double ) -> DispatchWallTime {
150
- let interval = seconds * Double( NSEC_PER_SEC)
180
+ let interval = - seconds * Double( NSEC_PER_SEC)
151
181
let t = CDispatch . dispatch_time ( time. rawValue,
152
182
interval. isInfinite || interval. isNaN ? Int64 . min : Int64 ( interval) )
153
183
return DispatchWallTime ( rawValue: t)
0 commit comments