bug report: [ #447945 ] time.time() is not non-decreasing

zooko at zooko.com zooko at zooko.com
Sat Aug 4 17:59:15 EDT 2001


Okay, I think what is happening is that `time.time()' uses `gettimeofday()'
which reads the hardware clock.  This means that in theory it can return
anything at all, and even in practice the following things can, I think, cause
`time.time()' to return decreasing (or even wildly varying) answers:

 * skew correction, where your OS notices that your hw clock runs too fast or
   too slow and resets it a little bit to keep it more accurate,
 * synchronizing with another clock e.g.
  + the user manually fixing the date, or
  + an NTP like service which automatically syncs using network signals,
 * daylight savings time, if your OS is silly enough to store that in the
   hardware clock.
 * your CPU speed varying can fool the linux kernel as far as how much time has
   passed (thanks to Johannes Stezenbach for explaining that[1]).

For some uses, you *want* your timer to reflect these kinds of changes (except
for the last one), but for my purposes in Mojo Nation I needed a guaranteed
monotonically increasing time.

So I wrote a monotonically-increasing timer[2], which was a bit trickier than 
I thought it would be.  It comes with its own unit test, and if you have
RunTests[3] then you can execute `timerutil.py' itself to run all of its tests.
The application which needs monotonically increasing timestamps is the `DoQ'[4]
(the "do queue") -- a simple event scheduler that we use in Mojo Nation.

Without the monotonically increasing guarantee, the scheduler was sometimes
doing some tasks in the opposite order that the tasks had been requested, which
was causing inexplicable behaviour.


If a floating point expert looks at IncreasingTimer, I would really like to
know if there is an efficient way to pick the *very next* floating point number
up.  I tried to approximate that in here by calculating the epsilon
experimentally and then doubling it whenever it failed to bump the float up.


Regards,

Zooko

[1] http://mail.python.org/pipermail/python-list/2001-August/058332.html
[2] http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/~checkout~/mojonation/evil/common/timeutil.py?content-type=text/plain
[3] http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/~checkout~/mojonation/evil/common/RunTests.py?content-type=text/plain
[4] http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/~checkout~/mojonation/evil/common/DoQ.py?content-type=text/plain




More information about the Python-list mailing list