Boost: converting xtime to posix_time

Here is some Boost code to convert (approximately) from an xtime to a posix_time. (The function forgets the nanoseconds.)

#include <boost/thread/xtime.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
::boost::posix_time::ptime
  convert_to_posix_time (const ::boost::xtime & xt) // from_xtime
{
  // this conversion is valid until
  // about the year 2038 = 1970 + 2^31 seconds
  // after which long(xt.sec) will wrap
  ::boost::posix_time::ptime pt (
      ::boost::gregorian::date (1970, boost::gregorian::Jan, 1),
      ::boost::posix_time::seconds (long (xt.sec)) );
  return pt;
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: