Commit c510c6b8 authored by Benjamin Peterson's avatar Benjamin Peterson Committed by GitHub

Simplify PyInit_timezone. (GH-9467)

Reduce the knotty preprocessor conditional logic, dedent unnecessarily nested
code, and handle errors properly.

The first edition of this change (afde1c1a)
failed (bpo-34715) because FreeBSD doesn't define the timezone globals. That's
why we're now checking for HAVE_DECL_TZNAME.
parent a4ae828e
...@@ -1522,7 +1522,7 @@ PyDoc_STRVAR(get_clock_info_doc, ...@@ -1522,7 +1522,7 @@ PyDoc_STRVAR(get_clock_info_doc,
\n\ \n\
Get information of the specified clock."); Get information of the specified clock.");
#if !defined(HAVE_TZNAME) || defined(__GLIBC__) || defined(__CYGWIN__) #ifndef HAVE_DECL_TZNAME
static void static void
get_zone(char *zone, int n, struct tm *p) get_zone(char *zone, int n, struct tm *p)
{ {
...@@ -1543,7 +1543,7 @@ get_gmtoff(time_t t, struct tm *p) ...@@ -1543,7 +1543,7 @@ get_gmtoff(time_t t, struct tm *p)
return timegm(p) - t; return timegm(p) - t;
#endif #endif
} }
#endif /* !defined(HAVE_TZNAME) || defined(__GLIBC__) || defined(__CYGWIN__) */ #endif // !HAVE_DECL_TZNAME
static void static void
PyInit_timezone(PyObject *m) { PyInit_timezone(PyObject *m) {
...@@ -1563,7 +1563,7 @@ PyInit_timezone(PyObject *m) { ...@@ -1563,7 +1563,7 @@ PyInit_timezone(PyObject *m) {
And I'm lazy and hate C so nyer. And I'm lazy and hate C so nyer.
*/ */
#if defined(HAVE_TZNAME) && !defined(__GLIBC__) && !defined(__CYGWIN__) #ifdef HAVE_DECL_TZNAME
PyObject *otz0, *otz1; PyObject *otz0, *otz1;
tzset(); tzset();
PyModule_AddIntConstant(m, "timezone", timezone); PyModule_AddIntConstant(m, "timezone", timezone);
...@@ -1574,54 +1574,52 @@ PyInit_timezone(PyObject *m) { ...@@ -1574,54 +1574,52 @@ PyInit_timezone(PyObject *m) {
#endif #endif
PyModule_AddIntConstant(m, "daylight", daylight); PyModule_AddIntConstant(m, "daylight", daylight);
otz0 = PyUnicode_DecodeLocale(tzname[0], "surrogateescape"); otz0 = PyUnicode_DecodeLocale(tzname[0], "surrogateescape");
otz1 = PyUnicode_DecodeLocale(tzname[1], "surrogateescape"); if (otz0 == NULL) {
PyModule_AddObject(m, "tzname", Py_BuildValue("(NN)", otz0, otz1)); return;
#else /* !HAVE_TZNAME || __GLIBC__ || __CYGWIN__*/
{
#define YEAR ((time_t)((365 * 24 + 6) * 3600))
time_t t;
struct tm p;
long janzone, julyzone;
char janname[10], julyname[10];
t = (time((time_t *)0) / YEAR) * YEAR;
_PyTime_localtime(t, &p);
get_zone(janname, 9, &p);
janzone = -get_gmtoff(t, &p);
janname[9] = '\0';
t += YEAR/2;
_PyTime_localtime(t, &p);
get_zone(julyname, 9, &p);
julyzone = -get_gmtoff(t, &p);
julyname[9] = '\0';
if( janzone < julyzone ) {
/* DST is reversed in the southern hemisphere */
PyModule_AddIntConstant(m, "timezone", julyzone);
PyModule_AddIntConstant(m, "altzone", janzone);
PyModule_AddIntConstant(m, "daylight",
janzone != julyzone);
PyModule_AddObject(m, "tzname",
Py_BuildValue("(zz)",
julyname, janname));
} else {
PyModule_AddIntConstant(m, "timezone", janzone);
PyModule_AddIntConstant(m, "altzone", julyzone);
PyModule_AddIntConstant(m, "daylight",
janzone != julyzone);
PyModule_AddObject(m, "tzname",
Py_BuildValue("(zz)",
janname, julyname));
}
} }
#ifdef __CYGWIN__ otz1 = PyUnicode_DecodeLocale(tzname[1], "surrogateescape");
tzset(); if (otz1 == NULL) {
PyModule_AddIntConstant(m, "timezone", _timezone); Py_DECREF(otz0);
PyModule_AddIntConstant(m, "altzone", _timezone-3600); return;
PyModule_AddIntConstant(m, "daylight", _daylight); }
PyModule_AddObject(m, "tzname", PyObject *tzname_obj = Py_BuildValue("(NN)", otz0, otz1);
Py_BuildValue("(zz)", _tzname[0], _tzname[1])); if (tzname_obj == NULL)
#endif /* __CYGWIN__ */ return;
#endif /* !HAVE_TZNAME || __GLIBC__ || __CYGWIN__*/ PyModule_AddObject(m, "tzname", tzname_obj);
#else // !HAVE_DECL_TZNAME
static const time_t YEAR = (365 * 24 + 6) * 3600;
time_t t;
struct tm p;
long janzone, julyzone;
char janname[10], julyname[10];
t = (time((time_t *)0) / YEAR) * YEAR;
_PyTime_localtime(t, &p);
get_zone(janname, 9, &p);
janzone = -get_gmtoff(t, &p);
janname[9] = '\0';
t += YEAR/2;
_PyTime_localtime(t, &p);
get_zone(julyname, 9, &p);
julyzone = -get_gmtoff(t, &p);
julyname[9] = '\0';
PyObject *tzname_obj;
if (janzone < julyzone) {
/* DST is reversed in the southern hemisphere */
PyModule_AddIntConstant(m, "timezone", julyzone);
PyModule_AddIntConstant(m, "altzone", janzone);
PyModule_AddIntConstant(m, "daylight", janzone != julyzone);
tzname_obj = Py_BuildValue("(zz)", julyname, janname);
} else {
PyModule_AddIntConstant(m, "timezone", janzone);
PyModule_AddIntConstant(m, "altzone", julyzone);
PyModule_AddIntConstant(m, "daylight", janzone != julyzone);
tzname_obj = Py_BuildValue("(zz)", janname, julyname);
}
if (tzname_obj == NULL)
return;
PyModule_AddObject(m, "tzname", tzname_obj);
#endif // !HAVE_DECL_TZNAME
} }
......
...@@ -396,6 +396,10 @@ Py_NO_ENABLE_SHARED to find out. Also support MS_NO_COREDLL for b/w compat */ ...@@ -396,6 +396,10 @@ Py_NO_ENABLE_SHARED to find out. Also support MS_NO_COREDLL for b/w compat */
/* Define to 1 if you have the <direct.h> header file. */ /* Define to 1 if you have the <direct.h> header file. */
#define HAVE_DIRECT_H 1 #define HAVE_DIRECT_H 1
/* Define to 1 if you have the declaration of `tzname', and to 0 if you don't.
*/
#define HAVE_DECL_TZNAME 1
/* Define if you have dirent.h. */ /* Define if you have dirent.h. */
/* #define DIRENT 1 */ /* #define DIRENT 1 */
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment