ENSMM / Mbed 2 deprecated RTC

Dependencies:   mbed

Fork of RTC by Joël Imbaud

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Main.cpp Source File

Main.cpp

00001 /*  https://os.mbed.com/docs/v5.7/reference/rtc.html
00002     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).
00003     
00004     https://fr.wikipedia.org/wiki/Heure_Unix
00005     
00006     https://www.epochconverter.com/programming/c
00007     
00008     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.
00009     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.
00010 
00011     https://os.mbed.com/blog/entry/103/
00012     
00013 */
00014 
00015 #include "mbed.h"
00016 
00017 int main() {
00018 
00019                 // Set the current time from the terminal
00020 
00021             struct tm t;
00022             printf("Enter current date and time:\n");
00023             printf("YYYY MM DD HH MM SS[enter]\n");
00024             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);
00025 
00026                 // Adjust for tm structure required values
00027             t.tm_year = t.tm_year - 1900;
00028             t.tm_mon = t.tm_mon - 1;
00029                 
00030                 // Set the time
00031             set_time(mktime(&t));
00032 
00033                 // Display the time
00034 
00035     while(1) {
00036                 time_t seconds = time(NULL);
00037                 printf("Time as a basic string = %s", ctime(&seconds));
00038                 wait(1);
00039                 char buffer[32];
00040                 strftime(buffer, 32, "%I:%M %p\n", localtime(&seconds));
00041                 printf("Time as a custom formatted string = %s", buffer);
00042                 wait(1);
00043             }
00044         }