Jelajahi Sumber

time_conversion: Move time zone conversion here and protect with mutex

Helmut Pozimski 1 tahun lalu
induk
melakukan
c4b281f57b
2 mengubah file dengan 38 tambahan dan 0 penghapusan
  1. 31 0
      main/time_conversion.c
  2. 7 0
      main/time_conversion.h

+ 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);