Add seconds to Time type, handle in time component

This commit is contained in:
Camden Dixie O'Brien 2023-05-18 01:24:42 +01:00
parent 7926ccc6fa
commit d7dd2025b8
2 changed files with 15 additions and 7 deletions

View File

@ -45,7 +45,7 @@ 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();
printf("%02u:%02u\n", time.hour, time.minute); printf("%02u:%02u:%02u\n", time.hour, time.minute, time.second);
return 0; return 0;
} else if (argc == 2) { } else if (argc == 2) {
if (strcmp(argv[1], "store") == 0) { if (strcmp(argv[1], "store") == 0) {
@ -53,9 +53,11 @@ static int time_command_func(int argc, char **argv)
return 0; return 0;
} else { } else {
Time time; Time time;
const int result const int result = sscanf(
= sscanf(argv[1], "%02u:%02u", &time.hour, &time.minute); argv[1], "%02u:%02u:%02u", &time.hour, &time.minute,
if (result < 2 || time.hour > 23 || time.minute > 59) { &time.second);
if (result < 2 || time.hour > 23 || time.minute > 59
|| time.second > 59) {
printf("Invalid time\n"); printf("Invalid time\n");
return 1; return 1;
} }
@ -128,8 +130,8 @@ void time_manager_init(void)
} }
console_register( console_register(
"time", "Get, set or store the time", "time [HH:MM] OR time store", "time", "Get, set or store the time",
time_command_func); "time [hh:mm:ss] OR time store", time_command_func);
console_register( console_register(
"date", "Get or set the date, or get the day of the week", "date", "Get or set the date, or get the day of the week",
"date [yyyy-mm-dd] OR date week-day", date_command_func); "date [yyyy-mm-dd] OR date week-day", date_command_func);
@ -152,7 +154,11 @@ Time get_time(void)
struct tm timeinfo; struct tm timeinfo;
(void)localtime_r(&tv.tv_sec, &timeinfo); (void)localtime_r(&tv.tv_sec, &timeinfo);
return (Time) { .hour = timeinfo.tm_hour, .minute = timeinfo.tm_min }; return (Time) {
.hour = timeinfo.tm_hour,
.minute = timeinfo.tm_min,
.second = timeinfo.tm_sec,
};
} }
void set_time(Time time) void set_time(Time time)
@ -164,6 +170,7 @@ 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;
timeinfo.tm_sec = time.second;
tv.tv_sec = mktime(&timeinfo); tv.tv_sec = mktime(&timeinfo);
settimeofday(&tv, NULL); settimeofday(&tv, NULL);

View File

@ -9,6 +9,7 @@
typedef struct { typedef struct {
unsigned hour; unsigned hour;
unsigned minute; unsigned minute;
unsigned second;
} Time; } Time;
typedef struct { typedef struct {