MCPcopy
hub / github.com/django/django / parse_time

Function parse_time

django/utils/dateparse.py:82–102  ·  view source on GitHub ↗

Parse a string and return a datetime.time. This function doesn't support time zone offsets. Raise ValueError if the input is well formatted but not a valid time. Return None if the input isn't well formatted, in particular if it contains an offset.

(value)

Source from the content-addressed store, hash-verified

80
81
82def parse_time(value):
83 """Parse a string and return a datetime.time.
84
85 This function doesn't support time zone offsets.
86
87 Raise ValueError if the input is well formatted but not a valid time.
88 Return None if the input isn't well formatted, in particular if it
89 contains an offset.
90 """
91 try:
92 # The fromisoformat() method takes time zone info into account and
93 # returns a time with a tzinfo component, if possible. However, there
94 # are no circumstances where aware datetime.time objects make sense, so
95 # remove the time zone offset.
96 return datetime.time.fromisoformat(value).replace(tzinfo=None)
97 except ValueError:
98 if match := time_re.match(value):
99 kw = match.groupdict()
100 kw["microsecond"] = kw["microsecond"] and kw["microsecond"].ljust(6, "0")
101 kw = {k: int(v) for k, v in kw.items() if v is not None}
102 return datetime.time(**kw)
103
104
105def parse_datetime(value):

Callers 4

split_tzname_deltaFunction · 0.90
to_pythonMethod · 0.90
test_parse_timeMethod · 0.90

Calls 3

timeMethod · 0.80
matchMethod · 0.45
itemsMethod · 0.45

Tested by 1

test_parse_timeMethod · 0.72