diff -r 55fa8e38a5f0 Modules/timemodule.c --- a/Modules/timemodule.c Wed Sep 14 10:26:20 2016 +0200 +++ b/Modules/timemodule.c Wed Sep 14 14:34:51 2016 +0200 @@ -339,25 +339,38 @@ return 1; } +#ifdef MS_WINDOWS +static struct tm *localtime_r(const time_t *timep, struct tm *result) +{ + if (localtime_s(result, timep) == 0) + return result; + return NULL; +} +static struct tm *gmtime_r(const time_t *timep, struct tm *result) +{ + if (gmtime_s(result, timep) == 0) + return result; + return NULL; +} +#endif + static PyObject * time_gmtime(PyObject *self, PyObject *args) { time_t when; - struct tm buf, *local; + struct tm buf; if (!parse_time_t_args(args, "|O:gmtime", &when)) return NULL; errno = 0; - local = gmtime(&when); - if (local == NULL) { + if (gmtime_r(&when, &buf) == NULL) { #ifdef EINVAL if (errno == 0) errno = EINVAL; #endif return PyErr_SetFromErrno(PyExc_OSError); } - buf = *local; #ifdef HAVE_STRUCT_TM_TM_ZONE return tmtotuple(&buf); #else @@ -391,11 +404,8 @@ static int pylocaltime(time_t *timep, struct tm *result) { - struct tm *local; - assert (timep != NULL); - local = localtime(timep); - if (local == NULL) { + if (localtime_r(timep, result) == NULL) { /* unconvertible time */ #ifdef EINVAL if (errno == 0) @@ -404,7 +414,6 @@ PyErr_SetFromErrno(PyExc_OSError); return -1; } - *result = *local; return 0; } @@ -1239,18 +1248,18 @@ { #define YEAR ((time_t)((365 * 24 + 6) * 3600)) time_t t; - struct tm *p; + struct tm p; long janzone, julyzone; char janname[10], julyname[10]; t = (time((time_t *)0) / YEAR) * YEAR; - p = localtime(&t); - get_zone(janname, 9, p); - janzone = -get_gmtoff(t, p); + localtime_r(&t, &p); + get_zone(janname, 9, &p); + janzone = -get_gmtoff(t, &p); janname[9] = '\0'; t += YEAR/2; - p = localtime(&t); - get_zone(julyname, 9, p); - julyzone = -get_gmtoff(t, p); + localtime_r(&t, &p); + get_zone(julyname, 9, &p); + julyzone = -get_gmtoff(t, &p); julyname[9] = '\0'; if( janzone < julyzone ) {