(original) (raw)

changeset: 69551:b55eac85e39c user: Alexander Belopolsky alexander.belopolsky@gmail.com date: Mon Apr 25 13:00:40 2011 -0400 files: Doc/library/datetime.rst description: Issue #2736: Documented how to compute seconds since epoch. diff -r 4658762a77cf -r b55eac85e39c Doc/library/datetime.rst --- a/Doc/library/datetime.rst Mon Apr 25 04:03:58 2011 +0200 +++ b/Doc/library/datetime.rst Mon Apr 25 13:00:40 2011 -0400 @@ -721,6 +721,22 @@ 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) + + There is no method to obtain the timestamp from a :class:`datetime` + instance, but POSIX timestamp corresponding to a :class:`datetime` + instance ``dt`` can be easily calculated as follows. For a naive + ``dt``:: + + timestamp = (dt - datetime(1970, 1, 1)) / timedelta(seconds=1) + + And for an aware ``dt``:: + + timestamp = (dt - datetime(1970, 1, 1, tzinfo=timezone.utc)) / timedelta(seconds=1) + .. classmethod:: datetime.fromordinal(ordinal) /alexander.belopolsky@gmail.com