he RTC (Real-Time Clock) class 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).
Dependencies: mbed
Fork of RTC by
Main.cpp@0:57aacc22012b, 2019-01-08 (annotated)
- Committer:
- jimbaud
- Date:
- Tue Jan 08 10:33:50 2019 +0000
- Revision:
- 0:57aacc22012b
he RTC (Real-Time Clock) class 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).
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jimbaud | 0:57aacc22012b | 1 | /* https://os.mbed.com/docs/v5.7/reference/rtc.html |
jimbaud | 0:57aacc22012b | 2 | The RTC (Real-Time Clock) class 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). |
jimbaud | 0:57aacc22012b | 3 | |
jimbaud | 0:57aacc22012b | 4 | https://fr.wikipedia.org/wiki/Heure_Unix |
jimbaud | 0:57aacc22012b | 5 | |
jimbaud | 0:57aacc22012b | 6 | https://www.epochconverter.com/programming/c |
jimbaud | 0:57aacc22012b | 7 | |
jimbaud | 0:57aacc22012b | 8 | You can use the attach_rtc API to hook external RTC for using C time functions. It provides you with init(), read(), write() and isenabled() functions to be attached. Time provides more information about C date and time standard library functions. |
jimbaud | 0:57aacc22012b | 9 | 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. |
jimbaud | 0:57aacc22012b | 10 | |
jimbaud | 0:57aacc22012b | 11 | https://os.mbed.com/blog/entry/103/ |
jimbaud | 0:57aacc22012b | 12 | |
jimbaud | 0:57aacc22012b | 13 | */ |
jimbaud | 0:57aacc22012b | 14 | |
jimbaud | 0:57aacc22012b | 15 | #include "mbed.h" |
jimbaud | 0:57aacc22012b | 16 | |
jimbaud | 0:57aacc22012b | 17 | int main() { |
jimbaud | 0:57aacc22012b | 18 | |
jimbaud | 0:57aacc22012b | 19 | // Set the current time from the terminal |
jimbaud | 0:57aacc22012b | 20 | |
jimbaud | 0:57aacc22012b | 21 | struct tm t; |
jimbaud | 0:57aacc22012b | 22 | printf("Enter current date and time:\n"); |
jimbaud | 0:57aacc22012b | 23 | printf("YYYY MM DD HH MM SS[enter]\n"); |
jimbaud | 0:57aacc22012b | 24 | scanf("%d %d %d %d %d %d", &t.tm_year, &t.tm_mon, &t.tm_mday, &t.tm_hour, &t.tm_min, &t.tm_sec); |
jimbaud | 0:57aacc22012b | 25 | |
jimbaud | 0:57aacc22012b | 26 | // Adjust for tm structure required values |
jimbaud | 0:57aacc22012b | 27 | t.tm_year = t.tm_year - 1900; |
jimbaud | 0:57aacc22012b | 28 | t.tm_mon = t.tm_mon - 1; |
jimbaud | 0:57aacc22012b | 29 | |
jimbaud | 0:57aacc22012b | 30 | // Set the time |
jimbaud | 0:57aacc22012b | 31 | set_time(mktime(&t)); |
jimbaud | 0:57aacc22012b | 32 | |
jimbaud | 0:57aacc22012b | 33 | // Display the time |
jimbaud | 0:57aacc22012b | 34 | |
jimbaud | 0:57aacc22012b | 35 | while(1) { |
jimbaud | 0:57aacc22012b | 36 | time_t seconds = time(NULL); |
jimbaud | 0:57aacc22012b | 37 | printf("Time as a basic string = %s", ctime(&seconds)); |
jimbaud | 0:57aacc22012b | 38 | wait(1); |
jimbaud | 0:57aacc22012b | 39 | char buffer[32]; |
jimbaud | 0:57aacc22012b | 40 | strftime(buffer, 32, "%I:%M %p\n", localtime(&seconds)); |
jimbaud | 0:57aacc22012b | 41 | printf("Time as a custom formatted string = %s", buffer); |
jimbaud | 0:57aacc22012b | 42 | wait(1); |
jimbaud | 0:57aacc22012b | 43 | } |
jimbaud | 0:57aacc22012b | 44 | } |