Opened 7 years ago

Closed 7 years ago

#29089 closed Bug (fixed)

Redundant date parsing in SelectDateWidget.format_value()

Reported by: Carlton Gibson Owned by: nobody
Component: Forms Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

SelectDateWidget.format_value() contains a block to parse a string value into year, month and day components.

The current implementation first attempts to parse value as a datetime.

Regardless of the outcome here it then parses value again against against a regex.

Current implementation:

https://github.com/django/django/blob/d4bbd3f41882104156cf9f2b02cb13376a59489d/django/forms/widgets.py#L1006-L1018

        elif isinstance(value, str):
            if settings.USE_L10N:
                input_format = get_format('DATE_INPUT_FORMATS')[0]
                try:
                    d = datetime.datetime.strptime(value, input_format)
                except ValueError:
                    pass
                else:
                    year, month, day = d.year, d.month, d.day
            match = self.date_re.match(value)
            if match:
                year, month, day = [int(val) for val in match.groups()]
        return {'year': year, 'month': month, 'day': day}

With default settings input_format is (essentially) the same as the date_re. We end up doing the work twice.

Should we not skip the re.match block if the strptime approach already succeeded?

Change History (3)

comment:1 by Tim Graham, 7 years ago

Has patch: set
Summary: Duplicate date parsing in `SelectDateWidget.format_value()`Redundant date parsing in SelectDateWidget.format_value()
Triage Stage: UnreviewedAccepted

comment:2 by Mariusz Felisiak, 7 years ago

Triage Stage: AcceptedReady for checkin

comment:3 by GitHub <noreply@…>, 7 years ago

Resolution: fixed
Status: newclosed

In 5538729e:

Fixed #29089 -- Avoided redundant date parsing in SelectDateWidget.format_value().

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