Skip to content

Commit afde1c1

Browse files
authored
Simplify PyInit_timezone. (GH-9323)
Assume tzname exists. Only use a hack to compute altzone if it's not defined.
1 parent 24f6846 commit afde1c1

File tree

1 file changed

+15
-68
lines changed

1 file changed

+15
-68
lines changed

Modules/timemodule.c

Lines changed: 15 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1522,29 +1522,6 @@ PyDoc_STRVAR(get_clock_info_doc,
15221522
\n\
15231523
Get information of the specified clock.");
15241524

1525-
#if !defined(HAVE_TZNAME) || defined(__GLIBC__) || defined(__CYGWIN__)
1526-
static void
1527-
get_zone(char *zone, int n, struct tm *p)
1528-
{
1529-
#ifdef HAVE_STRUCT_TM_TM_ZONE
1530-
strncpy(zone, p->tm_zone ? p->tm_zone : " ", n);
1531-
#else
1532-
tzset();
1533-
strftime(zone, n, "%Z", p);
1534-
#endif
1535-
}
1536-
1537-
static int
1538-
get_gmtoff(time_t t, struct tm *p)
1539-
{
1540-
#ifdef HAVE_STRUCT_TM_TM_ZONE
1541-
return p->tm_gmtoff;
1542-
#else
1543-
return timegm(p) - t;
1544-
#endif
1545-
}
1546-
#endif /* !defined(HAVE_TZNAME) || defined(__GLIBC__) || defined(__CYGWIN__) */
1547-
15481525
static void
15491526
PyInit_timezone(PyObject *m) {
15501527
/* This code moved from PyInit_time wholesale to allow calling it from
@@ -1563,65 +1540,35 @@ PyInit_timezone(PyObject *m) {
15631540
15641541
And I'm lazy and hate C so nyer.
15651542
*/
1566-
#if defined(HAVE_TZNAME) && !defined(__GLIBC__) && !defined(__CYGWIN__)
15671543
PyObject *otz0, *otz1;
15681544
tzset();
15691545
PyModule_AddIntConstant(m, "timezone", timezone);
15701546
#ifdef HAVE_ALTZONE
15711547
PyModule_AddIntConstant(m, "altzone", altzone);
1572-
#else
1573-
PyModule_AddIntConstant(m, "altzone", timezone-3600);
1574-
#endif
1575-
PyModule_AddIntConstant(m, "daylight", daylight);
1576-
otz0 = PyUnicode_DecodeLocale(tzname[0], "surrogateescape");
1577-
otz1 = PyUnicode_DecodeLocale(tzname[1], "surrogateescape");
1578-
PyModule_AddObject(m, "tzname", Py_BuildValue("(NN)", otz0, otz1));
1579-
#else /* !HAVE_TZNAME || __GLIBC__ || __CYGWIN__*/
1548+
#elif defined(HAVE_STRUCT_TM_TM_ZONE)
15801549
{
1581-
#define YEAR ((time_t)((365 * 24 + 6) * 3600))
1550+
static const time_t YEAR = (365 * 24 + 6) * 3600;
15821551
time_t t;
15831552
struct tm p;
15841553
long janzone, julyzone;
1585-
char janname[10], julyname[10];
15861554
t = (time((time_t *)0) / YEAR) * YEAR;
15871555
_PyTime_localtime(t, &p);
1588-
get_zone(janname, 9, &p);
1589-
janzone = -get_gmtoff(t, &p);
1590-
janname[9] = '\0';
1556+
janzone = -p.tm_gmtoff;
15911557
t += YEAR/2;
15921558
_PyTime_localtime(t, &p);
1593-
get_zone(julyname, 9, &p);
1594-
julyzone = -get_gmtoff(t, &p);
1595-
julyname[9] = '\0';
1596-
1597-
if( janzone < julyzone ) {
1598-
/* DST is reversed in the southern hemisphere */
1599-
PyModule_AddIntConstant(m, "timezone", julyzone);
1600-
PyModule_AddIntConstant(m, "altzone", janzone);
1601-
PyModule_AddIntConstant(m, "daylight",
1602-
janzone != julyzone);
1603-
PyModule_AddObject(m, "tzname",
1604-
Py_BuildValue("(zz)",
1605-
julyname, janname));
1606-
} else {
1607-
PyModule_AddIntConstant(m, "timezone", janzone);
1608-
PyModule_AddIntConstant(m, "altzone", julyzone);
1609-
PyModule_AddIntConstant(m, "daylight",
1610-
janzone != julyzone);
1611-
PyModule_AddObject(m, "tzname",
1612-
Py_BuildValue("(zz)",
1613-
janname, julyname));
1614-
}
1559+
julyzone = -p.tm_gmtoff;
1560+
1561+
// DST is reversed in the southern hemisphere.
1562+
PyModule_AddIntConstant(m, "altzone",
1563+
(janzone < julyzone) ? janzone : julyzone);
16151564
}
1616-
#ifdef __CYGWIN__
1617-
tzset();
1618-
PyModule_AddIntConstant(m, "timezone", _timezone);
1619-
PyModule_AddIntConstant(m, "altzone", _timezone-3600);
1620-
PyModule_AddIntConstant(m, "daylight", _daylight);
1621-
PyModule_AddObject(m, "tzname",
1622-
Py_BuildValue("(zz)", _tzname[0], _tzname[1]));
1623-
#endif /* __CYGWIN__ */
1624-
#endif /* !HAVE_TZNAME || __GLIBC__ || __CYGWIN__*/
1565+
#else
1566+
PyModule_AddIntConstant(m, "altzone", timezone-3600);
1567+
#endif
1568+
PyModule_AddIntConstant(m, "daylight", daylight);
1569+
otz0 = PyUnicode_DecodeLocale(tzname[0], "surrogateescape");
1570+
otz1 = PyUnicode_DecodeLocale(tzname[1], "surrogateescape");
1571+
PyModule_AddObject(m, "tzname", Py_BuildValue("(NN)", otz0, otz1));
16251572
}
16261573

16271574

0 commit comments

Comments
 (0)