Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1e459ae

Browse files
committed
Deprecate utcnow and utcfromtimestamp
1 parent 6c4124d commit 1e459ae

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

Doc/library/datetime.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -896,6 +896,10 @@ Other constructors, all class methods:
896896
in UTC. As such, the recommended way to create an object representing the
897897
current time in UTC is by calling ``datetime.now(timezone.utc)``.
898898

899+
.. deprecated:: 3.12
900+
901+
Use :meth:`datetime.now` with :attr:`UTC` instead.
902+
899903

900904
.. classmethod:: datetime.fromtimestamp(timestamp, tz=None)
901905

@@ -964,6 +968,10 @@ Other constructors, all class methods:
964968
:c:func:`gmtime` function. Raise :exc:`OSError` instead of
965969
:exc:`ValueError` on :c:func:`gmtime` failure.
966970

971+
.. deprecated:: 3.12
972+
973+
Use :meth:`datetime.fromtimestamp` with :attr:`UTC` instead.
974+
967975

968976
.. classmethod:: datetime.fromordinal(ordinal)
969977

Lib/datetime.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1801,6 +1801,14 @@ def fromtimestamp(cls, timestamp, tz=None):
18011801
@classmethod
18021802
def utcfromtimestamp(cls, t):
18031803
"""Construct a naive UTC datetime from a POSIX timestamp."""
1804+
import warnings
1805+
warnings.warn("datetime.utcfromtimestamp() is deprecated and scheduled "
1806+
"for removal in a future version. It is instead "
1807+
"recommended that users use timezone-aware objects to "
1808+
"represent datetimes in UTC. To get such an object from "
1809+
"a timestamp, use datetime.fromtimestamp(t, datetime.UTC)",
1810+
DeprecationWarning,
1811+
stacklevel=2)
18041812
return cls._fromtimestamp(t, True, None)
18051813

18061814
@classmethod
@@ -1812,8 +1820,16 @@ def now(cls, tz=None):
18121820
@classmethod
18131821
def utcnow(cls):
18141822
"Construct a UTC datetime from time.time()."
1823+
import warnings
1824+
warnings.warn("datetime.utcnow() is deprecated and scheduled for "
1825+
"removal in a future version. It is instead recommended "
1826+
"that users use timezone-aware objects to represent "
1827+
"datetimes in UTC. To get such an object representing "
1828+
"the current time, use datetime.now(datetime.UTC)",
1829+
DeprecationWarning,
1830+
stacklevel=2)
18151831
t = _time.time()
1816-
return cls.utcfromtimestamp(t)
1832+
return cls._fromtimestamp(t, True, None)
18171833

18181834
@classmethod
18191835
def combine(cls, date, time, tzinfo=True):

Modules/_datetimemodule.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5144,6 +5144,14 @@ datetime_datetime_now_impl(PyTypeObject *type, PyObject *tz)
51445144
static PyObject *
51455145
datetime_utcnow(PyObject *cls, PyObject *dummy)
51465146
{
5147+
PyErr_WarnEx(
5148+
PyExc_DeprecationWarning,
5149+
"datetime.utcnow() is deprecated and scheduled for removal in a future "
5150+
"version. It is instead recommended that users use timezone-aware "
5151+
"objects to represent datetimes in UTC. To get such an object "
5152+
"representing the current time, use datetime.now(datetime.UTC)",
5153+
2
5154+
);
51475155
return datetime_best_possible(cls, _PyTime_gmtime, Py_None);
51485156
}
51495157

@@ -5180,6 +5188,14 @@ datetime_fromtimestamp(PyObject *cls, PyObject *args, PyObject *kw)
51805188
static PyObject *
51815189
datetime_utcfromtimestamp(PyObject *cls, PyObject *args)
51825190
{
5191+
PyErr_WarnEx(
5192+
PyExc_DeprecationWarning,
5193+
"datetime.utcfromtimestamp() is deprecated and scheduled for removal "
5194+
"in a future version. It is instead recommended that users use "
5195+
"timezone-aware objects to represent datetimes in UTC. To get such an "
5196+
"object from a timestamp, use datetime.fromtimestamp(t, datetime.UTC)",
5197+
2
5198+
);
51835199
PyObject *timestamp;
51845200
PyObject *result = NULL;
51855201

0 commit comments

Comments
 (0)