3

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.

cbuchart
  • 10,847
  • 9
  • 53
  • 93
eold
  • 5,972
  • 11
  • 56
  • 75

1 Answers1

2

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";
  }
Robᵩ
  • 163,533
  • 20
  • 239
  • 308
  • 1
    Worth noting here that the timezone offset information found in pt will affect the outcome. For example, if pt was grabbed with microsec_clock::universal_time(), you will get a different answer than if you did microsec_clock::local_time(). – Ben Jan 30 '12 at 17:04