1212DateType::DateType (PyObject *object) : PyType(object) {}
1313
1414DateType::DateType (JSContext *cx, JS::HandleObject dateObj) {
15+ if (!PyDateTimeAPI) { PyDateTime_IMPORT; } // for PyDateTime_FromTimestamp
16+
1517 JS::Rooted<JS::ValueArray<0 >> args (cx);
1618 JS::Rooted<JS::Value> year (cx);
1719 JS::Rooted<JS::Value> month (cx);
@@ -20,19 +22,23 @@ DateType::DateType(JSContext *cx, JS::HandleObject dateObj) {
2022 JS::Rooted<JS::Value> minute (cx);
2123 JS::Rooted<JS::Value> second (cx);
2224 JS::Rooted<JS::Value> usecond (cx);
23- JS_CallFunctionName (cx, dateObj, " getFullYear" , args, &year);
24- JS_CallFunctionName (cx, dateObj, " getMonth" , args, &month);
25- JS_CallFunctionName (cx, dateObj, " getDate" , args, &day);
26- JS_CallFunctionName (cx, dateObj, " getHours" , args, &hour);
27- JS_CallFunctionName (cx, dateObj, " getMinutes" , args, &minute);
28- JS_CallFunctionName (cx, dateObj, " getSeconds" , args, &second);
29- JS_CallFunctionName (cx, dateObj, " getMilliseconds" , args, &usecond);
30-
31- PyDateTime_IMPORT;
32- pyObject = PyDateTime_FromDateAndTime (
25+ JS_CallFunctionName (cx, dateObj, " getUTCFullYear" , args, &year);
26+ JS_CallFunctionName (cx, dateObj, " getUTCMonth" , args, &month);
27+ JS_CallFunctionName (cx, dateObj, " getUTCDate" , args, &day);
28+ JS_CallFunctionName (cx, dateObj, " getUTCHours" , args, &hour);
29+ JS_CallFunctionName (cx, dateObj, " getUTCMinutes" , args, &minute);
30+ JS_CallFunctionName (cx, dateObj, " getUTCSeconds" , args, &second);
31+ JS_CallFunctionName (cx, dateObj, " getUTCMilliseconds" , args, &usecond);
32+
33+ pyObject = PyDateTimeAPI->DateTime_FromDateAndTime (
3334 year.toNumber (), month.toNumber () + 1 , day.toNumber (),
3435 hour.toNumber (), minute.toNumber (), second.toNumber (),
35- usecond.toNumber () * 1000 );
36+ usecond.toNumber () * 1000 ,
37+ PyDateTime_TimeZone_UTC, // Make the resulting Python datetime object timezone-aware
38+ // See https://docs.python.org/3/library/datetime.html#aware-and-naive-objects
39+ PyDateTimeAPI->DateTimeType
40+ );
41+ Py_INCREF (PyDateTime_TimeZone_UTC);
3642}
3743
3844JSObject *DateType::toJsDate (JSContext *cx) {
0 commit comments