HomeiOS Developmentios - Why is timeIntervalSince off by 1 persistently?

ios – Why is timeIntervalSince off by 1 persistently?


I’ve acquired just a little timer performance in my app. When the timer begins I create a date object and hold it as a property.

I then have a timer set within the code that repeatedly ticks each second and compares the saved property in opposition to Date() utilizing timeIntervalSince. Nonetheless, it appears like all constructive values are off by 1 till they attain zero. The code stays on zero for 2 ticks after which begins calculating the outcomes accurately from there on.

Is there any rationalization for this discrepancy? Extra importantly, is there a assure that this behaviour will stay constant in order that I can modify my very own logic to deal with it accurately? Sadly, I could not discover something that might clarify the behaviour in documentation.

Right here is the pattern code that I execute within my timer loop:

let now = Date()
print("Finish date: (endDate)")
print("Now: (now)")
print("Distance: (Int(endDate.timeIntervalSince(now)))")

And the instance output:

Finish date: 2023-03-14 07:11:28 +0000
Now: 2023-03-14 07:11:23 +0000
Distance: 4

Finish date: 2023-03-14 07:11:28 +0000
Now: 2023-03-14 07:11:24 +0000
Distance: 3

Finish date: 2023-03-14 07:11:28 +0000
Now: 2023-03-14 07:11:25 +0000
Distance: 2

Finish date: 2023-03-14 07:11:28 +0000
Now: 2023-03-14 07:11:26 +0000
Distance: 1

Finish date: 2023-03-14 07:11:28 +0000
Now: 2023-03-14 07:11:27 +0000
Distance: 0

Finish date: 2023-03-14 07:11:28 +0000
Now: 2023-03-14 07:11:28 +0000
Distance: 0

Finish date: 2023-03-14 07:11:28 +0000
Now: 2023-03-14 07:11:29 +0000
Distance: -1

Finish date: 2023-03-14 07:11:28 +0000
Now: 2023-03-14 07:11:30 +0000
Distance: -2

Finish date: 2023-03-14 07:11:28 +0000
Now: 2023-03-14 07:11:31 +0000
Distance: -3

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments