Skip to content

Prefer python's time.time_ns over libc if available #588

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion neo4j/time/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,10 @@ def local_offset(cls):

:raises OverflowError:
"""
return ClockTime(-int(mktime(gmtime(0))))
# Adding and subtracting two days to avoid passing a pre-epoch time to
# `mktime`, which can cause a `OverflowError` on some platforms (e.g.,
# Windows).
return ClockTime(-int(mktime(gmtime(172800))) + 172800)

def local_time(self):
""" Read and return the current local time from this clock, measured relative to the Unix Epoch.
Expand Down
50 changes: 25 additions & 25 deletions neo4j/time/clock_implementations.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,31 @@ def utc_time(self):
return ClockTime(seconds, nanoseconds * 1000)


class PEP564Clock(Clock):
""" Clock implementation based on the PEP564 additions to Python 3.7.
This clock is guaranteed nanosecond precision.
"""

@classmethod
def precision(cls):
return 9

@classmethod
def available(cls):
try:
from time import time_ns
except ImportError:
return False
else:
return True

def utc_time(self):
from time import time_ns
t = time_ns()
seconds, nanoseconds = divmod(t, 1000000000)
return ClockTime(seconds, nanoseconds)


class LibCClock(Clock):
""" Clock implementation that works only on platforms that provide
libc. This clock is guaranteed nanosecond precision.
Expand Down Expand Up @@ -79,28 +104,3 @@ def utc_time(self):
return ClockTime(ts.seconds, ts.nanoseconds)
else:
raise RuntimeError("clock_gettime failed with status %d" % status)


class PEP564Clock(Clock):
""" Clock implementation based on the PEP564 additions to Python 3.7.
This clock is guaranteed nanosecond precision.
"""

@classmethod
def precision(cls):
return 9

@classmethod
def available(cls):
try:
from time import time_ns
except ImportError:
return False
else:
return True

def utc_time(self):
from time import time_ns
t = time_ns()
seconds, nanoseconds = divmod(t, 1000000000)
return ClockTime(seconds, nanoseconds)
16 changes: 16 additions & 0 deletions tests/unit/time/test_clock.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,19 @@ def test_local_offset(self):
clock = object.__new__(Clock)
offset = clock.local_offset()
self.assertIsInstance(offset, ClockTime)

def test_local_time(self):
_ = Clock()
for impl in Clock._Clock__implementations:
self.assert_(issubclass(impl, Clock))
clock = object.__new__(impl)
time = clock.local_time()
self.assertIsInstance(time, ClockTime)

def test_utc_time(self):
_ = Clock()
for impl in Clock._Clock__implementations:
self.assert_(issubclass(impl, Clock))
clock = object.__new__(impl)
time = clock.utc_time()
self.assertIsInstance(time, ClockTime)