Opened 8 years ago
Closed 8 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:
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 , 8 years ago
| Has patch: | set |
|---|---|
| Summary: | Duplicate date parsing in `SelectDateWidget.format_value()` → Redundant date parsing in SelectDateWidget.format_value() |
| Triage Stage: | Unreviewed → Accepted |
comment:2 by , 8 years ago
| Triage Stage: | Accepted → Ready for checkin |
|---|
Note:
See TracTickets
for help on using tickets.
PR