Boost: How to print/convert posix_time::ptime in milliseconds from Epoch?

倾然丶 夕夏残阳落幕 提交于 2019-12-22 10:11:01

问题


I am having trouble converting posix_time::ptime to a timestamp represented by time_t or posix_time::milliseconds, or any other appropriate type which can be easily printed (from Epoch).

I actually need just to print the timestamp represented by the posix_time::ptime in milliseconds, so if there is an easy way to print in that format, I don't actually need the conversion.


回答1:


This code will print the number of milliseconds since 1941-12-07T00:00:00. Obviously, you can choose whatever epoch suits your need.

void print_ptime_in_ms_from_epoch(const boost::posix_time::ptime& pt)
  {
    using boost::posix_time::ptime;
    using namespace boost::gregorian;
    std::cout << (pt-ptime(date(1941, Dec, 7))).total_milliseconds() << "\n";
  }


来源:https://stackoverflow.com/questions/5607432/boost-how-to-print-convert-posix-timeptime-in-milliseconds-from-epoch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!