I've been trying to find a way to get the time since 1970-01-01 00:00:00 UTC in seconds and nanoseconds in python and I cannot find anything that will give me the proper precision.
I have tried using time module, but that precision is only to microseconds, so the code I tried was:
import time print time.time()
which gave me a result like this:
1267918039.01
However, I need a result that looks like this:
1267918039.331291406
Does anyone know a possible way to express UNIX time in seconds and nanoseconds? I cannot find a way to set the proper precision or get a result in the correct format. Thank you for any help
On Python 3, the time module gives you access to 5 different types of clock, each with different properties; some of these may offer you nanosecond precision timing.
time_ns() method of Time module is used to get the time in nanoseconds since the epoch. To get the time in seconds since the epoch, we can use time. time() method. The epoch is the point where the time starts and is platform dependent.
To manipulate dates and times in Python, you can use the datetime module. Use datetime. fromtimestamp() of the datetime module to convert Unix time (Epoch time) to datetime object.
To measure the elapsed time between two code instances in nanoseconds, you can use the time. time_ns() function, which returns the time in nanoseconds since the epoch in floating-point number.
Since Python 3.7 it's easy to achieve with time.time_ns()
Similar to
time()
but returns time as an integer number of nanoseconds since the epoch.
All new features that includes nanoseconds in Python 3.7 release: PEP 564: Add new time functions with nanosecond resolution
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With