Skip to content

Commit c079c2a

Browse files
Add deprecation for %e with no year
1 parent 895e837 commit c079c2a

File tree

4 files changed

+19
-8
lines changed

4 files changed

+19
-8
lines changed

Doc/library/datetime.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2531,13 +2531,13 @@ requires, and these work on all supported platforms.
25312531
| | truncated to an integer as a | | |
25322532
| | zero-padded decimal number. | | |
25332533
+-----------+--------------------------------+------------------------+-------+
2534-
| ``%d`` | Day of the month as a | 01, 02, ..., 31 | \(9) |
2535-
| | zero-padded decimal number. | | |
2534+
| ``%d`` | Day of the month as a | 01, 02, ..., 31 | \(9), |
2535+
| | zero-padded decimal number. | | \(10 |
25362536
+-----------+--------------------------------+------------------------+-------+
25372537
| ``%D`` | Equivalent to ``%m/%d/%y``. | 11/10/2025 | \(9), |
25382538
| | | | \(0) |
25392539
+-----------+--------------------------------+------------------------+-------+
2540-
| ``%e`` | The day of the month as a | ␣1, ␣2, ..., 31 | |
2540+
| ``%e`` | The day of the month as a | ␣1, ␣2, ..., 31 | \(10) |
25412541
| | space-padded decimal number. | | |
25422542
+-----------+--------------------------------+------------------------+-------+
25432543
| ``%F`` | Equivalent to ``%Y-%m-%d``, | 2025-10-11, | \(0) |

Lib/_strptime.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ def repl(m):
466466
case 'Y' | 'y' | 'G':
467467
nonlocal year_in_format
468468
year_in_format = True
469-
case 'd':
469+
case 'd' | 'e':
470470
nonlocal day_of_month_in_format
471471
day_of_month_in_format = True
472472
return self[directive]
@@ -475,8 +475,8 @@ def repl(m):
475475
import warnings
476476
warnings.warn("""\
477477
Parsing dates involving a day of month without a year specified is ambiguous
478-
and fails to parse leap day. The default behavior will change in Python 3.15
479-
to either always raise an exception or to use a different default year (TBD).
478+
and fails to parse leap day. The default behavior will change in a future Python
479+
version to either always raise an exception or to use a different default year.
480480
To avoid trouble, add a specific year to the input & format.
481481
See https://github.com/python/cpython/issues/70647.""",
482482
DeprecationWarning,

Lib/test/datetimetester.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1208,9 +1208,12 @@ def test_strptime_leap_year(self):
12081208
with self.assertRaises(ValueError):
12091209
# The existing behavior that GH-70647 seeks to change.
12101210
date.strptime('02-29', '%m-%d')
1211+
with self.assertRaises(ValueError):
1212+
date.strptime('02-29', '%m-%e')
12111213
with self._assertNotWarns(DeprecationWarning):
12121214
date.strptime('20-03-14', '%y-%m-%d')
12131215
date.strptime('02-29,2024', '%m-%d,%Y')
1216+
date.strptime('02-29,2024', '%m-%e,%Y')
12141217

12151218
class SubclassDate(date):
12161219
sub_var = 1
@@ -3096,10 +3099,15 @@ def test_strptime_leap_year(self):
30963099
with self.assertWarnsRegex(DeprecationWarning,
30973100
r'.*day of month without a year.*'):
30983101
self.theclass.strptime('03-14.159265', '%m-%d.%f')
3102+
with self.assertWarnsRegex(DeprecationWarning,
3103+
r'.*day of month without a year.*'):
3104+
self.theclass.strptime('03-14.159265', '%m-%e.%f')
30993105
with self._assertNotWarns(DeprecationWarning):
31003106
self.theclass.strptime('20-03-14.159265', '%y-%m-%d.%f')
31013107
with self._assertNotWarns(DeprecationWarning):
31023108
self.theclass.strptime('02-29,2024', '%m-%d,%Y')
3109+
with self._assertNotWarns(DeprecationWarning):
3110+
self.theclass.strptime('02-29,2024', '%m-%e,%Y')
31033111

31043112
def test_strptime_z_empty(self):
31053113
for directive in ('z', ':z'):

Lib/test/test_time.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -358,11 +358,11 @@ def test_strptime(self):
358358
# Should be able to go round-trip from strftime to strptime without
359359
# raising an exception.
360360
tt = time.gmtime(self.t)
361-
for directive in ('a', 'A', 'b', 'B', 'c', 'd', 'H', 'I',
361+
for directive in ('a', 'A', 'b', 'B', 'c', 'd', 'e', 'H', 'I',
362362
'j', 'm', 'M', 'p', 'S',
363363
'U', 'w', 'W', 'x', 'X', 'y', 'Y', 'Z', '%'):
364364
format = '%' + directive
365-
if directive == 'd':
365+
if directive in ('d', 'e'):
366366
format += ',%Y' # Avoid GH-70647.
367367
strf_output = time.strftime(format, tt)
368368
try:
@@ -391,6 +391,9 @@ def test_strptime_leap_year(self):
391391
with self.assertWarnsRegex(DeprecationWarning,
392392
r'.*day of month without a year.*'):
393393
time.strptime('02-07 18:28', '%m-%d %H:%M')
394+
with self.assertWarnsRegex(DeprecationWarning,
395+
r'.*day of month without a year.*'):
396+
time.strptime('02- 7 18:28', '%m-%e %H:%M')
394397

395398
def test_asctime(self):
396399
time.asctime(time.gmtime(self.t))

0 commit comments

Comments
 (0)