Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions src/swift/Time.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,13 @@ public struct DispatchTime : Comparable {
public init(uptimeNanoseconds: UInt64) {
var rawValue = uptimeNanoseconds
#if HAVE_MACH
if (DispatchTime.timebaseInfo.numer != DispatchTime.timebaseInfo.denom) {
rawValue = (rawValue * UInt64(DispatchTime.timebaseInfo.denom)
+ UInt64(DispatchTime.timebaseInfo.numer - 1)) / UInt64(DispatchTime.timebaseInfo.numer)
// UInt64.max means distantFuture. Do not try to scale it.
if rawValue != UInt64.max && DispatchTime.timebaseInfo.numer != DispatchTime.timebaseInfo.denom {
var (result, overflow) = rawValue.multipliedReportingOverflow(by: UInt64(DispatchTime.timebaseInfo.denom))
if !overflow {
(result, overflow) = result.addingReportingOverflow(UInt64(DispatchTime.timebaseInfo.numer - 1))
}
rawValue = overflow ? UInt64.max : result / UInt64(DispatchTime.timebaseInfo.numer)
}
#endif
self.rawValue = dispatch_time_t(rawValue)
Expand All @@ -70,8 +74,12 @@ public struct DispatchTime : Comparable {
public var uptimeNanoseconds: UInt64 {
var result = self.rawValue
#if HAVE_MACH
if (DispatchTime.timebaseInfo.numer != DispatchTime.timebaseInfo.denom) {
result = result * UInt64(DispatchTime.timebaseInfo.numer) / UInt64(DispatchTime.timebaseInfo.denom)
var overflow: Bool

// UInt64.max means distantFuture. Do not try to scale it.
if rawValue != UInt64.max && DispatchTime.timebaseInfo.numer != DispatchTime.timebaseInfo.denom {
(result, overflow) = result.multipliedReportingOverflow(by: UInt64(DispatchTime.timebaseInfo.numer))
result = overflow ? UInt64.max : result / UInt64(DispatchTime.timebaseInfo.denom)
}
#endif
return result
Expand Down