-
-
Notifications
You must be signed in to change notification settings - Fork 31.9k
datetime.fromisoformat() only accepts 3 or 6 decimal places for fractional seconds #95221
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
https://docs.python.org/3/library/datetime.html#datetime.datetime.fromisoformat
|
Except that it does support time zones. The only real limitation I have found, namely that only 3 or 6 decimal places for the fractional second is allowed, is rather arbitrary and causes the function to fail when parsing ISO date stamps produced by other systems. There is no good justification for this, and it would be easy to fix. |
I verified the bug exists in old versions of python, but when I forked, compiled, and made it from the newest source, the errors are gone. I am new to this codebase so maybe I missed something, but it seems to be fixed somehow. |
It looks like this was fixed in 3.11: Python 3.11.1 (main, Dec 23 2022, 09:28:24) [Clang 14.0.0 (clang-1400.0.29.202)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import datetime
>>> print(datetime.datetime.fromisoformat("2021-06-26T08:34:41.12-05:00"))
2021-06-26 08:34:41.120000-05:00 See GH-80010. CC: @pganssle |
As @tiran noted above, the limitation was intentional when the fromisoformat method was introduced in 3.7 and well-documented. See GH-60077. The supported format prior to 3.11 was
See datetime.fromisoformat. Since the requested behaviour is already implemented in 3.11, I am closing this issue. |
The ISO 8601 spec allows any number of decimal places for fractional seconds, but the
datetime.fromisoformat()
method raises aValueError
if there is a fractional part that has something other than 3 or 6 decimal places (I checked the source code). This causes problems with, for example, Adobe XMP metadata.This raises a
ValueError
:Tested in this environment:
The text was updated successfully, but these errors were encountered: