Opened 11 years ago

Closed 6 months ago

#11385 closed New feature (fixed)

DateTimeField doesn't accept ISO 8601 formatted date string

Reported by: Jani Tiainen Owned by: Claude Paroz
Component: Forms Version: master
Severity: Normal Keywords: datetime orm format
Cc: bronger@…, W. Trevor King 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

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 (11)

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

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 10 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 4 years 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 4 years 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 4 years ago by Torsten Bronger

Cc: bronger@… added

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

comment:6 Changed 4 years ago by Simon Charette

Resolution: wontfix
Status: closednew

comment:7 Changed 4 years 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.

comment:8 Changed 2 years ago by W. Trevor King

Cc: W. Trevor King added

comment:9 Changed 9 months ago by Claude Paroz

Has patch: set

Interest for this is revived by the HTML5 <input type="datetime-local"> which is sending input formatted with ISO 8601.

PR

comment:10 Changed 6 months ago by felixxm

Owner: changed from nobody to Claude Paroz
Status: newassigned
Triage Stage: AcceptedReady for checkin

comment:11 Changed 6 months ago by Mariusz Felisiak <felisiak.mariusz@…>

Resolution: fixed
Status: assignedclosed

In 1487f16f:

Fixed #11385 -- Made forms.DateTimeField accept ISO 8601 date inputs.

Thanks José Padilla for the initial patch, and Carlton Gibson for the
review.

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