Skip to content

Commit 1fb399b

Browse files
authored
bpo-34715: Revert "Simplify PyInit_timezone. (GH-9323)" (GH-9366)
This reverts commit afde1c1.
1 parent 394374e commit 1fb399b

File tree

1 file changed

+68
-15
lines changed

1 file changed

+68
-15
lines changed

Modules/timemodule.c

Lines changed: 68 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1522,6 +1522,29 @@ 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+
15251548
static void
15261549
PyInit_timezone(PyObject *m) {
15271550
/* This code moved from PyInit_time wholesale to allow calling it from
@@ -1540,35 +1563,65 @@ PyInit_timezone(PyObject *m) {
15401563
15411564
And I'm lazy and hate C so nyer.
15421565
*/
1566+
#if defined(HAVE_TZNAME) && !defined(__GLIBC__) && !defined(__CYGWIN__)
15431567
PyObject *otz0, *otz1;
15441568
tzset();
15451569
PyModule_AddIntConstant(m, "timezone", timezone);
15461570
#ifdef HAVE_ALTZONE
15471571
PyModule_AddIntConstant(m, "altzone", altzone);
1548-
#elif defined(HAVE_STRUCT_TM_TM_ZONE)
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__*/
15491580
{
1550-
static const time_t YEAR = (365 * 24 + 6) * 3600;
1581+
#define YEAR ((time_t)((365 * 24 + 6) * 3600))
15511582
time_t t;
15521583
struct tm p;
15531584
long janzone, julyzone;
1585+
char janname[10], julyname[10];
15541586
t = (time((time_t *)0) / YEAR) * YEAR;
15551587
_PyTime_localtime(t, &p);
1556-
janzone = -p.tm_gmtoff;
1588+
get_zone(janname, 9, &p);
1589+
janzone = -get_gmtoff(t, &p);
1590+
janname[9] = '\0';
15571591
t += YEAR/2;
15581592
_PyTime_localtime(t, &p);
1559-
julyzone = -p.tm_gmtoff;
1560-
1561-
// DST is reversed in the southern hemisphere.
1562-
PyModule_AddIntConstant(m, "altzone",
1563-
(janzone < julyzone) ? janzone : julyzone);
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+
}
15641615
}
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));
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__*/
15721625
}
15731626

15741627

0 commit comments

Comments
 (0)