Flesh out time module

This commit is contained in:
Camden Dixie O'Brien 2023-05-15 17:31:23 +01:00
parent 8be7ec1e28
commit cee2e368dc
5 changed files with 41 additions and 23 deletions

View File

@ -7,7 +7,7 @@
#include "display_driver.h" #include "display_driver.h"
#include "fatal.h" #include "fatal.h"
#include "time.h" #include "time_manager.h"
#include "esp_log.h" #include "esp_log.h"
#include "esp_timer.h" #include "esp_timer.h"
@ -57,7 +57,7 @@ static void show_time(unsigned hour, unsigned minute)
static void update_time(void *arg) static void update_time(void *arg)
{ {
(void)arg; (void)arg;
const Time time = time_get(); const Time time = get_time();
show_time(time.hour, time.minute); show_time(time.hour, time.minute);
} }

View File

@ -1,5 +1,5 @@
idf_component_register( idf_component_register(
SRCS "time.c" SRCS "time_manager.c"
INCLUDE_DIRS "." INCLUDE_DIRS "."
REQUIRES REQUIRES config
) )

View File

@ -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 };
}

View File

@ -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 <time.h>
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 };
}

View File

@ -3,8 +3,8 @@
* Copyright (c) Camden Dixie O'Brien * Copyright (c) Camden Dixie O'Brien
*/ */
#ifndef TIME_H #ifndef TIME_MANAGER_H
#define TIME_H #define TIME_MANAGER_H
typedef struct { typedef struct {
unsigned hour; unsigned hour;
@ -14,11 +14,11 @@ typedef struct {
/** /**
* Initialize the time module. * Initialize the time module.
*/ */
void time_init(void); void time_manager_init(void);
/** /**
* Get the current time. * Get the current time.
*/ */
Time time_get(void); Time get_time(void);
#endif #endif