Skip to content

Commit a0b0f07

Browse files
Add docs and tests for time.strptime
1 parent 5d1d53d commit a0b0f07

File tree

5 files changed

+31
-13
lines changed

5 files changed

+31
-13
lines changed

Doc/library/datetime.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2432,7 +2432,8 @@ Class attributes:
24322432

24332433
:class:`date`, :class:`.datetime`, and :class:`.time` objects all support a
24342434
``strftime(format)`` method, to create a string representing the time under the
2435-
control of an explicit format string. The methods only accept ASCII digits.
2435+
control of an explicit format string. A :exc:`ValueError` will be raised if digits
2436+
are not ASCII.
24362437

24372438
Conversely, the :meth:`date.strptime`, :meth:`datetime.strptime` and
24382439
:meth:`time.strptime` class methods create an object from a string
@@ -2612,7 +2613,7 @@ differences between platforms in handling of unsupported format specifiers.
26122613
``%:z`` was added.
26132614

26142615
.. versionchanged:: next
2615-
Digits must be ASCII
2616+
Non-ASCII digits are now rejected by ``strptime`` for numerical directives.
26162617

26172618
Technical Detail
26182619
^^^^^^^^^^^^^^^^

Doc/library/time.rst

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -594,9 +594,10 @@ Functions
594594
:func:`strftime`; it defaults to ``"%a %b %d %H:%M:%S %Y"`` which matches the
595595
formatting returned by :func:`ctime`. If *string* cannot be parsed according
596596
to *format*, or if it has excess data after parsing, :exc:`ValueError` is
597-
raised. The default values used to fill in any missing data when more
598-
accurate values cannot be inferred are ``(1900, 1, 1, 0, 0, 0, 0, 1, -1)``.
599-
Both *string* and *format* must be strings.
597+
raised. :exc:`ValueError` is raised if digits are not ASCII. The default
598+
values used to fill in any missing data when more accurate values cannot be
599+
inferred are ``(1900, 1, 1, 0, 0, 0, 0, 1, -1)``. Both *string* and *format*
600+
must be strings.
600601

601602
For example:
602603

@@ -616,6 +617,9 @@ Functions
616617
and thus does not necessarily support all directives available that are not
617618
documented as supported.
618619

620+
.. versionchanged:: next
621+
Non-ASCII digits are now rejected by ``strptime``.
622+
619623

620624
.. class:: struct_time
621625

Doc/whatsnew/3.14.rst

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -504,10 +504,10 @@ datetime
504504
* Add :meth:`datetime.time.strptime` and :meth:`datetime.date.strptime`.
505505
(Contributed by Wannes Boeykens in :gh:`41431`.)
506506

507-
* The ``strptime(format)`` methods: :meth:`datetime.date.strptime`,
508-
:meth:`datetime.datetime.strptime` and :meth:`datetime.time.strptime`
509-
only accept ASCII digits.
510-
(Contributed by Stan Ulbrych in :gh:`131008`
507+
* The :meth:`datetime.date.strptime`, :meth:`datetime.datetime.strptime` and
508+
:meth:`datetime.time.strptime` methods now only accept ASCII digits, will and
509+
raise a :exc:`ValueError` if non-ASCII digits are specified.
510+
(Contributed by Stan Ulbrych in :gh:`131008`.)
511511

512512
decimal
513513
-------
@@ -883,6 +883,15 @@ sys.monitoring
883883
* Two new events are added: :monitoring-event:`BRANCH_LEFT` and
884884
:monitoring-event:`BRANCH_RIGHT`. The ``BRANCH`` event is deprecated.
885885

886+
887+
time
888+
----
889+
890+
* The :meth:`time.strptime`, now only accept ASCII digits, and will raise a
891+
:exc:`ValueError` if non-ASCII digits are specified.
892+
(Contributed by Stan Ulbrych in :gh:`131008`.)
893+
894+
886895
threading
887896
---------
888897

Lib/_strptime.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import calendar
1717
from re import compile as re_compile
1818
from re import sub as re_sub
19-
from re import ASCII, IGNORECASE
19+
from re import IGNORECASE
2020
from re import escape as re_escape
2121
from datetime import (date as datetime_date,
2222
timedelta as datetime_timedelta,
@@ -290,7 +290,7 @@ def __init__(self, locale_time=None):
290290
'f': r"(?P<f>[0-9]{1,6})",
291291
'H': r"(?P<H>2[0-3]|[0-1][0-9]|[0-9])",
292292
'I': r"(?P<I>1[0-2]|0[1-9]|[1-9]| [1-9])",
293-
'G': r"(?P<G>[0-9][0-9][0-9][0-9])",
293+
'G': r"(?P<G>[0-9]{4})",
294294
'j': r"(?P<j>36[0-6]|3[0-5][0-9]|[1-2][0-9][0-9]|0[1-9][0-9]|00[1-9]|[1-9][0-9]|0[1-9]|[1-9])",
295295
'm': r"(?P<m>1[0-2]|0[1-9]|[1-9])",
296296
'M': r"(?P<M>[0-5][0-9]|[0-9])",
@@ -300,8 +300,8 @@ def __init__(self, locale_time=None):
300300
'u': r"(?P<u>[1-7])",
301301
'V': r"(?P<V>5[0-3]|0[1-9]|[1-4][0-9]|[0-9])",
302302
# W is set below by using 'U'
303-
'y': r"(?P<y>[0-9][0-9])",
304-
'Y': r"(?P<Y>[0-9][0-9][0-9][0-9])",
303+
'y': r"(?P<y>[0-9]{2})",
304+
'Y': r"(?P<Y>[0-9]{4})",
305305
'z': r"(?P<z>[+-][0-9][0-9]:?[0-5][0-9](:?[0-5][0-9](\.[0-9]{1,6})?)?|(?-i:Z))",
306306
'A': self.__seqToRE(self.locale_time.f_weekday, 'A'),
307307
'a': self.__seqToRE(self.locale_time.a_weekday, 'a'),

Lib/test/test_time.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,10 @@ def test_strptime_leap_year(self):
352352
r'.*day of month without a year.*'):
353353
time.strptime('02-07 18:28', '%m-%d %H:%M')
354354

355+
def test_strptime_non_ascii(self):
356+
with self.assertRaises(ValueError):
357+
time.strptime('٢025', '%Y')
358+
355359
def test_asctime(self):
356360
time.asctime(time.gmtime(self.t))
357361

0 commit comments

Comments
 (0)