0

I am trying to get the number of seconds since some fixed points cannot be manipulated. Meaning, if the user changed OS date and time, it would still give me the true absolute number of seconds between the fixed time point and the moment of executing the code lines, I've tried chrono::steady_clock, but it seems to be affected by the changes in the OS clock.

Regardless Runtime, Accuracy, or Resolution.

  • What OS? And if the user changes the date/time of the system, you're likely going to have to get the time from another system that you trust. – Andrew Henle Jan 06 '22 at 18:28
  • 5
    You can pull the time from `pool.ntp.org` – ChrisMM Jan 06 '22 at 18:30
  • Monotonic clocks tend to use something like the start of the computer as the epoch rather than a fixed point in real time. – user4581301 Jan 06 '22 at 18:32
  • This question might help https://stackoverflow.com/questions/14504870/convert-stdchronotime-point-to-unix-timestamp – blurryroots Jan 06 '22 at 18:36
  • Not so helpful. You can't get a `time_t` from a `steady_clock` because `steady_clock` has no guaranteed mapping to real time. – user4581301 Jan 06 '22 at 18:37
  • Mostafa, please add to the question how accurate you want this time to be and does this time have to remain constant across interruptions in the program (unchanging after restart of program/computer)? – user4581301 Jan 06 '22 at 18:40
  • Also worth adding the epoch you're targeting. – user4581301 Jan 06 '22 at 18:41
  • 5
    `std::steady_clock` is monotonic. https://en.cppreference.com/w/cpp/chrono/steady_clock The page explicitly states that it is not affected by the wall clock. Something in OP's reasoning is flawed, but with no code, how is anyone to know? – sweenish Jan 06 '22 at 19:10
  • @sweenish: the doc says `std::steady_clock` is not *related to* the wall clock, which is not the same as "not affected". You can't tell what time it is from a `std::steady_clock`. The OP says that they want "seconds since epoch", so `std::steady_clock` by itself doesn't help. – rici Jan 06 '22 at 19:58
  • The computer does not have any absolute time reference (except the aforementioned NTP - which still can be spoofed), unless there is some very specific hardware for that. So there is no way to do it in any somewhat generic way. – Eugene Sh. Jan 06 '22 at 20:01
  • 2
    @rici It also states that it's **monotonic**. The rest of your comment had already been addressed in an earlier comment that I did not contradict. I was speaking specifically to the point raised by OP that changing the wall clock had an affect. It shouldn't. Because `std::steady_clock` is monotonic. If OP is trying to say that it makes forward jumps, then I can happily admit my mistake. But I doubt that's the case, because `steady_clock` is also steady. "The time points of this clock cannot decrease as physical time moves forward and the time between ticks of this clock is constant" – sweenish Jan 06 '22 at 20:33
  • @sweenish: I agree that more information is needed for a diagnosis. Maybe we should just leave it at that. But I can tell you how to get non-steady behaviour from a steady clock, at least on some systems: let the computer go to sleep. When it wakes up, the system clock will still be related to wall time but the steady clock will have stopped for a while, so the difference between it and the wall time will have changed. It's still monotonic afaik, and I don't know how to get it to jump forward. – rici Jan 06 '22 at 22:53
  • That was fruitful, from your comments I get that it's not possible to get some sort of absolute time without some sort of hardware, I know that any motherboard should have some sort of offline oscillator that keeps running even while the computer is off, can't I use that somehow?, the question is more about if there some libraries interfacing that, or do I have to try researching more if it's doable in assembly ? – Mostafa Ahmed Asaad Jan 07 '22 at 09:34
  • 1
    You might be able to get some use out of the [Time Stamp Counter](https://en.wikipedia.org/wiki/Time_Stamp_Counter). – Steve Summit Jan 07 '22 at 14:08
  • 1
    It is still unclear what the exact requirements you are looking for. I get that you need a monotonic (never going backwards) clock, but do you need "epoch" to be a specific point in time? If yes, I do not think there is an exact solution. If not, C++ `steady_clock` or Posix `CLOCK_MONOTONIC` should do the job. Which problem did you observe with `steady_clock`? – nielsen Jan 07 '22 at 14:12
  • @nielsen, `steady_clock` changes value when I change systems date and time, I need it to have some absolute reference, because in the production settings, system's clock won't be that reliable. I will give Posix `CLOCK_MONOTONIC` a try – Mostafa Ahmed Asaad Jan 08 '22 at 14:06

1 Answers1

3

There is no such standard library clock. After all, how would that work? All clocks have to be based on something, set and maintained by someone. The system's clock has to know when it is relative to something else, and this happens when the user sets it.

The only way to achieve something like that is to access an off-system clock, like one of the websites that sends you the current UTC time.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
  • Are we talking in general, do you have an idea if it's doable in assembly, or it would be a waste of time? I know that any motherboard should have some sort of offline oscillator that keeps running even while the computer is off, can't I use that somehow? – Mostafa Ahmed Asaad Jan 07 '22 at 09:38
  • 1
    @MostafaAhmedAsaad well, that clock that you are talking about is not running since epoch (a.k.a. 1 January 1970) – bolov Jan 07 '22 at 13:55
  • @bolov, you are correct, alternative option is to keep track of seconds since the oscillator started, this could be reliable for my application than the system's clock. – Mostafa Ahmed Asaad Jan 08 '22 at 14:09
  • @MostafaAhmedAsaad: That's called "steady_clock". – Nicol Bolas Jan 08 '22 at 14:43