gh-120713: normalize year with century for datetime.strftime by blhsing · Pull Request #120820 · python/cpython · GitHub
Skip to content

gh-120713: normalize year with century for datetime.strftime - #120820

Merged
serhiy-storchaka merged 56 commits into
python:mainfrom
blhsing:normalize-datetime-strftime-year-with-century
Jun 29, 2024
Merged

serhiy-storchaka merged 56 commits into
python:mainfrom
blhsing:normalize-datetime-strftime-year-with-century

Conversation

@blhsing

@blhsing blhsing commented Jun 21, 2024

Copy link
Copy Markdown
Contributor

On some platforms such as Linux, datetime.strftime does not 0-pad a year <= 999 when formatting with '%Y' despite the documentation claiming an example output of "0001, 0002, …". The same issue applies when formatting with "%G".

This PR fixes the issue by formatting a year with century with '%04ld' using sprintf (in C) or '{:04}' using str.format (in Python) if the platform is found to show the aforementioned behavior.

@serhiy-storchaka serhiy-storchaka left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that %G also needs a fix.

Comment thread Modules/_datetimemodule.c Outdated
@blhsing

blhsing commented Jun 25, 2024

Copy link
Copy Markdown
Contributor Author

@serhiy-storchaka serhiy-storchaka left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for addressing my comments.

I think that this is the right way to fix this problem, but we should still get confirmation from other core developers.

The code is much more complicated now with the support of "%G", and the C code has some bugs.

Comment thread Modules/_datetimemodule.c Outdated
Comment thread Modules/_datetimemodule.c Outdated
Comment thread Modules/_datetimemodule.c Outdated
Comment thread Modules/_datetimemodule.c Outdated
@blhsing

blhsing commented Jun 29, 2024

Copy link
Copy Markdown
Contributor Author

Thanks for enduring my nitpicking, and thanks for the PR :)

Thank you so much for taking the time to point me in the right direction!

@serhiy-storchaka
serhiy-storchaka merged commit 6d34938 into python:main Jun 29, 2024
@miss-islington-app

Copy link
Copy Markdown

Thanks @blhsing for the PR, and @serhiy-storchaka for merging it 🌮🎉.. I'm working now to backport this PR to: 3.12, 3.13.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jun 29, 2024
…ythonGH-120820)

(cherry picked from commit 6d34938)

Co-authored-by: blhsing <blhsing@gmail.com>
@miss-islington-app

Copy link
Copy Markdown

Sorry, @blhsing and @serhiy-storchaka, I could not cleanly backport this to 3.12 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker 6d34938dc8163f4a4bcc68069a1645a7ab76e935 3.12

@bedevere-app

bedevere-app Bot commented Jun 29, 2024

Copy link
Copy Markdown

GH-121144 is a backport of this pull request to the 3.13 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.13 bugs and security fixes label Jun 29, 2024
@serhiy-storchaka

Copy link
Copy Markdown
Member

Thank you for your work and patience @blhsing. My apologies for our too picky reviews.

serhiy-storchaka pushed a commit to serhiy-storchaka/cpython that referenced this pull request Jun 29, 2024
…time (pythonGH-120820)

(cherry picked from commit 6d34938)

Co-authored-by: blhsing <blhsing@gmail.com>
@bedevere-app

bedevere-app Bot commented Jun 29, 2024

Copy link
Copy Markdown

GH-121145 is a backport of this pull request to the 3.12 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.12 only security fixes label Jun 29, 2024
serhiy-storchaka added a commit that referenced this pull request Jun 29, 2024
…H-120820) (GH-121145)

(cherry picked from commit 6d34938)

Co-authored-by: blhsing <blhsing@gmail.com>
serhiy-storchaka pushed a commit that referenced this pull request Jun 29, 2024
…H-120820) (GH-121144)

(cherry picked from commit 6d34938)

Co-authored-by: blhsing <blhsing@gmail.com>
noahbkim pushed a commit to hudson-trading/cpython that referenced this pull request Jul 11, 2024
serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this pull request Jul 29, 2024
serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this pull request Jul 29, 2024
serhiy-storchaka added a commit that referenced this pull request Jul 29, 2024
serhiy-storchaka added a commit that referenced this pull request Jul 29, 2024
@bedevere-bot

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants