From 22894ddef07cbe1a0f2277cec61e7b8a83614c7d Mon Sep 17 00:00:00 2001 From: Even Rouault Date: Thu, 11 Jan 2024 12:07:19 +0100 Subject: [PATCH] degrib: use gmtime_r() or gmtime_s() when possible --- frmts/grib/degrib/degrib/clock.c | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/frmts/grib/degrib/degrib/clock.c b/frmts/grib/degrib/degrib/clock.c index c7e0ba5887dc..5791787d2354 100644 --- a/frmts/grib/degrib/degrib/clock.c +++ b/frmts/grib/degrib/degrib/clock.c @@ -712,7 +712,6 @@ sChar Clock_GetTimeZone () { struct tm l_time; time_t ansTime; - struct tm *gmTime; static int timeZone = 9999; if (timeZone == 9999) { @@ -721,10 +720,21 @@ sChar Clock_GetTimeZone () l_time.tm_year = 70; l_time.tm_mday = 2; ansTime = mktime (&l_time); - gmTime = gmtime (&ansTime); - timeZone = gmTime->tm_hour; - if (gmTime->tm_mday != 2) { - timeZone -= 24; +#if HAVE_GMTIME_R + struct tm gmTime; + const struct tm *gmTimePtr = gmtime_r(&ansTime, &gmTime); +#elif defined(_WIN32) + struct tm gmTime; + const struct tm *gmTimePtr = gmtime_s(&gmTime, &ansTime) == 0 ? &gmTime : NULL; +#else + const struct tm *gmTimePtr = gmtime (&ansTime); +#endif + if (gmTimePtr) + { + timeZone = gmTimePtr->tm_hour; + if (gmTimePtr->tm_mday != 2) { + timeZone -= 24; + } } } return timeZone;