Index: r265/Doc/library/time.rst =================================================================== --- r265/Doc/library/time.rst (revision 80497) +++ r265/Doc/library/time.rst (working copy) @@ -7,7 +7,7 @@ This module provides various time-related functions. For related -functionality, see also the :mod:`datetime` and :mod:`calendar` modules. +functionality, see also the :mod:`datetime` module. Although this module is always available, not all functions are available on all platforms. Most of the functions @@ -77,10 +77,10 @@ available). * The time value as returned by :func:`gmtime`, :func:`localtime`, and - :func:`strptime`, and accepted by :func:`asctime`, :func:`mktime` and - :func:`strftime`, may be considered as a sequence of 9 integers. The return - values of :func:`gmtime`, :func:`localtime`, and :func:`strptime` also offer - attribute names for individual fields. + :func:`strptime`, and accepted by :func:`asctime`, :func:`mktime`, + :func:`timegm` and :func:`strftime`, may be considered as a sequence of 9 integers. + The return values of :func:`gmtime`, :func:`localtime`, and :func:`strptime` + also offer attribute names for individual fields. +-------+-------------------+---------------------------------+ | Index | Attribute | Values | @@ -107,8 +107,8 @@ Note that unlike the C structure, the month value is a range of 1-12, not 0-11. A year value will be handled as described under "Year 2000 (Y2K) issues" above. - A ``-1`` argument as the daylight savings flag, passed to :func:`mktime` will - usually result in the correct daylight savings state to be filled in. + A ``-1`` argument as the daylight savings flag, passed to :func:`mktime` or :func:`timegm` + will usually result in the correct daylight savings state to be filled in. When a tuple with an incorrect length is passed to a function expecting a :class:`struct_time`, or having elements of the wrong type, a :exc:`TypeError` @@ -129,7 +129,7 @@ | seconds since the epoch | :class:`struct_time` in | :func:`localtime` | | | local time | | +-------------------------+-------------------------+-------------------------+ - | :class:`struct_time` in | seconds since the epoch | :func:`calendar.timegm` | + | :class:`struct_time` in | seconds since the epoch | :func:`timegm` | | UTC | | | +-------------------------+-------------------------+-------------------------+ | :class:`struct_time` in | seconds since the epoch | :func:`mktime` | @@ -213,7 +213,7 @@ UTC in which the dst flag is always zero. If *secs* is not provided or :const:`None`, the current time as returned by :func:`time` is used. Fractions of a second are ignored. See above for a description of the - :class:`struct_time` object. See :func:`calendar.timegm` for the inverse of this + :class:`struct_time` object. See :func:`timegm` for the inverse of this function. .. versionchanged:: 2.1 @@ -223,6 +223,18 @@ If *secs* is :const:`None`, the current time is used. +.. function:: timegm(t) + + This is the inverse function of :func:`gmtime`. Its argument is the + :class:`struct_time` or full 9-tuple (since the dst flag is needed; use ``-1`` + as the dst flag if it is unknown) which expresses the time in UTC. + It returns a floating point number, for compatibility with :func:`time`. + If the input value cannot be represented as a valid time, either + :exc:`OverflowError` or :exc:`ValueError` will be raised (which depends on + whether the invalid value is caught by Python or the underlying C libraries). + The earliest date for which it can generate a time is platform-dependent. + + .. function:: localtime([secs]) Like :func:`gmtime` but converts to local time. If *secs* is not provided or @@ -543,10 +555,6 @@ Internationalization services. The locale settings can affect the return values for some of the functions in the :mod:`time` module. - Module :mod:`calendar` - General calendar-related functions. :func:`timegm` is the inverse of - :func:`gmtime` from this module. - .. rubric:: Footnotes .. [#] The use of ``%Z`` is now deprecated, but the ``%z`` escape that expands to the Index: r265/Lib/test/test_time.py =================================================================== --- r265/Lib/test/test_time.py (revision 80497) +++ r265/Lib/test/test_time.py (working copy) @@ -22,6 +22,8 @@ == time.asctime(time.localtime(self.t))) self.assert_(long(time.mktime(time.localtime(self.t))) == long(self.t)) + self.assert_(long(time.timegm(time.gmtime(self.t))) + == long(self.t)) def test_sleep(self): time.sleep(1.2) Index: r265/Modules/timemodule.c =================================================================== --- r265/Modules/timemodule.c (revision 80497) +++ r265/Modules/timemodule.c (working copy) @@ -382,6 +382,46 @@ return 1; } +#if defined(HAVE_TIMEGM) || (defined(HAVE_MKTIME) && defined(HAVE_WORKING_TZSET)) + +static PyObject * +time_timegm(PyObject *self, PyObject *tup) +{ + struct tm buf; + time_t tt; + if (!gettmarg(tup, &buf)) + return NULL; +#if defined(HAVE_TIMEGM) + /* Use timegm function where available */ + tt = timegm(&buf); +#elif defined(HAVE_MKTIME) && defined(HAVE_WORKING_TZSET) + /* Fallback to mktime, taken from timegm(3)*/ + char *tz; + tz = getenv("TZ"); + setenv("TZ", "", 1); + tzset(); + tt = mktime(&buf); + if (tz) + setenv("TZ", "", 1); + else + unsetenv("TZ"); + tzset(); +#endif + if (tt == (time_t)(-1)) { + PyErr_SetString(PyExc_OverflowError, + "timegm argument out of range"); + return NULL; + } + return PyFloat_FromDouble((double)tt); +} + +PyDoc_STRVAR(timegm_doc, +"timegm(tuple) -> Floating point number\n\ +\n\ +Convert a UTC tuple to seconds since the Epoch."); + +#endif + #ifdef HAVE_STRFTIME static PyObject * time_strftime(PyObject *self, PyObject *args) @@ -753,6 +793,9 @@ {"localtime", time_localtime, METH_VARARGS, localtime_doc}, {"asctime", time_asctime, METH_VARARGS, asctime_doc}, {"ctime", time_ctime, METH_VARARGS, ctime_doc}, +#if defined(HAVE_TIMEGM) || (defined(HAVE_MKTIME) && defined(HAVE_WORKING_TZSET)) + {"timegm", time_timegm, METH_O, timegm_doc}, +#endif #ifdef HAVE_MKTIME {"mktime", time_mktime, METH_O, mktime_doc}, #endif @@ -804,6 +847,7 @@ clock() -- return CPU time since process start as a float\n\ sleep() -- delay for a number of seconds given as a float\n\ gmtime() -- convert seconds since Epoch to UTC tuple\n\ +timegm() -- Convert a UTC tuple to seconds since the Epoch.\n\ localtime() -- convert seconds since Epoch to local time tuple\n\ asctime() -- convert time tuple to string\n\ ctime() -- convert time in seconds to string\n\