File tree Expand file tree Collapse file tree 1 file changed +14
-0
lines changed Expand file tree Collapse file tree 1 file changed +14
-0
lines changed Original file line number Diff line number Diff line change 9
9
#include < Python.h>
10
10
#include < datetime.h>
11
11
12
+ // https://github.com/python/cpython/blob/v3.10.11/Modules/_datetimemodule.c#L89-L92
13
+ #define DATE_SET_MICROSECOND (o, v ) \
14
+ (((o)->data[7 ] = ((v) & 0xff0000 ) >> 16 ), \
15
+ ((o)->data[8 ] = ((v) & 0x00ff00 ) >> 8 ), \
16
+ ((o)->data[9 ] = ((v) & 0x0000ff )))
17
+
12
18
DateType::DateType (PyObject *object) : PyType(object) {}
13
19
14
20
DateType::DateType (JSContext *cx, JS::HandleObject dateObj) {
@@ -25,7 +31,15 @@ DateType::DateType(JSContext *cx, JS::HandleObject dateObj) {
25
31
PyTuple_SetItem (timestampArg, 1 , PyDateTime_TimeZone_UTC); // Make the resulting Python datetime object timezone-aware
26
32
// See https://docs.python.org/3/library/datetime.html#aware-and-naive-objects
27
33
pyObject = PyDateTime_FromTimestamp (timestampArg);
34
+ Py_INCREF (PyDateTime_TimeZone_UTC); // PyTuple_SetItem steals the reference
28
35
Py_DECREF (timestampArg);
36
+
37
+ // Round to milliseconds precision because the smallest unit for a JS Date is 1ms
38
+ double microseconds = PyDateTime_DATE_GET_MICROSECOND (pyObject);
39
+ DATE_SET_MICROSECOND (
40
+ (PyDateTime_DateTime *)pyObject,
41
+ std::lround (microseconds / 1000 ) * 1000
42
+ );
29
43
}
30
44
31
45
JSObject *DateType::toJsDate (JSContext *cx) {
You can’t perform that action at this time.
0 commit comments