Time is an illusion. – Albert Einstein
Hybrid Logical Clocks for iOS.
Precise clock synchronization is hard. For mobile devices, this problem is even worse. It can be difficult to determine ordering of events when your clocks are out of sync.
This library contains tools to deal with problems related to clock synchronization.
Hybrid Logical Clock (HLC)
Hybrid logical clocks allow causal ordering between events, even when those events are created on multiple devices with differing clocks.
Hybrid Logical Clocks are useful when clocks differ slightly. User controlled mobile devices may have clocks that differ by hours (due to wrong time zone settings, etc), which is outside the bounds of what HLC is designed to compensate for.
A simple option is to maintain an offset based on the Date header received with http responses. This can be performed e.g. upon initial login so that the clock is synced when the app starts. This should be sufficient to get the iPad's clock to within a few seconds of "true time".
Another option is to employ NTP. NHNetworkTime is one NTP implementation that may be used: https://github.com/huynguyencong/NHNetworkTime
import HLClock // Create a new timestamp let newTimestamp = HLClock.global.now() // Update clock when receiving a new message HLClock.global.update(message.timestamp)
To run the example project, clone the repo, and run
pod install from the Example directory first.
HLClock is available through CocoaPods. To install it, simply add the following line to your Podfile:
- Implement HLC algorithm
- Make threadsafe using atomic swap
- Package as library, installable through cocoapods
- Clock offset uses moving median
- Overflow and overdrift protection
- Add hlclock usage to example app
- Generate UUIDv1 using HLC timestamp
- Add quickcheck tests
- Support SPM
HLClock is available under the MIT license. See the LICENSE file for more info.