#30141 closed Bug (fixed)
Fix parse_duration() for some negative durations
Reported by: | heikkimattila | Owned by: | Seunghun Lee |
---|---|---|---|
Component: | Utilities | Version: | 1.11 |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Accepted | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | yes |
Easy pickings: | no | UI/UX: | no |
Description
The https://docs.djangoproject.com/en/2.1/_modules/django/utils/dateparse/ defines:
standard_duration_re = re.compile( r'^' r'(?:(?P<days>-?\d+) (days?, )?)?' r'((?:(?P<hours>-?\d+):)(?=\d+:\d+))?' r'(?:(?P<minutes>-?\d+):)?' r'(?P<seconds>-?\d+)' r'(?:\.(?P<microseconds>\d{1,6})\d{0,6})?' r'$' )
that doesn't match to negative durations, because of the <hours> definition final (lookahead) part does not have '-?' in it. The following will work:
r'((?:(?P<hours>-?\d+):)(?=-?\d+:-?\d+))?'
(Thanks to Konstantin Senichev for finding the fix.)
Change History (10)
comment:1 by , 6 years ago
Component: | Uncategorized → Utilities |
---|---|
Easy pickings: | unset |
Type: | Uncategorized → Bug |
comment:2 by , 6 years ago
Right, this should have been fixed by #27699 which is included in 1.11.x.
comment:3 by , 6 years ago
Resolution: | → needsinfo |
---|---|
Status: | new → closed |
comment:4 by , 6 years ago
Resolution: | needsinfo |
---|---|
Status: | closed → new |
Example cases, can be discussed:
parse_duration('-00:01:01') => plus 61 seconds, so it is not -(00:01:01) but (-00):(+01):(+01)
parse_duration('00:-01:-01) => None , leading zeros will prevent parsing
parse_duration('-01:01') => minus 59 seconds
parse_duration('-01:-01') => minus 61 seconds
The fix presented would allow the second line to be parsed (which would help with generated durations).
And some instructions in the function/documentation/wiki would be useful, to clarify how the minus sign affects in duration.
comment:5 by , 6 years ago
Summary: | dateparse parse_duration to accept negative durations → Fix parse_duration() for some negative durations |
---|---|
Triage Stage: | Unreviewed → Accepted |
The fix from #27699 may not be entirely correct. I agree with your first and third examples. I'd expect a leading minus sign to negate the entire value so they would be minus 61 seconds. I think the second and fourth examples are invalid. I don't think a minus sign after a colon is valid.
comment:6 by , 6 years ago
Thanks for the extra details. I agree with Tim that everything but a leading -
seems like an invalid value that happened to work because of an inappropriate pattern as it was never tested.
comment:7 by , 6 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
Please give an example valid that's not working. There are some tests for negative values.