@@ -526,18 +526,20 @@ Other constructors, all class methods:
526
526
527
527
.. classmethod :: date.fromisoformat(date_string)
528
528
529
- Return a :class: `date ` corresponding to a *date_string * given in the format
530
- ``YYYY-MM-DD `` ::
529
+ Return a :class: `date ` corresponding to a *date_string * given in any valid
530
+ ISO 8601 format, except ordinal dates (e.g. ``YYYY-DDD ``) ::
531
531
532
532
>>> from datetime import date
533
533
>>> date.fromisoformat('2019-12-04')
534
534
datetime.date(2019, 12, 4)
535
-
536
- This is the inverse of :meth: `date.isoformat `. It only supports the format
537
- ``YYYY-MM-DD ``.
535
+ >>> date.fromisoformat('20191204')
536
+ datetime.date(2019, 12, 4)
537
+ >>> date.fromisoformat('2021-W01-1')
538
+ datetime.date(2021, 1, 4)
538
539
539
540
.. versionadded :: 3.7
540
-
541
+ .. versionchanged :: 3.11
542
+ Previously, this method only supported the format ``YYYY-MM-DD ``.
541
543
542
544
.. classmethod :: date.fromisocalendar(year, week, day)
543
545
@@ -710,8 +712,6 @@ Instance methods:
710
712
>>> date(2002, 12, 4).isoformat()
711
713
'2002-12-04'
712
714
713
- This is the inverse of :meth: `date.fromisoformat `.
714
-
715
715
.. method :: date.__str__()
716
716
717
717
For a date *d *, ``str(d) `` is equivalent to ``d.isoformat() ``.
@@ -994,31 +994,29 @@ Other constructors, all class methods:
994
994
995
995
.. classmethod :: datetime.fromisoformat(date_string)
996
996
997
- Return a :class: `.datetime ` corresponding to a *date_string * in one of the
998
- formats emitted by :meth: `date.isoformat ` and :meth: `datetime.isoformat `.
999
-
1000
- Specifically, this function supports strings in the format:
997
+ Return a :class: `.datetime ` corresponding to a *date_string * in any valid
998
+ ISO 8601 format, with the following exceptions:
1001
999
1002
- .. code-block :: none
1003
-
1004
- YYYY-MM-DD[*HH[:MM[:SS[.fff[fff]]]][+HH:MM[:SS[.ffffff]]]]
1005
-
1006
- where ``* `` can match any single character.
1007
-
1008
- .. caution ::
1009
-
1010
- This does *not * support parsing arbitrary ISO 8601 strings - it is only intended
1011
- as the inverse operation of :meth: `datetime.isoformat `. A more full-featured
1012
- ISO 8601 parser, ``dateutil.parser.isoparse `` is available in the third-party package
1013
- `dateutil <https://dateutil.readthedocs.io/en/stable/parser.html#dateutil.parser.isoparse >`__.
1000
+ 1. Time zone offsets may have fractional seconds.
1001
+ 2. The `T ` separator may be replaced by any single unicode character.
1002
+ 3. Ordinal dates are not currently supported.
1003
+ 4. Fractional hours and minutes are not supported.
1014
1004
1015
1005
Examples::
1016
1006
1017
1007
>>> from datetime import datetime
1018
1008
>>> datetime.fromisoformat('2011-11-04')
1019
1009
datetime.datetime(2011, 11, 4, 0, 0)
1010
+ >>> datetime.fromisoformat('20111104')
1011
+ datetime.datetime(2011, 11, 4, 0, 0)
1020
1012
>>> datetime.fromisoformat('2011-11-04T00:05:23')
1021
1013
datetime.datetime(2011, 11, 4, 0, 5, 23)
1014
+ >>> datetime.fromisoformat('2011-11-04T00:05:23Z')
1015
+ datetime.datetime(2011, 11, 4, 0, 5, 23, tzinfo=datetime.timezone.utc)
1016
+ >>> datetime.fromisoformat('20111104T000523')
1017
+ datetime.datetime(2011, 11, 4, 0, 5, 23)
1018
+ >>> datetime.fromisoformat('2011-W01-2T00:05:23.283')
1019
+ datetime.datetime(2011, 1, 4, 0, 5, 23, 283000)
1022
1020
>>> datetime.fromisoformat('2011-11-04 00:05:23.283')
1023
1021
datetime.datetime(2011, 11, 4, 0, 5, 23, 283000)
1024
1022
>>> datetime.fromisoformat('2011-11-04 00:05:23.283+00:00')
@@ -1028,6 +1026,10 @@ Other constructors, all class methods:
1028
1026
tzinfo=datetime.timezone(datetime.timedelta(seconds=14400)))
1029
1027
1030
1028
.. versionadded :: 3.7
1029
+ .. versionchanged :: 3.11
1030
+ Previously, this method only supported formats that could be emitted by
1031
+ :meth: `date.isoformat() ` or :meth: `datetime.isoformat() `.
1032
+
1031
1033
1032
1034
.. classmethod :: datetime.fromisocalendar(year, week, day)
1033
1035
@@ -1763,30 +1765,41 @@ Other constructor:
1763
1765
1764
1766
.. classmethod :: time.fromisoformat(time_string)
1765
1767
1766
- Return a :class: `.time ` corresponding to a *time_string * in one of the
1767
- formats emitted by :meth: `time.isoformat `. Specifically, this function supports
1768
- strings in the format:
1769
-
1770
- .. code-block :: none
1771
-
1772
- HH[:MM[:SS[.fff[fff]]]][+HH:MM[:SS[.ffffff]]]
1773
-
1774
- .. caution ::
1768
+ Return a :class: `.time ` corresponding to a *time_string * in any valid
1769
+ ISO 8601 format, with the following exceptions:
1775
1770
1776
- This does *not * support parsing arbitrary ISO 8601 strings. It is only
1777
- intended as the inverse operation of :meth: `time.isoformat `.
1771
+ 1. Time zone offsets may have fractional seconds.
1772
+ 2. The leading `T `, normally required in cases where there may be ambiguity between
1773
+ a date and a time, is not required.
1774
+ 3. Fractional seconds may have any number of digits (anything beyond 6 will
1775
+ be truncated).
1776
+ 4. Fractional hours and minutes are not supported.
1778
1777
1779
1778
Examples::
1780
1779
1781
1780
>>> from datetime import time
1782
1781
>>> time.fromisoformat('04:23:01')
1783
1782
datetime.time(4, 23, 1)
1783
+ >>> time.fromisoformat('T04:23:01')
1784
+ datetime.time(4, 23, 1)
1785
+ >>> time.fromisoformat('T042301')
1786
+ datetime.time(4, 23, 1)
1784
1787
>>> time.fromisoformat('04:23:01.000384')
1785
1788
datetime.time(4, 23, 1, 384)
1789
+ >>> time.fromisoformat('04:23:01,000')
1790
+ datetime.time(4, 23, 1, 384)
1786
1791
>>> time.fromisoformat('04:23:01+04:00')
1787
1792
datetime.time(4, 23, 1, tzinfo=datetime.timezone(datetime.timedelta(seconds=14400)))
1793
+ >>> time.fromisoformat('04:23:01Z')
1794
+ datetime.time(4, 23, 1, tzinfo=datetime.timezone.utc)
1795
+ >>> time.fromisoformat('04:23:01+00:00')
1796
+ datetime.time(4, 23, 1, tzinfo=datetime.timezone.utc)
1797
+
1788
1798
1789
1799
.. versionadded :: 3.7
1800
+ .. versionchanged :: 3.11
1801
+ Previously, this method only supported formats that could be emitted by
1802
+ :meth: `time.isoformat() `.
1790
1803
1791
1804
1792
1805
Instance methods:
0 commit comments