gh-124529: Fix _strptime
to make %c
/%x
accept a year with fewer digits
#124778
+291
−8
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Slightly relax (by modifying
_strptime.TimeRE
) howdatetime.datetime.strptime()
,datetime.date.strptime()
andtime.strptime()
parse format strings that include%c
or%x
-- so that year number representations consisting of fewer digits than the usual four or two are now accepted (note that nothing changes for%Y
and%y
). Thanks to that, the desired behavior described in gh-124529 is now consistently observed on all concerned platforms and locales, i.e., now certainstrftime/strptime
round trips (such asdatetime.strptime(dt.strftime("%c"), "%c"))
) never raiseValueError
(that previously might happen, e.g., on Linux -- for various locales, includingC
/C.UTF-8
).Directly related tests have been added, plus a few other (indirectly-related) tests have also been added, for consistency and better coverage.