RTC
The RTC (Real-Time Clock) provides mechanisms to set the current time of the hardware RTC with set_time
API. The time is set as an offset measured in seconds from the time epoch (Unix Epoch - January 1, 1970). Time provides more information about C date
and time
standard library functions.
Note: If your MCU does not have an RTC nor an LPTICKER, you need to provide hooks to the external RTC you are using to the attach_rtc
API. See the platform RTC API for more details about that function.
RTC class APIs are thread safe.
RTC can keep track of time even in a powered down state if the secondary source of power (battery) is connected.
RTC function reference
RTC Time example
/*
* Copyright (c) 2006-2020 Arm Limited and affiliates.
* SPDX-License-Identifier: Apache-2.0
*/
#include "mbed.h"
int main()
{
set_time(1256729737); // Set RTC time to Wed, 28 Oct 2009 11:35:37
while (true) {
time_t seconds = time(NULL);
printf("Time as seconds since January 1, 1970 = %u\n", (unsigned int)seconds);
printf("Time as a basic string = %s", ctime(&seconds));
char buffer[32];
strftime(buffer, 32, "%I:%M %p\n", localtime(&seconds));
printf("Time as a custom formatted string = %s", buffer);
ThisThread::sleep_for(1000);
}
}