python--获取-unix--时间-纳秒级
在 Python 中,可以使用 time 模块来获取当前时间的纳秒级时间戳。具体步骤如下:
-
导入
time模块。 -
使用
time.time()函数获取当前时间的秒级时间戳。 -
使用
time.clock_gettime()函数获取当前时间的纳秒级时间戳。
下面是一个示例代码:
import time
# 获取当前时间的秒级时间戳
timestamp_sec = int(time.time())
# 获取当前时间的纳秒级时间戳
timestamp_nsec = time.clock_gettime_ns(time.CLOCK_REALTIME)
print(f"秒级时间戳:{timestamp_sec}")
print(f"纳秒级时间戳:{timestamp_nsec}")
输出结果类似于:
秒级时间戳:1631149032
纳秒级时间戳:3054182919383
原文地址: https://www.cveoy.top/t/topic/gUQ 著作权归作者所有。请勿转载和采集!