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

Consider supporting fromisoformat as the default datetime parser #100

Open
bruno-f-cruz opened this issue Nov 15, 2024 · 2 comments · May be fixed by #123
Open

Consider supporting fromisoformat as the default datetime parser #100

bruno-f-cruz opened this issue Nov 15, 2024 · 2 comments · May be fixed by #123
Assignees
Labels

Comments

@bruno-f-cruz
Copy link

bruno-f-cruz commented Nov 15, 2024

The current parser for datetime in file names uses the following format "%Y-%m-%d_%H-%M-%S"

def datetime_to_name_string(dt: datetime) -> str:

More documentation on this choice is available here. However, reading the document I found at least 4 instances of different standards (some of which are compatible with ISO8061 which is confusing from my perspective).

This format does not conform to ISO8061 conventions since, as per the ISO:

3.2.6   Separator symbols
In date and time expressions and date and time representations, the following characters are used as separators.
“-” (hyphen)	the “-” hyphen character, in extended format, separates the time scale components for “year” and “month”, “year” and “week”, “year” and “day”, “month” and “day”, and “week” and “day”.
“:” (colon)	the “:” colon character, in extended format, seperates the time scale components for “hour” and “minute”, and “minute” and “second”.

I understand the intentions behind this choice since ":" is probably a difficult character to support (e.g. not allowed in paths).
However, using a custom parser ensures that users have to rely on this library to parse their datetimes. Instead, I propose we support the ISO8601 format by relying on the stdlib datetime.datetime.fromisoformat method. E.g.:

import datetime

tz_not_iso = "2022-04-26_11-48-09"
tz_proposed = "2022-04-26T114809"

print(datetime.datetime.fromisoformat(tz_proposed))
# 2022-04-26 11:48:09
print(datetime.datetime.fromisoformat(tz_not_iso))
# Throws error ValueError: Invalid isoformat string: '2022-04-26_11-48-09'

(I should point out that the string that I am using is not necessarily ISO8061 compliant since it mixes the compact with the extended format, I would prefer to not use "-" at all for the date portion, but people usually like to have it for readability and python seems to consider it ISO compliant, so....)

Having this implemented would allow users to use datetimes that are ISO8061 compliant instead of being forced to use the current non-ISO format.

The idea would be to modify the aforementioned parser method to have try-catch routine that uses the fromisoformat as default and falls back on the current parser to ensure backward compatiblity.

Finally, I would encourage people to use the format that is laid out on this PR where feedback for the current datetime format is being discussed.

@dyf
Copy link
Member

dyf commented Dec 19, 2024

@dbirman @saskiad we should consider this for 2.0

@dbirman dbirman self-assigned this Jan 6, 2025
@dbirman
Copy link
Member

dbirman commented Jan 6, 2025

This will be updated in v1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
3 participants