@@ -548,6 +548,39 @@ Other constructors, all class methods:
548548
549549 .. versionadded :: 3.8
550550
551+ .. classmethod :: date.strptime(date_string, format)
552+
553+ Return a :class: `.date ` corresponding to *date_string *, parsed according to
554+ *format *. This is equivalent to::
555+
556+ date(*(time.strptime(date_string, format)[0:3]))
557+
558+ :exc: `ValueError ` is raised if the date_string and format
559+ can't be parsed by :func: `time.strptime ` or if it returns a value which isn't a
560+ time tuple. See also :ref: `strftime-strptime-behavior ` and
561+ :meth: `date.fromisoformat `.
562+
563+ .. note ::
564+
565+ If *format * specifies a day of month without a year a
566+ :exc: `DeprecationWarning ` is emitted. This is to avoid a quadrennial
567+ leap year bug in code seeking to parse only a month and day as the
568+ default year used in absence of one in the format is not a leap year.
569+ Such *format * values may raise an error as of Python 3.15. The
570+ workaround is to always include a year in your *format *. If parsing
571+ *date_string * values that do not have a year, explicitly add a year that
572+ is a leap year before parsing:
573+
574+ .. doctest ::
575+
576+ >>> from datetime import date
577+ >>> date_string = " 02/29"
578+ >>> when = date.strptime(f " { date_string} ;1984 " , " %m/%d ;%Y" ) # Avoids leap year bug.
579+ >>> when.strftime(" %B %d " ) # doctest: +SKIP
580+ 'February 29'
581+
582+ .. versionadded :: 3.14
583+
551584
552585Class attributes:
553586
@@ -1827,7 +1860,7 @@ In Boolean contexts, a :class:`.time` object is always considered to be true.
18271860 details.
18281861
18291862
1830- Other constructor :
1863+ Other constructors :
18311864
18321865.. classmethod :: time.fromisoformat(time_string)
18331866
@@ -1869,6 +1902,22 @@ Other constructor:
18691902 Previously, this method only supported formats that could be emitted by
18701903 :meth: `time.isoformat `.
18711904
1905+ .. classmethod :: time.strptime(date_string, format)
1906+
1907+ Return a :class: `.time ` corresponding to *date_string *, parsed according to
1908+ *format *.
1909+
1910+ If *format * does not contain microseconds or timezone information, this is equivalent to::
1911+
1912+ time(*(time.strptime(date_string, format)[3:6]))
1913+
1914+ :exc: `ValueError ` is raised if the *date_string * and *format *
1915+ cannot be parsed by :func: `time.strptime ` or if it returns a value which is not a
1916+ time tuple. See also :ref: `strftime-strptime-behavior ` and
1917+ :meth: `time.fromisoformat `.
1918+
1919+ .. versionadded :: 3.14
1920+
18721921
18731922Instance methods:
18741923
@@ -2367,24 +2416,22 @@ Class attributes:
23672416``strftime(format) `` method, to create a string representing the time under the
23682417control of an explicit format string.
23692418
2370- Conversely, the :meth: `datetime.strptime ` class method creates a
2371- :class: ` .datetime ` object from a string representing a date and time and a
2372- corresponding format string.
2419+ Conversely, the :meth: `date.strptime `, :meth: ` datetime.strptime ` and
2420+ :meth: ` time.strptime ` class methods create an object from a string
2421+ representing the time and a corresponding format string.
23732422
23742423The table below provides a high-level comparison of :meth: `~.datetime.strftime `
23752424versus :meth: `~.datetime.strptime `:
23762425
2377- +----------------+--------------------------------------------------------+------------------------------------------------------------------------------+
2378- | | ``strftime `` | ``strptime `` |
2379- +================+========================================================+==============================================================================+
2380- | Usage | Convert object to a string according to a given format | Parse a string into a :class: `.datetime ` object given a corresponding format |
2381- +----------------+--------------------------------------------------------+------------------------------------------------------------------------------+
2382- | Type of method | Instance method | Class method |
2383- +----------------+--------------------------------------------------------+------------------------------------------------------------------------------+
2384- | Method of | :class: `date `; :class: `.datetime `; :class: `.time ` | :class: `.datetime ` |
2385- +----------------+--------------------------------------------------------+------------------------------------------------------------------------------+
2386- | Signature | ``strftime(format) `` | ``strptime(date_string, format) `` |
2387- +----------------+--------------------------------------------------------+------------------------------------------------------------------------------+
2426+ +----------------+--------------------------------------------------------+------------------------------------------------------------+
2427+ | | ``strftime `` | ``strptime `` |
2428+ +================+========================================================+============================================================+
2429+ | Usage | Convert object to a string according to a given format | Parse a string into an object given a corresponding format |
2430+ +----------------+--------------------------------------------------------+------------------------------------------------------------+
2431+ | Type of method | Instance method | Class method |
2432+ +----------------+--------------------------------------------------------+------------------------------------------------------------+
2433+ | Signature | ``strftime(format) `` | ``strptime(date_string, format) `` |
2434+ +----------------+--------------------------------------------------------+------------------------------------------------------------+
23882435
23892436
23902437 .. _format-codes :
0 commit comments