time_conversion.c 686 B

12345678910111213141516171819202122232425262728293031
  1. /*
  2. * SPDX-FileCopyrightText: 2023 Helmut Pozimski <helmut@pozimski.eu>
  3. *
  4. * SPDX-License-Identifier: GPL-2.0-only
  5. */
  6. #include <freertos/FreeRTOS.h>
  7. #include <freertos/semphr.h>
  8. #include <time.h>
  9. #include "configuration.h"
  10. static SemaphoreHandle_t mutex = NULL;
  11. struct tm* convert_to_local(struct tm *timeinfo) {
  12. if (mutex == NULL) {
  13. mutex = xSemaphoreCreateMutex();
  14. }
  15. xSemaphoreTake(mutex, portMAX_DELAY);
  16. setenv("TZ", "GMT+0GMT+0", 1);
  17. tzset();
  18. time_t epoch_time = mktime(timeinfo);
  19. setenv("TZ", LOCAL_TIMEZONE, 1);
  20. tzset();
  21. struct tm* result = localtime(&epoch_time);
  22. setenv("TZ", "GMT+0GMT+0", 1);
  23. tzset();
  24. xSemaphoreGive(mutex);
  25. return result;
  26. }