Totemi1324
Totemi1324

Reputation: 518

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. 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. This is what my C++ program (using std::chrono::system_clock()) then picks up.

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. 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?

Upvotes: 0

Views: 517

Answers (1)

Asteroids With Wings
Asteroids With Wings

Reputation: 17454

Somehow, make does know when system time and date are set incorrect even if the device is not connected to the internet.

'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 (e.g. by a NTP server connected to a local time server, or one on the internet), you'll never be able to trust it.

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 (e.g. wristwatch!).

Upvotes: 1

Related Questions