How to convert std::chrono::monotonic_clock::now() to milliseconds and cast it to long?
using steady_clock or high_resolution_clock from chrono is also same. I have seen into std::chrono::duration_cast<std::chrono::milliseconds> but I only want the current timestamp and not any duration gaps.
The current timestamp is defined with respect to some point in time (hence it is a duration). For instance, it is "typical" to get a timestamp with respect to the beginning of the Epoch (January 1st 1970, in Unix). You can do that by using time_since_epoch():
namespace chr = std::chrono;
chr::time_point<chr::steady_clock> tp = chr::steady_clock::now();
std::cout << "hours since epoch: "
<< chr::duration_cast<chr::hours>(tp.time_since_epoch()).count()
<< '\n';
To get the value in milliseconds you would need to cast it to std::chrono::milliseconds, instead.
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