Opened 16 years ago

Closed 5 years 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: dev
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 by Russell Keith-Magee, 15 years ago

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 by Mikhail Korobov, 14 years ago

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.

in reply to:  1 comment:3 by Matthew Laney, 9 years ago

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 by Claude Paroz, 9 years ago

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 by Torsten Bronger, 9 years ago

Cc: bronger@… added

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

comment:6 by Simon Charette, 9 years ago

Resolution: wontfix
Status: closednew

comment:7 by julen, 8 years ago

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 by W. Trevor King, 7 years ago

Cc: W. Trevor King added

comment:9 by Claude Paroz, 5 years ago

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 by Mariusz Felisiak, 5 years ago

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

comment:11 by Mariusz Felisiak <felisiak.mariusz@…>, 5 years ago

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