Parse a datetime/int/float/string and return a datetime.datetime. This function supports time zone offsets. When the input contains one, the output uses a timezone with a fixed offset from UTC. Raise ValueError if the input is well formatted but not a valid datetime. Raise Val
(value: Union[datetime, StrBytesIntFloat])
| 67 | |
| 68 | |
| 69 | def parse_datetime(value: Union[datetime, StrBytesIntFloat]) -> datetime: |
| 70 | """ |
| 71 | Parse a datetime/int/float/string and return a datetime.datetime. |
| 72 | |
| 73 | This function supports time zone offsets. When the input contains one, |
| 74 | the output uses a timezone with a fixed offset from UTC. |
| 75 | |
| 76 | Raise ValueError if the input is well formatted but not a valid datetime. |
| 77 | Raise ValueError if the input isn't well formatted. |
| 78 | """ |
| 79 | if isinstance(value, datetime): |
| 80 | return value |
| 81 | |
| 82 | number = _get_numeric(value, "datetime") |
| 83 | if number is not None: |
| 84 | return _from_unix_seconds(number) |
| 85 | |
| 86 | if isinstance(value, bytes): |
| 87 | value = value.decode() |
| 88 | |
| 89 | assert not isinstance(value, (float, int)) |
| 90 | |
| 91 | match = datetime_re.match(value) |
| 92 | if match is None: |
| 93 | raise ValueError("invalid datetime format") |
| 94 | |
| 95 | kw = match.groupdict() |
| 96 | if kw["microsecond"]: |
| 97 | kw["microsecond"] = kw["microsecond"].ljust(6, "0") |
| 98 | |
| 99 | tzinfo = _parse_timezone(kw.pop("tzinfo")) |
| 100 | kw_: Dict[str, Union[None, int, timezone]] = {k: int(v) for k, v in kw.items() if v is not None} |
| 101 | kw_["tzinfo"] = tzinfo |
| 102 | |
| 103 | return datetime(**kw_) # type: ignore |
| 104 | |
| 105 | |
| 106 | def parse_date(value: Union[date, StrBytesIntFloat]) -> date: |
nothing calls this directly
no test coverage detected