0

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?

Lo1234
  • 45
  • 1
  • 8

2 Answers2

0

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.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
0

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:

Community
  • 1
  • 1
sehe
  • 374,641
  • 47
  • 450
  • 633