Add date support to time manager

This commit is contained in:
Camden Dixie O'Brien 2023-05-16 22:32:19 +01:00
parent a4a4a035f2
commit 2ca513b98d
2 changed files with 78 additions and 3 deletions

View File

@ -19,6 +19,9 @@
#define NVS_NAMESPACE "time" #define NVS_NAMESPACE "time"
#define TIMESTAMP_KEY "timestamp" #define TIMESTAMP_KEY "timestamp"
#define TM_YEAR_OFFSET 1900
#define TM_MONTH_OFFSET 1
static void handle_timezone_update(const char *timezone) static void handle_timezone_update(const char *timezone)
{ {
setenv("TZ", timezone, 1); setenv("TZ", timezone, 1);
@ -82,7 +85,7 @@ static int store_time(void)
return error == ESP_OK ? 0 : 1; 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) { if (argc == 1) {
const Time time = get_time(); 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) static void time_saver_func(void *arg)
{ {
(void)arg; (void)arg;
@ -139,8 +165,11 @@ void time_manager_init(void)
settings_add_sntp_server_callback(&handle_sntp_server_update); settings_add_sntp_server_callback(&handle_sntp_server_update);
console_register( console_register(
"time", "Get / set time and SNTP status", "time", "Get, set or store the time, or get SNTP status",
"time OR time <HH:MM> OR time sntp-status", command_func); "time [HH:MM] OR time <sntp-status|store>", time_command_func);
console_register(
"date", "Get or set the date", "date [yyyy-mm-dd]",
date_command_func);
// Attempt to load time from storage // Attempt to load time from storage
esp_err_t error; esp_err_t error;
@ -187,6 +216,36 @@ void set_time(Time time)
timeinfo.tm_hour = time.hour; timeinfo.tm_hour = time.hour;
timeinfo.tm_min = time.minute; 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); tv.tv_sec = mktime(&timeinfo);
settimeofday(&tv, NULL); settimeofday(&tv, NULL);
} }

View File

@ -11,6 +11,12 @@ typedef struct {
unsigned minute; unsigned minute;
} Time; } Time;
typedef struct {
unsigned year;
unsigned month;
unsigned day;
} Date;
/** /**
* Initialize the time module. * Initialize the time module.
*/ */
@ -26,4 +32,14 @@ Time get_time(void);
*/ */
void set_time(Time time); void set_time(Time time);
/**
* Get the current date.
*/
Date get_date(void);
/**
* Set the date.
*/
void set_date(Date date);
#endif #endif