classification
Title: Improve ISO 8601 timezone support in the datetime.fromisoformat() method
Type: enhancement Stage: resolved
Components: Versions:
process
Status: closed Resolution: duplicate
Dependencies: Superseder: datetime: parse "Z" timezone suffix in fromisoformat()
View: 35829
Assigned To: Nosy List: belopolsky, p-ganssle, zeth
Priority: normal Keywords:

Created on 2019-08-27 12:07 by zeth, last changed 2019-08-28 05:36 by zeth. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 15547 closed zeth, 2019-08-27 12:07
Messages (3)
msg350630 - (view) Author: Zeth (zeth) * Date: 2019-08-27 12:07
The datetime.datetime.fromisoformat() method unnecessarily rejects datetime strings that are valid under ISO 8601 if timezone uses the UTC  designator or it only has hours.

In ISO 8601, section 4.2.5.1: "When it is required to indicate the difference between local time and UTC of day, the representation of the difference can be expressed in hours and minutes, or hours only."

And Section 4.2.4, UTC shall be expressed "by the UTC designator [Z]".

A key use case of the latter is being able to parse JavaScript Date objects (e.g. dates that have come from a web frontend or a JSON document). This considerably improves the usefulness of the datetime.fromisoformat method.
msg350633 - (view) Author: Paul Ganssle (p-ganssle) * (Python committer) Date: 2019-08-27 14:36
This is a duplicate of #35829.

The reason that 'Z' is not supported is that `fromisoformat()` is not a general ISO 8601 parser, but rather is intended to be the inverse of `isoformat()`. See the documentation here: https://docs.python.org/dev/library/datetime.html#datetime.datetime.fromisoformat

The current state of #35829 is that expanding to support all of ISO 8601 is an option, but determining the scope an the API are a bit tricky. ISO 8601 is more complicated than most people think.

In the meantime, `dateutil.parser.isoparse` is intentionally scoped to parse all valid ISO 8601 datetimes.
msg350658 - (view) Author: Zeth (zeth) * Date: 2019-08-28 05:36
Did you read the attached PR? For three lines longer, the method becomes more generally useful now.

As pointed out in the other thread that you pointed to, Javascript can parse Python's seralised datetime object but Python cannot parse Javascript's due to the fact that you don't want to.

That other thread points to a third thread that starts in 2012 so it seems that you really think that is a feature rather than a bug.

The name of the method is fromisoformat, not "parse our internal home made format". The line in the documentation seems a bit weird to me to be honest. It is saying that we could make it generally useful for you user but we don't want to.

The reply to this bug, "use the dateutil.parser.isoparse that I maintain instead" is beside the point, we are talking about the standard library one. Why not put that in the standard library then if it can actually parse iso 8601 format?
History
Date User Action Args
2019-08-28 05:36:13zethsetmessages: + msg350658
2019-08-27 14:36:37p-gansslesetstatus: open -> closed
superseder: datetime: parse "Z" timezone suffix in fromisoformat()
messages: + msg350633

type: enhancement
resolution: duplicate
stage: resolved
2019-08-27 12:16:29xtreaksetnosy: + belopolsky, p-ganssle
2019-08-27 12:07:38zethcreate