[Python-checkins] cpython: Closes issue #22791: Improved datetime from timestamp methods documentation.

alexander.belopolsky python-checkins at python.org
Sun Mar 1 20:52:15 CET 2015


https://hg.python.org/cpython/rev/4a590c66070d
changeset:   94803:4a590c66070d
user:        Alexander Belopolsky <alexander.belopolsky at gmail.com>
date:        Sun Mar 01 14:52:07 2015 -0500
summary:
  Closes issue #22791: Improved datetime from timestamp methods documentation.

Original patch by Akira Li.

files:
  Doc/library/datetime.rst  |  20 +++++++++++++-------
  Lib/datetime.py           |   2 +-
  Modules/_datetimemodule.c |   3 +--
  3 files changed, 15 insertions(+), 10 deletions(-)


diff --git a/Doc/library/datetime.rst b/Doc/library/datetime.rst
--- a/Doc/library/datetime.rst
+++ b/Doc/library/datetime.rst
@@ -759,13 +759,19 @@
    :attr:`tzinfo` ``None``. This may raise :exc:`OverflowError`, if the timestamp is
    out of the range of values supported by the platform C :c:func:`gmtime` function,
    and :exc:`OSError` on :c:func:`gmtime` failure.
-   It's common for this to be restricted to years in 1970 through 2038. See also
-   :meth:`fromtimestamp`.
-
-   On the POSIX compliant platforms, ``utcfromtimestamp(timestamp)``
-   is equivalent to the following expression::
-
-     datetime(1970, 1, 1) + timedelta(seconds=timestamp)
+   It's common for this to be restricted to years in 1970 through 2038.
+
+   To get an aware :class:`.datetime` object, call :meth:`fromtimestamp`::
+
+     datetime.fromtimestamp(timestamp, timezone.utc)
+
+   On the POSIX compliant platforms, it is equivalent to the following
+   expression::
+
+     datetime(1970, 1, 1, tzinfo=timezone.utc) + timedelta(seconds=timestamp)
+
+   except the latter formula always supports the full years range: between
+   :const:`MINYEAR` and :const:`MAXYEAR` inclusive.
 
    .. versionchanged:: 3.3
       Raise :exc:`OverflowError` instead of :exc:`ValueError` if the timestamp
diff --git a/Lib/datetime.py b/Lib/datetime.py
--- a/Lib/datetime.py
+++ b/Lib/datetime.py
@@ -1393,7 +1393,7 @@
 
     @classmethod
     def utcfromtimestamp(cls, t):
-        "Construct a UTC datetime from a POSIX timestamp (like time.time())."
+        """Construct a naive UTC datetime from a POSIX timestamp."""
         t, frac = divmod(t, 1.0)
         us = int(frac * 1e6)
 
diff --git a/Modules/_datetimemodule.c b/Modules/_datetimemodule.c
--- a/Modules/_datetimemodule.c
+++ b/Modules/_datetimemodule.c
@@ -5020,8 +5020,7 @@
 
     {"utcfromtimestamp", (PyCFunction)datetime_utcfromtimestamp,
      METH_VARARGS | METH_CLASS,
-     PyDoc_STR("timestamp -> UTC datetime from a POSIX timestamp "
-               "(like time.time()).")},
+     PyDoc_STR("Construct a naive UTC datetime from a POSIX timestamp.")},
 
     {"strptime", (PyCFunction)datetime_strptime,
      METH_VARARGS | METH_CLASS,

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list