-
-
Notifications
You must be signed in to change notification settings - Fork 34.5k
gh-137165: Standardized non-zero-padded date formatting for datetime.strftime
#139088
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
base: main
Are you sure you want to change the base?
Changes from 12 commits
2f6c0ce
8d16901
11e0218
eb5c883
224654c
551f16e
ddc75ed
a51dad6
0e5ef8b
375cf41
40d0e1c
2054356
6ff5f4f
d5f2014
0d53ced
9212a5a
2373670
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -213,6 +213,11 @@ def _need_normalize_century(): | |
| _normalize_century = True | ||
| return _normalize_century | ||
|
|
||
| def _make_dash_replacement(ch, timetuple): | ||
| fmt = '%' + ch | ||
| val = _time.strftime(fmt, timetuple) | ||
| return val.lstrip('0') or '0' | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will this not eat the dash for unsupported codes, e.g what will
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've added an explicit check to ensure that only valid format specifiers (as documented in Doc/library/datetime.rst) are passed on Windows and Android.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So, removing one inconsistency you introduce another? I would expect them all to be the same as on linux, why are you raising an error?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've removed the ValueError and unified the behavior for unsupported specifiers like on Linux.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I'd rather leave that to separate PR.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @StanFromIreland Moved this code to PR #145018 and rolled back here. |
||
|
|
||
| # Correctly substitute for %z and %Z escapes in strftime formats. | ||
| def _wrap_strftime(object, format, timetuple): | ||
| # Don't call utcoffset() or tzname() unless actually needed. | ||
|
|
@@ -284,6 +289,16 @@ def _wrap_strftime(object, format, timetuple): | |
| push('{:04}'.format(year)) | ||
| if ch == 'F': | ||
| push('-{:02}-{:02}'.format(*timetuple[1:3])) | ||
| elif ch == '-': | ||
| if i < n: | ||
| next_ch = format[i] | ||
| i += 1 | ||
| if sys.platform in ['win32', 'android']: | ||
| push(_make_dash_replacement(next_ch, timetuple)) | ||
| else: | ||
| push('%-' + next_ch) | ||
| else: | ||
| push('%-') | ||
| else: | ||
| push('%') | ||
| push(ch) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| Standardized non-zero-padded numeric formatting for dates and times in | ||
| :func:`datetime.datetime.strftime` and :func:`datetime.date.strftime` across | ||
| all platforms. |
Uh oh!
There was an error while loading. Please reload this page.