Skip to content

Commit 6c981c8

Browse files
authored
Merge pull request #7432 from ktopley-apple/dispatch-after-mach
2 parents e58c6b3 + fe8d8bb commit 6c981c8

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

stdlib/public/SDK/Dispatch/Time.swift

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
//===----------------------------------------------------------------------===//
23
//
34
// This source file is part of the Swift.org open source project
@@ -41,12 +42,22 @@ public struct DispatchTime : Comparable {
4142
/// - Returns: A new `DispatchTime`
4243
/// - Discussion: This clock is the same as the value returned by
4344
/// `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.
4450
/// Note that `DispatchTime(uptimeNanoseconds: 0)` is
4551
/// equivalent to `DispatchTime.now()`, that is, its value
4652
/// represents the number of nanoseconds since boot (excluding
4753
/// system sleep time), not zero nanoseconds since boot.
4854
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)
5061
}
5162

5263
public var uptimeNanoseconds: UInt64 {

0 commit comments

Comments
 (0)