I cannot find a way to have the stopwatch display start at 00:00:00 using QuartzCore. It always starts at 07:00:00.
Is there a way to format the start time to be 00:00:00? Code is below.
Thanks!!
Class:
import Foundation
import QuartzCore
class StopWatch: NSObject{
private var displayLink: CADisplayLink!
private let formatter = DateFormatter()
var callback: (() -> Void)?
var elapsedTime: CFTimeInterval!
override init() {
super.init()
self.displayLink = CADisplayLink(target: self, selector: #selector(tick(sender:)))
displayLink.isPaused = true
displayLink.add(to: RunLoop.main, forMode: RunLoopMode.commonModes)
self.elapsedTime = 0.0
formatter.dateFormat = "HH:mm:ss"
}
convenience init(withCallback callback: @escaping () -> Void) {
self.init()
self.callback = callback
}
deinit {
displayLink.invalidate()
}
func tick(sender: CADisplayLink) {
elapsedTime = elapsedTime + displayLink.duration
callback?()
}
func elapsedTimeAsString() -> String {
return formatter.string(from: Date(timeIntervalSinceReferenceDate:elapsedTime))
}
}
CORRECTED as mentioned in the comment below: As the commenter above mentioned, this was a timezone issue since I am located in GMT +7. I added the following code to solve the issue:
formatter.timeZone = TimeZone(abbreviation: "GMT")!