1 // Copyright Daniel Wallin 2006. Use, modification and distribution is
2 // subject to the Boost Software License, Version 1.0. (See accompanying
3 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5 #include <boost/python.hpp>
6 #include <boost/date_time/posix_time/posix_time_types.hpp>
7 #include "optional.hpp"
8 #include <boost/version.hpp>
10 using namespace boost::python
;
12 #if BOOST_VERSION < 103400
15 object
import(str name
)
17 // should be 'char const *' but older python versions don't use 'const' yet.
18 char *n
= extract
<char *>(name
);
19 handle
<> module(borrowed(PyImport_ImportModule(n
)));
20 return object(module
);
25 object datetime_timedelta
;
26 object datetime_datetime
;
28 struct time_duration_to_python
30 static PyObject
* convert(boost::posix_time::time_duration
const& d
)
32 object result
= datetime_timedelta(
35 , d
.total_microseconds()
38 return incref(result
.ptr());
42 struct ptime_to_python
44 static PyObject
* convert(boost::posix_time::ptime
const& pt
)
46 boost::gregorian::date date
= pt
.date();
47 boost::posix_time::time_duration td
= pt
.time_of_day();
49 object result
= datetime_datetime(
58 return incref(result
.ptr());
64 object datetime
= import("datetime").attr("__dict__");
66 datetime_timedelta
= datetime
["timedelta"];
67 datetime_datetime
= datetime
["datetime"];
70 boost::posix_time::time_duration
71 , time_duration_to_python
75 boost::posix_time::ptime
79 optional_to_python
<boost::posix_time::ptime
>();