HLClock 0.2.0

HLClock 0.2.0

LangLanguage SwiftSwift
License MIT
ReleasedLast Release Jun 2016
SPMSupports SPM

Maintained by Stephen Spalding.

HLClock 0.2.0

  • By
  • Stephen Spalding


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.

See: http://muratbuffalo.blogspot.com/2014/07/hybrid-logical-clocks.html

Network Time

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


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:

pod "HLClock"


  • [x] Implement HLC algorithm
  • [x] Make threadsafe using atomic swap
  • [x] Package as library, installable through cocoapods
  • [ ] Clock offset uses moving median
  • [ ] Add hlclock usage to example app
  • [ ] Generate UUIDv1 using HLC timestamp
  • [ ] Add quickcheck tests


HLClock is available under the MIT license. See the LICENSE file for more info.