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

Skip to content

Commit e2e178e

Browse files
committed
Closes issue #22791: Improved datetime from timestamp methods documentation.
Original patch by Akira Li.
1 parent 3de4aae commit e2e178e

3 files changed

Lines changed: 13 additions & 8 deletions

File tree

Doc/library/datetime.rst

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -759,13 +759,19 @@ Other constructors, all class methods:
759759
:attr:`tzinfo` ``None``. This may raise :exc:`OverflowError`, if the timestamp is
760760
out of the range of values supported by the platform C :c:func:`gmtime` function,
761761
and :exc:`OSError` on :c:func:`gmtime` failure.
762-
It's common for this to be restricted to years in 1970 through 2038. See also
763-
:meth:`fromtimestamp`.
762+
It's common for this to be restricted to years in 1970 through 2038.
763+
764+
To get an aware :class:`.datetime` object, call :meth:`fromtimestamp`::
765+
766+
datetime.fromtimestamp(timestamp, timezone.utc)
767+
768+
On the POSIX compliant platforms, it is equivalent to the following
769+
expression::
764770

765-
On the POSIX compliant platforms, ``utcfromtimestamp(timestamp)``
766-
is equivalent to the following expression::
771+
datetime(1970, 1, 1, tzinfo=timezone.utc) + timedelta(seconds=timestamp)
767772

768-
datetime(1970, 1, 1) + timedelta(seconds=timestamp)
773+
except the latter formula always supports the full years range: between
774+
:const:`MINYEAR` and :const:`MAXYEAR` inclusive.
769775

770776
.. versionchanged:: 3.3
771777
Raise :exc:`OverflowError` instead of :exc:`ValueError` if the timestamp

Lib/datetime.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1393,7 +1393,7 @@ def fromtimestamp(cls, t, tz=None):
13931393

13941394
@classmethod
13951395
def utcfromtimestamp(cls, t):
1396-
"Construct a UTC datetime from a POSIX timestamp (like time.time())."
1396+
"""Construct a naive UTC datetime from a POSIX timestamp."""
13971397
t, frac = divmod(t, 1.0)
13981398
us = int(frac * 1e6)
13991399

Modules/_datetimemodule.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5020,8 +5020,7 @@ static PyMethodDef datetime_methods[] = {
50205020

50215021
{"utcfromtimestamp", (PyCFunction)datetime_utcfromtimestamp,
50225022
METH_VARARGS | METH_CLASS,
5023-
PyDoc_STR("timestamp -> UTC datetime from a POSIX timestamp "
5024-
"(like time.time()).")},
5023+
PyDoc_STR("Construct a naive UTC datetime from a POSIX timestamp.")},
50255024

50265025
{"strptime", (PyCFunction)datetime_strptime,
50275026
METH_VARARGS | METH_CLASS,

0 commit comments

Comments
 (0)