Skip to content

How TrueTime works

Kaushik Gopal edited this page Sep 5, 2018 · 2 revisions

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 Date object.

Validity of TrueTime

boot time

Also, once we have this information it's valid until the next time you boot your device. This means if you enable the disk caching feature, after a single successful NTP request you can use the information on disk directly without ever making another network request. This applies even across application kills which can happen frequently if your users have a memory starved device.

If the device is booted, the information received as part of the previous NTP request is no longer valid.

We have a BootCompletedBroadcastReceiver class that should automatically invalidate the cached information.

clock drift

Every clock tick (nanosecond) in your phone is controlled by the quality of the quartz crystal in the phone's CPU.

If the quality of that quartz crystal is good, then it keeps time better. If it isn't that good, there's a chance again that there's a clock drift. This means that even if you get the TrueTime NTP call to successfully run through once, that isn't enough for the perpetuity of time.

You will have to rerun the Truetime.init call at regular/frequent intervals so you resynchronize with the true time. Have a look at the "How to use TrueTime section" for more details.

Clone this wiki locally