cpython: a96101dd105c (original) (raw)
--- a/Doc/library/time.rst
+++ b/Doc/library/time.rst
@@ -83,6 +83,10 @@ An explanation of some terminology and c
and :attr:tm_zone
attributes when platform supports corresponding
struct tm
members.
- .. versionchanged:: 3.6
The :class:`struct_time` attributes :attr:`tm_gmtoff` and :attr:`tm_zone`[](#l1.8)
are now available on all platforms.[](#l1.9)
- Use the following functions to convert between time representations:
+-------------------------+-------------------------+-------------------------+
@@ -566,10 +570,6 @@ The module defines the following functio
:class:
struct_time
, or having elements of the wrong type, a :exc:TypeError
is raised.
- .. versionchanged:: 3.3
:attr:`tm_gmtoff` and :attr:`tm_zone` attributes are available on platforms[](#l1.19)
with C library supporting the corresponding fields in ``struct tm``.[](#l1.20)
- .. function:: time() Return the time in seconds since the epoch as a floating point number.
--- a/Misc/NEWS +++ b/Misc/NEWS @@ -143,6 +143,10 @@ Core and Builtins Library ------- +- Issue #25283: Attributes tm_gmtoff and tm_zone are now available on
- Issue #24454: Regular expression match object groups are now accessible using getitem. "mo[x]" is equivalent to "mo.group(x)".
--- a/Modules/timemodule.c +++ b/Modules/timemodule.c @@ -250,10 +250,8 @@ static PyStructSequence_Field struct_tim {"tm_wday", "day of week, range [0, 6], Monday is 0"}, {"tm_yday", "day of year, range [1, 366]"}, {"tm_isdst", "1 if summer time is in effect, 0 if not, and -1 if unknown"}, -#ifdef HAVE_STRUCT_TM_TM_ZONE {"tm_zone", "abbreviation of timezone name"}, {"tm_gmtoff", "offset from UTC in seconds"}, -#endif /* HAVE_STRUCT_TM_TM_ZONE */ {0} }; @@ -275,7 +273,11 @@ static PyTypeObject StructTimeType; static PyObject * -tmtotuple(struct tm *p) +tmtotuple(struct tm *p +#ifndef HAVE_STRUCT_TM_TM_ZONE
, const char *zone, int gmtoff[](#l3.21)
+#endif +) { PyObject *v = PyStructSequence_New(&StructTimeType); if (v == NULL) @@ -296,6 +298,10 @@ tmtotuple(struct tm *p) PyStructSequence_SET_ITEM(v, 9, PyUnicode_DecodeLocale(p->tm_zone, "surrogateescape")); SET(10, p->tm_gmtoff); +#else
- PyStructSequence_SET_ITEM(v, 9,
PyUnicode_DecodeLocale(zone, "surrogateescape"));[](#l3.33)
- SET(10, gmtoff);
#endif /* HAVE_STRUCT_TM_TM_ZONE */ #undef SET if (PyErr_Occurred()) { @@ -348,9 +354,26 @@ time_gmtime(PyObject *self, PyObject *ar return PyErr_SetFromErrno(PyExc_OSError); } buf = *local; +#ifdef HAVE_STRUCT_TM_TM_ZONE return tmtotuple(&buf); +#else
+#endif } +#ifndef HAVE_TIMEGM +static time_t +timegm(struct tm *p) +{
- /* XXX: the following implementation will not work for tm_year < 1970.
but it is likely that platforms that don't have timegm do not support[](#l3.54)
negative timestamps anyways. */[](#l3.55)
- return p->tm_sec + p->tm_min60 + p->tm_hour3600 + p->tm_yday*86400 +
(p->tm_year-70)*31536000 + ((p->tm_year-69)/4)*86400 -[](#l3.57)
((p->tm_year-1)/100)*86400 + ((p->tm_year+299)/400)*86400;[](#l3.58)
+} +#endif + PyDoc_STRVAR(gmtime_doc, "gmtime([seconds]) -> (tm_year, tm_mon, tm_mday, tm_hour, tm_min,\n[](#l3.63) tm_sec, tm_wday, tm_yday, tm_isdst)\n[](#l3.64) @@ -391,7 +414,18 @@ time_localtime(PyObject *self, PyObject return NULL; if (pylocaltime(&when, &buf) == -1) return NULL; +#ifdef HAVE_STRUCT_TM_TM_ZONE return tmtotuple(&buf); +#else
- {
struct tm local = buf;[](#l3.73)
char zone[100];[](#l3.74)
int gmtoff;[](#l3.75)
strftime(zone, sizeof(buf), "%Z", &buf);[](#l3.76)
gmtoff = timegm(&buf) - when;[](#l3.77)
return tmtotuple(&local, zone, gmtoff);[](#l3.78)
- }
+#endif } PyDoc_STRVAR(localtime_doc, @@ -1146,6 +1180,27 @@ PyDoc_STRVAR(get_clock_info_doc, Get information of the specified clock."); static void +get_zone(char *zone, int n, struct tm *p) +{ +#ifdef HAVE_STRUCT_TM_TM_ZONE
+#endif +} + +static int +get_gmtoff(time_t t, struct tm *p) +{ +#ifdef HAVE_STRUCT_TM_TM_ZONE
+#endif +} + +static void PyInit_timezone(PyObject m) { / This code moved from PyInit_time wholesale to allow calling it from time_tzset. In the future, some parts of it can be moved back @@ -1177,7 +1232,6 @@ PyInit_timezone(PyObject m) { otz1 = PyUnicode_DecodeLocale(tzname[1], "surrogateescape"); PyModule_AddObject(m, "tzname", Py_BuildValue("(NN)", otz0, otz1)); #else / !HAVE_TZNAME || GLIBC || CYGWIN*/ -#ifdef HAVE_STRUCT_TM_TM_ZONE { #define YEAR ((time_t)((365 * 24 + 6) * 3600)) time_t t; @@ -1186,13 +1240,13 @@ PyInit_timezone(PyObject *m) { char janname[10], julyname[10]; t = (time((time_t *)0) / YEAR) * YEAR; p = localtime(&t);
janzone = -p->tm_gmtoff;[](#l3.124)
strncpy(janname, p->tm_zone ? p->tm_zone : " ", 9);[](#l3.125)
get_zone(janname, 9, p);[](#l3.126)
janzone = -get_gmtoff(t, p);[](#l3.127) janname[9] = '\0';[](#l3.128) t += YEAR/2;[](#l3.129) p = localtime(&t);[](#l3.130)
julyzone = -p->tm_gmtoff;[](#l3.131)
strncpy(julyname, p->tm_zone ? p->tm_zone : " ", 9);[](#l3.132)
get_zone(julyname, 9, p);[](#l3.133)
julyzone = -get_gmtoff(t, p);[](#l3.134) julyname[9] = '\0';[](#l3.135)
if( janzone < julyzone ) { @@ -1214,8 +1268,6 @@ PyInit_timezone(PyObject m) { janname, julyname)); } } -#else -#endif / HAVE_STRUCT_TM_TM_ZONE / #ifdef CYGWIN tzset(); PyModule_AddIntConstant(m, "timezone", _timezone); @@ -1225,25 +1277,6 @@ PyInit_timezone(PyObject m) { Py_BuildValue("(zz)", _tzname[0], _tzname[1])); #endif / CYGWIN / #endif / !HAVE_TZNAME || GLIBC || CYGWIN/ - -#if defined(HAVE_CLOCK_GETTIME)
-#endif -#ifdef CLOCK_MONOTONIC_RAW
-#endif -#ifdef CLOCK_PROCESS_CPUTIME_ID
-#endif -#ifdef CLOCK_THREAD_CPUTIME_ID
-#endif -#endif /* HAVE_CLOCK_GETTIME / } @@ -1350,17 +1383,32 @@ PyInit_time(void) / Set, or reset, module variables like time.timezone */ PyInit_timezone(m); +#if defined(HAVE_CLOCK_GETTIME)
+#endif +#ifdef CLOCK_MONOTONIC_RAW
+#endif +#ifdef CLOCK_PROCESS_CPUTIME_ID
+#endif +#ifdef CLOCK_THREAD_CPUTIME_ID
+#endif +#endif /* HAVE_CLOCK_GETTIME */ + if (!initialized) { if (PyStructSequence_InitType2(&StructTimeType, &struct_time_type_desc) < 0) return NULL; } Py_INCREF(&StructTimeType); -#ifdef HAVE_STRUCT_TM_TM_ZONE PyModule_AddIntConstant(m, "_STRUCT_TM_ITEMS", 11); -#else
-#endif PyModule_AddObject(m, "struct_time", (PyObject*) &StructTimeType); initialized = 1; return m;