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. It is available on all platforms and cannot fail.
Pseudo-code:: Pseudo-code [#pseudo]_: ::
if os.name == "nt": if os.name == "nt":
def time(): def time():
return _time.GetSystemTimeAsFileTime() 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: else:
def time(): 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"): if hasattr(_time, "gettimeofday"):
try: try:
# resolution = 1 microsecond # resolution = 1 microsecond
return _time.gettimeofday() return _time.gettimeofday()
except OSError: except OSError:
# gettimeofday() should not fail
pass pass
if hasattr(_time, "ftime"): if hasattr(_time, "ftime"):
# resolution = 1 millisecond # resolution = 1 millisecond
@ -100,9 +104,10 @@ valid.
It is not avaialble on all platforms and raise an OSError on error. It is not avaialble on all platforms and raise an OSError on error.
The monotonic clock may stop while the system is suspended. The monotonic clock may stop while the system is suspended.
Pseudo-code:: Pseudo-code [#pseudo]_: ::
if os.name == 'nt': if os.name == 'nt':
# GetTickCount64() requires Windows Vista, Server 2008 or later
if hasattr(time, '_GetTickCount64'): if hasattr(time, '_GetTickCount64'):
_get_tick_count = _time.GetTickCount64 _get_tick_count = _time.GetTickCount64
else: else:
@ -138,7 +143,7 @@ Pseudo-code::
return _time.mach_absolute_time() * monotonic.factor return _time.mach_absolute_time() * monotonic.factor
monotonic.factor = None monotonic.factor = None
elif hasattr(time, "clock_gettime") and hasattr(time, "CLOCK_MONOTONIC"): elif hasattr(time, "clock_gettime"):
def monotonic(): def monotonic():
if monotonic.use_monotonic_raw: if monotonic.use_monotonic_raw:
try: 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 adjusted. The clock starting point changes when the system is resumed after
being suspended. being suspended.
Pseudo-code:: Pseudo-code [#pseudo]_: ::
if os.name == 'nt': if os.name == 'nt':
def hires(): def hires():
@ -243,9 +248,11 @@ May fail?
requires a kernel version 2.6.28 or later. requires a kernel version 2.6.28 or later.
* GetTickCount() and GetTickCount64() cannot fail * 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 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 * 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 Links
===== =====