Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Implement a fast path for date types in :func:`datetime.date.today`
Comment thread
StanFromIreland marked this conversation as resolved.
Outdated
43 changes: 29 additions & 14 deletions Modules/_datetimemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -3259,21 +3259,36 @@ date_fromtimestamp(PyObject *cls, PyObject *obj)
static PyObject *
date_today(PyObject *cls, PyObject *Py_UNUSED(dummy))
{
PyObject *time;
PyObject *result;
time = time_time();
if (time == NULL)
return NULL;
/* Use C implementation to boost performance for date type */
if ((PyTypeObject *)cls == &PyDateTime_DateType) {
Comment thread
StanFromIreland marked this conversation as resolved.
Outdated
struct tm tm;
Comment thread
StanFromIreland marked this conversation as resolved.
time_t t;
time(&t);

/* Note well: today() is a class method, so this may not call
* date.fromtimestamp. For example, it may call
* datetime.fromtimestamp. That's why we need all the accuracy
* time.time() delivers; if someone were gonzo about optimization,
* date.today() could get away with plain C time().
*/
result = PyObject_CallMethodOneArg(cls, &_Py_ID(fromtimestamp), time);
Py_DECREF(time);
return result;
if (_PyTime_localtime(t, &tm) != 0)
return NULL;
Comment thread
StanFromIreland marked this conversation as resolved.

return new_date_ex(tm.tm_year + 1900,
tm.tm_mon + 1,
tm.tm_mday,
(PyTypeObject*)cls);
Comment thread
StanFromIreland marked this conversation as resolved.
Outdated
} else {
Comment thread
StanFromIreland marked this conversation as resolved.
Outdated
PyObject *time;
PyObject *result;
time = time_time();
if (time == NULL)
return NULL;
Comment thread
StanFromIreland marked this conversation as resolved.
Outdated

/* Note well: today() is a class method, so this may not call
* date.fromtimestamp. For example, it may call
* datetime.fromtimestamp. That's why we need all the accuracy
* time.time() delivers; if someone were gonzo about optimization,
* date.today() could get away with plain C time().
Comment thread
StanFromIreland marked this conversation as resolved.
Outdated
*/
result = PyObject_CallMethodOneArg(cls, &_Py_ID(fromtimestamp), time);
Py_DECREF(time);
return result;
}
}

/*[clinic input]
Expand Down