Skip to content
This repository was archived by the owner on Aug 6, 2024. It is now read-only.
Open
Show file tree
Hide file tree
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
16 changes: 16 additions & 0 deletions Chronos.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */; };
Expand Down Expand Up @@ -101,6 +104,7 @@
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
1323B0D91E43AF1C00901259 /* Stopwatch.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Stopwatch.swift; sourceTree = "<group>"; };
6422C4A51AD38BBC00D480E8 /* Timer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Timer.swift; sourceTree = "<group>"; };
644FA4141ADE19560007B808 /* TimerInternal.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TimerInternal.swift; sourceTree = "<group>"; };
644FA4161ADE19760007B808 /* VariableTimer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = VariableTimer.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -168,6 +172,14 @@
/* End PBXFrameworksBuildPhase section */

/* Begin PBXGroup section */
1323B0DD1E43AF3B00901259 /* Utils */ = {
isa = PBXGroup;
children = (
1323B0D91E43AF1C00901259 /* Stopwatch.swift */,
);
name = Utils;
sourceTree = "<group>";
};
64B3CE121ACDDA2800145E36 = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -217,6 +229,7 @@
children = (
644FA4221ADE19A50007B808 /* DispatchTimerTests.swift */,
64502B951ADE19C50008443B /* VariableTimerTests.swift */,
1323B0DD1E43AF3B00901259 /* Utils */,
64B3CE2C1ACDDA2800145E36 /* Supporting Files */,
);
path = ChronosTests;
Expand Down Expand Up @@ -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;
};
Expand All @@ -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;
};
Expand All @@ -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;
};
Expand Down
10 changes: 1 addition & 9 deletions Chronos/DispatchTimer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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()

Expand All @@ -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()
}
}
Expand Down
2 changes: 1 addition & 1 deletion Chronos/TimerInternal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}


Expand Down
60 changes: 60 additions & 0 deletions ChronosTests/DispatchTimerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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..<Int(floor(timeout/interval)) {
XCTAssertTrue(records[i] == floor(interval*Double(i + 1)))
}
}
}

func testTimeIntervalStartingNow() {
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)) {
exp.fulfill()
}
}

dispatchTimer.start(true)

waitForExpectations (timeout: timeout) { _ in
XCTAssertTrue(records.count == Int(timeout/interval)+1)
for i in 0...Int(floor(timeout/interval)) {
XCTAssertTrue(records[i] == floor(interval*Double(i)))
}
}
}
}
43 changes: 43 additions & 0 deletions ChronosTests/Stopwatch.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//
// Stopwatch.swift
// Chronos
//
// Created by ggiampietro on 2/2/17.
// Copyright © 2017 com.comyarzaheri. All rights reserved.
//

import Foundation

struct Stopwatch {
var startTime: UInt64 = 0
var stopTime: UInt64 = 0
let numer: UInt64
let denom: UInt64

init() {
var info = mach_timebase_info(numer: 0, denom: 0)
mach_timebase_info(&info)
numer = UInt64(info.numer)
denom = UInt64(info.denom)
}

mutating func start() {
startTime = mach_absolute_time()
}

mutating func stop() {
stopTime = mach_absolute_time()
}

var nanoseconds: UInt64 {
return ((stopTime - startTime) * numer) / denom
}

var milliseconds: Double {
return Double(nanoseconds) / 1_000_000
}

var seconds: Double {
return Double(nanoseconds) / 1_000_000_000
}
}