mirror of
https://github.com/ultrajson/ultrajson.git
synced 2024-11-24 05:12:02 +01:00
better datetime & date handling
This commit is contained in:
parent
a1fd2a582a
commit
35ba3e9da0
@ -3,8 +3,7 @@
|
|||||||
#include <datetime.h>
|
#include <datetime.h>
|
||||||
#include <ultrajson.h>
|
#include <ultrajson.h>
|
||||||
|
|
||||||
static PyObject* meth_timegm;
|
#define EPOCH_ORD 719163
|
||||||
static PyObject* mod_calendar;
|
|
||||||
|
|
||||||
typedef void *(*PFN_PyTypeToJSON)(JSOBJ obj, JSONTypeContext *ti, void *outValue, size_t *_outLen);
|
typedef void *(*PFN_PyTypeToJSON)(JSOBJ obj, JSONTypeContext *ti, void *outValue, size_t *_outLen);
|
||||||
|
|
||||||
@ -49,17 +48,7 @@ struct PyDictIterState
|
|||||||
|
|
||||||
void initObjToJSON(void)
|
void initObjToJSON(void)
|
||||||
{
|
{
|
||||||
//FIXME: DECREF on these?
|
|
||||||
PyDateTime_IMPORT;
|
PyDateTime_IMPORT;
|
||||||
|
|
||||||
/*
|
|
||||||
FIXME: Find the direct function pointer here instead and use it when time conversion is performed */
|
|
||||||
|
|
||||||
meth_timegm = PyString_FromString("timegm");
|
|
||||||
mod_calendar = PyImport_ImportModule("calendar");
|
|
||||||
|
|
||||||
Py_INCREF(mod_calendar);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void *PyIntToINT32(JSOBJ _obj, JSONTypeContext *tc, void *outValue, size_t *_outLen)
|
static void *PyIntToINT32(JSOBJ _obj, JSONTypeContext *tc, void *outValue, size_t *_outLen)
|
||||||
@ -110,38 +99,32 @@ static void *PyUnicodeToUTF8(JSOBJ _obj, JSONTypeContext *tc, void *outValue, si
|
|||||||
static void *PyDateTimeToINT64(JSOBJ _obj, JSONTypeContext *tc, void *outValue, size_t *_outLen)
|
static void *PyDateTimeToINT64(JSOBJ _obj, JSONTypeContext *tc, void *outValue, size_t *_outLen)
|
||||||
{
|
{
|
||||||
PyObject *obj = (PyObject *) _obj;
|
PyObject *obj = (PyObject *) _obj;
|
||||||
|
int y, m, d, h, mn, s, days;
|
||||||
|
|
||||||
PyObject* timetuple = PyObject_CallMethod(obj, "utctimetuple", NULL);
|
y = PyDateTime_GET_YEAR(obj);
|
||||||
PyObject* unixTimestamp = PyObject_CallMethodObjArgs(mod_calendar, meth_timegm, timetuple, NULL);
|
m = PyDateTime_GET_MONTH(obj);
|
||||||
|
d = PyDateTime_GET_DAY(obj);
|
||||||
*( (JSINT64 *) outValue) = PyLong_AsLongLong (unixTimestamp);
|
h = PyDateTime_DATE_GET_HOUR(obj);
|
||||||
Py_DECREF(timetuple);
|
mn = PyDateTime_DATE_GET_MINUTE(obj);
|
||||||
Py_DECREF(unixTimestamp);
|
s = PyDateTime_DATE_GET_SECOND(obj);
|
||||||
|
|
||||||
|
days = PyInt_AS_LONG(PyObject_CallMethod(PyDate_FromDate(y, m, 1), "toordinal", NULL)) - EPOCH_ORD + d - 1;
|
||||||
|
*( (JSINT64 *) outValue) = (((JSINT64) ((days * 24 + h) * 60 + mn)) * 60 + s);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void *PyDateToINT64(JSOBJ _obj, JSONTypeContext *tc, void *outValue, size_t *_outLen)
|
static void *PyDateToINT64(JSOBJ _obj, JSONTypeContext *tc, void *outValue, size_t *_outLen)
|
||||||
{
|
{
|
||||||
PyObject *obj = (PyObject *) _obj;
|
PyObject *obj = (PyObject *) _obj;
|
||||||
|
int y, m, d, days;
|
||||||
|
|
||||||
PyObject* timetuple = PyTuple_New(6);
|
y = PyDateTime_GET_YEAR(obj);
|
||||||
PyObject* year = PyObject_GetAttrString(obj, "year");
|
m = PyDateTime_GET_MONTH(obj);
|
||||||
PyObject* month = PyObject_GetAttrString(obj, "month");
|
d = PyDateTime_GET_DAY(obj);
|
||||||
PyObject* day = PyObject_GetAttrString(obj, "day");
|
|
||||||
PyObject* unixTimestamp;
|
|
||||||
|
|
||||||
PyTuple_SET_ITEM(timetuple, 0, year);
|
days = PyInt_AS_LONG(PyObject_CallMethod(PyDate_FromDate(y, m, 1), "toordinal", NULL)) - EPOCH_ORD + d - 1;
|
||||||
PyTuple_SET_ITEM(timetuple, 1, month);
|
*( (JSINT64 *) outValue) = ((JSINT64) days * 86400);
|
||||||
PyTuple_SET_ITEM(timetuple, 2, day);
|
|
||||||
PyTuple_SET_ITEM(timetuple, 3, PyInt_FromLong(0));
|
|
||||||
PyTuple_SET_ITEM(timetuple, 4, PyInt_FromLong(0));
|
|
||||||
PyTuple_SET_ITEM(timetuple, 5, PyInt_FromLong(0));
|
|
||||||
|
|
||||||
unixTimestamp = PyObject_CallMethodObjArgs(mod_calendar, meth_timegm, timetuple, NULL);
|
|
||||||
|
|
||||||
*( (JSINT64 *) outValue) = PyLong_AsLongLong (unixTimestamp);
|
|
||||||
Py_DECREF(timetuple);
|
|
||||||
Py_DECREF(unixTimestamp);
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user