Convert between boost::posix_time::ptime and mongo::Date_t
It seems your solution convert from ptime to Date_t has some problem, I try it and get incorrect result as I commented.I have a better way:
mongo::Date_t convert(const boost::posix_time::ptime& time){ boost::posix_time::ptime epoch(boost::gregorian::date(1970,boost::date_time::Jan,1)); boost::posix_time::time_duration d = time - epoch; return mongo::Date_t(d.total_milliseconds());}boost::posix_time::ptime convert(const mongo::Date_t& time){ boost::posix_time::ptime epoch(boost::gregorian::date(1970,boost::date_time::Jan,1)); boost::posix_time::time_duration d = boost::posix_time::milliseconds(time.millis);//- epoch; return boost::posix_time::ptime(epoch+d);}
Notice that 2.4.3(as I know) Date_t::toString() has bug, the 'date' has been add 8 hours. You can verify 'total milliseconds' at here
Basically, the Date_t struct is constructed from an unsigned long long millisecond count. I can only assume from the 1970-1-1:00:00.00 epoch.
You are correct.
I think your code is about as good as you're going to get. You have to somehow get to a time_t-like seconds-since-the epoch, which seems to be a little complex with ptime.