How do I print out a time_point when the time_point is obtained from high_resolution_clock?
timestamp = std::chrono::high_resolution_clock::now();
std::time_t now = std::chrono::system_clock::to_time_t(timestamp);
std::cout << std::ctime(&now) << std::endl;
I get the following error message when compiling:
error: no viable conversion from 'time_point<class std::__1::chrono::steady_clock, duration<[...], ratio<[...], 1000000000>>>' to 'const time_point<class std::__1::chrono::system_clock, duration<[...], ratio<[...], 1000000>>>'
time_t tt = std::chrono::system_clock::to_time_t(timestamp);
Class template std::chrono::time_point represents a point in time. It is implemented as if it stores a value of type Duration indicating the time interval from the start of the Clock 's epoch. Clock must meet the requirements for Clock or be std::chrono::local_t (since C++20).
(C++11 feature) Class std::chrono::high_resolution_clock represents the clock with the smallest tick period available on the system. It may be an alias of std::chrono::system_clock or std::chrono::steady_clock, or a third, independent clock.
Class std::chrono::high_resolution_clock represents the clock with the smallest tick period provided by the implementation. It may be an alias of std::chrono::system_clock or std::chrono::steady_clock, or a third, independent clock.
high_resolution_clock - this is a clock with the shortest tick period possible on the current system; steady_clock - this is a monotonic clock that is guaranteed to never be adjusted.
There is no truly graceful way to do this. high_resolution_clock
is not known to be related to UTC, or any other calendar. One thing you can portably do is output its current duration from its unspecified epoch, along with the units of that duration:
#include <chrono>
#include <iostream>
int
main()
{
using Clock = std::chrono::high_resolution_clock;
constexpr auto num = Clock::period::num;
constexpr auto den = Clock::period::den;
std::cout << Clock::now().time_since_epoch().count()
<< " [" << num << '/' << den << "] units since epoch\n";
}
Which for me outputs:
516583779589531 [1/1000000000] units since epoch
which means it is 516583779589531 nanoseconds (or 516,583.779589531 seconds) since the epoch of this clock on my machine. On my machine this translates to: my machine has been booted up for nearly 6 days. But that translation is not portable.
Ah! And I note from your error message that you are using libc++. If you are also on OS X, then we have the same definition of high_resolution_clock
: It counts nanoseconds since your computer was booted.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With