简体   繁体   English

有没有办法在不使用系统时钟的情况下获取 C++ 中的时间和日期?

[英]Is there a way to get time and day in C++ without using the system clock?

The question may sound stupid, but here's my case: I have a C++ program on Jetson Nano (a Single Board Computer) and want to get current time and date.这个问题听起来可能很愚蠢,但这是我的情况:我在 Jetson Nano(单板计算机)上有一个 C++ 程序,并且想要获取当前时间和日期。 In my application however, I can't always connect the internet cable to the device.然而,在我的应用程序中,我不能总是将互联网电缆连接到设备。 Although in the Ubuntu settings I told it to automatically get the time from the internet, when it powers on after a longer time period (when it was not connected to power), the system clock gets confused and has a completely wrong time and date.虽然在 Ubuntu 设置中我告诉它自动从互联网获取时间,但当它在较长时间后(未连接电源时)开机时,系统时钟会混淆并且时间和日期完全错误。 This is what my C++ program (using std::chrono::system_clock()) then picks up.这就是我的 C++ 程序(使用 std::chrono::system_clock())然后拾取的内容。

Is there a way to get around this issue?有没有办法解决这个问题? Somehow, make does know when system time and date are set incorrect even if the device is not connected to the internet.不知何故,即使设备未连接到互联网, make也知道系统时间和日期何时设置不正确。 I can tell because it fails when trying to compile with "Clock skew detected".我可以说是因为它在尝试使用“检测到时钟偏差”进行编译时失败。 How can I get the correct time and date when not connected to the internet?未连接到互联网时如何获取正确的时间和日期?

Somehow, make does know when system time and date are set incorrect even if the device is not connected to the internet.不知何故,即使设备未连接到互联网,make 也知道系统时间和日期何时设置不正确。

'Fraid not. '不怕。

It merely spots that the time has gone backwards since the last time it checked, and knows that this is not naturally how time works.它只是发现自上次检查以来时间已经倒退,并且知道这自然不是时间的运作方式。 It's guessing.是猜测。 The clock could be wrong now, it could have been wrong before, or it could be wrong both times.时钟现在可能错了,也可能以前错了,或者两次都错了。

A clock is where you get the time from.时钟是您获取时间的地方。 If you don't have a clock, you don't have the time.如果你没有时钟,你就没有时间。 ♂️ And if you do have a clock (as most computers do, including yours), but it's not traceable (eg by a NTP server connected to a local time server, or one on the internet), you'll never be able to trust it. ♂️ 如果您确实有一个时钟(就像大多数计算机一样,包括您的),但它是不可追踪的(例如,通过连接到本地时间服务器的 NTP 服务器或互联网上的一个),您将永远无法信任它。

You have a clock that's not traceable, and already unreliable because apparently no battery is present to let it keep time over the long term.您有一个无法追踪的时钟,并且已经不可靠,因为显然没有电池可以让它长期保持时间。 If you want to know the time, you'll need to fix at least one, ideally both, of these problems.如果您想知道时间,您需要解决至少一个问题,最好同时解决这两个问题。

Fortunately, it seems to be possible to install a backup battery for your Real-Time Clock , which solves the reliability.幸运的是,似乎可以为您的实时时钟安装备用电池,从而解决可靠性问题。 Without traceability you'd still be relying on the clock's own properties, and it may gradually lose or gain time, but if you manually set it once then you'll at least be in the ballpark for a good while.如果没有可追溯性,您仍然会依赖时钟自身的属性,它可能会逐渐失去或获得时间,但如果您手动设置一次,那么您至少会在球场上呆一段时间。 You can look up the clock's specs to find out how large that ballpark is, and thus how often you may need to update the time from another source (eg wristwatch.).您可以查看时钟的规格以了解该球场有多大,因此您可能需要多久从另一个来源(例如手表)更新时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM