classification
Title: Format issue with strftime and %Y
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.10, Python 3.9
process
Status: closed Resolution: duplicate
Dependencies: Superseder:
Assigned To: Nosy List: bkaznowski, iritkatriel
Priority: normal Keywords:

Created on 2021-07-12 15:23 by bkaznowski, last changed 2021-07-19 15:38 by bkaznowski. This issue is now closed.

Messages (3)
msg397329 - (view) Author: Bartosz Kaznowski (bkaznowski) Date: 2021-07-12 15:23
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.
msg397707 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-07-17 08:32
See also Issue13305.
msg397808 - (view) Author: Bartosz Kaznowski (bkaznowski) Date: 2021-07-19 15:38
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!
History
Date User Action Args
2021-07-19 15:38:10bkaznowskisetstatus: open -> closed
resolution: duplicate
messages: + msg397808

stage: resolved
2021-07-17 08:32:51iritkatrielsetnosy: + iritkatriel
messages: + msg397707
2021-07-17 03:35:54terry.reedysetversions: - Python 3.6, Python 3.7, Python 3.8
2021-07-12 18:15:53bkaznowskisetversions: + Python 3.6, Python 3.7, Python 3.8, Python 3.10
2021-07-12 18:10:20bkaznowskisetversions: + Python 3.9, - Python 3.7
2021-07-12 15:23:50bkaznowskicreate