In Python 3.7, time_ns()
gives epoch timestamp in nanoseconds. How can it be got in 3.6.9?
When I use time()
, it gives me seconds.
Asked
Active
Viewed 912 times
-1

phileoseda
- 292
- 1
- 6
- 29
-
That should be resolvable with simple math. – Klaus D. Dec 08 '20 at 07:19
-
Maybe this will help you: https://stackoverflow.com/questions/2394485/get-posix-unix-time-in-seconds-and-nanoseconds-in-python – Sharmiko Dec 08 '20 at 08:04
-
1`time()` returns a floating point number, so it is much more precise than seconds. You just need to multiply it. – zvone Dec 08 '20 at 08:06
1 Answers
-2
You can't get nanosecond accuracy even using time_ns(). It's better than time(), but still not real nano-second accuracy.
Take a look at PEP-564 (https://www.python.org/dev/peps/pep-0564/#annex-clocks-resolution-in-python) - maybe you can just multiply by 1e9 and get a satisfactory result.

Radek
- 22
- 2
-
1`time()` and `time_ns()` are using the same underlying function to get the time. The only difference is that one returns a floating point and the other returns an integer. The precision (of both of them) depends on the operating system. – zvone Dec 08 '20 at 08:25
-
The link that I posted contains a resolution analysis. I suppose you didn't open it, so here is the summary: "The resolution of time.time_ns() is much better than time.time(): 84 ns (2.8x better) vs 239 ns on Linux and 318 us (2.8x better) vs 894 us on Windows." – Radek Dec 08 '20 at 08:42