[C++-sig] boost::posix_time::ptime conversion

Håkon Groven hgroven at emgs.com
Wed Apr 23 16:50:11 CEST 2008


Hi!

I'm converting boost::posix_time::ptime to python like this:

namespace
{
     struct ptime_to_python
     {	
	static PyObject* convert(boost::posix_time::ptime const& pt)
	{
	    boost::gregorian::date date = pt.date();
	    boost::posix_time::time_duration td = pt.time_of_day();
	
	    object result = datetime_datetime(
					      (int)date.year()
					      , (int)date.month()
					      , (int)date.day()
					      , td.hours()
					      , td.minutes()
					      , td.seconds()
					      );
	
	    return incref(result.ptr());
	}
     };
}

BOOST_PYTHON_MODULE(Pyelio)
{
     to_python_converter<boost::posix_time::ptime, ptime_to_python>();

     .
     .
     .
     .
     .
     .
     .
}

This works fine, but I cannot find out how to convert from python 
(datetime.datetime) into
boost::posix_time::ptime in the opposite direction.

I'd be glad if anybody could help me out!

Best wishes, Haakon



More information about the Cplusplus-sig mailing list