@@ -548,6 +548,39 @@ Other constructors, all class methods:
548
548
549
549
.. versionadded :: 3.8
550
550
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
+
551
584
552
585
Class attributes:
553
586
@@ -1827,7 +1860,7 @@ In Boolean contexts, a :class:`.time` object is always considered to be true.
1827
1860
details.
1828
1861
1829
1862
1830
- Other constructor :
1863
+ Other constructors :
1831
1864
1832
1865
.. classmethod :: time.fromisoformat(time_string)
1833
1866
@@ -1869,6 +1902,22 @@ Other constructor:
1869
1902
Previously, this method only supported formats that could be emitted by
1870
1903
:meth: `time.isoformat `.
1871
1904
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
+
1872
1921
1873
1922
Instance methods:
1874
1923
@@ -2367,24 +2416,22 @@ Class attributes:
2367
2416
``strftime(format) `` method, to create a string representing the time under the
2368
2417
control of an explicit format string.
2369
2418
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.
2373
2422
2374
2423
The table below provides a high-level comparison of :meth: `~.datetime.strftime `
2375
2424
versus :meth: `~.datetime.strptime `:
2376
2425
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
+ +----------------+--------------------------------------------------------+------------------------------------------------------------+
2388
2435
2389
2436
2390
2437
.. _format-codes :
0 commit comments