Latest 0.2.0
Homepage https://github.com/opentable/HLClock
License MIT
Platforms ios 8.0
Authors

Time is an illusion. – Albert Einstein

Hybrid Logical Clocks for iOS.

CI Status
Version
License
Platform

Overview

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

Usage

import HLClock

// Create a new timestamp
let newTimestamp = HLClock.global.now()

// Update clock when receiving a new message
HLClock.global.update(message.timestamp)

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Installation

HLClock is available through CocoaPods. To install
it, simply add the following line to your Podfile:

pod "HLClock"

Punchlist

  • [x] Implement HLC algorithm
  • [x] Make threadsafe using atomic swap
  • [x] 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

License

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

Latest podspec

{
    "name": "HLClock",
    "version": "0.2.0",
    "summary": "Hybrid Logical Clocks for iOS.",
    "description": "Hybrid Logical Clocks enable causal ordering of events within a distributed system with imperfect clocks.nSee: http://muratbuffalo.blogspot.com/2014/07/hybrid-logical-clocks.html",
    "homepage": "https://github.com/opentable/HLClock",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "Stephen Spalding": "[email protected]"
    },
    "source": {
        "git": "https://github.com/opentable/HLClock.git",
        "tag": "0.2.0"
    },
    "platforms": {
        "ios": "8.0"
    },
    "source_files": "HLClock/Classes/**/*"
}

Pin It on Pinterest

Share This