【问题标题】:How to get timestamp in nanoseconds in Python 3? [duplicate]如何在 Python 3 中以纳秒为单位获取时间戳? [复制]
【发布时间】:2020-09-16 01:44:08
【问题描述】:
import time
print(time.time())

这给了我输出:

1590683242.8507078

所需的输出是 19 位长的纳秒 Unix 时间戳。

【问题讨论】:

标签: python python-3.x timestamp influxdb epoch


【解决方案1】:

使用

import time
print(time.time_ns())

【讨论】:

    【解决方案2】:

    time.time_ns()

    与 time() 类似,但以自纪元以​​来的纳秒整数形式返回时间。

    https://docs.python.org/3.7/library/time.html#time.time_ns

    【讨论】:

    • 我的 python 版本是 3.6。谢谢!
    【解决方案3】:
    import time
    print(time.time_ns())
    
    1590683731073953000
    

    【讨论】:

      【解决方案4】:

      您可以使用 Time 模块的 time.time_ns() 方法。

      import time
      print(time.time_ns())
      
      • time.time() -- 以秒为单位检索时间。
      • time.time_ns() -- 以纳秒为单位检索时间。

      【讨论】:

      • 显示“时间没有属性time_ns()
      • 你是这样导入时间模块的,还是不小心从时间模块导入了时间函数?
      • 从 python3.6 升级到 3.7 工作。谢谢!!
      猜你喜欢
      • 2021-03-19
      • 1970-01-01
      • 2011-01-24
      • 1970-01-01
      • 1970-01-01
      • 2012-10-07
      • 1970-01-01
      • 2015-10-09
      • 1970-01-01
      相关资源
      最近更新 更多