Python| time.time_ns() 方法
Python中的时间模块提供了各种与时间相关的功能。该模块属于 Python 的标准实用程序模块。
Time 模块的time.time_ns()
方法用于获取自纪元以来的时间(以纳秒为单位)。要获取自纪元以来的秒数,我们可以使用time.time()
方法。
纪元是时间开始的点并且取决于平台。在 Windows 和大多数 Unix 系统上,纪元是 1970 年 1 月 1 日 00:00:00 (UTC),并且闰秒不计入纪元以来的时间(以秒为单位)。要检查给定平台上的纪元,我们可以使用 time.gmtime(0)。
注意: time.time_ns()
方法是Python 3.7 版本中的新方法
Syntax:
time.time_ns()
Parameter:
No parameter is required.
Return type:
This method returns an integer value which
represents the time in nanoseconds since the epoch.
代码: time.time_ns()
方法的使用
# Python program to explain time.time_ns() method
# importing time module
import time
# Get the epoch
obj = time.gmtime(0)
epoch = time.asctime(obj)
print("epoch is:", epoch)
# Get the time in seconds
# since the epoch
# using time.time() method
time_sec = time.time()
# Get the time in nanoseconds
# since the epoch
# using time.time_ns() method
time_nanosec = time.time_ns()
# Print the time
# in seconds since the epoch
print("Time in seconds since the epoch:", time_sec)
# Print the time
# in nanoseconds since the epoch
print("Time in nanoseconds since the epoch:", time_nanosec)
输出:
epoch is: Thu Jan 1 00:00:00 1970
Time in seconds since the epoch: 1567451658.4676464
Time in nanoseconds since the epoch: 1567451658467647709
参考资料: https://docs。 Python.org/3/library/time.html#time.time_ns