diff --git a/Chronos.xcodeproj/project.pbxproj b/Chronos.xcodeproj/project.pbxproj index 581e68a..531bbc1 100644 --- a/Chronos.xcodeproj/project.pbxproj +++ b/Chronos.xcodeproj/project.pbxproj @@ -7,6 +7,9 @@ objects = { /* Begin PBXBuildFile section */ + 1323B0DA1E43AF1C00901259 /* Stopwatch.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1323B0D91E43AF1C00901259 /* Stopwatch.swift */; }; + 1323B0DB1E43AF1C00901259 /* Stopwatch.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1323B0D91E43AF1C00901259 /* Stopwatch.swift */; }; + 1323B0DC1E43AF1C00901259 /* Stopwatch.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1323B0D91E43AF1C00901259 /* Stopwatch.swift */; }; 6422C4A61AD38BBC00D480E8 /* Timer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6422C4A51AD38BBC00D480E8 /* Timer.swift */; }; 6422C4A71AD38C6F00D480E8 /* Timer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6422C4A51AD38BBC00D480E8 /* Timer.swift */; }; 6422C4A81AD38C6F00D480E8 /* Timer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6422C4A51AD38BBC00D480E8 /* Timer.swift */; }; @@ -101,6 +104,7 @@ /* End PBXCopyFilesBuildPhase section */ /* Begin PBXFileReference section */ + 1323B0D91E43AF1C00901259 /* Stopwatch.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Stopwatch.swift; sourceTree = ""; }; 6422C4A51AD38BBC00D480E8 /* Timer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Timer.swift; sourceTree = ""; }; 644FA4141ADE19560007B808 /* TimerInternal.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TimerInternal.swift; sourceTree = ""; }; 644FA4161ADE19760007B808 /* VariableTimer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = VariableTimer.swift; sourceTree = ""; }; @@ -168,6 +172,14 @@ /* End PBXFrameworksBuildPhase section */ /* Begin PBXGroup section */ + 1323B0DD1E43AF3B00901259 /* Utils */ = { + isa = PBXGroup; + children = ( + 1323B0D91E43AF1C00901259 /* Stopwatch.swift */, + ); + name = Utils; + sourceTree = ""; + }; 64B3CE121ACDDA2800145E36 = { isa = PBXGroup; children = ( @@ -217,6 +229,7 @@ children = ( 644FA4221ADE19A50007B808 /* DispatchTimerTests.swift */, 64502B951ADE19C50008443B /* VariableTimerTests.swift */, + 1323B0DD1E43AF3B00901259 /* Utils */, 64B3CE2C1ACDDA2800145E36 /* Supporting Files */, ); path = ChronosTests; @@ -502,6 +515,7 @@ 644FA4191ADE197C0007B808 /* VariableTimer.swift in Sources */, 644FA41E1ADE19810007B808 /* TimerInternal.swift in Sources */, 64B8678C1ACFCE070020EDDA /* DispatchTimer.swift in Sources */, + 1323B0DA1E43AF1C00901259 /* Stopwatch.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -528,6 +542,7 @@ 64C8842E1C0268A1005DB548 /* RepeatingTimer.swift in Sources */, 64C8842F1C0268A1005DB548 /* DispatchTimer.swift in Sources */, 64C884301C0268A1005DB548 /* VariableTimer.swift in Sources */, + 1323B0DC1E43AF1C00901259 /* Stopwatch.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -554,6 +569,7 @@ 644FA41A1ADE197C0007B808 /* VariableTimer.swift in Sources */, 644FA41C1ADE19800007B808 /* TimerInternal.swift in Sources */, 64B0D0F71AD0E58A0020EFE1 /* DispatchTimer.swift in Sources */, + 1323B0DB1E43AF1C00901259 /* Stopwatch.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/Chronos/DispatchTimer.swift b/Chronos/DispatchTimer.swift index f622cef..e547a70 100644 --- a/Chronos/DispatchTimer.swift +++ b/Chronos/DispatchTimer.swift @@ -56,8 +56,6 @@ open class DispatchTimer : NSObject, RepeatingTimer { fileprivate var leeway: UInt64 { return UInt64(0.05 * interval) * NSEC_PER_SEC; } - private let timebaseNumer: Double - private let timebaseDenom: Double // MARK: Properties @@ -133,12 +131,6 @@ open class DispatchTimer : NSObject, RepeatingTimer { self.queue = queue self.interval = interval self.closure = closure - - //set timebase info - var info = mach_timebase_info(numer: 0, denom: 0) - mach_timebase_info(&info) - self.timebaseNumer = Double(info.numer) - self.timebaseDenom = Double(info.denom) super.init() @@ -163,7 +155,7 @@ open class DispatchTimer : NSObject, RepeatingTimer { validate() if OSAtomicCompareAndSwap32Barrier(State.paused, State.running, &running) { timer.scheduleRepeating(deadline: startTime(interval, now: now), - interval:.nanoseconds(Int((interval * self.timebaseNumer) / self.timebaseDenom))) + interval: interval) timer.resume() } } diff --git a/Chronos/TimerInternal.swift b/Chronos/TimerInternal.swift index 6820551..0faf254 100644 --- a/Chronos/TimerInternal.swift +++ b/Chronos/TimerInternal.swift @@ -25,7 +25,7 @@ internal struct State { // Mark:- Constants and Functions internal func startTime(_ interval: Double, now: Bool) -> DispatchTime { - return DispatchTime.now() + Double(now ? 0 : Int64(interval * Double(NSEC_PER_SEC))) / Double(NSEC_PER_SEC) + return DispatchTime.now() + Double(now ? 0 : interval) } diff --git a/ChronosTests/DispatchTimerTests.swift b/ChronosTests/DispatchTimerTests.swift index f1f576e..c572f22 100644 --- a/ChronosTests/DispatchTimerTests.swift +++ b/ChronosTests/DispatchTimerTests.swift @@ -163,4 +163,64 @@ class DispatchTimerTests : XCTestCase { XCTAssertFalse(dispatchTimer.isValid) } + + func testTimeInterval() { + var stopwatch = Stopwatch() + stopwatch.start() + + let exp = expectation(description: "\(#function)\(#line)") + + var records = [Double]() + + let timeout = 22.2 + let interval = 5.5 + + let dispatchTimer = DispatchTimer(interval: interval) { (timer: RepeatingTimer, invocations: Int) in + stopwatch.stop() + records += [floor(stopwatch.seconds)] + + if invocations == Int(floor(timeout/interval))-1 { + exp.fulfill() + } + } + + dispatchTimer.start(false) + + waitForExpectations (timeout: timeout) { _ in + XCTAssertTrue(records.count == Int(timeout/interval)) + for i in 0..