12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- /*
- * SPDX-FileCopyrightText: 2023 Helmut Pozimski <helmut@pozimski.eu>
- *
- * SPDX-License-Identifier: GPL-2.0-only
- */
- #include <freertos/FreeRTOS.h>
- #include <freertos/semphr.h>
- #include <time.h>
- #include <string.h>
- #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;
- }
|