[ntp:questions] Hoes a IoT device discover the NTP era with NTPv4?
terje.mathisen at tmsw.no
Mon Mar 11 20:26:13 UTC 2019
Nelson Bolyard wrote:
> I'm working on an IoT device whose clock only runs when it has power.
> When it first starts up, and also after every crash, its clock starts
> at zero. Today this device uses SNTPv3 to set its clock by
> contacting a nearby NTP server, and always assumes it is in NTP era
> But 2036 is fast approaching. I want to know how, using NTPv4, a
> device like mine can figure out that it is no longer in era zero.
> Once it figures out the era that it is in, the rest should be easy.
Instead of assuming you are in era 0, which started at 1900-01-01, you
should assume that you are in the era starting at your compile/build date.
I.e. upon startup the local clock gets set to 2019-03-11, then it
contacts an (S)NTP server and calculates the corresponding date.
While that date is less than the compile date, add 2^32 seconds to the date.
With just a single iteration this will last you about 134 years past
your build date, which is probably more than enough for an IoT device.
BTW, the easiest way to do this is to make sure you have a 64-bit
time_t, so that you can convert the NTP timestamp to Unix time_t (i.e.
adding 70 years), then add 2^32 until that value is greater than your
- <Terje.Mathisen at tmsw.no>
"almost all programming can be viewed as an exercise in caching"
More information about the questions