-
-
Notifications
You must be signed in to change notification settings - Fork 30.7k
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.strptime
accepts incompatible arguments
#124549
Comments
I'd be ok with raising exception, seems like it's a user error for passing these kind of conflicting format specifiers. We could also start documenting this case, saying that the latest format specifiers that appears will prevail. |
To avoid the issue where the last directive overwrites earlier values: |
This is an example of what I'm getting at: def parse_date(input_str):
if len(input_str) == 6: # 4-digit year followed by 2-digit year
full_year = input_str[:4]
short_year = input_str[4:]
year_full = datetime.strptime(full_year, "%Y")
year_short = datetime.strptime(short_year, "%y")
return year_short.replace(year=year_full.year) # Use full year from the first part
elif len(input_str) == 4: # Only 4-digit year
return datetime.strptime(input_str, "%Y")
else:
raise ValueError("Invalid date format") Example usage:input_str = "200099"
parsed_date = parse_date(input_str)
print(parsed_date) # datetime.datetime(1999, 1, 1, 0, 0) |
Bug report
Bug description:
If you over-specify a datetime strptime string, it seems that
datetime.strptime
when encountering a component that has been specified twice, will overwrite earlier values with more recent ones.One approach would be to ban over-specified formats, but that would be a problem for someone who wants to, use a format like "%M %B" (e.g. something sortable by month number while also showing month name), so I think we don't want to do that.
Alternatively we could start raising an exception if you get different values from these two things, or possibly a warning.
Or we can just accept that if you use a weird format you should expect weird behavior.
CPython versions tested on:
CPython main branch
Operating systems tested on:
Linux
Linked PRs
The text was updated successfully, but these errors were encountered: