fixes bug where priorities where lost when force-rechecking.
[libtorrent.git] / bindings / python / src / datetime.cpp
blobda13476718250605abaacb00550d6d2c53277155
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
14 // From Boost 1.34
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);
23 #endif
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(
33 0 // days
34 , 0 // seconds
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(
50 (int)date.year()
51 , (int)date.month()
52 , (int)date.day()
53 , td.hours()
54 , td.minutes()
55 , td.seconds()
58 return incref(result.ptr());
62 void bind_datetime()
64 object datetime = import("datetime").attr("__dict__");
66 datetime_timedelta = datetime["timedelta"];
67 datetime_datetime = datetime["datetime"];
69 to_python_converter<
70 boost::posix_time::time_duration
71 , time_duration_to_python
72 >();
74 to_python_converter<
75 boost::posix_time::ptime
76 , ptime_to_python
77 >();
79 optional_to_python<boost::posix_time::ptime>();