I would like to transfer a boost::posix_time::ptime
over the network as a boost::int64_t
. According to A way to turn boost::posix_time::ptime into an __int64, I can easily define my own epoch and only transfer the time_duration
from that reference epoch as a 64 bits integer. But how to convert back to a 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 is not working since the time_duration
constructor taking a tick count as argument is private. I am also interested in any other way to simply transfer that ptime
over simple data types.