Opened 6 years ago

Closed 6 years ago

Last modified 6 years ago

#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 Tim Graham, 6 years ago

Component: UncategorizedUtilities
Easy pickings: unset
Type: UncategorizedBug

Please give an example valid that's not working. There are some tests for negative values.

comment:2 by Simon Charette, 6 years ago

Right, this should have been fixed by #27699 which is included in 1.11.x.

comment:3 by Tim Graham, 6 years ago

Resolution: needsinfo
Status: newclosed

comment:4 by heikkimattila, 6 years ago

Resolution: needsinfo
Status: closednew

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 Tim Graham, 6 years ago

Summary: dateparse parse_duration to accept negative durationsFix parse_duration() for some negative durations
Triage Stage: UnreviewedAccepted

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 Simon Charette, 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 Seunghun Lee, 6 years ago

Owner: changed from nobody to Seunghun Lee
Status: newassigned

comment:8 by Tim Graham, 6 years ago

Has patch: set
Patch needs improvement: set

comment:9 by Tim Graham <timograham@…>, 6 years ago

Resolution: fixed
Status: assignedclosed

In 99fc5dc:

Fixed #30141 -- Fixed parse_duration() for some negative durations.

comment:10 by Tim Graham <timograham@…>, 6 years ago

In 99fc5dc:

Fixed #30141 -- Fixed parse_duration() for some negative durations.

Note: See TracTickets for help on using tickets.
Back to Top