-
-
Notifications
You must be signed in to change notification settings - Fork 602
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #718 from Quick/timeinterval
Replaced TimeInterval with DispatchTimeInterval
- Loading branch information
Showing
7 changed files
with
95 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import Dispatch | ||
import Foundation | ||
|
||
#if canImport(CDispatch) | ||
import CDispatch | ||
#endif | ||
|
||
extension DispatchTimeInterval { | ||
// ** Note: We cannot simply divide the time interval because DispatchTimeInterval associated value type is Int | ||
var divided: DispatchTimeInterval { | ||
switch self { | ||
case let .seconds(val): return val < 2 ? .milliseconds(Int(Float(val)/2*1000)) : .seconds(val/2) | ||
case let .milliseconds(val): return .milliseconds(val/2) | ||
case let .microseconds(val): return .microseconds(val/2) | ||
case let .nanoseconds(val): return .nanoseconds(val/2) | ||
case .never: return .never | ||
@unknown default: fatalError("Unknown DispatchTimeInterval value") | ||
} | ||
} | ||
|
||
var description: String { | ||
switch self { | ||
case let .seconds(val): return val == 1 ? "\(Float(val)) second" : "\(Float(val)) seconds" | ||
case let .milliseconds(val): return "\(Float(val)/1_000) seconds" | ||
case let .microseconds(val): return "\(Float(val)/1_000_000) seconds" | ||
case let .nanoseconds(val): return "\(Float(val)/1_000_000_000) seconds" | ||
default: fatalError("Unknown DispatchTimeInterval value") | ||
} | ||
} | ||
} | ||
|
||
extension TimeInterval { | ||
var dispatchInterval: DispatchTimeInterval { | ||
let microseconds = Int64(self * TimeInterval(USEC_PER_SEC)) | ||
// perhaps use nanoseconds, though would more often be > Int.max | ||
return microseconds < Int.max ? .microseconds(Int(microseconds)) : .seconds(Int(self)) | ||
} | ||
} |
Oops, something went wrong.