8000 Inconsistent `datetime.*.strftime()` year padding behavior across `%y`, `%Y`, `%F` and `%C` · Issue #122272 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content
Inconsistent datetime.*.strftime() year padding behavior across %y, %Y, %F and %C #122272
Closed
@mgorny

Description

@mgorny

Bug report

Bug description:

#120713 changed the way %y and %Y format works with strftime() to pad "short" years with leading zeros. However, the %F and %C formats were left alone, creating inconsistency:

>>> from datetime import date
>>> date(909, 9, 9).strftime("%Y")
'0909'
>>> date(909, 9, 9).strftime("%y")
'09'
>>> date(909, 9, 9).strftime("%C")
'9'
>>> date(909, 9, 9).strftime("%F")
'909-09-09'

While I don't have a strong opinion about %C, the change to %Y now means that %Y-%m-%d no longer matches %F.

Notably, this change broke the logic in Django that assumed that if %Y returns a zero-padded year number, no padding needs to be done for all formats:

https://github.com/django/django/blob/0e94f292cda632153f2b3d9a9037eb0141ae9c2e/django/utils/formats.py#L266-L273

I will report a bug there as well, but I think it would be beneficial to preserve consistency here.

Django bug: https://code.djangoproject.com/ticket/35630

CC @blhsing @martinvuyk

CPython versions tested on:

3.13, CPython main branch

Operating systems tested on:

Linux

Linked PRs

Metadata

Metadata

Labels

3.12only security fixes3.13bugs and security fixes3.14bugs and security fixestype-bugAn unexpected behavior, bug, or error

Projects

Status

Done

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

    0