【问题标题】:How to get the RTC time in python Windows OS如何在 python Windows OS 中获取 RTC 时间
【发布时间】:2019-07-02 12:54:21
【问题描述】:

目前正在使用 python 代码中的 datetime.datetime.now() 来获取日期/时间戳。但是,这是在系统上设置的可以更改/更改的日期/时区。 如何从 python 检索实时时间,即 RTC 时间。任何帮助将不胜感激。

【问题讨论】:

  • 你使用“python-3.7”吗?如果是这样:你知道time.monotonic_ns吗?它也必须在 Windows 上运行

标签: python python-3.x time


【解决方案1】:

如果你使用的是Linux/Unix,你可以通过标准库中的time模块获取系统范围的实时时钟,如下:

import time
rtc = time.clock_gettime(time.CLOCK_REALTIME)
print("System RTC = {rtc}")

> System RTC = 1549619678.899073

对于 Windows,RTC 不可用。您可以使用多种其他时钟,具体取决于您的应用程序,它们不会受到系统时间更新的影响。例如,如果您尝试测量两个单独调用之间的时间,并且不希望这受到系统日期时间/时区更改的影响,您可以使用 Windows 上也可用的time.monotonic()。但是,仅相对于对time.monotonic() 的另一个调用有用(即用于测量持续时间)并且没有定义的参考点,因此您不能调用time.monotonic() 来询问“现在几点?”

【讨论】:

  • @taylor 是否适用于 Windows 操作系统?看起来支持 Unix,但是如何获得 Windows 操作系统的 RTC 呢?
  • @Sudeep 你是对的。 Documentation 声明“可用性:Unix”
  • 道歉 - 原始问题没有指定操作系统。我已更新以包含 Windows
猜你喜欢
  • 2018-10-18
  • 2019-10-30
  • 1970-01-01
  • 2023-03-10
  • 1970-01-01
  • 2013-04-03
  • 2017-08-02
  • 2021-06-02
相关资源
最近更新 更多