davedelong / time

Robust and type-safe date and time calculations for Swift
MIT License
2.32k stars 77 forks source link

Precision tighter than milliseconds is problematic #42

Open davedelong opened 4 years ago

davedelong commented 4 years ago

It's difficult to deal with precision higher than milliseconds.

I'm attempting to write tests dealing with nanosecond precision and they're failing all over the place. Couple this with the face that DateFormatter won't format anything smaller than milliseconds and maybe Time should just cap things off there.

akshayjshah commented 4 years ago

Forgive me if I'm telling you stuff you already know, but...

Macs coalesce timers into 15ms windows to save power (and I assume iOS devices are even more aggressive). That makes writing tests for code dealing with smaller units of duration really hard. If you're running into trouble, you may want to try disabling coalescing: https://timoliver.blog/2014/01/25/disabling-timer-coalescing-in-os-x-mavericks/.

I've been bitten by this many times, and I forget this little nugget each time and waste hours debugging.

davedelong commented 7 months ago

Maybe a way to approach this could be having the system clocks pull their initial date via Date(), but then count and track intervals since then via a ContinuousClock, which would has higher precision than the milliseconds-by-default values coming out of Date.