Construct a datetime from a string in one of the ISO 8601 formats.
(cls, date_string)
| 1916 | |
| 1917 | @classmethod |
| 1918 | def fromisoformat(cls, date_string): |
| 1919 | """Construct a datetime from a string in one of the ISO 8601 formats.""" |
| 1920 | if not isinstance(date_string, str): |
| 1921 | raise TypeError('fromisoformat: argument must be str') |
| 1922 | |
| 1923 | if len(date_string) < 7: |
| 1924 | raise ValueError(f'Invalid isoformat string: {date_string!r}') |
| 1925 | |
| 1926 | # Split this at the separator |
| 1927 | try: |
| 1928 | separator_location = _find_isoformat_datetime_separator(date_string) |
| 1929 | dstr = date_string[0:separator_location] |
| 1930 | tstr = date_string[(separator_location+1):] |
| 1931 | |
| 1932 | date_components = _parse_isoformat_date(dstr) |
| 1933 | except ValueError: |
| 1934 | raise ValueError( |
| 1935 | f'Invalid isoformat string: {date_string!r}') from None |
| 1936 | |
| 1937 | if tstr: |
| 1938 | try: |
| 1939 | time_components, became_next_day, error_from_components = _parse_isoformat_time(tstr) |
| 1940 | except ValueError: |
| 1941 | raise ValueError( |
| 1942 | f'Invalid isoformat string: {date_string!r}') from None |
| 1943 | else: |
| 1944 | if error_from_components: |
| 1945 | raise ValueError("minute, second, and microsecond must be 0 when hour is 24") |
| 1946 | |
| 1947 | if became_next_day: |
| 1948 | year, month, day = date_components |
| 1949 | # Only wrap day/month when it was previously valid |
| 1950 | if month <= 12 and day <= (days_in_month := _days_in_month(year, month)): |
| 1951 | # Calculate midnight of the next day |
| 1952 | day += 1 |
| 1953 | if day > days_in_month: |
| 1954 | day = 1 |
| 1955 | month += 1 |
| 1956 | if month > 12: |
| 1957 | month = 1 |
| 1958 | year += 1 |
| 1959 | date_components = [year, month, day] |
| 1960 | else: |
| 1961 | time_components = [0, 0, 0, 0, None] |
| 1962 | |
| 1963 | return cls(*(date_components + time_components)) |
| 1964 | |
| 1965 | def timetuple(self): |
| 1966 | "Return local time tuple compatible with time.localtime()." |
nothing calls this directly
no test coverage detected