Opened 3 years ago

Closed 3 years ago

#32727 closed Bug (fixed)

Support spaces separaters in ISO-8601 datetimes between times and timezone strings

Reported by: Ben Wilber Owned by: Ben Wilber
Component: Utilities 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 (last modified by Ben Wilber)

GitHub Pull Request: https://github.com/django/django/pull/14368

According to ISO-8601, there can be any number of whitespace characters between the time strings and timezone strings.

Unfortunately the spec isn't public, but here's the link anyway https://www.iso.org/iso-8601-date-and-time-format.html.

Examples:

This is a valid ISO-8601 datetime string:

2012-04-23T10:20:30.400-02

django.utils.dateparse.parse_datetime parses this correctly.

This is also a valid ISO-8601 datetime string:

2012-04-23T10:20:30.400 -02

django.utils.dateparse.parse_datetime does not parse this correctly and returns None,

However, python-dateutil parses it correctly. The difference is that Django uses a (brittle) regex to parse ISO-8601 datetime strings, and python-dateutil does not.

https://github.com/django/django/blob/main/django/utils/dateparse.py#L22
https://github.com/dateutil/dateutil/blob/master/dateutil/parser/isoparser.py

I recommend that Django:

1) Depend on python-dateutil for datetime string parsing

OR

2) Inline python-dateutils' parsing functions

As far as I know there is no regex that can parse the full spec of ISO-8601 datetime strings.

In the meantime, this is a patch to support (valid) whitespace characters between the seconds/millseconds part and the timezone string.

Change History (15)

comment:1 by Ben Wilber, 3 years ago

Description: modified (diff)

comment:2 by Ben Wilber, 3 years ago

Description: modified (diff)

comment:3 by Ben Wilber, 3 years ago

Description: modified (diff)

comment:4 by Ben Wilber, 3 years ago

Description: modified (diff)

comment:5 by Ben Wilber, 3 years ago

Description: modified (diff)

comment:6 by Ben Wilber, 3 years ago

Description: modified (diff)

comment:7 by Ben Wilber, 3 years ago

Description: modified (diff)

comment:8 by Ben Wilber, 3 years ago

Description: modified (diff)

comment:9 by Ben Wilber, 3 years ago

Description: modified (diff)

comment:10 by Ben Wilber, 3 years ago

Description: modified (diff)

comment:11 by Claude Paroz, 3 years ago

Triage Stage: UnreviewedAccepted
Version: 3.2dev

Thanks for the patch. Ticket accepted at least to fix the space issue.

Could you please open a new thread with your rationales on the django-developers mailing list about the suggestion of depending on python-dateutil? Adding a new project dependency is no light decision, so it would need to be supported by a clear majority of developers.

comment:12 by Claude Paroz, 3 years ago

Has patch: set

in reply to:  12 comment:13 by Ben Wilber, 3 years ago

Replying to Claude Paroz:

Yes I'll start a thread on django devs. Thanks!

comment:14 by Mariusz Felisiak, 3 years ago

Owner: changed from nobody to Ben Wilber
Status: newassigned
Triage Stage: AcceptedReady for checkin

comment:15 by Mariusz Felisiak <felisiak.mariusz@…>, 3 years ago

Resolution: fixed
Status: assignedclosed

In fff4870:

Fixed #32727 -- Allowed spaces before time zone offset in parse_datetime().

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