Skip to content

strftime does not always return the date in the expected format #111817

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
jubuehrer opened this issue Nov 7, 2023 · 3 comments
Closed

strftime does not always return the date in the expected format #111817

jubuehrer opened this issue Nov 7, 2023 · 3 comments
Labels
type-bug An unexpected behavior, bug, or error

Comments

@jubuehrer
Copy link

jubuehrer commented Nov 7, 2023

Bug report

Bug description:

now = datetime.datetime.now().date()
lol = datetime.date(22, 11, 11)

lol.strftime("%Y-%m-%d") # -> '22-11-11' / This is not correct, it should be '0022-11-11'
now.strftime("%Y-%m-%d") # -> '2023-11-07'
lol.strftime("%y-%m-%d") # -> '22-11-11' / Now its correct

When using %Y in the format I would expect that the year in the return value has a length of four digits.
When converting the string back to a date using strptime() with the format %Y-%m-%d it does crash because it expects the string to have four digits.

CPython versions tested on:

3.11

Operating systems tested on:

Linux

@jubuehrer jubuehrer added the type-bug An unexpected behavior, bug, or error label Nov 7, 2023
@jubuehrer
Copy link
Author

Maybe the bug is not in strftime but in strptime because it should interpret the year 22 as 22 instead of throwing the error ValueError: time data '22-11-11' does not match format '%Y-%m-%d'?

@sobolevn
Copy link
Member

sobolevn commented Nov 7, 2023

Docs clearly state that it should be 0022 for %Y:
Снимок экрана 2023-11-07 в 14 40 40

https://docs.python.org/3/library/datetime.html#strftime-and-strptime-format-codes

And it works correctly for 3.13:

Python 3.13.0a1+ (heads/issue-111495-dirty:340d25676be, Nov  5 2023, 14:21:42) [Clang 15.0.0 (clang-1500.0.40.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import datetime
>>> datetime.date(22, 11, 11).strftime("%Y-%m-%d")
'0022-11-11'

and for 3.11.5:

Python 3.11.5 (main, Sep  5 2023, 10:34:31) [Clang 14.0.3 (clang-1403.0.22.14.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import datetime
>>> datetime.date(22, 11, 11).strftime("%Y-%m-%d")
'0022-11-11'

@sobolevn sobolevn added the pending The issue will be closed if no feedback is provided label Nov 7, 2023
@zware
Copy link
Member

zware commented Nov 7, 2023

This is a glibc issue. @sobolevn showed that Darwin (macOS) is unaffected, here's Windows:

Python 3.11.3 (tags/v3.11.3:f3909b8, Apr  4 2023, 23:49:59) [MSC v.1934 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import datetime
>>> datetime.datetime(22, 11, 11).strftime('%Y-%m-%d')
'0022-11-11'

FreeBSD:

Python 3.9.18 (main, Oct  3 2023, 01:17:31) 
[Clang 14.0.5 (https://github.com/llvm/llvm-project.git llvmorg-14.0.5-0-gc1238 on freebsd13
Type "help", "copyright", "credits" or "license" for more information.
>>> import datetime
>>> datetime.datetime(22, 11, 11).strftime('%Y-%m-%d')
'0022-11-11'

musl:

$ docker run -it --rm python:3.11-alpine
Python 3.11.6 (main, Oct 19 2023, 05:43:48) [GCC 12.2.1 20220924] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import datetime
>>> datetime.datetime(22, 11, 11).strftime('%Y-%m-%d')
'0022-11-11'

cosmo:

$ ./o/third_party/python/python.com
Python 3.6.14+ (Actually Portable Python) [GCC 11.2.0] on cosmo
Type "help", "copyright", "credits" or "license" for more information.
>>: import datetime
>>: datetime.datetime(22, 11, 11).strftime('%Y-%m-%d')
'0022-11-11'

And finally, glibc:

$ docker run -it --rm python:3.11-slim
Python 3.11.6 (main, Nov  1 2023, 13:45:43) [GCC 12.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import datetime
>>> datetime.datetime(22, 11, 11).strftime('%Y-%m-%d')
'22-11-11'
>>> datetime.datetime(22, 11, 11).strftime('%4Y-%m-%d')
'0022-11-11'

See also gh-57514, gh-88776, and probably others. Closing as duplicate of gh-57514.

@zware zware closed this as not planned Won't fix, can't repro, duplicate, stale Nov 7, 2023
@zware zware removed the pending The issue will be closed if no feedback is provided label Nov 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

3 participants