From 99d4be6a441a0072d11c75fdaeb63464261465c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?King=20K=C3=A9vin?= Date: Thu, 5 Mar 2020 14:04:51 +0100 Subject: [PATCH] application: fix date display and add offset --- application.c | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/application.c b/application.c index f137ec2..355629a 100644 --- a/application.c +++ b/application.c @@ -52,6 +52,13 @@ */ #define RTC_TICKS_SECOND 4 +#if defined(RTC_DATE_TIME) && RTC_DATE_TIME +/** the start time from which to RTC ticks count + * @note this allows the 32-bit value to reach further in time, particularly when there are several ticks per second + */ +const time_t rtc_offset = 1577833200; // We 1. Jan 00:00:00 CET 2020 +#endif + /** RTC time when device is started */ static time_t time_start = 0; @@ -426,9 +433,10 @@ static void command_datetime(void* argument) { char* datetime = (char*)argument; // argument is optional date time if (NULL == argument) { // no date and time provided, just show the current day and time - time_t time_rtc = rtc_get_counter_val() / RTC_TICKS_SECOND; // get time from internal RTC - struct tm* time_tm = localtime(&time_rtc); // convert time - printf("date: %d-%02d-%02d %02d:%02d:%02d\n", 1900 + time_tm->tm_year, time_tm->tm_mon, time_tm->tm_mday, time_tm->tm_hour, time_tm->tm_min, time_tm->tm_sec); + const time_t time_rtc = rtc_get_counter_val() / RTC_TICKS_SECOND + rtc_offset; // get time from internal RTC + const struct tm* time_tm = localtime(&time_rtc); // convert time + const char* days[] = { "Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"}; // the days of the week + printf("date: %s %d-%02d-%02d %02d:%02d:%02d\n", days[time_tm->tm_wday], 1900 + time_tm->tm_year, 1 + time_tm->tm_mon, time_tm->tm_mday, time_tm->tm_hour, time_tm->tm_min, time_tm->tm_sec); } else { // date and time provided, set it const char* malformed = "date and time malformed, expecting YYYY-MM-DD HH:MM:SS\n"; struct tm time_tm; // to store the parsed date time @@ -441,15 +449,16 @@ static void command_datetime(void* argument) return; } time_tm.tm_year = strtol(&datetime[0], NULL, 10) - 1900; // parse year - time_tm.tm_mon = strtol(&datetime[5], NULL, 10); // parse month + time_tm.tm_mon = strtol(&datetime[5], NULL, 10) - 1; // parse month time_tm.tm_mday = strtol(&datetime[8], NULL, 10); // parse day time_tm.tm_hour = strtol(&datetime[11], NULL, 10); // parse hour time_tm.tm_min = strtol(&datetime[14], NULL, 10); // parse minutes time_tm.tm_sec = strtol(&datetime[17], NULL, 10); // parse seconds time_t time_rtc = mktime(&time_tm); // get back seconds + time_rtc -= rtc_offset; // remove start offset time_start = time_rtc * RTC_TICKS_SECOND + (rtc_get_counter_val() - time_start); // update uptime with current date rtc_set_counter_val(time_rtc * RTC_TICKS_SECOND); // save date/time to internal RTC - printf("date and time saved: %d-%02d-%02d %02d:%02d:%02d\n", 1900 + time_tm.tm_year, time_tm.tm_mon, time_tm.tm_mday, time_tm.tm_hour, time_tm.tm_min, time_tm.tm_sec); + printf("date and time saved: %d-%02d-%02d %02d:%02d:%02d\n", 1900 + time_tm.tm_year, 1 + time_tm.tm_mon, time_tm.tm_mday, time_tm.tm_hour, time_tm.tm_min, time_tm.tm_sec); } } #endif