Skip to content

gh-137165: Standardized non-zero-padded date formatting for datetime.strftime - #139088

Open
F0RRZZ wants to merge 20 commits into
python:mainfrom
F0RRZZ:issue-137165
Open

F0RRZZ wants to merge 20 commits into
python:mainfrom
F0RRZZ:issue-137165

Conversation

@F0RRZZ

@F0RRZZ F0RRZZ commented Sep 18, 2025 •

Copy link
Copy Markdown
Contributor

Previously, using non-zero-padded directives (e.g., %-d) in strftime caused test failures on Windows and Android (x86_64), while other platforms worked as expected.
This has been fixed by manually handling such specifiers on these platforms.
Corresponding tests have been added to strftime tests.

On Windows using non-zero-padded directives raised ValueError.

On Android (x86_64) dicrectives were not replaced:
Screenshot 2025-09-18 at 03 45 28

@F0RRZZ F0RRZZ changed the title gh-137165: Add non-zero-padded Windows support for datetime.strftime gh-137165: Standardized non-zero-padded date formatting for datetime.strftime Sep 18, 2025
@mhsmith

mhsmith commented Sep 18, 2025

Copy link
Copy Markdown
Member

!buildbot iOS

@bedevere-bot

This comment was marked as outdated.

Comment thread Lib/test/datetimetester.py
@mhsmith

mhsmith commented Sep 18, 2025

Copy link
Copy Markdown
Member

The implementation looks reasonable, but this should also be covered in the documentation, along with the fact that the - flag apparently has no effect on %y on Apple platforms.

@F0RRZZ

F0RRZZ commented Sep 18, 2025

Copy link
Copy Markdown
Contributor Author

@mhsmith I adapted the tests for iOS and added documentation in the strftime notes.

@F0RRZZ
F0RRZZ requested a review from mhsmith September 18, 2025 14:39
Comment thread Doc/library/datetime.rst Outdated
Comment thread Doc/library/datetime.rst Outdated
Comment thread Lib/test/datetimetester.py Outdated
Comment thread Lib/test/datetimetester.py Outdated
Comment thread Lib/test/datetimetester.py
@F0RRZZ

F0RRZZ commented Sep 18, 2025

Copy link
Copy Markdown
Contributor Author

@StanFromIreland Made all changes

@mhsmith

This comment was marked as resolved.

@bedevere-bot

This comment was marked as outdated.

Comment thread Doc/library/datetime.rst Outdated
Comment thread Doc/library/datetime.rst Outdated
Comment thread Lib/_pydatetime.py Outdated
@F0RRZZ

F0RRZZ commented Sep 19, 2025

Copy link
Copy Markdown
Contributor Author

@mhsmith All done, but TSan seems to be stuck

@F0RRZZ
F0RRZZ requested a review from mhsmith September 19, 2025 05:46
Comment thread Doc/library/datetime.rst Outdated
@bedevere-bot

Copy link
Copy Markdown

🤖 New build scheduled with the buildbot fleet by @mhsmith for commit 9212a5a 🤖

Results will be shown at:

https://buildbot.python.org/all/#/grid?branch=refs%2Fpull%2F139088%2Fmerge

If you want to schedule another build, you need to add the 🔨 test-with-buildbots label again.

@bedevere-bot bedevere-bot removed the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Oct 1, 2025

@mhsmith mhsmith 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.

@pganssle: I think this is ready for final review.

@F0RRZZ

F0RRZZ commented Nov 5, 2025 •

Copy link
Copy Markdown
Contributor Author

@pganssle Just a gentle ping on this PR review🙏

Comment thread Lib/_pydatetime.py
@github-actions

Copy link
Copy Markdown

This PR is stale because it has been open for 30 days with no activity.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label Apr 29, 2026
@F0RRZZ

F0RRZZ commented Jul 28, 2026

Copy link
Copy Markdown
Contributor Author

@StanFromIreland gentle ping
should be good for another look whenever you have a sec

@github-actions github-actions Bot removed the stale Stale PR or inactive for long period of time. label Jul 30, 2026
@ambv

ambv commented Oct 20, 2026

Copy link
Copy Markdown
Contributor

This PR is superseded by #145018, a newer open pull request for the same issue (gh-137165), and it no longer merges cleanly. Closing to cull the number of open pull requests. Do not reopen this pull request without getting a go-ahead from a triager or core developer.

@ambv ambv closed this Oct 20, 2026
@F0RRZZ

F0RRZZ commented Oct 21, 2026

Copy link
Copy Markdown
Contributor Author

unified the behavior for unsupported specifiers like on Linux.

I'd rather leave that to separate PR.

@StanFromIreland Since we decided to split this task into two PRs, could you please reopen this one?

@bedevere-app bedevere-app Bot added the type-feature A feature request or enhancement label Oct 21, 2026
@read-the-docs-community

read-the-docs-community Bot commented Oct 21, 2026 •

Copy link
Copy Markdown

Documentation build overview

📚 cpython-previews | 🛠️ Build #35361199 | 📁 Comparing ddf17ec against main (b577a54)

  🔍 Preview build  

61 files changed · + 1 added · ± 60 modified

+ Added

± Modified

ambv added 2 commits October 21, 2026 10:21
Co-authored-by: Muse Code (Meta Muse Spark)
On Windows and Android, every `%-X` was emulated by formatting `%X`
and stripping leading zeros.  That misbehaved for anything outside the
documented numeric codes: non-ASCII was narrowed to a byte (`%-Ť`
formatted the day, `%-é` raised OverflowError), `%-%` stuffed an
unescaped `%` into the format (`%-%d` gave the day), and `%-z`/`%-Z`
bypassed datetime's tzinfo handling.  Emulate only `%-d`, `%-m`,
`%-H`, `%-I`, `%-M`, `%-S`, `%-j`, `%-U`, `%-W`, `%-V` and `%-y`, and
leave everything else to the platform as before.

Gate the pure-Python branch the same way, so it no longer consumes
`%-%` as a unit on other platforms, which made `%-%f` differ from the
C implementation.  Use the cached time.strftime instead of importing it
on every call.

Co-authored-by: Muse Code (Meta Muse Spark)
@StanFromIreland

Copy link
Copy Markdown
Member

@ambv, this will require some careful review before merging, and Buildbots will be handy too.

@ambv

ambv commented Oct 21, 2026

Copy link
Copy Markdown
Contributor

@StanFromIreland agreed! My changes are effect of my review. I'll get somebody at the sprint to look at it before we land it.

@ambv ambv added the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Oct 21, 2026
@bedevere-bot

Copy link
Copy Markdown

🤖 New build scheduled with the buildbot fleet by @ambv for commit f74ad49 🤖

Results will be shown at:

https://buildbot.python.org/all/#/grid?branch=refs%2Fpull%2F139088%2Fmerge

If you want to schedule another build, you need to add the 🔨 test-with-buildbots label again.

@bedevere-bot bedevere-bot removed the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Oct 21, 2026
Emulate documented numeric dash flags on OpenBSD and account for NetBSD's
native zero padding of the short year. Cover 12-hour formatting at midnight,
noon and single-digit hours, document the version introducing support, and
include datetime.time.strftime in the NEWS entry.

Co-authored-by: Muse Code (Meta Muse Spark)

@gpshead gpshead 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.

@ambv asked me to run this through Claude as a font-of-other-platforms-knowledge reviewer looking for defects. findings:

  1. Test gating by platform.system() is wrong for some Apple platforms (Lib/test/datetimetester.py:1603). platform.system() returns 'iPadOS' on iPads, and tvOS/watchOS/visionOS have their own names too. Those fall into the else branch expecting y:5, where Apple libc gives y:05. DragonFly (FreeBSD-derived libc) likely has the same problem, though I didn't check it. support.is_apple or sys.platform.startswith(('freebsd', 'netbsd')) would be safer and would match the sys.platform gating in _pydatetime.py. platform conditionals are a mess and often need abstracting (what do we have in test.support for these? -gps)
  2. Hardcoded platform list instead of a probe. The Windows/Android/OpenBSD list is duplicated in C (#if) and Python (sys.platform). The new tests assert %-j %-U %-W %-V, %-H %-M %-S and %-I unconditionally, so any other libc without the - flag fails them. AIX and Solaris are the candidates; I did not verify either. A one-time probe in the style of _need_normalize_century() (does time.strftime('%-d', …) give '1'?) would remove the list.
  3. NEWS overstates what the change does. It says "standardized … across all platforms", but the docs and tests in the same PR carve out %-y staying zero-padded on Apple, FreeBSD and NetBSD. Either emulate %-y there too, or soften the NEWS wording.
  4. My own human review comment added below is half addressed (datetimetester.py:1602). The docs describe the %-y inconsistency, but the test just says # See gh-137165 above the platform branch. It needs a comment there saying why expectations differ.
  5. Doc placement. The new paragraph about strftime is appended to note (9), which is the strptime leading-zero note. It reads oddly there and probably deserves its own note, or at least a clear separation.
  6. Edge case: %-%-d. After an un-emulated %-, the scanner does not consume the next character as the conversion. So %-%-d becomes %-2 under emulation, where glibc natively gives %-d. This is the same existing quirk the new test deliberately pins for %-%f, so it is low priority.
  7. Nit: import platform is appended after import warnings, out of alphabetical order; it goes away if finding 1 is fixed with sys.platform.

self.assertEqual(t.strftime(""), "") # SF bug #761337
self.assertEqual(t.strftime('x'*1000), 'x'*1000) # SF bug #1556784

# See gh-137165

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.

the issue doesn't explain why there should be different strftime calls and expectations based on platform. describe that in here, and make sure we document this inconsistency in behavior across platforms as intentional and let users know what they should expect.

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

Labels

awaiting core review type-feature A feature request or enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Enhancement Proposal: Standardized Support for Non-Zero-Padded Day and Month Formatting in datetime.strftime

7 participants