Skip to content

Port changes to Time.swift from the swift repository. #238

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 13, 2017
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
40 changes: 35 additions & 5 deletions src/swift/Time.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@
import CDispatch

public struct DispatchTime : Comparable {
#if HAVE_MACH
private static let timebaseInfo: mach_timebase_info_data_t = {
var info = mach_timebase_info_data_t(numer: 1, denom: 1)
mach_timebase_info(&info)
return info
}()
#endif

public let rawValue: dispatch_time_t

public static func now() -> DispatchTime {
Expand All @@ -39,16 +47,34 @@ public struct DispatchTime : Comparable {
/// - Returns: A new `DispatchTime`
/// - Discussion: This clock is the same as the value returned by
/// `mach_absolute_time` when converted into nanoseconds.
/// On some platforms, the nanosecond value is rounded up to a
/// multiple of the Mach timebase, using the conversion factors
/// returned by `mach_timebase_info()`. The nanosecond equivalent
/// of the rounded result can be obtained by reading the
/// `uptimeNanoseconds` property.
/// Note that `DispatchTime(uptimeNanoseconds: 0)` is
/// equivalent to `DispatchTime.now()`, that is, its value
/// represents the number of nanoseconds since boot (excluding
/// system sleep time), not zero nanoseconds since boot.
public init(uptimeNanoseconds: UInt64) {
self.rawValue = dispatch_time_t(uptimeNanoseconds)
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)
}
#endif
self.rawValue = dispatch_time_t(rawValue)
}

public var uptimeNanoseconds: UInt64 {
return UInt64(self.rawValue)
var result = self.rawValue
#if HAVE_MACH
if (DispatchTime.timebaseInfo.numer != DispatchTime.timebaseInfo.denom) {
result = result * UInt64(DispatchTime.timebaseInfo.numer) / UInt64(DispatchTime.timebaseInfo.denom)
}
#endif
return result
}
}

Expand Down Expand Up @@ -81,8 +107,12 @@ public struct DispatchWallTime : Comparable {
}

public func <(a: DispatchWallTime, b: DispatchWallTime) -> Bool {
if a.rawValue == ~0 || b.rawValue == ~0 { return false }
return -Int64(a.rawValue) < -Int64(b.rawValue)
if b.rawValue == ~0 {
return a.rawValue != ~0
} else if a.rawValue == ~0 {
return false
}
return -Int64(bitPattern: a.rawValue) < -Int64(bitPattern: b.rawValue)
}

public func ==(a: DispatchWallTime, b: DispatchWallTime) -> Bool {
Expand Down Expand Up @@ -147,7 +177,7 @@ public func +(time: DispatchWallTime, seconds: Double) -> DispatchWallTime {
}

public func -(time: DispatchWallTime, seconds: Double) -> DispatchWallTime {
let interval = seconds * Double(NSEC_PER_SEC)
let interval = -seconds * Double(NSEC_PER_SEC)
let t = CDispatch.dispatch_time(time.rawValue,
interval.isInfinite || interval.isNaN ? Int64.min : Int64(interval))
return DispatchWallTime(rawValue: t)
Expand Down