diff options
author | John Wiegley <johnw@newartisans.com> | 2007-05-04 11:41:58 +0000 |
---|---|---|
committer | John Wiegley <johnw@newartisans.com> | 2008-04-13 03:38:38 -0400 |
commit | 96684b72ca367bfd4dbe2e45a9a66c56204eb533 (patch) | |
tree | 3e218d1048d5941baa1d1064bc2539ef2df55a45 /src/py_times.cc | |
parent | 93096b77f3c03b826c8857e4817ccd1bca52f9ee (diff) | |
download | fork-ledger-96684b72ca367bfd4dbe2e45a9a66c56204eb533.tar.gz fork-ledger-96684b72ca367bfd4dbe2e45a9a66c56204eb533.tar.bz2 fork-ledger-96684b72ca367bfd4dbe2e45a9a66c56204eb533.zip |
Added code for converting ledger::string and boost::date_time to their
respective Python counterparts.
Diffstat (limited to 'src/py_times.cc')
-rw-r--r-- | src/py_times.cc | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/src/py_times.cc b/src/py_times.cc new file mode 100644 index 00000000..578d887b --- /dev/null +++ b/src/py_times.cc @@ -0,0 +1,99 @@ +#include "pyinterp.h" +#include "pyutils.h" + +#include <boost/cast.hpp> +#include <boost/python/module.hpp> +#include <boost/python/def.hpp> +#include <boost/python/to_python_converter.hpp> + +#include <Python.h> +#include <datetime.h> + +namespace ledger { + +using namespace boost::python; + +typedef boost::gregorian::date date; + +struct date_to_python +{ + static PyObject* convert(const date& dte) + { + PyDateTime_IMPORT; + return PyDate_FromDate(dte.year(), dte.month(), dte.day()); + } +}; + +struct date_from_python +{ + static void* convertible(PyObject* obj_ptr) + { + PyDateTime_IMPORT; + if(PyDate_Check(obj_ptr) || PyDateTime_Check(obj_ptr)) return obj_ptr; + return 0; + } + + static void construct(PyObject* obj_ptr, converter::rvalue_from_python_stage1_data* data) + { + PyDateTime_IMPORT; + int y = PyDateTime_GET_YEAR(obj_ptr); + int m = PyDateTime_GET_MONTH(obj_ptr); + int d = PyDateTime_GET_DAY(obj_ptr); + date* dte = new date(y,m,d); + data->convertible = (void*)dte; + } +}; + +typedef register_python_conversion<date, date_to_python, date_from_python> + date_python_conversion; + + +typedef boost::posix_time::ptime datetime; + +struct datetime_to_python +{ + static PyObject* convert(const datetime& moment) + { + PyDateTime_IMPORT; + date dte = moment.date(); + datetime::time_duration_type tod = moment.time_of_day(); + return PyDateTime_FromDateAndTime(dte.year(), dte.month(), dte.day(), + tod.hours(), tod.minutes(), tod.seconds(), + tod.total_microseconds() % 1000000); + } +}; + +struct datetime_from_python +{ + static void* convertible(PyObject* obj_ptr) + { + PyDateTime_IMPORT; + if(PyDateTime_Check(obj_ptr)) return obj_ptr; + return 0; + } + + static void construct(PyObject* obj_ptr, converter::rvalue_from_python_stage1_data* data) + { + PyDateTime_IMPORT; + int y = PyDateTime_GET_YEAR(obj_ptr); + int m = PyDateTime_GET_MONTH(obj_ptr); + int d = PyDateTime_GET_DAY(obj_ptr); + int h = PyDateTime_DATE_GET_HOUR(obj_ptr); + int min = PyDateTime_DATE_GET_MINUTE(obj_ptr); + int s = PyDateTime_DATE_GET_SECOND(obj_ptr); + datetime* moment = new datetime(date(y,m,d), + datetime::time_duration_type(h, min, s)); + data->convertible = (void*)moment; + } +}; + +typedef register_python_conversion<datetime, datetime_to_python, datetime_from_python> + datetime_python_conversion; + +void export_times() +{ + date_python_conversion(); + datetime_python_conversion(); +} + +} // namespace ledger |