compat UPDATE embedded platform improvements
diff --git a/CMakeModules/UseCompat.cmake b/CMakeModules/UseCompat.cmake
index ef3df89..8fe9f2c 100644
--- a/CMakeModules/UseCompat.cmake
+++ b/CMakeModules/UseCompat.cmake
@@ -24,11 +24,10 @@
 
 macro(USE_COMPAT)
     # compatibility checks
-    set(CMAKE_REQUIRED_DEFINITIONS -D_POSIX_C_SOURCE=200809L)
+    list(APPEND CMAKE_REQUIRED_DEFINITIONS -D_POSIX_C_SOURCE=200809L)
     list(APPEND CMAKE_REQUIRED_DEFINITIONS -D_GNU_SOURCE)
     list(APPEND CMAKE_REQUIRED_DEFINITIONS -D__BSD_VISIBLE=1)
     list(APPEND CMAKE_REQUIRED_DEFINITIONS -D_DEFAULT_SOURCE)
-    set(CMAKE_REQUIRED_LIBRARIES pthread)
 
     check_symbol_exists(vdprintf "stdio.h;stdarg.h" HAVE_VDPRINTF)
     check_symbol_exists(asprintf "stdio.h" HAVE_ASPRINTF)
@@ -42,9 +41,13 @@
 
     check_symbol_exists(get_current_dir_name "unistd.h" HAVE_GET_CURRENT_DIR_NAME)
 
+    set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
+    find_package(Threads)
+    list(APPEND CMAKE_REQUIRED_LIBRARIES ${CMAKE_THREAD_LIBS_INIT})
     check_function_exists(pthread_mutex_timedlock HAVE_PTHREAD_MUTEX_TIMEDLOCK)
+    list(REMOVE_ITEM CMAKE_REQUIRED_LIBRARIES ${CMAKE_THREAD_LIBS_INIT})
 
-    TEST_BIG_ENDIAN(IS_BIG_ENDIAN)
+    test_big_endian(IS_BIG_ENDIAN)
 
     check_include_file("stdatomic.h" HAVE_STDATOMIC)
 
@@ -56,8 +59,10 @@
     check_symbol_exists(mmap "sys/mman.h" HAVE_MMAP)
     check_symbol_exists(setenv "stdlib.h" HAVE_SETENV)
 
-    unset(CMAKE_REQUIRED_DEFINITIONS)
-    unset(CMAKE_REQUIRED_LIBRARIES)
+    list(REMOVE_ITEM CMAKE_REQUIRED_DEFINITIONS -D_POSIX_C_SOURCE=200809L)
+    list(REMOVE_ITEM CMAKE_REQUIRED_DEFINITIONS -D_GNU_SOURCE)
+    list(REMOVE_ITEM CMAKE_REQUIRED_DEFINITIONS -D__BSD_VISIBLE=1)
+    list(REMOVE_ITEM CMAKE_REQUIRED_DEFINITIONS -D_DEFAULT_SOURCE)
 
     # header and source file (adding the source directly allows for hiding its symbols)
     configure_file(${PROJECT_SOURCE_DIR}/compat/compat.h.in ${PROJECT_BINARY_DIR}/compat/compat.h @ONLY)
diff --git a/compat/compat.c b/compat/compat.c
index 24f235c..380c756 100644
--- a/compat/compat.c
+++ b/compat/compat.c
@@ -325,6 +325,40 @@
 #endif
 #endif
 
+#ifndef HAVE_TIMEGM
+time_t
+timegm(struct tm *tm)
+{
+    pthread_mutex_t tz_lock = PTHREAD_MUTEX_INITIALIZER;
+    time_t ret;
+    char *tz;
+
+    pthread_mutex_lock(&tz_lock);
+
+    tz = getenv("TZ");
+    if (tz) {
+        tz = strdup(tz);
+    }
+    setenv("TZ", "", 1);
+    tzset();
+
+    ret = mktime(tm);
+
+    if (tz) {
+        setenv("TZ", tz, 1);
+        free(tz);
+    } else {
+        unsetenv("TZ");
+    }
+    tzset();
+
+    pthread_mutex_unlock(&tz_lock);
+
+    return ret;
+}
+
+#endif
+
 #ifndef HAVE_SETENV
 #ifdef _WIN32
 int
diff --git a/compat/compat.h.in b/compat/compat.h.in
index 3baa489..49468ab 100644
--- a/compat/compat.h.in
+++ b/compat/compat.h.in
@@ -194,7 +194,7 @@
 # if defined (_WIN32)
 #  define timegm _mkgmtime
 # else
-#  error No timegm() implementation for this platform is available.
+time_t timegm(struct tm *tm);
 # endif
 #endif