- December 17, 2024
- Mins Read
Make sure to check out our counterpart too: TrueTime, an NTP library for Android.
NTP client for Swift. Calculate the time “now” impervious to manual changes to device clock time.
In certain applications it becomes important to get the real or “true” date and time. On most devices, if the clock has been changed manually, then an NSDate()
instance gives you a time impacted by local settings.
Users may do this for a variety of reasons, like being in different timezones, trying to be punctual and setting their clocks 5 – 10 minutes early, etc. Your application or service may want a date that is unaffected by these changes and reliable as a source of truth. TrueTime gives you that.
It’s pretty simple actually. We make a request to an NTP server that gives us the actual time. We then establish the delta between device uptime and uptime at the time of the network response. Each time now()
is requested subsequently, we account for that offset and return a corrected NSDate
value.
import TrueTime
// At an opportune time (e.g. app start):
let client = TrueTimeClient.sharedInstance
client.start()
// You can now use this instead of NSDate():
let now = client.referenceTime?.now()
// To block waiting for fetch, use the following:
client.fetchIfNeeded { result in
switch result {
case let .success(referenceTime):
let now = referenceTime.now()
case let .failure(error):
print(“Error! \(error)”)
}
}
@import TrueTime;
// At an opportune time (e.g. app start):
TrueTimeClient *client = [TrueTimeClient sharedInstance];
[client startWithPool:@[@”time.apple.com”] port:123];
// You can now use this instead of [NSDate date]:
NSDate *now = [[client referenceTime] now];
// To block waiting for fetch, use the following:
[client fetchIfNeededWithSuccess:^(NTPReferenceTime *referenceTime) {
NSLog(@”True time: %@”, [referenceTime now]);
} failure:^(NSError *error) {
NSLog(@”Error! %@”, error);
}];
You can also listen to the TrueTimeUpdated
notification to detect when a reference time has been fetched:
let client = TrueTimeClient.sharedInstance
let _ = NSNotificationCenter.default.addObserver(forName: .TrueTimeUpdated, object: client) { _ in
// Now guaranteed to be non-nil.
print(“Got time: \(client.referenceTime?.now()”)
}
TrueTime is currently compatible with iOS 8 and up, macOS 10.10 and tvOS 9.
Add this to your Cartfile
:
github “instacart/TrueTime.swift”
Then run:
$ carthage update
Add this to your Podfile
:
pod ‘TrueTime’
Then run:
$ pod install
git submodule update --init
.carthage bootstrap
.brew install swiftlint
if not already installed.TrueTime.xcodeproj
, choose TrueTimeExample
and hit run. This will build everything and run the sample app.
$ git submodule add https://github.com/instacart/TrueTime.swift.git
TrueTime.xcodeproj
into the Project Navigator.Project > Targets > Build Phases > Link Binary With Libraries
, click +
and select the TrueTime
target.NSDates
are just Unix timestamps, it’s safe to hold onto values returned by ReferenceTime.now()
or persist them to disk without having to adjust them later.This project adheres to the Contributor Covenant code of conduct. By participating (including but not limited to; reporting issues, commenting on issues and contributing code) you are expected to uphold this code. Please report unacceptable behavior to opensource@instacart.com.
Development depends on some Carthage dependencies and a xcconfig git submodule.
Clone the repo and setup dependencies with:
git submodule update –init –recursive
carthage bootstrap
A vertical stackview which takes subviews with different widths and adds them to it's rows with paddings, spacings etc.
AudioManager is a Swift package that provides a modular and easy-to-use interface for implementing audio feedback in your applications. It ...