I would like to pass boost::posix_time::ptime over the network as boost::int64_t . According to the Way to turn boost :: posix_time :: ptime into __int64 , I can easily define my own era and transfer only time_duration from this reference era as a 64-bit integer. But how to convert back to ptime ?
#include <iostream> #include <cassert> #include <boost/date_time/posix_time/posix_time.hpp> #include <boost/date_time/gregorian/greg_month.hpp> using namespace std; using boost::posix_time::ptime; using boost::posix_time::time_duration; using boost::gregorian::date; int main(int argc, char ** argv){ ptime t = boost::posix_time::microsec_clock::local_time(); // convert to int64_t ptime myEpoch(date(1970,boost::gregorian::Jan,1)); time_duration myTimeFromEpoch = t - myEpoch; boost::int64_t myTimeAsInt = myTimeFromEpoch.ticks(); // convert back to ptime ptime test = myEpoch + time_duration(myTimeAsInt); assert(test == t); return 0; }
This does not work, since the constructor time_duration , which takes the number of ticks as an argument, is private. I am also interested in any other way to simply pass this ptime over simple data types.
c ++ boost time
tibur
source share