Skip to content
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

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

Conversation

blhsing
Copy link
Contributor

@blhsing blhsing commented Jun 21, 2024

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.

Copy link
Member

@serhiy-storchaka serhiy-storchaka left a comment

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.

Modules/_datetimemodule.c Outdated Show resolved Hide resolved
@blhsing
Copy link
Contributor Author

blhsing commented Jun 25, 2024

Note that %G also needs a fix.

Right. It's now accounted for. The calculation of ISO-8601 year for '%G' isn't straightforward so I'm obtaining it from the C strftime instead.

Copy link
Member

@serhiy-storchaka serhiy-storchaka left a comment

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.

Modules/_datetimemodule.c Outdated Show resolved Hide resolved
Modules/_datetimemodule.c Outdated Show resolved Hide resolved
Modules/_datetimemodule.c Outdated Show resolved Hide resolved
Modules/_datetimemodule.c Outdated Show resolved Hide resolved
blhsing and others added 2 commits June 29, 2024 08:42
Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com>
@blhsing
Copy link
Contributor Author

blhsing commented Jun 29, 2024

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
36 checks passed
@miss-islington-app
Copy link

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

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
Copy link

bedevere-app bot commented Jun 29, 2024

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
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
Copy link

bedevere-app bot commented Jun 29, 2024

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 bug and 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

⚠️⚠️⚠️ Buildbot failure ⚠️⚠️⚠️

Hi! The buildbot AMD64 Ubuntu NoGIL Refleaks 3.13 has failed when building commit 9f6f879.

What do you need to do:

  1. Don't panic.
  2. Check the buildbot page in the devguide if you don't know what the buildbots are or how they work.
  3. Go to the page of the buildbot that failed (https://buildbot.python.org/#builders/1431/builds/335) and take a look at the build logs.
  4. Check if the failure is related to this commit (9f6f879) or if it is a false positive.
  5. If the failure is related to this commit, please, reflect that on the issue and make a new Pull Request with a fix.

You can take a look at the buildbot page here:

https://buildbot.python.org/#builders/1431/builds/335

Failed tests:

  • test_pickle

Test leaking resources:

  • test_pickle: memory blocks
  • test_pickle: references

Summary of the results of the build (if available):

==

Click to see traceback logs
remote: Enumerating objects: 10, done.        
remote: Counting objects:  10% (1/10)        
remote: Counting objects:  20% (2/10)        
remote: Counting objects:  30% (3/10)        
remote: Counting objects:  40% (4/10)        
remote: Counting objects:  50% (5/10)        
remote: Counting objects:  60% (6/10)        
remote: Counting objects:  70% (7/10)        
remote: Counting objects:  80% (8/10)        
remote: Counting objects:  90% (9/10)        
remote: Counting objects: 100% (10/10)        
remote: Counting objects: 100% (10/10), done.        
remote: Compressing objects:  12% (1/8)        
remote: Compressing objects:  25% (2/8)        
remote: Compressing objects:  37% (3/8)        
remote: Compressing objects:  50% (4/8)        
remote: Compressing objects:  62% (5/8)        
remote: Compressing objects:  75% (6/8)        
remote: Compressing objects:  87% (7/8)        
remote: Compressing objects: 100% (8/8)        
remote: Compressing objects: 100% (8/8), done.        
remote: Total 10 (delta 2), reused 5 (delta 2), pack-reused 0        
From https://github.com/python/cpython
 * branch                  3.13       -> FETCH_HEAD
Note: switching to '9f6f8790ef39fd035f6ada1a9dfcb47d58d08fea'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:

  git switch -c <new-branch-name>

Or undo this operation with:

  git switch -

Turn off this advice by setting config variable advice.detachedHead to false

HEAD is now at 9f6f8790ef Revert "[3.13] gh-120713: Normalize year with century for datetime.strftime (GH-120820) (GH-121144)" (GH-122408)
Switched to and reset branch '3.13'

configure: WARNING: no system libmpdecimal found; falling back to bundled libmpdecimal (deprecated and scheduled for removal in Python 3.15)

make: *** [Makefile:2264: buildbottest] Error 2

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