From eb63f10d864112ac32954c49aeb7a2f960876739 Mon Sep 17 00:00:00 2001 From: Stan Ulbrych Date: Mon, 14 Apr 2025 18:26:51 +0100 Subject: [PATCH 1/2] Raise a warning when passing conflicting directives Co-authored-by: Mariatta --- Doc/library/datetime.rst | 9 +++++++++ Lib/_strptime.py | 7 +++++-- .../2025-04-14-18-18-00.gh-issue-124549.hcsdgf.rst | 2 ++ 3 files changed, 16 insertions(+), 2 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2025-04-14-18-18-00.gh-issue-124549.hcsdgf.rst diff --git a/Doc/library/datetime.rst b/Doc/library/datetime.rst index 1ce2013f05da2e..37396f6d20cbc8 100644 --- a/Doc/library/datetime.rst +++ b/Doc/library/datetime.rst @@ -2765,6 +2765,15 @@ Notes: :exc:`DeprecationWarning`. In 3.15 or later we may change this into an error or change the default year to a leap year. See :gh:`70647`. +(11) + When parsing a string using :meth:`~.datetime.strptime`, if more than one + directives of the same time unit were used, a SyntaxWarning is raised as + only the last directive gets parsed and applied to the datetime object. For + example, if the format string contains both ``%Y`` and ``%y``, like ``%Y%y``, + only the last directive on the string (``%y``) gets used on the resulting + datetime object. + + .. rubric:: Footnotes .. [#] If, that is, we ignore the effects of Relativity diff --git a/Lib/_strptime.py b/Lib/_strptime.py index aa63933a49d16d..59d3f7b3cf1b84 100644 --- a/Lib/_strptime.py +++ b/Lib/_strptime.py @@ -357,12 +357,15 @@ def pattern(self, format): day_of_month_in_format = False def repl(m): format_char = m[1] + nonlocal year_in_format, day_of_month_in_format match format_char: case 'Y' | 'y' | 'G': - nonlocal year_in_format + if year_in_format: + import warnings + warnings.warn("When using conflicting directives only the last one will be used.", + SyntaxWarning, skip_file_prefixes=(os.path.dirname(__file__),)) year_in_format = True case 'd': - nonlocal day_of_month_in_format day_of_month_in_format = True return self[format_char] format = re_sub(r'%([OE]?\\?.?)', repl, format) diff --git a/Misc/NEWS.d/next/Library/2025-04-14-18-18-00.gh-issue-124549.hcsdgf.rst b/Misc/NEWS.d/next/Library/2025-04-14-18-18-00.gh-issue-124549.hcsdgf.rst new file mode 100644 index 00000000000000..24197ab185c504 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-04-14-18-18-00.gh-issue-124549.hcsdgf.rst @@ -0,0 +1,2 @@ +Raise a warning in :func:`datetime.datetime.strptime` and :func:`datetime.date.strptime` +when conflicting directives are provided. From e670d33772fa88ab7367fb7d093fcad6ff2e5054 Mon Sep 17 00:00:00 2001 From: Stan Ulbrych <89152624+StanFromIreland@users.noreply.github.com> Date: Mon, 19 May 2025 17:06:40 +0100 Subject: [PATCH 2/2] Reword --- Doc/library/datetime.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Doc/library/datetime.rst b/Doc/library/datetime.rst index 37396f6d20cbc8..b6c6e1f69be528 100644 --- a/Doc/library/datetime.rst +++ b/Doc/library/datetime.rst @@ -2767,10 +2767,10 @@ Notes: (11) When parsing a string using :meth:`~.datetime.strptime`, if more than one - directives of the same time unit were used, a SyntaxWarning is raised as + directive of the same time unit is used, a SyntaxWarning is raised as only the last directive gets parsed and applied to the datetime object. For example, if the format string contains both ``%Y`` and ``%y``, like ``%Y%y``, - only the last directive on the string (``%y``) gets used on the resulting + only the last directive of the string (``%y``) gets used on the resulting datetime object.