/* * SPDX-FileCopyrightText: 2023 Helmut Pozimski * * SPDX-License-Identifier: GPL-2.0-only */ #include #include #include #include #include "configuration.h" static SemaphoreHandle_t mutex = NULL; static char* tz = NULL; static void initialize_tz() { unsetenv("TZ"); setenv("TZ", LOCAL_TIMEZONE, 1); tz = getenv("TZ"); } static void set_tz_gmt() { strncpy(tz, "GMT0", 5); tzset(); } static void set_tz_local() { strncpy(tz, LOCAL_TIMEZONE, strlen(LOCAL_TIMEZONE) +1); tzset(); } struct tm* convert_to_local(struct tm *timeinfo) { if (mutex == NULL) { mutex = xSemaphoreCreateMutex(); } xSemaphoreTake(mutex, portMAX_DELAY); if (tz == NULL) { initialize_tz(); } set_tz_gmt(); time_t epoch_time = mktime(timeinfo); set_tz_local(); struct tm* result = localtime(&epoch_time); set_tz_gmt(); xSemaphoreGive(mutex); return result; }