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);
输出: 19700114T232422.133653 这是不正确的,我做错了什么?
答案 0 :(得分:4)
在某些系统上,自启动以来,steady_clock的纪元是纳秒。
您将从其他时钟获得更有用的预期结果:
<强> 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 now = time_epoch + boost::posix_time::microseconds(ms);
std::cout << boost::posix_time::to_iso_string(now);
}
打印
microseconds: 1415115992000000
20141104T154632