Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 29 additions & 9 deletions Modules/timemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -994,7 +994,7 @@ of the timezone or altzone attributes on the time module.");
#endif /* HAVE_MKTIME */

#ifdef HAVE_WORKING_TZSET
static void PyInit_timezone(PyObject *module);
static int PyInit_timezone(PyObject *module);

static PyObject *
time_tzset(PyObject *self, PyObject *unused)
Expand All @@ -1009,7 +1009,9 @@ time_tzset(PyObject *self, PyObject *unused)
tzset();

/* Reset timezone, altzone, daylight and tzname */
PyInit_timezone(m);
if (PyInit_timezone(m) < 0) {
return NULL;
}
Py_DECREF(m);
if (PyErr_Occurred())
return NULL;
Expand Down Expand Up @@ -1510,7 +1512,7 @@ get_zone(char *zone, int n, struct tm *p)
#endif
}

static int
static time_t
get_gmtoff(time_t t, struct tm *p)
{
#ifdef HAVE_STRUCT_TM_TM_ZONE
Expand All @@ -1521,8 +1523,11 @@ get_gmtoff(time_t t, struct tm *p)
}
#endif /* !defined(HAVE_TZNAME) || defined(__GLIBC__) || defined(__CYGWIN__) */

static void
PyInit_timezone(PyObject *m) {
static int
PyInit_timezone(PyObject *m)
{
assert(!PyErr_Occurred());

/* 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
(for platforms that don't HAVE_WORKING_TZSET, when we know what they
Expand Down Expand Up @@ -1557,19 +1562,31 @@ PyInit_timezone(PyObject *m) {
#define YEAR ((time_t)((365 * 24 + 6) * 3600))
time_t t;
struct tm p;
long janzone, julyzone;
time_t janzone_t, julyzone_t;
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);
janzone_t = -get_gmtoff(t, &p);
janname[9] = '\0';
t += YEAR/2;
_PyTime_localtime(t, &p);
get_zone(julyname, 9, &p);
julyzone = -get_gmtoff(t, &p);
julyzone_t = -get_gmtoff(t, &p);
julyname[9] = '\0';

/* Sanity check, don't check for the validity of timezones.
In practice, it should be more in range -12 hours .. +14 hours. */
#define MAX_TIMEZONE (48 * 3600)
if (janzone_t < -MAX_TIMEZONE || janzone_t > MAX_TIMEZONE
|| julyzone_t < -MAX_TIMEZONE || julyzone_t > MAX_TIMEZONE)
{
PyErr_SetString(PyExc_RuntimeError, "invalid GMT offset");
return -1;
}
int janzone = (int)janzone_t;
int julyzone = (int)julyzone_t;

if( janzone < julyzone ) {
/* DST is reversed in the southern hemisphere */
PyModule_AddIntConstant(m, "timezone", julyzone);
Expand Down Expand Up @@ -1598,6 +1615,7 @@ PyInit_timezone(PyObject *m) {
Py_BuildValue("(zz)", _tzname[0], _tzname[1]));
#endif /* __CYGWIN__ */
#endif /* !HAVE_TZNAME || __GLIBC__ || __CYGWIN__*/
return 0;
}


Expand Down Expand Up @@ -1698,7 +1716,9 @@ PyInit_time(void)
return NULL;

/* Set, or reset, module variables like time.timezone */
PyInit_timezone(m);
if (PyInit_timezone(m) < 0) {
return NULL;
}

#if defined(HAVE_CLOCK_GETTIME) || defined(HAVE_CLOCK_SETTIME) || defined(HAVE_CLOCK_GETRES)

Expand Down