Skip to content

Format issue with strftime and %Y #88776

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

Closed
bkaznowski mannequin opened this issue Jul 12, 2021 · 3 comments
Closed

Format issue with strftime and %Y #88776

bkaznowski mannequin opened this issue Jul 12, 2021 · 3 comments
Labels
3.9 only security fixes 3.10 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@bkaznowski
Copy link
Mannequin

bkaznowski mannequin commented Jul 12, 2021

BPO 44610
Nosy @iritkatriel, @bkaznowski

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2021-07-19.15:38:10.378>
created_at = <Date 2021-07-12.15:23:50.524>
labels = ['type-bug', 'library', '3.9', '3.10']
title = 'Format issue with strftime and %Y'
updated_at = <Date 2021-07-19.15:38:10.377>
user = 'https://github.com/bkaznowski'

bugs.python.org fields:

activity = <Date 2021-07-19.15:38:10.377>
actor = 'bkaznowski'
assignee = 'none'
closed = True
closed_date = <Date 2021-07-19.15:38:10.378>
closer = 'bkaznowski'
components = ['Library (Lib)']
creation = <Date 2021-07-12.15:23:50.524>
creator = 'bkaznowski'
dependencies = []
files = []
hgrepos = []
issue_num = 44610
keywords = []
message_count = 3.0
messages = ['397329', '397707', '397808']
nosy_count = 2.0
nosy_names = ['iritkatriel', 'bkaznowski']
pr_nums = []
priority = 'normal'
resolution = 'duplicate'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue44610'
versions = ['Python 3.9', 'Python 3.10']

@bkaznowski
Copy link
Mannequin Author

bkaznowski mannequin commented Jul 12, 2021

When you convert a date pre the year 1000 to a string with %Y as the formatter and then back to a date again then it fails. This is because %Y expects it to be formatted with leading zeroes. For example, the year 1/01/01 (yyyy/mm/dd) should be 0001/01/01 when formatting using %Y/%m/%d. However, %Y returns 1.
You can see this in action here:

from datetime import date, datetime
format = "%Y-%m-%d"
formatted = date.min.strftime("%Y-%m-%d")
datetime.strptime(formatted, format).date()

ValueError: time data '1-01-01' does not match format '%Y-%m-%d' is raised on the forth line.

@bkaznowski bkaznowski mannequin added 3.7 (EOL) end of life stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error 3.9 only security fixes 3.8 (EOL) end of life 3.10 only security fixes and removed 3.7 (EOL) end of life labels Jul 12, 2021
@terryjreedy terryjreedy removed 3.7 (EOL) end of life 3.8 (EOL) end of life labels Jul 17, 2021
@iritkatriel
Copy link
Member

See also bpo-13305.

@bkaznowski
Copy link
Mannequin Author

bkaznowski mannequin commented Jul 19, 2021

Ok, thanks @iritkatriel. I have closed it as a duplicate.

I did notice after posting this that in the cpython source code people work around this issue. Now I can see why.

I guess I will just have to work around this issue too. Luckily it doesn't cause too many problems for what I need.

Thanks!

@bkaznowski bkaznowski mannequin closed this as completed Jul 19, 2021
@bkaznowski bkaznowski mannequin closed this as completed Jul 19, 2021
@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.9 only security fixes 3.10 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

2 participants