00001
00002
00003
00004
00005
00006
00007
00008 #include <boost/python.hpp>
00009 #include <boost/date_time/posix_time/posix_time_types.hpp>
00010 #include "optional.hpp"
00011 #include <boost/version.hpp>
00012
00013 using namespace boost::python;
00014
00015 #if BOOST_VERSION < 103400
00016
00017 object import(str name)
00018 {
00019
00020 char *n = extract<char *>(name);
00021 handle<> module(borrowed(PyImport_ImportModule(n)));
00022 return object(module);
00023 }
00024
00025 #endif
00026
00027 object datetime_timedelta;
00028 object datetime_datetime;
00029
00030 struct time_duration_to_python
00031 {
00032 static PyObject* convert(boost::posix_time::time_duration const& d)
00033 {
00034 object result = datetime_timedelta(
00035 0
00036 , 0
00037 , d.total_microseconds()
00038 );
00039
00040 return incref(result.ptr());
00041 }
00042 };
00043
00044 struct ptime_to_python
00045 {
00046 static PyObject* convert(boost::posix_time::ptime const& pt)
00047 {
00048 boost::gregorian::date date = pt.date();
00049 boost::posix_time::time_duration td = pt.time_of_day();
00050
00051 object result = datetime_datetime(
00052 (int)date.year()
00053 , (int)date.month()
00054 , (int)date.day()
00055 , td.hours()
00056 , td.minutes()
00057 , td.seconds()
00058 );
00059
00060 return incref(result.ptr());
00061 }
00062 };
00063
00064 void bind_datetime()
00065 {
00066 object datetime = import("datetime").attr("__dict__");
00067
00068 datetime_timedelta = datetime["timedelta"];
00069 datetime_datetime = datetime["datetime"];
00070
00071 to_python_converter<
00072 boost::posix_time::time_duration
00073 , time_duration_to_python
00074 >();
00075
00076 to_python_converter<
00077 boost::posix_time::ptime
00078 , ptime_to_python
00079 >();
00080
00081 optional_to_python<boost::posix_time::ptime>();
00082 }
00083