From 5bb376b3f9d0d103f7d7d5568a4985ea99df2e5c Mon Sep 17 00:00:00 2001 From: John Wiegley Date: Tue, 10 Nov 2009 20:52:36 -0500 Subject: Added implicit Python conversion of time_duration_t --- src/py_times.cc | 98 ++++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 86 insertions(+), 12 deletions(-) (limited to 'src/py_times.cc') diff --git a/src/py_times.cc b/src/py_times.cc index e140a23c..a62bb9b6 100644 --- a/src/py_times.cc +++ b/src/py_times.cc @@ -33,6 +33,7 @@ #include "pyinterp.h" #include "pyutils.h" +#include "times.h" // jww (2007-05-04): Convert time duration objects to PyDelta @@ -40,8 +41,6 @@ namespace ledger { using namespace boost::python; -typedef boost::gregorian::date date; - #define MY_PyDateTime_IMPORT \ PyDateTimeAPI = (PyDateTime_CAPI*) \ PyCObject_Import(const_cast("datetime"), \ @@ -49,7 +48,7 @@ typedef boost::gregorian::date date; struct date_to_python { - static PyObject* convert(const date& dte) + static PyObject* convert(const date_t& dte) { MY_PyDateTime_IMPORT; return PyDate_FromDate(dte.year(), dte.month(), dte.day()); @@ -77,13 +76,13 @@ struct date_from_python date::day_type d = static_cast(PyDateTime_GET_DAY(obj_ptr)); - date * dte = new date(y, m, d); + date_t * dte = new date_t(y, m, d); data->convertible = (void *) dte; } }; -typedef register_python_conversion +typedef register_python_conversion date_python_conversion; @@ -93,7 +92,7 @@ struct datetime_to_python { MY_PyDateTime_IMPORT; - date dte = moment.date(); + date_t dte = moment.date(); datetime_t::time_duration_type tod = moment.time_of_day(); return PyDateTime_FromDateAndTime @@ -127,28 +126,102 @@ struct datetime_from_python datetime_t::time_duration_type::hour_type h = static_cast - (PyDateTime_DATE_GET_HOUR(obj_ptr)); + (PyDateTime_DATE_GET_HOUR(obj_ptr)); datetime_t::time_duration_type::min_type min = static_cast - (PyDateTime_DATE_GET_MINUTE(obj_ptr)); + (PyDateTime_DATE_GET_MINUTE(obj_ptr)); datetime_t::time_duration_type::sec_type s = static_cast - (PyDateTime_DATE_GET_SECOND(obj_ptr)); + (PyDateTime_DATE_GET_SECOND(obj_ptr)); datetime_t::time_duration_type::fractional_seconds_type ms = static_cast - (PyDateTime_DATE_GET_MICROSECOND(obj_ptr)) * 1000000; + (PyDateTime_DATE_GET_MICROSECOND(obj_ptr)) * 1000000; datetime_t * moment - = new datetime_t(date(y, m, d), + = new datetime_t(date_t(y, m, d), datetime_t::time_duration_type(h, min, s, ms)); data->convertible = (void *) moment; } }; -typedef register_python_conversion +typedef register_python_conversion datetime_python_conversion; + +/* Convert time_duration to/from python */ +struct duration_to_python +{ + static int get_usecs(boost::posix_time::time_duration const& d) + { + static int64_t resolution = + boost::posix_time::time_duration::ticks_per_second(); + int64_t fracsecs = d.fractional_seconds(); + if (resolution > 1000000) + return static_cast(fracsecs / (resolution / 1000000)); + else + return static_cast(fracsecs * (1000000 / resolution)); + } + + static PyObject * convert(posix_time::time_duration d) + { + int days = d.hours() / 24; + if (days < 0) + days --; + int seconds = d.total_seconds() - days*(24*3600); + int usecs = get_usecs(d); + if (days < 0) + usecs = 1000000-1 - usecs; + return PyDelta_FromDSU(days, seconds, usecs); + } +}; + +/* Should support the negative values, but not the special boost time + durations */ +struct duration_from_python +{ + static void* convertible(PyObject * obj_ptr) + { + if ( ! PyDelta_Check(obj_ptr)) + return 0; + return obj_ptr; + } + + static void construct(PyObject* obj_ptr, + python::converter::rvalue_from_python_stage1_data * data) + { + PyDateTime_Delta const* pydelta + = reinterpret_cast(obj_ptr); + + int days = pydelta->days; + bool is_negative = (days < 0); + if (is_negative) + days = -days; + + // Create time duration object + posix_time::time_duration + duration = (posix_time::hours(24) * days + + posix_time::seconds(pydelta->seconds) + + posix_time::microseconds(pydelta->microseconds)); + if (is_negative) + duration = duration.invert_sign(); + + void * storage = + reinterpret_cast *> + (data)->storage.bytes; + + new (storage) posix_time::time_duration(duration); + data->convertible = storage; + } +}; + +typedef register_python_conversion + duration_python_conversion; + + datetime_t py_parse_datetime(const string& str) { return parse_datetime(str); } @@ -161,6 +234,7 @@ void export_times() { datetime_python_conversion(); date_python_conversion(); + duration_python_conversion(); register_optional_to_python(); register_optional_to_python(); -- cgit v1.2.3