From 1cf79161581386919b91c572c5517c731829ea29 Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade Date: Tue, 16 May 2023 14:00:03 +0300 Subject: [PATCH 1/4] Document utcnow and utcfromtimestamp deprecations in What's New --- Doc/whatsnew/3.12.rst | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Doc/whatsnew/3.12.rst b/Doc/whatsnew/3.12.rst index 3e55b3fa0f4734..484980f18155c7 100644 --- a/Doc/whatsnew/3.12.rst +++ b/Doc/whatsnew/3.12.rst @@ -771,6 +771,16 @@ Deprecated ``int``, convert to int explicitly with ``~int(x)``. (Contributed by Tim Hoffmann in :gh:`103487`.) +* :class:`datetime.datetime`'s + :meth:`~datetime.datetime.utcnow` and + :meth:`~datetime.datetime.utcfromtimestamp` are deprecated and will be + removed in a future version. Instead, use timezone-aware objects to represent + datetimes in UTC: respectively, call + :meth:`~datetime.datetime.now` and + :meth:`~datetime.datetime.fromtimestamp` with the *tz* parameter set to + :attr:`datetime.UTC`. + (Contributed by Paul Ganssle in :gh:`103857`.) + Pending Removal in Python 3.13 ------------------------------ From e0ff7f5c6d38043b1f573edf5b8d1d305b4cf69a Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade Date: Tue, 16 May 2023 14:29:22 +0300 Subject: [PATCH 2/4] Fix typo: use fromtimestamp(datetime.UTC) instead of utcfromtimestamp() --- Modules/_datetimemodule.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Modules/_datetimemodule.c b/Modules/_datetimemodule.c index 8b417bdd0fb5b6..9126cd1bc2cfcc 100644 --- a/Modules/_datetimemodule.c +++ b/Modules/_datetimemodule.c @@ -5190,7 +5190,7 @@ datetime_utcfromtimestamp(PyObject *cls, PyObject *args) if (PyErr_WarnEx(PyExc_DeprecationWarning, "datetime.utcfromtimestamp() is deprecated and scheduled for removal " "in a future version. Use timezone-aware objects to represent " - "datetimes in UTC: datetime.now(datetime.UTC).", 1)) + "datetimes in UTC: datetime.fromtimestamp(datetime.UTC).", 1)) { return NULL; } From efc1e9908d0941999562b3249ba56fbda3ed2ad8 Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade Date: Tue, 16 May 2023 16:32:19 +0300 Subject: [PATCH 3/4] Add timestamp arg to datetime.now call --- Modules/_datetimemodule.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Modules/_datetimemodule.c b/Modules/_datetimemodule.c index 9126cd1bc2cfcc..4b660a688c9545 100644 --- a/Modules/_datetimemodule.c +++ b/Modules/_datetimemodule.c @@ -5147,7 +5147,7 @@ datetime_utcnow(PyObject *cls, PyObject *dummy) if (PyErr_WarnEx(PyExc_DeprecationWarning, "datetime.utcnow() is deprecated and scheduled for removal in a " "future version. Use timezone-aware objects to represent datetimes " - "in UTC: datetime.now(datetime.UTC).", 1)) + "in UTC: datetime.now(timestamp, datetime.UTC).", 1)) { return NULL; } From 24ef1f302e9af627df8a7a450917ccc0a4f936a3 Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade Date: Fri, 19 May 2023 15:17:43 +0300 Subject: [PATCH 4/4] Fix params in docs Co-authored-by: Paul Ganssle <1377457+pganssle@users.noreply.github.com> --- Modules/_datetimemodule.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Modules/_datetimemodule.c b/Modules/_datetimemodule.c index 4b660a688c9545..19e11780ec6e19 100644 --- a/Modules/_datetimemodule.c +++ b/Modules/_datetimemodule.c @@ -5147,7 +5147,7 @@ datetime_utcnow(PyObject *cls, PyObject *dummy) if (PyErr_WarnEx(PyExc_DeprecationWarning, "datetime.utcnow() is deprecated and scheduled for removal in a " "future version. Use timezone-aware objects to represent datetimes " - "in UTC: datetime.now(timestamp, datetime.UTC).", 1)) + "in UTC: datetime.now(datetime.UTC).", 1)) { return NULL; } @@ -5190,7 +5190,7 @@ datetime_utcfromtimestamp(PyObject *cls, PyObject *args) if (PyErr_WarnEx(PyExc_DeprecationWarning, "datetime.utcfromtimestamp() is deprecated and scheduled for removal " "in a future version. Use timezone-aware objects to represent " - "datetimes in UTC: datetime.fromtimestamp(datetime.UTC).", 1)) + "datetimes in UTC: datetime.fromtimestamp(timestamp, datetime.UTC).", 1)) { return NULL; }