Skip to content

Commit 9e71b40

Browse files
committed
perf(datetime): convert Dates by timestamp value
1 parent a021253 commit 9e71b40

File tree

1 file changed

+11
-20
lines changed

1 file changed

+11
-20
lines changed

src/DateType.cc

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,18 @@
1212
DateType::DateType(PyObject *object) : PyType(object) {}
1313

1414
DateType::DateType(JSContext *cx, JS::HandleObject dateObj) {
15+
if (!PyDateTimeAPI) { PyDateTime_IMPORT; } // for PyDateTime_FromTimestamp
16+
17+
// Convert by the timestamp value
1518
JS::Rooted<JS::ValueArray<0>> args(cx);
16-
JS::Rooted<JS::Value> year(cx);
17-
JS::Rooted<JS::Value> month(cx);
18-
JS::Rooted<JS::Value> day(cx);
19-
JS::Rooted<JS::Value> hour(cx);
20-
JS::Rooted<JS::Value> minute(cx);
21-
JS::Rooted<JS::Value> second(cx);
22-
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-
if (!PyDateTimeAPI) { PyDateTime_IMPORT; }
32-
pyObject = PyDateTime_FromDateAndTime(
33-
year.toNumber(), month.toNumber() + 1, day.toNumber(),
34-
hour.toNumber(), minute.toNumber(), second.toNumber(),
35-
usecond.toNumber() * 1000);
19+
JS::Rooted<JS::Value> timeValue(cx);
20+
JS_CallFunctionName(cx, dateObj, "getTime", args, &timeValue);
21+
double milliseconds = timeValue.toNumber();
22+
23+
PyObject *timestampArg = PyTuple_New(1);
24+
PyTuple_SetItem(timestampArg, 0, PyFloat_FromDouble(milliseconds / 1000));
25+
pyObject = PyDateTime_FromTimestamp(timestampArg);
26+
Py_DECREF(timestampArg);
3627
}
3728

3829
JSObject *DateType::toJsDate(JSContext *cx) {

0 commit comments

Comments
 (0)