12
12
DateType::DateType (PyObject *object) : PyType(object) {}
13
13
14
14
DateType::DateType (JSContext *cx, JS::HandleObject dateObj) {
15
+ if (!PyDateTimeAPI) { PyDateTime_IMPORT; } // for PyDateTime_FromTimestamp
16
+
15
17
JS::Rooted<JS::ValueArray<0 >> args (cx);
16
18
JS::Rooted<JS::Value> year (cx);
17
19
JS::Rooted<JS::Value> month (cx);
@@ -20,19 +22,23 @@ DateType::DateType(JSContext *cx, JS::HandleObject dateObj) {
20
22
JS::Rooted<JS::Value> minute (cx);
21
23
JS::Rooted<JS::Value> second (cx);
22
24
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 (
33
34
year.toNumber (), month.toNumber () + 1 , day.toNumber (),
34
35
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);
36
42
}
37
43
38
44
JSObject *DateType::toJsDate (JSContext *cx) {
0 commit comments