[Django] #32738: Deprecate django.utils.datetime_safe, use alternate method to ensure four-digit year with strftime.

18 views
Skip to first unread message

Django

unread,
May 10, 2021, 5:19:16 PM5/10/21
to django-...@googlegroups.com
#32738: Deprecate django.utils.datetime_safe, use alternate method to ensure four-
digit year with strftime.
-------------------------------------+-------------------------------------
Reporter: Nick Pope | Owner: Nick Pope
Type: | Status: assigned
Cleanup/optimization |
Component: Utilities | Version: dev
Severity: Normal | Keywords: datetime_safe,
Triage Stage: | datetime
Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
While working on #32366, I came across `django.utils.datetime_safe`. It
exists to ensure that `%Y` for `datetime.datetime.strftime()` produces a
correct zero-padded 4-digit year. It is only used in three places in
Django:

The module refers to https://bugs.python.org/issue13305. On reading
further, I understand the following:

For years < 1000 specifiers `%C`, `%F`, `%G`, and `%Y` don't work as
expected for `strftime` provided by glibc on Linux as they don't pad the
year or century with leading zeros. Support for specifying the padding
explicitly is available, however, which can be used to fix this issue,
e.g. `%04Y`.

FreeBSD, macOS, and Windows do not support explicitly specifying the
padding, but return four digit years (with leading zeros) as expected.

It seems to me that the current approach is quite complex and that we
could simply implement a check whether `%Y` produces the expected value
and, if not, make the following substitutions:

- `%C` → `%02C`
- `%F` → `%010F`
- `%G` → `%04G`
- `%Y` → `%04Y`

This changes from wrapping `date`/`datetime` objects in subclasses with
overridden `.strftime()` to a simple function call to check whether we
need to fix the format and rewrite the format if so. (This can be cached.)
We also gain the benefit of fixing other specifiers that are also
affected, rather than just `%Y`.

There has been some precedence for cleaning up `datetime_safe` in #29600.
Also, Aymeric [https://code.djangoproject.com/ticket/21256#comment:1
doesn't really like it]... :D

--
Ticket URL: <https://code.djangoproject.com/ticket/32738>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
May 10, 2021, 5:24:22 PM5/10/21
to django-...@googlegroups.com
#32738: Deprecate django.utils.datetime_safe, use alternate method to ensure four-
digit year with strftime.
-------------------------------------+-------------------------------------
Reporter: Nick Pope | Owner: Nick Pope
Type: | Status: assigned
Cleanup/optimization |
Component: Utilities | Version: dev
Severity: Normal | Resolution:

Keywords: datetime_safe, | Triage Stage:
datetime | Unreviewed
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Nick Pope):

* has_patch: 0 => 1


Comment:

[https://github.com/django/django/pull/14378 PR]

--
Ticket URL: <https://code.djangoproject.com/ticket/32738#comment:1>

Django

unread,
May 11, 2021, 3:46:36 AM5/11/21
to django-...@googlegroups.com
#32738: Deprecate django.utils.datetime_safe, use alternate method to ensure four-
digit year with strftime.
-------------------------------------+-------------------------------------
Reporter: Nick Pope | Owner: Nick Pope
Type: | Status: assigned
Cleanup/optimization |
Component: Utilities | Version: dev
Severity: Normal | Resolution:
Keywords: datetime_safe, | Triage Stage: Accepted
datetime |
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Carlton Gibson):

* stage: Unreviewed => Accepted


Comment:

OK, I like this: the single documented function is clearer for me that the
subclass approach.

--
Ticket URL: <https://code.djangoproject.com/ticket/32738#comment:2>

Django

unread,
May 12, 2021, 9:43:49 AM5/12/21
to django-...@googlegroups.com
#32738: Deprecate django.utils.datetime_safe, use alternate method to ensure four-
digit year with strftime.
-------------------------------------+-------------------------------------
Reporter: Nick Pope | Owner: Nick Pope
Type: | Status: assigned
Cleanup/optimization |
Component: Utilities | Version: dev
Severity: Normal | Resolution:
Keywords: datetime_safe, | Triage Stage: Accepted
datetime |
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Carlton Gibson <carlton.gibson@…>):

In [changeset:"46346f8ea08020d503b25472a26b949a5ce980a6" 46346f8]:
{{{
#!CommitTicketReference repository=""
revision="46346f8ea08020d503b25472a26b949a5ce980a6"
Refs #32738 -- Added sanitize_strftime_format() to replace datetime_safe.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/32738#comment:4>

Django

unread,
May 12, 2021, 9:43:50 AM5/12/21
to django-...@googlegroups.com
#32738: Deprecate django.utils.datetime_safe, use alternate method to ensure four-
digit year with strftime.
-------------------------------------+-------------------------------------
Reporter: Nick Pope | Owner: Nick Pope
Type: | Status: assigned
Cleanup/optimization |
Component: Utilities | Version: dev
Severity: Normal | Resolution:
Keywords: datetime_safe, | Triage Stage: Accepted
datetime |
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Carlton Gibson <carlton.gibson@…>):

In [changeset:"44accb066a51d15f27e38f203c73830eddea16df" 44accb0]:
{{{
#!CommitTicketReference repository=""
revision="44accb066a51d15f27e38f203c73830eddea16df"
Refs #32738, Refs #29600, Refs #29595 -- Removed unused
django.utils.datetime_safe.time().

Unused since c72dde41e603093ab0bb12fa24fa69cfda0d35f9.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/32738#comment:3>

Django

unread,
May 12, 2021, 9:43:50 AM5/12/21
to django-...@googlegroups.com
#32738: Deprecate django.utils.datetime_safe, use alternate method to ensure four-
digit year with strftime.
-------------------------------------+-------------------------------------
Reporter: Nick Pope | Owner: Nick Pope
Type: | Status: closed

Cleanup/optimization |
Component: Utilities | Version: dev
Severity: Normal | Resolution: fixed

Keywords: datetime_safe, | Triage Stage: Accepted
datetime |
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Carlton Gibson <carlton.gibson@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"29e4ccb1a2d5aab21fbd7effcd48d33dc73f9b1e" 29e4ccb1]:
{{{
#!CommitTicketReference repository=""
revision="29e4ccb1a2d5aab21fbd7effcd48d33dc73f9b1e"
Fixed #32738 -- Deprecated django.utils.datetime_safe module.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/32738#comment:5>

Reply all
Reply to author
Forward
0 new messages