diff options
author | Gregory P. Smith <greg@krypto.org> | 2024-04-03 12:19:49 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-04-03 12:19:49 (GMT) |
commit | 33ee5cb3e92ea8798e7f1a2f3a13b92b39cee6d6 (patch) | |
tree | e9ae52885bb8f2c56a07b6b20b4ebb177ccb4ed7 /Lib/_strptime.py | |
parent | 595bb496b0504429cf01a76fd1ada718d9dd25ca (diff) | |
download | cpython-33ee5cb3e92ea8798e7f1a2f3a13b92b39cee6d6.zip cpython-33ee5cb3e92ea8798e7f1a2f3a13b92b39cee6d6.tar.gz cpython-33ee5cb3e92ea8798e7f1a2f3a13b92b39cee6d6.tar.bz2 |
GH-70647: Deprecate strptime day of month parsing without a year present to avoid leap-year bugs (GH-117107)
Diffstat (limited to 'Lib/_strptime.py')
-rw-r--r-- | Lib/_strptime.py | 21 |
1 files changed, 20 insertions, 1 deletions
diff --git a/Lib/_strptime.py b/Lib/_strptime.py index 798cf9f..e42af75 100644 --- a/Lib/_strptime.py +++ b/Lib/_strptime.py @@ -10,6 +10,7 @@ FUNCTIONS: strptime -- Calculates the time struct represented by the passed-in string """ +import os import time import locale import calendar @@ -250,12 +251,30 @@ class TimeRE(dict): format = regex_chars.sub(r"\\\1", format) whitespace_replacement = re_compile(r'\s+') format = whitespace_replacement.sub(r'\\s+', format) + year_in_format = False + day_of_month_in_format = False while '%' in format: directive_index = format.index('%')+1 + format_char = format[directive_index] processed_format = "%s%s%s" % (processed_format, format[:directive_index-1], - self[format[directive_index]]) + self[format_char]) format = format[directive_index+1:] + match format_char: + case 'Y' | 'y' | 'G': + year_in_format = True + case 'd': + day_of_month_in_format = True + if day_of_month_in_format and not year_in_format: + import warnings + warnings.warn("""\ +Parsing dates involving a day of month without a year specified is ambiguious +and fails to parse leap day. The default behavior will change in Python 3.15 +to either always raise an exception or to use a different default year (TBD). +To avoid trouble, add a specific year to the input & format. +See https://github.com/python/cpython/issues/70647.""", + DeprecationWarning, + skip_file_prefixes=(os.path.dirname(__file__),)) return "%s%s" % (processed_format, format) def compile(self, format): |