From c5d3780f0cb49c22a7b59a4fe376ab0283c0288d Mon Sep 17 00:00:00 2001 From: Stan Ulbrych Date: Mon, 10 Mar 2025 18:36:48 +0000 Subject: [PATCH 1/4] Add note --- Doc/library/datetime.rst | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Doc/library/datetime.rst b/Doc/library/datetime.rst index 1af7d6be750102..091cea38e82e10 100644 --- a/Doc/library/datetime.rst +++ b/Doc/library/datetime.rst @@ -2599,6 +2599,14 @@ method. The ISO 8601 year and ISO 8601 week directives are not interchangeable with the year and week number directives above. Calling :meth:`~.datetime.strptime` with incomplete or ambiguous ISO 8601 directives will raise a :exc:`ValueError`. +.. Note:: + Provided input may be split unexpectedly to cope with the provided directives + in the format string, this aligns with the behaviour of the C implementation.:: + + >>> datetime.strptime('20110817T12','%Y%m%dT%H%M') + datetime.datetime(2011, 8, 17, 1, 2) + + The full set of format codes supported varies across platforms, because Python calls the platform C library's :c:func:`strftime` function, and platform variations are common. To see the full set of format codes supported on your From c149570fb16220fb1e3e9e5604d3693a51239d3e Mon Sep 17 00:00:00 2001 From: Stan Ulbrych Date: Mon, 10 Mar 2025 18:52:08 +0000 Subject: [PATCH 2/4] Make it meaner --- Doc/library/datetime.rst | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Doc/library/datetime.rst b/Doc/library/datetime.rst index 091cea38e82e10..d6e1c7ebe86bed 100644 --- a/Doc/library/datetime.rst +++ b/Doc/library/datetime.rst @@ -2600,11 +2600,11 @@ with the year and week number directives above. Calling :meth:`~.datetime.strpti incomplete or ambiguous ISO 8601 directives will raise a :exc:`ValueError`. .. Note:: - Provided input may be split unexpectedly to cope with the provided directives - in the format string, this aligns with the behaviour of the C implementation.:: + Input will be split to cope with the provided directives in the format string, + this aligns with the behaviour of the C implementation.:: - >>> datetime.strptime('20110817T12','%Y%m%dT%H%M') - datetime.datetime(2011, 8, 17, 1, 2) + >>> datetime.strptime('20250310T12','%Y%m%dT%H%M') + datetime.datetime(2025, 3, 10, 1, 2) The full set of format codes supported varies across platforms, because Python From 998e4e8f3d7b8e37f31cef088410f3d05b32720d Mon Sep 17 00:00:00 2001 From: Stan Ulbrych Date: Tue, 11 Mar 2025 16:31:02 +0000 Subject: [PATCH 3/4] *Borrow* GNU docs wording --- Doc/library/datetime.rst | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/Doc/library/datetime.rst b/Doc/library/datetime.rst index d6e1c7ebe86bed..7650da98e9d1de 100644 --- a/Doc/library/datetime.rst +++ b/Doc/library/datetime.rst @@ -2465,6 +2465,11 @@ These methods accept format codes that can be used to parse and format dates:: >>> _.strftime('%a %d %b %Y, %I:%M%p') 'Mon 31 Jan 2022, 11:59PM' +The user has to make sure, though, that the input can be parsed in a unambiguous +way. The string ``2025112`` can be parsed using the format ``%Y%m%d`` as ``2025-1-12``, +``2025-11-2``, or even ``20251-1-2``. It is necessary to add appropriate separators to +reliably get results. + The following is a list of all the format codes that the 1989 C standard requires, and these work on all platforms with a standard C implementation. @@ -2599,14 +2604,6 @@ method. The ISO 8601 year and ISO 8601 week directives are not interchangeable with the year and week number directives above. Calling :meth:`~.datetime.strptime` with incomplete or ambiguous ISO 8601 directives will raise a :exc:`ValueError`. -.. Note:: - Input will be split to cope with the provided directives in the format string, - this aligns with the behaviour of the C implementation.:: - - >>> datetime.strptime('20250310T12','%Y%m%dT%H%M') - datetime.datetime(2025, 3, 10, 1, 2) - - The full set of format codes supported varies across platforms, because Python calls the platform C library's :c:func:`strftime` function, and platform variations are common. To see the full set of format codes supported on your From 12a77ccab3cdaaaacd392c8c2b4ed368a63271e2 Mon Sep 17 00:00:00 2001 From: Stan Ulbrych Date: Fri, 6 Feb 2026 19:28:02 +0000 Subject: [PATCH 4/4] Move & reword --- Doc/library/datetime.rst | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Doc/library/datetime.rst b/Doc/library/datetime.rst index daadc035e87d12..9005119df551bc 100644 --- a/Doc/library/datetime.rst +++ b/Doc/library/datetime.rst @@ -2495,11 +2495,6 @@ These methods accept format codes that can be used to parse and format dates:: >>> _.strftime('%a %d %b %Y, %I:%M%p') 'Mon 31 Jan 2022, 11:59PM' -The user has to make sure, though, that the input can be parsed in a unambiguous -way. The string ``2025112`` can be parsed using the format ``%Y%m%d`` as ``2025-1-12``, -``2025-11-2``, or even ``20251-1-2``. It is necessary to add appropriate separators to -reliably get results. - The following is a list of all the format codes that the 2011 C standard requires, and these work on all supported platforms. @@ -2694,6 +2689,11 @@ For the :meth:`.datetime.strptime` and :meth:`.date.strptime` class methods, the default value is ``1900-01-01T00:00:00.000``: any components not specified in the format string will be pulled from the default value. +.. note:: + Format strings without separators can be ambiguous for parsing. For + example, ``%Y%m%d`` parses the string ``2026111`` as ``2026-11-01``, + not ``2026-01-11``. Use separators to ensure the input is parsed as intended. + .. note:: When used to parse partial dates lacking a year, :meth:`.datetime.strptime` and :meth:`.date.strptime` will raise when encountering February 29 because