MSVC: compat: implement gmtime_r()

Technically, there's no need for gmtime_r() because Windows gmtime() is
thread-safe already, but we're using these thread-safe versions
almost everywhere already.
diff --git a/CMakeModules/UseCompat.cmake b/CMakeModules/UseCompat.cmake
index 08c29a2..c1befd7 100644
--- a/CMakeModules/UseCompat.cmake
+++ b/CMakeModules/UseCompat.cmake
@@ -53,6 +53,7 @@
 
     check_symbol_exists(realpath "stdlib.h" HAVE_REALPATH)
     check_symbol_exists(localtime_r "time.h" HAVE_LOCALTIME_R)
+    check_symbol_exists(gmtime_r "time.h" HAVE_GMTIME_R)
     check_symbol_exists(strptime "time.h" HAVE_STRPTIME)
     check_symbol_exists(mmap "sys/mman.h" HAVE_MMAP)
     check_symbol_exists(dirname "libgen.h" HAVE_DIRNAME)
diff --git a/compat/compat.c b/compat/compat.c
index 1290cac..d11dbc2 100644
--- a/compat/compat.c
+++ b/compat/compat.c
@@ -290,6 +290,25 @@
 #endif
 #endif
 
+#ifndef HAVE_GMTIME_R
+#ifdef _WIN32
+struct tm *
+gmtime_r(const time_t *timep, struct tm *result)
+{
+    errno_t res = gmtime_s(result, timep);
+
+    if (res) {
+        return NULL;
+    } else {
+        return result;
+    }
+}
+
+#else
+#error No gmtime_r() implementation for this platform is available.
+#endif
+#endif
+
 #ifndef HAVE_DIRNAME
 #ifdef _WIN32
 #include <shlwapi.h>
diff --git a/compat/compat.h.in b/compat/compat.h.in
index 0f99765..c697d6c 100644
--- a/compat/compat.h.in
+++ b/compat/compat.h.in
@@ -69,6 +69,7 @@
 #cmakedefine HAVE_TIME_H_TIMEZONE
 #cmakedefine HAVE_REALPATH
 #cmakedefine HAVE_LOCALTIME_R
+#cmakedefine HAVE_GMTIME_R
 #cmakedefine HAVE_STRPTIME
 #cmakedefine HAVE_MMAP
 #cmakedefine HAVE_DIRNAME