I´m assembling an application with regular web updates. Every update leaves an unix time stamp. Now i try to create an editor, which shows the elapsed time since last update. I´m convering the unix time stamp with this code:
import Cocoa
struct UnixConverter {
static func converUnix(_ timestamp: Int) -> NSDate? {
let date = NSDate(timeIntervalSince1970: TimeInterval(timestamp))
return date
}
}
Now I´m getting a time in this format: "2017-06-16 17:47:45 +0000"
The following function gives me a reals time push update inside a NSTextFieldCell
:
import Cocoa
class ViewController: NSViewController {
var timer = Timer()
override func viewDidLoad() {
super.viewDidLoad()
// This function pushes the time NSTextFieldCell
timer = Timer.scheduledTimer(timeInterval: 1.0,
target: self, s
elector: #selector(tick),
userInfo: nil,
repeats: true)
}
// This is the NSTextFieldCell formatter/ receiving container
@objc func tick() {
timerTextField.stringValue = DateFormatter.localizedString(from: NSDate() as Date, dateStyle: .medium, timeStyle: .medium)
}
}
Now "all" I would have to do, is to make a subtraction to get the difference. At this point I must confess I don´t have any idea how to do this. After that the time difference has to be pushed inside the NSTextFieldCell
.
I would be very glad if someone could give me a hint how to do it.