I have been using a hack like this in a lot of my code:
import time
if not hasattr(time, 'time_ns'):
time.time_ns = lambda: int(time.time() * 1e9)
It works around the limitation of Python 3.6 and earlier, which did not have a time_ns
method. The issue is that the above workaround is based on time.time
, which returns a float. In 2019's UTC, this is about accurate to a micro-second scale.
How would I implement time_ns
for older versions of Python with full nano-second accuracy? (Primarily targeting UNIX-like systems.)