MCPcopy
hub / github.com/pydantic/pydantic / parse_time

Function parse_time

pydantic/v1/datetime_parse.py:137–172  ·  view source on GitHub ↗

Parse a time/string and return a datetime.time. Raise ValueError if the input is well formatted but not a valid time. Raise ValueError if the input isn't well formatted, in particular if it contains an offset.

(value: Union[time, StrBytesIntFloat])

Source from the content-addressed store, hash-verified

135
136
137def parse_time(value: Union[time, StrBytesIntFloat]) -> time:
138 """
139 Parse a time/string and return a datetime.time.
140
141 Raise ValueError if the input is well formatted but not a valid time.
142 Raise ValueError if the input isn't well formatted, in particular if it contains an offset.
143 """
144 if isinstance(value, time):
145 return value
146
147 number = get_numeric(value, 'time')
148 if number is not None:
149 if number >= 86400:
150 # doesn't make sense since the time time loop back around to 0
151 raise errors.TimeError()
152 return (datetime.min + timedelta(seconds=number)).time()
153
154 if isinstance(value, bytes):
155 value = value.decode()
156
157 match = time_re.match(value) # type: ignore
158 if match is None:
159 raise errors.TimeError()
160
161 kw = match.groupdict()
162 if kw['microsecond']:
163 kw['microsecond'] = kw['microsecond'].ljust(6, '0')
164
165 tzinfo = _parse_timezone(kw.pop('tzinfo'), errors.TimeError)
166 kw_: Dict[str, Union[None, int, timezone]] = {k: int(v) for k, v in kw.items() if v is not None}
167 kw_['tzinfo'] = tzinfo
168
169 try:
170 return time(**kw_) # type: ignore
171 except ValueError:
172 raise errors.TimeError()
173
174
175def parse_datetime(value: Union[datetime, StrBytesIntFloat]) -> datetime:

Callers

nothing calls this directly

Calls 4

get_numericFunction · 0.85
_parse_timezoneFunction · 0.85
decodeMethod · 0.45
itemsMethod · 0.45

Tested by

no test coverage detected