From 5b2924d8d945c111c592a24bd0586417513c4d58 Mon Sep 17 00:00:00 2001 From: mehdihassan93 Date: Fri, 5 Sep 2025 20:41:31 +0100 Subject: [PATCH 1/6] Add documentation links to datetime strftime/strptime docstrings - Add links to format codes documentation for all strftime methods - Add links to format codes documentation for all strptime methods - Addresses issue #97517 --- Lib/_pydatetime.py | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/Lib/_pydatetime.py b/Lib/_pydatetime.py index ab1b1722b7c254..5021c0ce9d9031 100644 --- a/Lib/_pydatetime.py +++ b/Lib/_pydatetime.py @@ -1072,7 +1072,10 @@ def fromisocalendar(cls, year, week, day): @classmethod def strptime(cls, date_string, format): - """Parse string according to the given date format (like time.strptime()).""" + """Parse string according to the given date format (like time.strptime()). + For a list of supported format codes, see the documentation: + https://docs.python.org/3/library/datetime.html#format-codes + """ import _strptime return _strptime._strptime_datetime_date(cls, date_string, format) @@ -1109,6 +1112,8 @@ def strftime(self, format): Format using strftime(). Example: "%d/%m/%Y, %H:%M:%S" + For a list of supported format codes, see the documentation: + https://docs.python.org/3/library/datetime.html#format-codes """ return _wrap_strftime(self, format, self.timetuple()) @@ -1456,8 +1461,12 @@ def __new__(cls, hour=0, minute=0, second=0, microsecond=0, tzinfo=None, *, fold return self @classmethod + def strptime(cls, date_string, format): - """Parse string according to the given time format (like time.strptime()).""" + """Parse string according to the given time format (like time.strptime()). + For a list of supported format codes, see the documentation: + https://docs.python.org/3/library/datetime.html#format-codes + """ import _strptime return _strptime._strptime_datetime_time(cls, date_string, format) @@ -1650,6 +1659,8 @@ def fromisoformat(cls, time_string): def strftime(self, format): """Format using strftime(). The date part of the timestamp passed to underlying strftime should not be used. + For a list of supported format codes, see the documentation: + https://docs.python.org/3/library/datetime.html#format-codes """ # The year must be >= 1000 else Python's strftime implementation # can raise a bogus exception. @@ -2198,7 +2209,10 @@ def __str__(self): @classmethod def strptime(cls, date_string, format): - """Parse string according to the given date and time format (like time.strptime()).""" + """Parse string according to the given time format (like time.strptime()). + For a list of supported format codes, see the documentation: + https://docs.python.org/3/library/datetime.html#format-codes + """ import _strptime return _strptime._strptime_datetime_datetime(cls, date_string, format) From ea78e0fe718fe7f41a380cb5fd82531630488b89 Mon Sep 17 00:00:00 2001 From: mehdihassan93 Date: Fri, 5 Sep 2025 21:13:46 +0100 Subject: [PATCH 2/6] Update C extension docstrings with format codes documentation --- Modules/_datetimemodule.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/Modules/_datetimemodule.c b/Modules/_datetimemodule.c index eb52eb72614fa5..8f8e912b06e324 100644 --- a/Modules/_datetimemodule.c +++ b/Modules/_datetimemodule.c @@ -3468,6 +3468,9 @@ datetime.date.strptime / Parse string according to the given date format (like time.strptime()). + +For a list of supported format codes, see the documentation: + https://docs.python.org/3/library/datetime.html#format-codes [clinic start generated code]*/ static PyObject * @@ -3608,6 +3611,9 @@ datetime.date.strftime Format using strftime(). Example: "%d/%m/%Y, %H:%M:%S". + +For a list of supported format codes, see the documentation: + https://docs.python.org/3/library/datetime.html#format-codes [clinic start generated code]*/ static PyObject * @@ -4711,6 +4717,9 @@ datetime.time.strptime / Parse string according to the given time format (like time.strptime()). + +For a list of supported format codes, see the documentation: + https://docs.python.org/3/library/datetime.html#format-codes [clinic start generated code]*/ static PyObject * @@ -4891,6 +4900,8 @@ datetime.time.strftime Format using strftime(). The date part of the timestamp passed to underlying strftime should not be used. +For a list of supported format codes, see the documentation: + https://docs.python.org/3/library/datetime.html#format-codes [clinic start generated code]*/ static PyObject * @@ -5787,6 +5798,8 @@ datetime.datetime.strptime / Parse string according to the given date and time format (like time.strptime()). +For a list of supported format codes, see the documentation: + https://docs.python.org/3/library/datetime.html#format-codes [clinic start generated code]*/ static PyObject * From b04b5c316bd4b230f85310f74808671b9c2ecb1d Mon Sep 17 00:00:00 2001 From: mehdihassan93 Date: Fri, 5 Sep 2025 21:31:38 +0100 Subject: [PATCH 3/6] Regenerate clinic code for updated docstrings --- Modules/_datetimemodule.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/Modules/_datetimemodule.c b/Modules/_datetimemodule.c index 8f8e912b06e324..68c4a869e89233 100644 --- a/Modules/_datetimemodule.c +++ b/Modules/_datetimemodule.c @@ -3476,7 +3476,7 @@ For a list of supported format codes, see the documentation: static PyObject * datetime_date_strptime_impl(PyTypeObject *type, PyObject *string, PyObject *format) -/*[clinic end generated code: output=454d473bee2d5161 input=001904ab34f594a1]*/ +/*[clinic end generated code: output=454d473bee2d5161 input=31d57bb789433e99]*/ { PyObject *result; @@ -3618,7 +3618,7 @@ For a list of supported format codes, see the documentation: static PyObject * datetime_date_strftime_impl(PyObject *self, PyObject *format) -/*[clinic end generated code: output=6529b70095e16778 input=72af55077e606ed8]*/ +/*[clinic end generated code: output=6529b70095e16778 input=b6fd4a2ded27b557]*/ { /* This method can be inherited, and needs to call the * timetuple() method appropriate to self's class. @@ -4725,7 +4725,7 @@ For a list of supported format codes, see the documentation: static PyObject * datetime_time_strptime_impl(PyTypeObject *type, PyObject *string, PyObject *format) -/*[clinic end generated code: output=ae05a9bc0241d3bf input=6d0f263a5f94d78d]*/ +/*[clinic end generated code: output=ae05a9bc0241d3bf input=82ba425ecacc54aa]*/ { PyObject *result; @@ -4906,7 +4906,7 @@ For a list of supported format codes, see the documentation: static PyObject * datetime_time_strftime_impl(PyDateTime_Time *self, PyObject *format) -/*[clinic end generated code: output=10f65af20e2a78c7 input=541934a2860f7db5]*/ +/*[clinic end generated code: output=10f65af20e2a78c7 input=9d15aeed1527935a]*/ { PyObject *result; PyObject *tuple; @@ -5798,6 +5798,7 @@ datetime.datetime.strptime / Parse string according to the given date and time format (like time.strptime()). + For a list of supported format codes, see the documentation: https://docs.python.org/3/library/datetime.html#format-codes [clinic start generated code]*/ @@ -5805,7 +5806,7 @@ For a list of supported format codes, see the documentation: static PyObject * datetime_datetime_strptime_impl(PyTypeObject *type, PyObject *string, PyObject *format) -/*[clinic end generated code: output=af2c2d024f3203f5 input=d7597c7f5327117b]*/ +/*[clinic end generated code: output=af2c2d024f3203f5 input=ef7807589f1d50e7]*/ { PyObject *result; From 87b4d509c871994902539b5d482796b651743e74 Mon Sep 17 00:00:00 2001 From: mehdihassan93 Date: Fri, 5 Sep 2025 21:42:28 +0100 Subject: [PATCH 4/6] Add clinic-generated header file for updated docstrings --- Modules/clinic/_datetimemodule.c.h | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/Modules/clinic/_datetimemodule.c.h b/Modules/clinic/_datetimemodule.c.h index 7c4bd5503ed56b..15a650ceae6948 100644 --- a/Modules/clinic/_datetimemodule.c.h +++ b/Modules/clinic/_datetimemodule.c.h @@ -371,7 +371,10 @@ PyDoc_STRVAR(datetime_date_strptime__doc__, "strptime($type, string, format, /)\n" "--\n" "\n" -"Parse string according to the given date format (like time.strptime())."); +"Parse string according to the given date format (like time.strptime()).\n" +"\n" +"For a list of supported format codes, see the documentation:\n" +" https://docs.python.org/3/library/datetime.html#format-codes"); #define DATETIME_DATE_STRPTIME_METHODDEF \ {"strptime", _PyCFunction_CAST(datetime_date_strptime), METH_FASTCALL|METH_CLASS, datetime_date_strptime__doc__}, @@ -412,7 +415,10 @@ PyDoc_STRVAR(datetime_date_strftime__doc__, "\n" "Format using strftime().\n" "\n" -"Example: \"%d/%m/%Y, %H:%M:%S\"."); +"Example: \"%d/%m/%Y, %H:%M:%S\".\n" +"\n" +"For a list of supported format codes, see the documentation:\n" +" https://docs.python.org/3/library/datetime.html#format-codes"); #define DATETIME_DATE_STRFTIME_METHODDEF \ {"strftime", _PyCFunction_CAST(datetime_date_strftime), METH_FASTCALL|METH_KEYWORDS, datetime_date_strftime__doc__}, @@ -847,7 +853,10 @@ PyDoc_STRVAR(datetime_time_strptime__doc__, "strptime($type, string, format, /)\n" "--\n" "\n" -"Parse string according to the given time format (like time.strptime())."); +"Parse string according to the given time format (like time.strptime()).\n" +"\n" +"For a list of supported format codes, see the documentation:\n" +" https://docs.python.org/3/library/datetime.html#format-codes"); #define DATETIME_TIME_STRPTIME_METHODDEF \ {"strptime", _PyCFunction_CAST(datetime_time_strptime), METH_FASTCALL|METH_CLASS, datetime_time_strptime__doc__}, @@ -970,7 +979,9 @@ PyDoc_STRVAR(datetime_time_strftime__doc__, "\n" "Format using strftime().\n" "\n" -"The date part of the timestamp passed to underlying strftime should not be used."); +"The date part of the timestamp passed to underlying strftime should not be used.\n" +"For a list of supported format codes, see the documentation:\n" +" https://docs.python.org/3/library/datetime.html#format-codes"); #define DATETIME_TIME_STRFTIME_METHODDEF \ {"strftime", _PyCFunction_CAST(datetime_time_strftime), METH_FASTCALL|METH_KEYWORDS, datetime_time_strftime__doc__}, @@ -1569,7 +1580,10 @@ PyDoc_STRVAR(datetime_datetime_strptime__doc__, "strptime($type, string, format, /)\n" "--\n" "\n" -"Parse string according to the given date and time format (like time.strptime())."); +"Parse string according to the given date and time format (like time.strptime()).\n" +"\n" +"For a list of supported format codes, see the documentation:\n" +" https://docs.python.org/3/library/datetime.html#format-codes"); #define DATETIME_DATETIME_STRPTIME_METHODDEF \ {"strptime", _PyCFunction_CAST(datetime_datetime_strptime), METH_FASTCALL|METH_CLASS, datetime_datetime_strptime__doc__}, @@ -2075,4 +2089,4 @@ datetime_datetime___reduce__(PyObject *self, PyObject *Py_UNUSED(ignored)) { return datetime_datetime___reduce___impl((PyDateTime_DateTime *)self); } -/*[clinic end generated code: output=0b8403bc58982e60 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=873da4353440f0d5 input=a9049054013a1b77]*/ From 28b193eacbf3b5c0149f56787218ae088cb0c912 Mon Sep 17 00:00:00 2001 From: mehdihassan93 Date: Fri, 5 Sep 2025 21:58:18 +0100 Subject: [PATCH 5/6] Fix docstring spacing consistency in both Python and C files --- Lib/_pydatetime.py | 4 ++++ Modules/_datetimemodule.c | 3 ++- Modules/clinic/_datetimemodule.c.h | 3 ++- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/Lib/_pydatetime.py b/Lib/_pydatetime.py index 5021c0ce9d9031..79be8d19c1e0ec 100644 --- a/Lib/_pydatetime.py +++ b/Lib/_pydatetime.py @@ -1073,6 +1073,7 @@ def fromisocalendar(cls, year, week, day): @classmethod def strptime(cls, date_string, format): """Parse string according to the given date format (like time.strptime()). + For a list of supported format codes, see the documentation: https://docs.python.org/3/library/datetime.html#format-codes """ @@ -1464,6 +1465,7 @@ def __new__(cls, hour=0, minute=0, second=0, microsecond=0, tzinfo=None, *, fold def strptime(cls, date_string, format): """Parse string according to the given time format (like time.strptime()). + For a list of supported format codes, see the documentation: https://docs.python.org/3/library/datetime.html#format-codes """ @@ -1659,6 +1661,7 @@ def fromisoformat(cls, time_string): def strftime(self, format): """Format using strftime(). The date part of the timestamp passed to underlying strftime should not be used. + For a list of supported format codes, see the documentation: https://docs.python.org/3/library/datetime.html#format-codes """ @@ -2210,6 +2213,7 @@ def __str__(self): @classmethod def strptime(cls, date_string, format): """Parse string according to the given time format (like time.strptime()). + For a list of supported format codes, see the documentation: https://docs.python.org/3/library/datetime.html#format-codes """ diff --git a/Modules/_datetimemodule.c b/Modules/_datetimemodule.c index a75effa07e38c9..329be68d5b8285 100644 --- a/Modules/_datetimemodule.c +++ b/Modules/_datetimemodule.c @@ -4900,13 +4900,14 @@ datetime.time.strftime Format using strftime(). The date part of the timestamp passed to underlying strftime should not be used. + For a list of supported format codes, see the documentation: https://docs.python.org/3/library/datetime.html#format-codes [clinic start generated code]*/ static PyObject * datetime_time_strftime_impl(PyDateTime_Time *self, PyObject *format) -/*[clinic end generated code: output=10f65af20e2a78c7 input=9d15aeed1527935a]*/ +/*[clinic end generated code: output=10f65af20e2a78c7 input=c4a5bbecd798654b]*/ { PyObject *result; PyObject *tuple; diff --git a/Modules/clinic/_datetimemodule.c.h b/Modules/clinic/_datetimemodule.c.h index 15a650ceae6948..ee621c150c31e4 100644 --- a/Modules/clinic/_datetimemodule.c.h +++ b/Modules/clinic/_datetimemodule.c.h @@ -980,6 +980,7 @@ PyDoc_STRVAR(datetime_time_strftime__doc__, "Format using strftime().\n" "\n" "The date part of the timestamp passed to underlying strftime should not be used.\n" +"\n" "For a list of supported format codes, see the documentation:\n" " https://docs.python.org/3/library/datetime.html#format-codes"); @@ -2089,4 +2090,4 @@ datetime_datetime___reduce__(PyObject *self, PyObject *Py_UNUSED(ignored)) { return datetime_datetime___reduce___impl((PyDateTime_DateTime *)self); } -/*[clinic end generated code: output=873da4353440f0d5 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=69658acff6a43ac4 input=a9049054013a1b77]*/ From 7094699ff9469241bf3708450302449491eab725 Mon Sep 17 00:00:00 2001 From: Mehdi Hassan <44486547+mehdihassan93@users.noreply.github.com> Date: Fri, 5 Sep 2025 22:14:50 +0100 Subject: [PATCH 6/6] Update Lib/_pydatetime.py Co-authored-by: Stan Ulbrych <89152624+StanFromIreland@users.noreply.github.com> --- Lib/_pydatetime.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/_pydatetime.py b/Lib/_pydatetime.py index 79be8d19c1e0ec..b6d68f2372850a 100644 --- a/Lib/_pydatetime.py +++ b/Lib/_pydatetime.py @@ -1073,7 +1073,7 @@ def fromisocalendar(cls, year, week, day): @classmethod def strptime(cls, date_string, format): """Parse string according to the given date format (like time.strptime()). - + For a list of supported format codes, see the documentation: https://docs.python.org/3/library/datetime.html#format-codes """