Construct a datetime from a string in one of the ISO 8601 formats.
(cls, date_string)
| 1951 | |
| 1952 | @classmethod |
| 1953 | def fromisoformat(cls, date_string): |
| 1954 | """Construct a datetime from a string in one of the ISO 8601 formats.""" |
| 1955 | if not isinstance(date_string, str): |
| 1956 | raise TypeError('fromisoformat: argument must be str') |
| 1957 | |
| 1958 | if len(date_string) < 7: |
| 1959 | raise ValueError(f'Invalid isoformat string: {date_string!r}') |
| 1960 | |
| 1961 | # Split this at the separator |
| 1962 | try: |
| 1963 | separator_location = _find_isoformat_datetime_separator(date_string) |
| 1964 | dstr = date_string[0:separator_location] |
| 1965 | tstr = date_string[(separator_location+1):] |
| 1966 | |
| 1967 | date_components = _parse_isoformat_date(dstr) |
| 1968 | except ValueError: |
| 1969 | raise ValueError( |
| 1970 | f'Invalid isoformat string: {date_string!r}') from None |
| 1971 | |
| 1972 | if tstr: |
| 1973 | try: |
| 1974 | (time_components, |
| 1975 | became_next_day, |
| 1976 | error_from_components, |
| 1977 | error_from_tz) = _parse_isoformat_time(tstr) |
| 1978 | except ValueError: |
| 1979 | raise ValueError( |
| 1980 | f'Invalid isoformat string: {date_string!r}') from None |
| 1981 | else: |
| 1982 | if error_from_tz: |
| 1983 | raise error_from_tz |
| 1984 | if error_from_components: |
| 1985 | raise ValueError("minute, second, and microsecond must be 0 when hour is 24") |
| 1986 | |
| 1987 | if became_next_day: |
| 1988 | year, month, day = date_components |
| 1989 | # Only wrap day/month when it was previously valid |
| 1990 | if month <= 12 and day <= (days_in_month := _days_in_month(year, month)): |
| 1991 | # Calculate midnight of the next day |
| 1992 | day += 1 |
| 1993 | if day > days_in_month: |
| 1994 | day = 1 |
| 1995 | month += 1 |
| 1996 | if month > 12: |
| 1997 | month = 1 |
| 1998 | year += 1 |
| 1999 | date_components = [year, month, day] |
| 2000 | else: |
| 2001 | time_components = [0, 0, 0, 0, None] |
| 2002 | |
| 2003 | return cls(*(date_components + time_components)) |
| 2004 | |
| 2005 | def timetuple(self): |
| 2006 | "Return local time tuple compatible with time.localtime()." |
nothing calls this directly
no test coverage detected