Skip to content

Commit 4f526fe

Browse files
committed
Merge pull request #9044 from cpcloud/timedelta-json
Fix timedelta json on windows
2 parents 136eb23 + 067066f commit 4f526fe

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-11
lines changed

pandas/src/datetime_helper.h

+11-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
#include "datetime.h"
2+
#include "numpy/arrayobject.h"
3+
#include "numpy/arrayscalars.h"
4+
#include <stdio.h>
25

36
#if PY_MAJOR_VERSION >= 3
47
#define PyInt_AS_LONG PyLong_AsLong
@@ -9,15 +12,16 @@ void mangle_nat(PyObject *val) {
912
PyDateTime_GET_DAY(val) = -1;
1013
}
1114

12-
long get_long_attr(PyObject *o, const char *attr) {
13-
return PyInt_AS_LONG(PyObject_GetAttrString(o, attr));
15+
npy_int64 get_long_attr(PyObject *o, const char *attr) {
16+
PyObject *value = PyObject_GetAttrString(o, attr);
17+
return PyLong_Check(value) ? PyLong_AsLongLong(value) : PyInt_AS_LONG(value);
1418
}
1519

16-
double total_seconds(PyObject *td) {
20+
npy_float64 total_seconds(PyObject *td) {
1721
// Python 2.6 compat
18-
long microseconds = get_long_attr(td, "microseconds");
19-
long seconds = get_long_attr(td, "seconds");
20-
long days = get_long_attr(td, "days");
21-
long days_in_seconds = days * 24 * 3600;
22+
npy_int64 microseconds = get_long_attr(td, "microseconds");
23+
npy_int64 seconds = get_long_attr(td, "seconds");
24+
npy_int64 days = get_long_attr(td, "days");
25+
npy_int64 days_in_seconds = days * 24LL * 3600LL;
2226
return (microseconds + (seconds + days_in_seconds) * 1000000.0) / 1000000.0;
2327
}

pandas/src/ujson/python/objToJSON.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -1452,12 +1452,12 @@ void Object_beginTypeContext (JSOBJ _obj, JSONTypeContext *tc)
14521452
else
14531453
if (PyDelta_Check(obj))
14541454
{
1455-
long value;
1455+
npy_int64 value;
14561456

1457-
if (PyObject_HasAttrString(obj, "value"))
1457+
if (PyObject_HasAttrString(obj, "value")) {
14581458
value = get_long_attr(obj, "value");
1459-
else
1460-
value = total_seconds(obj) * 1000000000; // nanoseconds per second
1459+
} else
1460+
value = total_seconds(obj) * 1000000000LL; // nanoseconds per second
14611461

14621462
exc = PyErr_Occurred();
14631463

0 commit comments

Comments
 (0)