I tried to use boost/chrono.... using boost::chrono::high_resolution_clock::now().time_since_epoch()
I am not able to get the current system time instead I get Thu Jan 01 09:53:53 1970... but with std::chrono I am able to get the correct system time.
How to get current system time using boost/chrono same as std:: chrono?
To get the system time, use system_clock
not high_resolution_clock
.
There's no guarantee, in either Boost or the standard library, that high_resolution_clock
will use the same epoch as the system clock.
Use simple math
boost::posix_time::ptime const time_epoch(boost::gregorian::date(1970, 1, 1));
auto ms = (boost::posix_time::microsec_clock::local_time() - time_epoch).total_microseconds();
std::cout << "microseconds: " << ms << "\n";
boost::posix_time::ptime now = time_epoch + boost::posix_time::microseconds(ms);
std::cout << boost::posix_time::to_iso_string(now);
More samples:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.