TrueTime.swift
TrueTime.swift copied to clipboard
Does this implementation survive app restarts?
Is the time association persisted to disk in case the user later opens the app while disconnected from the internet?
Not currently, but this is actually the next feature in the pipeline. TrueTime for Android currently does (as-of https://github.com/instacart/truetime-android/pull/15) so we'll probably be copying the method used there.
Awesome. I implement something similar to TrueTime in my game but this appears much more robust so I'll be stoked to drop this in instead :)
@warpling @msanders Is this currently supported?
@otymartin don't think so. I've added it on my own by adding an abstraction around TrueTime that saves a time from TrueTime associated with a bootTime and uptime. With that you can check if the association is valid by verifying that the saved bootTime matches the current bootTime (since it'll only change when the device is restarted) and then calculate the current time (if TrueTime can't) by taking the saved time in the association and adding the interval that is the difference between the current uptime and the uptime saved with the time association.
This is the method I use to get uptime:
(A simpler one I used previously that read kern.boottime seemed to drift?)
// Alternate method that doesn't drift
// Source: https://github.com/jmah/MyLilTimer/blob/master/Classes/MyLilTimer.m
+ (NSTimeInterval) timeIntervalSinceBoot {
// TODO: Potentially a race condition if the system clock changes between reading `bootTime` and `now`
int status;
struct timeval bootTime;
status = sysctl((int[]){CTL_KERN, KERN_BOOTTIME}, 2,
&bootTime, &(size_t){sizeof(bootTime)},
NULL, 0);
NSCAssert(status == 0, nil);
struct timeval now;
status = gettimeofday(&now, NULL);
NSCAssert(status == 0, nil);
struct timeval difference;
timersub(&now, &bootTime, &difference);
return (difference.tv_sec + difference.tv_usec * 1.e-6);
}
Hi @msanders, First of all thanks for open sourcing this useful lib. Secondary I have question which should help me to pick best NTP lib for iOS: Do you have any rough estimates when could we expect this feature to be implemented?