The following code on Windows 10:
#include <chrono>
#include <iostream>
int main(void)
{
const auto p1 = std::chrono::system_clock::now();
const auto p2 = std::chrono::steady_clock::now();
std::cout << "system_clock seconds since epoch: "
<< std::chrono::duration_cast<std::chrono::seconds>(
p1.time_since_epoch()).count()
<< '\n';
std::cout << "steady_clock seconds since epoch: "
<< std::chrono::duration_cast<std::chrono::seconds>(
p2.time_since_epoch()).count()
<< '\n';
return 0;
}
Outputs:
system_clock seconds since epoch: 1690157550
steady_clock seconds since epoch: 434342
So steady_clock
appears to count the time since the last boot---even if I ask for time since the Epoch. To me this is not correct. It is not giving me the time since the Epoch.
The C++ standard does not impose any specific definition of an epoch, for all clocks, as a general requirement:
27.3 Cpp17Clock requirements [time.clock.req]
1A clock is a bundle consisting of a duration, a time_point, and a function now() to get the currenttime_point. The origin of the clock’s time_point is referred to as the clock’s epoch.
Full stop. A particular clock has some kind of an origin epoch, but what it is, is not specified in general. It is specified (if any) for each specific clock.
The system_clock
's origin, for example, is explicitly given as Jan 1 1970:
Class system_clock [time.clock.system]
[ ... ] Objects of type system_clock represent wall clock time from the system-wide realtime clock. Objects of type sys_time measure time since 1970-01-01 00:00:00 UTC excluding leap seconds.
No such requirement is given for a steady_clock
. The C++ standard leaves the origin, or the epoch, of steady_clock
unspecified. The only guarantee that steady_clock
gives you is that in never decreases. From the C++ standard:
Class steady_clock [time.clock.steady]
[...]
Objects of class steady_clock represent clocks for which values of time_point never decrease as physical time advances and for which values of time_point advance at a steady rate relative to real time. That is, the clock may not be adjusted.
That's it. There is no representation of steady_clock
have any kind of a relationship to wall clock time and any specific wall clock-based starting point. As such MS-Windows' implementation of counting time since system boot is within the scope of this definition.
Now it is true that the clock would effectively reset itself on the next boot, and a question can be asked whether this strictly complies with the "never decrease" requirement, but that would be a different question. In so far is to why steady_clock
does not count seconds since the Unix epoch, like system_clock
, is because this is not required by the C++ standard.