diff options
author | John Wiegley <johnw@newartisans.com> | 2009-11-10 20:52:36 -0500 |
---|---|---|
committer | John Wiegley <johnw@newartisans.com> | 2009-11-10 20:52:36 -0500 |
commit | 5bb376b3f9d0d103f7d7d5568a4985ea99df2e5c (patch) | |
tree | bee58c1a6ff53c70dbc0db08e015cb2646441f95 /src/py_times.cc | |
parent | 048845184a91daaf6d23c09d43367b6d128fdf2b (diff) | |
download | fork-ledger-5bb376b3f9d0d103f7d7d5568a4985ea99df2e5c.tar.gz fork-ledger-5bb376b3f9d0d103f7d7d5568a4985ea99df2e5c.tar.bz2 fork-ledger-5bb376b3f9d0d103f7d7d5568a4985ea99df2e5c.zip |
Added implicit Python conversion of time_duration_t
Diffstat (limited to 'src/py_times.cc')
-rw-r--r-- | src/py_times.cc | 98 |
1 files changed, 86 insertions, 12 deletions
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<char *>("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<date::day_type>(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<date, date_to_python, date_from_python> +typedef register_python_conversion<date_t, date_to_python, date_from_python> 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<datetime_t::time_duration_type::hour_type> - (PyDateTime_DATE_GET_HOUR(obj_ptr)); + (PyDateTime_DATE_GET_HOUR(obj_ptr)); datetime_t::time_duration_type::min_type min = static_cast<datetime_t::time_duration_type::min_type> - (PyDateTime_DATE_GET_MINUTE(obj_ptr)); + (PyDateTime_DATE_GET_MINUTE(obj_ptr)); datetime_t::time_duration_type::sec_type s = static_cast<datetime_t::time_duration_type::sec_type> - (PyDateTime_DATE_GET_SECOND(obj_ptr)); + (PyDateTime_DATE_GET_SECOND(obj_ptr)); datetime_t::time_duration_type::fractional_seconds_type ms = static_cast<datetime_t::time_duration_type::fractional_seconds_type> - (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<datetime_t, datetime_to_python, datetime_from_python> +typedef register_python_conversion<datetime_t, + datetime_to_python, datetime_from_python> 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<int>(fracsecs / (resolution / 1000000)); + else + return static_cast<int>(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<PyDateTime_Delta*>(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<converter::rvalue_from_python_storage + <posix_time::time_duration> *> + (data)->storage.bytes; + + new (storage) posix_time::time_duration(duration); + data->convertible = storage; + } +}; + +typedef register_python_conversion<time_duration_t, + duration_to_python, duration_from_python> + 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<datetime_t>(); register_optional_to_python<date_t>(); |