Sunday 15 February 2015

c++ - time since epoch to string using boost -



c++ - time since epoch to string using boost -

auto timesinceepoch = boost::chrono::duration_cast<boost::chrono::microseconds>(boost::chrono::steady_clock::now().time_since_epoch()).count(); boost::posix_time::ptime now(boost::gregorian::date(1970, 1, 1), boost::posix_time::microsec(static_cast<std::int64_t>(timesinceepoch))); std::string str = boost::posix_time::to_iso_string(now);

output : 19700114t232422.133653 incorrect, doing wrong ?

on systems, epoch of steady_clock nanoseconds since boot.

you more useful expected result other clocks:

live on coliru

#include <boost/date_time/gregorian/gregorian.hpp> #include <boost/date_time/posix_time/posix_time.hpp> #include <boost/chrono.hpp> #include <string> #include <iostream> int main() { 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 = time_epoch + boost::posix_time::microseconds(ms); std::cout << boost::posix_time::to_iso_string(now); }

prints

microseconds: 1415115992000000 20141104t154632

c++ boost time

No comments:

Post a Comment