|
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 | + |
| 17 | + // Convert by the timestamp value |
15 | 18 | 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); |
36 | 27 | }
|
37 | 28 |
|
38 | 29 | JSObject *DateType::toJsDate(JSContext *cx) {
|
|
0 commit comments