2007-01-10 17:11:43 +01:00
|
|
|
// Copyright Daniel Wallin 2006. Use, modification and distribution is
|
|
|
|
// subject to the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
|
2016-04-08 04:45:23 +02:00
|
|
|
#include "boost_python.hpp"
|
2007-01-10 17:11:43 +01:00
|
|
|
#include <boost/date_time/posix_time/posix_time_types.hpp>
|
|
|
|
#include "optional.hpp"
|
2007-03-18 02:59:00 +01:00
|
|
|
#include <boost/version.hpp>
|
2016-01-06 06:17:51 +01:00
|
|
|
#include "libtorrent/time.hpp"
|
2016-11-10 23:08:32 +01:00
|
|
|
#include <ctime>
|
2007-01-10 17:11:43 +01:00
|
|
|
|
|
|
|
using namespace boost::python;
|
2016-01-06 06:17:51 +01:00
|
|
|
namespace lt = libtorrent;
|
2007-01-10 17:11:43 +01:00
|
|
|
|
2007-03-18 02:59:00 +01:00
|
|
|
#if BOOST_VERSION < 103400
|
|
|
|
|
2007-01-10 17:11:43 +01:00
|
|
|
// From Boost 1.34
|
|
|
|
object import(str name)
|
|
|
|
{
|
|
|
|
// should be 'char const *' but older python versions don't use 'const' yet.
|
|
|
|
char *n = extract<char *>(name);
|
|
|
|
handle<> module(borrowed(PyImport_ImportModule(n)));
|
|
|
|
return object(module);
|
|
|
|
}
|
|
|
|
|
2007-03-18 02:59:00 +01:00
|
|
|
#endif
|
|
|
|
|
2007-01-10 17:11:43 +01:00
|
|
|
object datetime_timedelta;
|
|
|
|
object datetime_datetime;
|
|
|
|
|
2016-11-10 23:08:32 +01:00
|
|
|
template <typename Duration>
|
|
|
|
struct chrono_duration_to_python
|
2016-01-06 06:17:51 +01:00
|
|
|
{
|
2016-11-10 23:08:32 +01:00
|
|
|
static PyObject* convert(Duration const& d)
|
2016-01-06 06:17:51 +01:00
|
|
|
{
|
2016-11-10 23:08:32 +01:00
|
|
|
std::int64_t const us = lt::total_microseconds(d);
|
2016-01-07 23:47:53 +01:00
|
|
|
object result = datetime_timedelta(
|
|
|
|
0 // days
|
2016-11-10 23:08:32 +01:00
|
|
|
, us / 1000000 // seconds
|
|
|
|
, us % 1000000 // microseconds
|
2016-01-07 23:47:53 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
return incref(result.ptr());
|
2016-01-06 06:17:51 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2007-01-10 17:11:43 +01:00
|
|
|
struct time_duration_to_python
|
|
|
|
{
|
|
|
|
static PyObject* convert(boost::posix_time::time_duration const& d)
|
|
|
|
{
|
|
|
|
object result = datetime_timedelta(
|
|
|
|
0 // days
|
|
|
|
, 0 // seconds
|
|
|
|
, d.total_microseconds()
|
|
|
|
);
|
|
|
|
|
|
|
|
return incref(result.ptr());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-11-10 23:08:32 +01:00
|
|
|
struct time_point_to_python
|
|
|
|
{
|
2017-02-06 01:18:06 +01:00
|
|
|
static PyObject* convert(lt::time_point const pt)
|
2016-11-10 23:08:32 +01:00
|
|
|
{
|
|
|
|
using std::chrono::system_clock;
|
|
|
|
using std::chrono::duration_cast;
|
|
|
|
time_t const tm = system_clock::to_time_t(system_clock::now()
|
|
|
|
+ duration_cast<system_clock::duration>(pt - lt::clock_type::now()));
|
|
|
|
|
2017-02-05 18:51:48 +01:00
|
|
|
std::tm* date = std::localtime(&tm);
|
2016-11-10 23:08:32 +01:00
|
|
|
object result = datetime_datetime(
|
|
|
|
(int)1900 + date->tm_year
|
2016-12-31 18:33:50 +01:00
|
|
|
// tm use 0-11 and we need 1-12
|
|
|
|
, (int)date->tm_mon + 1
|
2016-11-10 23:08:32 +01:00
|
|
|
, (int)date->tm_mday
|
|
|
|
, date->tm_hour
|
|
|
|
, date->tm_min
|
|
|
|
, date->tm_sec
|
|
|
|
);
|
|
|
|
return incref(result.ptr());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2007-01-10 17:11:43 +01:00
|
|
|
struct ptime_to_python
|
|
|
|
{
|
|
|
|
static PyObject* convert(boost::posix_time::ptime const& pt)
|
|
|
|
{
|
|
|
|
boost::gregorian::date date = pt.date();
|
|
|
|
boost::posix_time::time_duration td = pt.time_of_day();
|
|
|
|
|
|
|
|
object result = datetime_datetime(
|
|
|
|
(int)date.year()
|
|
|
|
, (int)date.month()
|
|
|
|
, (int)date.day()
|
|
|
|
, td.hours()
|
|
|
|
, td.minutes()
|
|
|
|
, td.seconds()
|
|
|
|
);
|
|
|
|
|
|
|
|
return incref(result.ptr());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void bind_datetime()
|
|
|
|
{
|
|
|
|
object datetime = import("datetime").attr("__dict__");
|
|
|
|
|
|
|
|
datetime_timedelta = datetime["timedelta"];
|
|
|
|
datetime_datetime = datetime["datetime"];
|
|
|
|
|
2016-11-10 23:08:32 +01:00
|
|
|
to_python_converter<boost::posix_time::time_duration
|
|
|
|
, time_duration_to_python>();
|
|
|
|
|
|
|
|
to_python_converter<boost::posix_time::ptime
|
|
|
|
, ptime_to_python>();
|
|
|
|
|
|
|
|
to_python_converter<lt::time_point
|
|
|
|
, time_point_to_python>();
|
2007-01-10 17:11:43 +01:00
|
|
|
|
2016-11-10 23:08:32 +01:00
|
|
|
to_python_converter<lt::time_duration
|
|
|
|
, chrono_duration_to_python<lt::time_duration>>();
|
2007-01-10 17:11:43 +01:00
|
|
|
|
2016-11-10 23:08:32 +01:00
|
|
|
to_python_converter<std::chrono::seconds
|
|
|
|
, chrono_duration_to_python<std::chrono::seconds>>();
|
2016-01-06 06:17:51 +01:00
|
|
|
|
2007-01-10 17:11:43 +01:00
|
|
|
optional_to_python<boost::posix_time::ptime>();
|
|
|
|
}
|
|
|
|
|