Opened 7 years ago

Last modified 3 weeks ago

#11385 new New feature

DateTimeField doesn't accept ISO 8601 formatted date string

Reported by: Jani Tiainen Owned by: nobody
Component: Forms Version: master
Severity: Normal Keywords: datetime orm format
Cc: bronger@… Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

DateTimeField doesn't accept ISO 8601 formatted date string. Differene is that ISO format allows date and time separator to be capital T letter. (Format being YYYY-MM-DDTHH:MM:SS. Django expects to have only space as a date and time separator.

Change History (7)

comment:1 Changed 7 years ago by Russell Keith-Magee

Needs documentation: unset
Needs tests: unset
Patch needs improvement: unset
Resolution: wontfix
Status: newclosed

ISO8601 is a good machine format, but not a particularly nice human readable format. Form processing is primarily about human-readable input.

If you disagree, the DateTimeField input formats are configurable (DATETIME_INPUT_FORMATS), so you can add ISO8601 format in your own projects if you want.

comment:2 Changed 6 years ago by Mikhail Korobov

I think the problem can't be resolved with DATETIME_INPUT_FORMATS tweaking.

ISO8601 format allows timezone info: '2010-09-01T19:52:15+04:00'. Such strings can't be parsed with python's strptime because python's strptime doesn't support '%z' format char (http://bugs.python.org/issue6641). So DATETIME_INPUT_FORMATS directive is not helpful for ISO8601 handling.

The solution is probably to use custom form field.

comment:3 in reply to:  1 Changed 7 months ago by Matthew Laney

Easy pickings: unset
Severity: Normal
Type: Uncategorized
UI/UX: unset

Replying to russellm:

ISO8601 is a good machine format, but not a particularly nice human readable format. Form processing is primarily about human-readable input.

If you disagree, the DateTimeField input formats are configurable (DATETIME_INPUT_FORMATS), so you can add ISO8601 format in your own projects if you want.

Hi Russell,

I understand your reasoning at the time this was closed for not supporting the T separator. However, this is not relevant again because of the way HTML5 provides the new Input Types. By default, using the datetime-local Input Type results in the format of YYYY-MM-DDTHH:MM. It would definitely make it nice to allow for that Input Type default to work properly with DateTimeField.

comment:4 Changed 7 months ago by Claude Paroz

Component: Database layer (models, ORM)Forms
Triage Stage: UnreviewedAccepted
Type: UncategorizedNew feature
Version: 1.0master

Reopening, considering comment:3, Python issue 6641 being fixed on Python 3 and the presence of django.utils.formats.ISO_INPUT_FORMATS.

comment:5 Changed 5 months ago by Torsten Bronger

Cc: bronger@… added

Note that this is not yet really reopened. An oversight?

comment:6 Changed 5 months ago by Simon Charette

Resolution: wontfix
Status: closednew

comment:7 Changed 3 weeks ago by julen

As kmike mentioned above, customizing the form field with an input_formats containing the timezone marker %z doesn't help — at least on Python 2.7. For anyone hitting this, I worked around it by using a custom form field and overriding the strptime method:

from django.utils.dateparse import parse_datetime
from django.utils.encoding import force_str


class ISODateTimeField(forms.DateTimeField):

    def strptime(self, value, format):
        return parse_datetime(force_str(value))

I use Django's own parse_datetime utility. Note this is limited to ISO datetimes, and effectively any input_formats are omitted.

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