get_gmtoff() now returns time_t (GH-10838) (GH-10839)

get_gmtoff() now returns time_t instead of int to fix the following
Visual Studio warning:

    Modules\timemodule.c(1183): warning C4244: 'return':
    conversion from 'time_t' to 'int', possible loss of data

(cherry picked from commit 503ce5c482)
This commit is contained in:
Victor Stinner 2018-12-01 01:24:21 +01:00 committed by GitHub
parent 38bed786a2
commit 38c06d9193
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 29 additions and 9 deletions

View File

@ -994,7 +994,7 @@ of the timezone or altzone attributes on the time module.");
#endif /* HAVE_MKTIME */ #endif /* HAVE_MKTIME */
#ifdef HAVE_WORKING_TZSET #ifdef HAVE_WORKING_TZSET
static void PyInit_timezone(PyObject *module); static int PyInit_timezone(PyObject *module);
static PyObject * static PyObject *
time_tzset(PyObject *self, PyObject *unused) time_tzset(PyObject *self, PyObject *unused)
@ -1009,7 +1009,9 @@ time_tzset(PyObject *self, PyObject *unused)
tzset(); tzset();
/* Reset timezone, altzone, daylight and tzname */ /* Reset timezone, altzone, daylight and tzname */
PyInit_timezone(m); if (PyInit_timezone(m) < 0) {
return NULL;
}
Py_DECREF(m); Py_DECREF(m);
if (PyErr_Occurred()) if (PyErr_Occurred())
return NULL; return NULL;
@ -1510,7 +1512,7 @@ get_zone(char *zone, int n, struct tm *p)
#endif #endif
} }
static int static time_t
get_gmtoff(time_t t, struct tm *p) get_gmtoff(time_t t, struct tm *p)
{ {
#ifdef HAVE_STRUCT_TM_TM_ZONE #ifdef HAVE_STRUCT_TM_TM_ZONE
@ -1521,8 +1523,11 @@ get_gmtoff(time_t t, struct tm *p)
} }
#endif /* !defined(HAVE_TZNAME) || defined(__GLIBC__) || defined(__CYGWIN__) */ #endif /* !defined(HAVE_TZNAME) || defined(__GLIBC__) || defined(__CYGWIN__) */
static void static int
PyInit_timezone(PyObject *m) { PyInit_timezone(PyObject *m)
{
assert(!PyErr_Occurred());
/* This code moved from PyInit_time wholesale to allow calling it from /* 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 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 (for platforms that don't HAVE_WORKING_TZSET, when we know what they
@ -1557,19 +1562,31 @@ PyInit_timezone(PyObject *m) {
#define YEAR ((time_t)((365 * 24 + 6) * 3600)) #define YEAR ((time_t)((365 * 24 + 6) * 3600))
time_t t; time_t t;
struct tm p; struct tm p;
long janzone, julyzone; time_t janzone_t, julyzone_t;
char janname[10], julyname[10]; char janname[10], julyname[10];
t = (time((time_t *)0) / YEAR) * YEAR; t = (time((time_t *)0) / YEAR) * YEAR;
_PyTime_localtime(t, &p); _PyTime_localtime(t, &p);
get_zone(janname, 9, &p); get_zone(janname, 9, &p);
janzone = -get_gmtoff(t, &p); janzone_t = -get_gmtoff(t, &p);
janname[9] = '\0'; janname[9] = '\0';
t += YEAR/2; t += YEAR/2;
_PyTime_localtime(t, &p); _PyTime_localtime(t, &p);
get_zone(julyname, 9, &p); get_zone(julyname, 9, &p);
julyzone = -get_gmtoff(t, &p); julyzone_t = -get_gmtoff(t, &p);
julyname[9] = '\0'; 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 ) { if( janzone < julyzone ) {
/* DST is reversed in the southern hemisphere */ /* DST is reversed in the southern hemisphere */
PyModule_AddIntConstant(m, "timezone", julyzone); PyModule_AddIntConstant(m, "timezone", julyzone);
@ -1598,6 +1615,7 @@ PyInit_timezone(PyObject *m) {
Py_BuildValue("(zz)", _tzname[0], _tzname[1])); Py_BuildValue("(zz)", _tzname[0], _tzname[1]));
#endif /* __CYGWIN__ */ #endif /* __CYGWIN__ */
#endif /* !HAVE_TZNAME || __GLIBC__ || __CYGWIN__*/ #endif /* !HAVE_TZNAME || __GLIBC__ || __CYGWIN__*/
return 0;
} }
@ -1698,7 +1716,9 @@ PyInit_time(void)
return NULL; return NULL;
/* Set, or reset, module variables like time.timezone */ /* 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) #if defined(HAVE_CLOCK_GETTIME) || defined(HAVE_CLOCK_SETTIME) || defined(HAVE_CLOCK_GETRES)