|
| 1 | + |
1 | 2 | //===----------------------------------------------------------------------===//
|
2 | 3 | //
|
3 | 4 | // This source file is part of the Swift.org open source project
|
@@ -41,12 +42,22 @@ public struct DispatchTime : Comparable {
|
41 | 42 | /// - Returns: A new `DispatchTime`
|
42 | 43 | /// - Discussion: This clock is the same as the value returned by
|
43 | 44 | /// `mach_absolute_time` when converted into nanoseconds.
|
| 45 | + /// On some platforms, the nanosecond value is rounded up to a |
| 46 | + /// multiple of the Mach timebase, using the conversion factors |
| 47 | + /// returned by `mach_timebase_info()`. The nanosecond equivalent |
| 48 | + /// of the rounded result can be obtained by reading the |
| 49 | + /// `uptimeNanoseconds` property. |
44 | 50 | /// Note that `DispatchTime(uptimeNanoseconds: 0)` is
|
45 | 51 | /// equivalent to `DispatchTime.now()`, that is, its value
|
46 | 52 | /// represents the number of nanoseconds since boot (excluding
|
47 | 53 | /// system sleep time), not zero nanoseconds since boot.
|
48 | 54 | public init(uptimeNanoseconds: UInt64) {
|
49 |
| - self.rawValue = dispatch_time_t(uptimeNanoseconds) |
| 55 | + var rawValue = uptimeNanoseconds |
| 56 | + if (DispatchTime.timebaseInfo.numer != DispatchTime.timebaseInfo.denom) { |
| 57 | + rawValue = (rawValue * UInt64(DispatchTime.timebaseInfo.denom) |
| 58 | + + UInt64(DispatchTime.timebaseInfo.numer - 1)) / UInt64(DispatchTime.timebaseInfo.numer) |
| 59 | + } |
| 60 | + self.rawValue = dispatch_time_t(rawValue) |
50 | 61 | }
|
51 | 62 |
|
52 | 63 | public var uptimeNanoseconds: UInt64 {
|
|
0 commit comments