time.monotonic() roll over

Nobody nobody at nowhere.invalid
Thu Dec 4 19:54:07 EST 2014


On Thu, 04 Dec 2014 16:25:44 +0100, ast wrote:

> There is no roll over problem with time.time() since the very
> first one in planned far in the future, but time.time() can go
> backward when a date update throught NTP server is done.

> time.monotonic() is monotonic but roll over often (every 49.7 
> days)
> 
> So what to do ? 
> 
> Using time.monotonic() and take some actions if a roll over 
> is detected ?

One possibility: use both and monitor the skew (the difference between the
two clocks).

The first time you read them, calculate and store the epoch for
time.monotic(). On subsequent calls, repeat the calculation and check that
the epoch is approximately the same. If it isn't, the difference between
the actual and expected values should be close to an exact multiple of
2**32 milliseconds, which tells you how many times time.monotonic() has
rolled over.

Adjustments to the clock on which time.time() is based shouldn't exceed a
few seconds per year, so there's not much risk that you won't be able to
figure out the correct adjustment.




More information about the Python-list mailing list