-
-
Notifications
You must be signed in to change notification settings - Fork 31.4k
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
Add ISO Basic format support to datetime.isoformat() and date.isoformat() #118948
Comments
Does this need dicussion on Discourse, or is the issue minor enough? |
Should it be
On other hand, adding parameters to |
I am maybe -0.5 on this feature. There is a case for putting stuff in isoformat if people are usually going to want automatic truncation, but the in the use cases put forward like filenames, you would almost certainly prefer a fixed format, so
We should definitely not do this, because ISO8601 makes no provision for arbitrary separators, and to the extent that |
First of all, note that my comments are based on ISO 8601:2004 which is superseeded by 8601:2019, which I need to buy (but I won't). I nevertheless assume that the informative parts remain the same (namely sections 1 and 2).
ISO 8601:2004 section 2.3.3 says
In this case, I would agree but this is not exactly the same as having the |
What do you mean by automatic truncation? The idea is to add an opt-in format |
When >>> dts = [datetime(2024, 3, 7, 12, 15, 30, 123456),
datetime(2024, 4, 9, 13),
datetime(2024, 5, 1, 16, 30, 2, 456123, tzinfo=timezone(timedelta(hours=5))),
datetime(2024, 6, 1, 16, 15, tzinfo=timezone(timedelta(hours=5, minutes=3, seconds=14)))]
>>> for dt in dts:
... print(dt.isoformat())
...
2024-03-07T12:15:30.123456
2024-04-09T13:00:00
2024-05-01T16:30:02.456123+05:00
2024-06-01T16:15:00+05:03:14
>>> for dt in dts:
... print(dt.strftime("%Y-%m-%dT%H:%M:%S.%f%z"))
2024-03-07T12:15:30.123456
2024-04-09T13:00:00.000000
2024-05-01T16:30:02.456123+0500
2024-06-01T16:15:00.000000+050314 The main reasons to use I don't think we should automatically say
I suppose you could use |
How about
|
The term basic is the term in ISO standards and shoud be left as is IMO (if we were to support it). |
I agree with @picnixz, the name here is not the problem. If the survey on the API for outputting That said, almost everyone will have to google that term. I have read ISO 8601 several times, and I implemented two mostly full-featured ISO 8601 parsers, and I had to look up the term to see if it was an official term. No one is going to know what I think the main blocker here is that there's no compelling use case (and there actually kind of is a compelling use case for #90772, and we still didn't do that one because we couldn't come up with a non-confusing UX for it). |
The motivation for the ISO basic format is the same as the extended format - that it is a standardized machine-readable format that ensures seamless interoperability. You do not get that with many potentially subtly incorrect strftime/strptime implementations, as doing it right requires reading and implementing the spec correctly. That machinery is already implemented in Python, and you can also specify your desired granularity with
IMO, unless one has the specification table memorized, I don't think "ISO but without - and :" would be more of a cognitive load than figuring out what |
Or |
I don't agree that you would need to craft pairs, because as long as you are spec compliant |
Feature or enhancement
Proposal:
In additional to the popular ISO 8601 Extended format, there's also an ISO 8601 Basic format for datetimes which is useful for filenames and URL components as it avoids characters such as eg. colon and is more compact.
datetime.fromisoformat
already supports parsing this format.Example code:
Has this already been discussed elsewhere?
This is a minor feature, which does not need previous discussion elsewhere
Links to previous discussion of this feature:
No response
Linked PRs
datetime
#120553The text was updated successfully, but these errors were encountered: