diff --git a/bindings/python/src/datetime.cpp b/bindings/python/src/datetime.cpp index f1e91d702..79ece6add 100644 --- a/bindings/python/src/datetime.cpp +++ b/bindings/python/src/datetime.cpp @@ -5,6 +5,7 @@ #include "boost_python.hpp" #include #include "optional.hpp" +#include #include #include "libtorrent/time.hpp" @@ -55,6 +56,33 @@ struct time_duration_to_python } }; +struct time_point_to_python +{ + static PyObject* convert(lt::time_point tpt) + { + object result; + if (tpt > lt::min_time()) { + time_t const tm = boost::chrono::system_clock::to_time_t(boost::chrono::system_clock::now() + + boost::chrono::duration_cast(tpt - lt::clock_type::now())); + + std::tm* date = std::localtime(&tm); + result = datetime_datetime( + (int)1900 + date->tm_year + // tm use 0-11 and we need 1-12 + , (int)date->tm_mon + 1 + , (int)date->tm_mday + , date->tm_hour + , date->tm_min + , date->tm_sec + ); + } + else { + result = object(); + } + return incref(result.ptr()); + } +}; + struct ptime_to_python { static PyObject* convert(boost::posix_time::ptime const& pt) @@ -87,6 +115,11 @@ void bind_datetime() , time_duration_to_python >(); + to_python_converter< + lt::time_point + , time_point_to_python + >(); + to_python_converter< boost::posix_time::ptime , ptime_to_python