#29089: Duplicate date parsing in `SelectDateWidget.format_value()`
------------------------------------------+------------------------
               Reporter:  Carlton Gibson  |          Owner:  nobody
                   Type:  Bug             |         Status:  new
              Component:  Forms           |        Version:  master
               Severity:  Normal          |       Keywords:
           Triage Stage:  Unreviewed      |      Has patch:  0
    Needs documentation:  0               |    Needs tests:  0
Patch needs improvement:  0               |  Easy pickings:  0
                  UI/UX:  0               |
------------------------------------------+------------------------
 `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?

-- 
Ticket URL: <https://code.djangoproject.com/ticket/29089>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/056.3ffd2d78a78a0c40b8d0723d856066c3%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to