Skip to content
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

datetime.fromisoformat() accepts invalid ISO 8601 timestamps #115783

Open
Paebbels opened this issue Feb 21, 2024 · 3 comments
Open

datetime.fromisoformat() accepts invalid ISO 8601 timestamps #115783

Paebbels opened this issue Feb 21, 2024 · 3 comments
Labels
type-bug An unexpected behavior, bug, or error

Comments

@Paebbels
Copy link

Paebbels commented Feb 21, 2024

Bug report

Bug description:

Since Python 3.11, datetime.fromisoformat(...) accepts invalid timestamps. The same code was tested in many Python versions via GitHub Actions:

Wrong format: 2024-01-17T15:21:00-0800
Fixed format: 2024-01-17T15:21:00-08:00

Python code to check:

datetime.fromisoformat("2024-01-17T15:21:00-0800")

ISO 8601 defines 2 formats:

  • basic format (in short: without separators) and
  • extended format (with separators).

When checking the EBNF rules, all parts of the timestamp must be with or without separators. A mix of formats is not allowed.

I haven't checked for other basic and extended format mixes.
My CI checked many Python versions and platforms. Here a failed CI pipeline, shows a correct implementation. A successful pipeline denotes a buggy Python version.
image

Other resources:

CPython versions tested on:

3.8, 3.9, 3.10, 3.11, 3.12, 3.13

Operating systems tested on:

Linux, macOS, Windows

@Paebbels Paebbels added the type-bug An unexpected behavior, bug, or error label Feb 21, 2024
@skoehler
Copy link

skoehler commented Feb 23, 2024

I'd like to extend the list of accepted invalid timestamps:

2024-01-17T15:21:00-0800
2024-01-17T152100-08:00
20240117T15:21:00-08:00

2024-01-17T152100-0800
20240117T15:21:00-0800
20240117T152100-08:00

All these should be rejected by fromisoformat().
I can report however, that removing just a single dash or a single colon from the date or time is properly rejected.

@TooPercentMilk
Copy link

I'm a beginner contributor and I'd like to work on this issue. How can I move forward?

@maxcyd
Copy link

maxcyd commented Apr 26, 2024

It is worth noting, the test_datetime defined within datetimetester.py test assumes inconsistent typing is acceptable. For example, '20250102T03:04:05,6' is considered an acceptable string to the datetime.isoformat() function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-bug An unexpected behavior, bug, or error
Projects
Development

No branches or pull requests

4 participants