MCPcopy
hub / github.com/openai/openai-python / parse_datetime

Function parse_datetime

src/openai/_utils/_datetime_parse.py:69–103  ·  view source on GitHub ↗

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])

Source from the content-addressed store, hash-verified

67
68
69def 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
106def parse_date(value: Union[date, StrBytesIntFloat]) -> date:

Callers

nothing calls this directly

Calls 5

_get_numericFunction · 0.85
_from_unix_secondsFunction · 0.85
_parse_timezoneFunction · 0.85
decodeMethod · 0.80
itemsMethod · 0.45

Tested by

no test coverage detected