TrueTime.swift icon indicating copy to clipboard operation
TrueTime.swift copied to clipboard

Does this implementation survive app restarts?

Open warpling opened this issue 9 years ago • 6 comments

Is the time association persisted to disk in case the user later opens the app while disconnected from the internet?

warpling avatar Nov 09 '16 21:11 warpling

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.

msanders avatar Nov 09 '16 22:11 msanders

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 avatar Nov 09 '16 22:11 warpling

@warpling @msanders Is this currently supported?

otymartin avatar Feb 18 '17 06:02 otymartin

@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.

warpling avatar Feb 20 '17 20:02 warpling

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);
}

warpling avatar Feb 20 '17 20:02 warpling

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?

Visput avatar Apr 12 '17 16:04 Visput