Эх сурвалжийг харах

time_conversion: Move time zone conversion here and protect with mutex

Helmut Pozimski 1 жил өмнө
parent
commit
c4b281f57b

+ 31 - 0
main/time_conversion.c

@@ -0,0 +1,31 @@
+/*
+ * 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 "configuration.h"
+ 
+static SemaphoreHandle_t mutex = NULL;
+ 
+struct tm* convert_to_local(struct tm *timeinfo) {
+	if (mutex == NULL) {
+		mutex = xSemaphoreCreateMutex();
+	}
+	xSemaphoreTake(mutex, portMAX_DELAY);
+	setenv("TZ", "GMT+0GMT+0", 1);
+   tzset();
+   time_t epoch_time = mktime(timeinfo);
+   setenv("TZ", LOCAL_TIMEZONE, 1);
+   tzset();
+   struct tm* result = localtime(&epoch_time);
+   setenv("TZ", "GMT+0GMT+0", 1);
+	tzset();
+	xSemaphoreGive(mutex);
+	return result;
+}

+ 7 - 0
main/time_conversion.h

@@ -0,0 +1,7 @@
+/*
+ * SPDX-FileCopyrightText: 2023 Helmut Pozimski <helmut@pozimski.eu>
+ *
+ * SPDX-License-Identifier: GPL-2.0-only
+ */
+ 
+ struct tm* convert_to_local(struct tm *timeinfo);