application: fix date display and add offset
This commit is contained in:
parent
9417cbf392
commit
fdef34e663
@ -65,6 +65,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;
|
||||
|
||||
@ -435,9 +442,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
|
||||
@ -450,15 +458,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
|
||||
|
Loading…
Reference in New Issue
Block a user