Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert between boost::posix_time::ptime and mongo::Date_t

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?

like image 227
Anthony Avatar asked Jun 11 '12 01:06

Anthony


1 Answers

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

like image 190
jean Avatar answered Oct 02 '22 09:10

jean