From 2ca513b98ddfb8281ad87f65578994c4e9fef1e5 Mon Sep 17 00:00:00 2001 From: Camden Dixie O'Brien Date: Tue, 16 May 2023 22:32:19 +0100 Subject: [PATCH] Add date support to time manager --- components/time/time_manager.c | 65 ++++++++++++++++++++++++++++++++-- components/time/time_manager.h | 16 +++++++++ 2 files changed, 78 insertions(+), 3 deletions(-) diff --git a/components/time/time_manager.c b/components/time/time_manager.c index d820769..521f3f2 100644 --- a/components/time/time_manager.c +++ b/components/time/time_manager.c @@ -19,6 +19,9 @@ #define NVS_NAMESPACE "time" #define TIMESTAMP_KEY "timestamp" +#define TM_YEAR_OFFSET 1900 +#define TM_MONTH_OFFSET 1 + static void handle_timezone_update(const char *timezone) { setenv("TZ", timezone, 1); @@ -82,7 +85,7 @@ static int store_time(void) return error == ESP_OK ? 0 : 1; } -static int command_func(int argc, char **argv) +static int time_command_func(int argc, char **argv) { if (argc == 1) { const Time time = get_time(); @@ -112,6 +115,29 @@ static int command_func(int argc, char **argv) } } +static int date_command_func(int argc, char **argv) +{ + if (argc == 1) { + const Date date = get_date(); + printf("%04u-%02u-%02u\n", date.year, date.month, date.day); + return 0; + } else if (argc == 2) { + Date date; + const int result = sscanf( + argv[1], "%04u-%02u-%02u", &date.year, &date.month, &date.day); + if (result != 3 || date.month == 0 || date.month > 12 + || date.day == 0 || date.day > 31) { + printf("Invalid date\n"); + return 1; + } + set_date(date); + return 0; + } else { + printf("Invalid number of arguments\n"); + return 1; + } +} + static void time_saver_func(void *arg) { (void)arg; @@ -139,8 +165,11 @@ void time_manager_init(void) settings_add_sntp_server_callback(&handle_sntp_server_update); console_register( - "time", "Get / set time and SNTP status", - "time OR time OR time sntp-status", command_func); + "time", "Get, set or store the time, or get SNTP status", + "time [HH:MM] OR time ", time_command_func); + console_register( + "date", "Get or set the date", "date [yyyy-mm-dd]", + date_command_func); // Attempt to load time from storage esp_err_t error; @@ -187,6 +216,36 @@ void set_time(Time time) timeinfo.tm_hour = time.hour; timeinfo.tm_min = time.minute; + + tv.tv_sec = mktime(&timeinfo); + settimeofday(&tv, NULL); +} + +Date get_date(void) +{ + struct timeval tv; + gettimeofday(&tv, NULL); + struct tm timeinfo; + (void)localtime_r(&tv.tv_sec, &timeinfo); + + return (Date) { + .year = timeinfo.tm_year + TM_YEAR_OFFSET, + .month = timeinfo.tm_mon + TM_MONTH_OFFSET, + .day = timeinfo.tm_mday, + }; +} + +void set_date(Date date) +{ + struct timeval tv; + gettimeofday(&tv, NULL); + struct tm timeinfo; + (void)localtime_r(&tv.tv_sec, &timeinfo); + + timeinfo.tm_year = date.year - TM_YEAR_OFFSET; + timeinfo.tm_mon = date.month - TM_MONTH_OFFSET; + timeinfo.tm_mday = date.day; + tv.tv_sec = mktime(&timeinfo); settimeofday(&tv, NULL); } diff --git a/components/time/time_manager.h b/components/time/time_manager.h index 0591634..123f60c 100644 --- a/components/time/time_manager.h +++ b/components/time/time_manager.h @@ -11,6 +11,12 @@ typedef struct { unsigned minute; } Time; +typedef struct { + unsigned year; + unsigned month; + unsigned day; +} Date; + /** * Initialize the time module. */ @@ -26,4 +32,14 @@ Time get_time(void); */ void set_time(Time time); +/** + * Get the current date. + */ +Date get_date(void); + +/** + * Set the date. + */ +void set_date(Date date); + #endif