Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 723
eold Avatar asked Dec 02 '25 04:12

eold


1 Answers

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";
  }
like image 184
Robᵩ Avatar answered Dec 04 '25 19:12

Robᵩ



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!