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

Function parse_date

src/openai/_utils/_datetime_parse.py:106–136  ·  view source on GitHub ↗

Parse a date/int/float/string and return a datetime.date. Raise ValueError if the input is well formatted but not a valid date. Raise ValueError if the input isn't well formatted.

(value: Union[date, StrBytesIntFloat])

Source from the content-addressed store, hash-verified

104
105
106def parse_date(value: Union[date, StrBytesIntFloat]) -> date:
107 """
108 Parse a date/int/float/string and return a datetime.date.
109
110 Raise ValueError if the input is well formatted but not a valid date.
111 Raise ValueError if the input isn't well formatted.
112 """
113 if isinstance(value, date):
114 if isinstance(value, datetime):
115 return value.date()
116 else:
117 return value
118
119 number = _get_numeric(value, "date")
120 if number is not None:
121 return _from_unix_seconds(number).date()
122
123 if isinstance(value, bytes):
124 value = value.decode()
125
126 assert not isinstance(value, (float, int))
127 match = date_re.match(value)
128 if match is None:
129 raise ValueError("invalid date format")
130
131 kw = {k: int(v) for k, v in match.groupdict().items()}
132
133 try:
134 return date(**kw)
135 except ValueError:
136 raise ValueError("invalid date format") from None

Callers

nothing calls this directly

Calls 4

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

Tested by

no test coverage detected