MCPcopy Index your code
hub / github.com/python/cpython / fromisoformat

Method fromisoformat

Lib/_pydatetime.py:1634–1659  ·  view source on GitHub ↗

Construct a time from a string in one of the ISO 8601 formats.

(cls, time_string)

Source from the content-addressed store, hash-verified

1632
1633 @classmethod
1634 def fromisoformat(cls, time_string):
1635 """Construct a time from a string in one of the ISO 8601 formats."""
1636 if not isinstance(time_string, str):
1637 raise TypeError('fromisoformat: argument must be str')
1638
1639 # The spec actually requires that time-only ISO 8601 strings start with
1640 # T, but the extended format allows this to be omitted as long as there
1641 # is no ambiguity with date strings.
1642 time_string = time_string.removeprefix('T')
1643
1644 try:
1645 time_components, _, error_from_components, error_from_tz = (
1646 _parse_isoformat_time(time_string)
1647 )
1648 except ValueError:
1649 raise ValueError(
1650 f'Invalid isoformat string: {time_string!r}') from None
1651 else:
1652 if error_from_tz:
1653 raise error_from_tz
1654 if error_from_components:
1655 raise ValueError(
1656 "Minute, second, and microsecond must be 0 when hour is 24"
1657 )
1658
1659 return cls(*time_components)
1660
1661 def strftime(self, format):
1662 """Format using strftime(). The date part of the timestamp passed

Callers

nothing calls this directly

Calls 3

_parse_isoformat_timeFunction · 0.85
clsClass · 0.50
removeprefixMethod · 0.45

Tested by

no test coverage detected