Is there a succinct way, or a generally accepted way to convert from a boost::posix_time::ptime
to a mongo::Date_t
and back again?
Mongo to Boost
The Boost documentation seems incomplete or incorrect. It documents a function date_from_tm
which constructs a date
struct from a tm
. However, the following example is given:
tm pt_tm;
/* snip */
ptime pt = ptime_from_tm(pt_tm);
But there is no documented function ptime_from_tm
. However this header file does include that function.
So, I can at least go from mongo::Date_t
to boost::posix_time::ptime
:
mongo::Date_t d = ...;
std::tm t;
d.toTm(&t);
boost::posix_time::ptime pt = ptime_from_tm(t);
Boost to Mongo
I'm kind of stuck when it comes to going in the opposite direction. The MongoDB documentation is rather incomplete, and the associated header file doesn't have many helpful comments. 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.
So my current solution to go from a boost::posix_time::ptime
to a mongo::Date_t
is:
boost::posix_time::ptime pt = ...;
std::tm pt_tm = boost::posix_time::to_tm(pt);
std::time_t t = mktime(pt_tm);
mongo::Date_t d(t);
Of course, I can probably collapse that into one line, but it seems that the whole round trip from one date/time representation to the other and back again is becoming convoluted and messy.
Finally
Is there a better way? Does anybody with a better knowledge of both libraries and a good understanding of date/time programming know a succinct, simple way to achieve what I'm trying to achieve?