Python Glossary Русский

Python 3.7+timefunction

time.time_ns()

clocks and timing

The current time since the Unix epoch as an integer number of nanoseconds — the same as time.time(), but without float precision loss.

Syntax

time.time_ns() -> int

A float holds about 16 significant digits, so at today's ~1.8e9 seconds only a few hundred nanoseconds of precision remain for the fraction: two close time() stamps can be equal. time_ns() returns an int, but a nanosecond unit does not mean nanosecond accuracy — the step comes from the OS. To get seconds, use integer division: time_ns() // 10**9.

Examples

import time
ns = time.time_ns()
print(type(ns).__name__)  # → int
print(ns)  # → ? 1791331200123456789
print(ns // 10**9 > 1_700_000_000)  # → True

See also

Open in the full glossarydocs.python.orgReport an inaccuracy