Backport time_point to python convertor. Fix bindings returning invalid date from time_point convertor

This commit is contained in:
Calum Lind 2017-02-05 22:58:02 +00:00 committed by Arvid Norberg
parent de30f357b3
commit 4492cada4a
1 changed files with 33 additions and 0 deletions

View File

@ -5,6 +5,7 @@
#include "boost_python.hpp"
#include <boost/date_time/posix_time/posix_time_types.hpp>
#include "optional.hpp"
#include <boost/chrono.hpp>
#include <boost/version.hpp>
#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<boost::chrono::system_clock::duration>(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