1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
#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>
// jww (2007-05-04): Convert time duration objects to PyDelta
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)) 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;
struct datetime_to_python
{
static PyObject* convert(const moment_t& moment)
{
PyDateTime_IMPORT;
date dte = moment.date();
moment_t::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);
moment_t* moment = new moment_t(date(y,m,d),
moment_t::time_duration_type(h, min, s));
data->convertible = (void*)moment;
}
};
typedef register_python_conversion<moment_t, datetime_to_python, datetime_from_python>
datetime_python_conversion;
void export_times()
{
date_python_conversion();
datetime_python_conversion();
python_optional<moment_t>();
}
} // namespace ledger
|