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

Ralf W. Grosse-Kunstleve rwgk at yahoo.com
Thu Apr 24 09:35:43 CEST 2008


You need a custom r-value converter. This is the essence:

http://www.boost.org/doc/libs/1_35_0/libs/python/doc/v2/faq.html#custom_string

Don't try this without valgrind! :-)

----- Original Message ----
From: Håkon Groven <hgroven at emgs.com>
To: Development of Python/C++ integration <c++-sig at python.org>; hgroven at emgs.com
Sent: Wednesday, April 23, 2008 7:50:11 AM
Subject: [C++-sig] boost::posix_time::ptime conversion

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
_______________________________________________
C++-sig mailing list
C++-sig at python.org
http://mail.python.org/mailman/listinfo/c++-sig




More information about the Cplusplus-sig mailing list