MCPcopy
hub / github.com/django/django / parse_duration

Function parse_duration

django/utils/dateparse.py:133–155  ·  view source on GitHub ↗

Parse a duration string and return a datetime.timedelta. The preferred format for durations in Django is '%d %H:%M:%S.%f'. Also supports ISO 8601 representation (excluding years and months) and PostgreSQL's day-time interval format.

(value)

Source from the content-addressed store, hash-verified

131
132
133def parse_duration(value):
134 """Parse a duration string and return a datetime.timedelta.
135
136 The preferred format for durations in Django is '%d %H:%M:%S.%f'.
137
138 Also supports ISO 8601 representation (excluding years and months) and
139 PostgreSQL's day-time interval format.
140 """
141 match = (
142 standard_duration_re.match(value)
143 or iso8601_duration_re.match(value)
144 or postgres_interval_re.match(value)
145 )
146 if match:
147 kw = match.groupdict()
148 sign = -1 if kw.pop("sign", "+") == "-" else 1
149 if kw.get("microseconds"):
150 kw["microseconds"] = kw["microseconds"].ljust(6, "0")
151 kw = {k: float(v.replace(",", ".")) for k, v in kw.items() if v is not None}
152 days = datetime.timedelta(kw.pop("days", 0.0) or 0.0)
153 if match.re == iso8601_duration_re:
154 days *= sign
155 return days + sign * datetime.timedelta(**kw)

Callers 15

to_pythonMethod · 0.90
to_pythonMethod · 0.90
test_simpleMethod · 0.90
test_daysMethod · 0.90
test_microsecondsMethod · 0.90
test_negativeMethod · 0.90
test_simpleMethod · 0.90
test_daysMethod · 0.90
test_microsecondsMethod · 0.90
test_negativeMethod · 0.90

Calls 4

matchMethod · 0.45
popMethod · 0.45
getMethod · 0.45
itemsMethod · 0.45

Tested by 15

test_simpleMethod · 0.72
test_daysMethod · 0.72
test_microsecondsMethod · 0.72
test_negativeMethod · 0.72
test_simpleMethod · 0.72
test_daysMethod · 0.72
test_microsecondsMethod · 0.72
test_negativeMethod · 0.72
test_secondsMethod · 0.72
test_minutes_secondsMethod · 0.72