PEP 418: Fix time.time() pseudo-code, clock_gettime(CLOCK_REALTIME) falls back

to get _time.gettimeofday() on OSError

Add also a note about the _time module.
This commit is contained in:
Victor Stinner 2012-03-28 01:57:37 +02:00
parent 8d75780f92
commit 280378cb3a
1 changed files with 24 additions and 10 deletions

View File

@ -64,22 +64,26 @@ forward, and is not monotonic.
It is available on all platforms and cannot fail.
Pseudo-code::
Pseudo-code [#pseudo]_: ::
if os.name == "nt":
def time():
return _time.GetSystemTimeAsFileTime()
elif hasattr(time, "clock_gettime") and hasattr(time, "CLOCK_REALTIME"):
def time():
# resolution = 1 nanosecond
return time.clock_gettime(time.CLOCK_REALTIME)
else:
def time():
if hasattr(time, "clock_gettime"):
try:
# resolution = 1 nanosecond
return time.clock_gettime(time.CLOCK_REALTIME)
except OSError:
# CLOCK_REALTIME is not supported (unlikely)
pass
if hasattr(_time, "gettimeofday"):
try:
# resolution = 1 microsecond
return _time.gettimeofday()
except OSError:
# gettimeofday() should not fail
pass
if hasattr(_time, "ftime"):
# resolution = 1 millisecond
@ -100,9 +104,10 @@ valid.
It is not avaialble on all platforms and raise an OSError on error.
The monotonic clock may stop while the system is suspended.
Pseudo-code::
Pseudo-code [#pseudo]_: ::
if os.name == 'nt':
# GetTickCount64() requires Windows Vista, Server 2008 or later
if hasattr(time, '_GetTickCount64'):
_get_tick_count = _time.GetTickCount64
else:
@ -138,7 +143,7 @@ Pseudo-code::
return _time.mach_absolute_time() * monotonic.factor
monotonic.factor = None
elif hasattr(time, "clock_gettime") and hasattr(time, "CLOCK_MONOTONIC"):
elif hasattr(time, "clock_gettime"):
def monotonic():
if monotonic.use_monotonic_raw:
try:
@ -156,7 +161,7 @@ High-resolution clock. It has an unspecified starting point and may be
adjusted. The clock starting point changes when the system is resumed after
being suspended.
Pseudo-code::
Pseudo-code [#pseudo]_: ::
if os.name == 'nt':
def hires():
@ -243,9 +248,11 @@ May fail?
requires a kernel version 2.6.28 or later.
* GetTickCount() and GetTickCount64() cannot fail
Note: clock_gettime() requires to link the program with the realtime ("rt") library.
.. note::
clock_gettime() requires to link the program with the realtime ("rt") library.
Note: GetTickCount64() was added to Windows Vista and Windows Server 2008.
.. note::
GetTickCount64() was added to Windows Vista and Windows Server 2008.
System time
@ -396,6 +403,13 @@ computing the maximum with the clock value and the previous value?
* QueryPerformanceCounter() had a bug in 2006 on multiprocessor computers
Footnotes
=========
.. [#pseudo] _time is an hypothetical module used for the example. In practice,
functions will be implemented in C and so don't need a module.
Links
=====