[Python-checkins] peps: PEP 418: Write the pseudo-code of time.time()

victor.stinner python-checkins at python.org
Tue Mar 27 19:34:08 CEST 2012


http://hg.python.org/peps/rev/2580540e4d62
changeset:   4154:2580540e4d62
user:        Victor Stinner <vstinner at wyplay.com>
date:        Tue Mar 27 19:34:04 2012 +0200
summary:
  PEP 418: Write the pseudo-code of time.time()

files:
  pep-0418.txt |  27 ++++++++++++++++++++++++---
  1 files changed, 24 insertions(+), 3 deletions(-)


diff --git a/pep-0418.txt b/pep-0418.txt
--- a/pep-0418.txt
+++ b/pep-0418.txt
@@ -62,10 +62,31 @@
 administrator or automatically by a NTP daemon.  It can jump backward and
 forward, and is not monotonic.
 
- * Windows: GetSystemTimeAsFileTime()
- * clock_gettime(CLOCK_REALTIME), gettimeofday(), ftime() or time()
+It is avaialble on all platforms and cannot fail.
 
-It is avaialble on all platforms and cannot fail.
+Pseudo-code::
+
+    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, "gettimeofday"):
+                try:
+                    # resolution = 1 microsecond
+                    return _time.gettimeofday()
+                except OSError:
+                    pass
+            if hasattr(_time, "ftime"):
+                # resolution = 1 millisecond
+                return _time.ftime()
+            else:
+                # resolution = 1 second
+                return _time.time()
 
 
 time.monotonic()

-- 
Repository URL: http://hg.python.org/peps


More information about the Python-checkins mailing list