Skip to content

Commit 5db7312

Browse files
committed
feat(datetime): make the resulting Python datetime object timezone-aware (UTC)
See https://docs.python.org/3/library/datetime.html#aware-and-naive-objects
1 parent 9e71b40 commit 5db7312

File tree

2 files changed

+4
-2
lines changed

2 files changed

+4
-2
lines changed

src/DateType.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ DateType::DateType(JSContext *cx, JS::HandleObject dateObj) {
2020
JS_CallFunctionName(cx, dateObj, "getTime", args, &timeValue);
2121
double milliseconds = timeValue.toNumber();
2222

23-
PyObject *timestampArg = PyTuple_New(1);
23+
PyObject *timestampArg = PyTuple_New(2);
2424
PyTuple_SetItem(timestampArg, 0, PyFloat_FromDouble(milliseconds / 1000));
25+
PyTuple_SetItem(timestampArg, 1, PyDateTime_TimeZone_UTC); // Make the resulting Python datetime object timezone-aware
26+
// See https://docs.python.org/3/library/datetime.html#aware-and-naive-objects
2527
pyObject = PyDateTime_FromTimestamp(timestampArg);
2628
Py_DECREF(timestampArg);
2729
}

tests/js/js2py/datetime2.simple

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def eq(date1, date2):
5252
def eqUnixTime(date, timet):
5353
import datetime
5454
global eq
55-
return eq(date, datetime.datetime.utcfromtimestamp(timet))
55+
return eq(date, datetime.datetime.fromtimestamp(timet, tz=datetime.timezone.utc))
5656
`);
5757

5858
const eq = python.eval('eq');

0 commit comments

Comments
 (0)