From cee2e368dc7cc05d180997365d8243004c53d774 Mon Sep 17 00:00:00 2001 From: Camden Dixie O'Brien Date: Mon, 15 May 2023 17:31:23 +0100 Subject: [PATCH] Flesh out time module --- components/display/display.c | 4 +-- components/time/CMakeLists.txt | 4 +-- components/time/time.c | 15 ---------- components/time/time_manager.c | 33 ++++++++++++++++++++++ components/time/{time.h => time_manager.h} | 8 +++--- 5 files changed, 41 insertions(+), 23 deletions(-) delete mode 100644 components/time/time.c create mode 100644 components/time/time_manager.c rename components/time/{time.h => time_manager.h} (70%) diff --git a/components/display/display.c b/components/display/display.c index 22f8424..c0be9f3 100644 --- a/components/display/display.c +++ b/components/display/display.c @@ -7,7 +7,7 @@ #include "display_driver.h" #include "fatal.h" -#include "time.h" +#include "time_manager.h" #include "esp_log.h" #include "esp_timer.h" @@ -57,7 +57,7 @@ static void show_time(unsigned hour, unsigned minute) static void update_time(void *arg) { (void)arg; - const Time time = time_get(); + const Time time = get_time(); show_time(time.hour, time.minute); } diff --git a/components/time/CMakeLists.txt b/components/time/CMakeLists.txt index 956f92b..130f44d 100644 --- a/components/time/CMakeLists.txt +++ b/components/time/CMakeLists.txt @@ -1,5 +1,5 @@ idf_component_register( - SRCS "time.c" + SRCS "time_manager.c" INCLUDE_DIRS "." - REQUIRES + REQUIRES config ) diff --git a/components/time/time.c b/components/time/time.c deleted file mode 100644 index d514eaf..0000000 --- a/components/time/time.c +++ /dev/null @@ -1,15 +0,0 @@ -/* - * SPDX-License-Identifier: AGPL-3.0-only - * Copyright (c) Camden Dixie O'Brien - */ - -#include "time.h" - -void time_init(void) -{ -} - -Time time_get(void) -{ - return (Time){ .hour = 13, .minute = 37 }; -} diff --git a/components/time/time_manager.c b/components/time/time_manager.c new file mode 100644 index 0000000..b8cf755 --- /dev/null +++ b/components/time/time_manager.c @@ -0,0 +1,33 @@ +/* + * SPDX-License-Identifier: AGPL-3.0-only + * Copyright (c) Camden Dixie O'Brien + */ + +#include "time_manager.h" + +#include "config.h" + +#include + +static void handle_timezone_update(const char *timezone) +{ + setenv("TZ", timezone, 1); + tzset(); +} + +void time_manager_init(void) +{ + char timezone[CONFIG_MAX_VALUE_SIZE]; + (void)config_get_timezone(timezone, CONFIG_MAX_VALUE_SIZE); + handle_timezone_update(timezone); + + config_add_timezone_callback(&handle_timezone_update); +} + +Time get_time(void) +{ + const time_t now = time(NULL); + struct tm timeinfo; + (void)localtime_r(&now, &timeinfo); + return (Time) { .hour = timeinfo.tm_hour, .minute = timeinfo.tm_min }; +} diff --git a/components/time/time.h b/components/time/time_manager.h similarity index 70% rename from components/time/time.h rename to components/time/time_manager.h index 843d9c1..41e83a2 100644 --- a/components/time/time.h +++ b/components/time/time_manager.h @@ -3,8 +3,8 @@ * Copyright (c) Camden Dixie O'Brien */ -#ifndef TIME_H -#define TIME_H +#ifndef TIME_MANAGER_H +#define TIME_MANAGER_H typedef struct { unsigned hour; @@ -14,11 +14,11 @@ typedef struct { /** * Initialize the time module. */ -void time_init(void); +void time_manager_init(void); /** * Get the current time. */ -Time time_get(void); +Time get_time(void); #endif