Search code examples
iphoneiosdatetimensdate

How to get time interval since unix epoch reference date with precision down to the second?


We have a simple multiplayer table game that uses no networking connection. It has a pulsing animation which we want to play in sync with all other devices.

To do this I looked at NSDate -timeIntervalSinceReferenceDate to calculate a delay. Assuming all devices deliver the exact same value at the exact same time. But we found there is a difference of several seconds so this value is not very precise. Each device is a bit off from the true, correct time.

Is there an alternative, more precise way to get the time interval since reference date?


Solution

  • Assuming all devices deliver the exact same value at the exact same time.

    That's a big (and invalid) assumption, because it's unlikely that the system time is exactly in sync on multiple devices. Asking for the current time depends on what the device thinks the current time is, and this is not guaranteed to be correct to sub-second precision.

    If you wanted more precise time, you'd probably need to embed network time protocol code in your app and check with a time server. You might be better off looking into local device-to-device communication to try to sync up animations-- Bluetooth, wifi, or even something like having one device play a sound that the others can hear that would trigger the animation (e.g. a "click track", playing a repetitive sound, that other devices could sync up to).