Parse a string and return a datetime.time. This function doesn't support time zone offsets. Raise ValueError if the input is well formatted but not a valid time. Return None if the input isn't well formatted, in particular if it contains an offset.
(value)
| 80 | |
| 81 | |
| 82 | def parse_time(value): |
| 83 | """Parse a string and return a datetime.time. |
| 84 | |
| 85 | This function doesn't support time zone offsets. |
| 86 | |
| 87 | Raise ValueError if the input is well formatted but not a valid time. |
| 88 | Return None if the input isn't well formatted, in particular if it |
| 89 | contains an offset. |
| 90 | """ |
| 91 | try: |
| 92 | # The fromisoformat() method takes time zone info into account and |
| 93 | # returns a time with a tzinfo component, if possible. However, there |
| 94 | # are no circumstances where aware datetime.time objects make sense, so |
| 95 | # remove the time zone offset. |
| 96 | return datetime.time.fromisoformat(value).replace(tzinfo=None) |
| 97 | except ValueError: |
| 98 | if match := time_re.match(value): |
| 99 | kw = match.groupdict() |
| 100 | kw["microsecond"] = kw["microsecond"] and kw["microsecond"].ljust(6, "0") |
| 101 | kw = {k: int(v) for k, v in kw.items() if v is not None} |
| 102 | return datetime.time(**kw) |
| 103 | |
| 104 | |
| 105 | def parse_datetime(value): |