MCPcopy
hub / github.com/pydantic/pydantic / parse_date

Function parse_date

pydantic/v1/datetime_parse.py:105–134  ·  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

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

Callers

nothing calls this directly

Calls 4

get_numericFunction · 0.85
from_unix_secondsFunction · 0.85
decodeMethod · 0.45
itemsMethod · 0.45

Tested by

no test coverage detected