Skip to content

Commit 5d0fda7

Browse files
add Time example
1 parent 73f5e9f commit 5d0fda7

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Time example
2+
3+
The time interface is used to access the Real Time Clock (RTC). The time is set as an offset measured in seconds from the time epoch, which is library specific. In general the accepted time epoch is the Unix Epoch. An online converter between human readable time and Unix Epoch time is handy, try this one. If the system is not battery powered then on each reset the rtc time will be reset. Make sure to either provide battery power to keep the time or to reset it each time the device is run.
4+
5+
There is no official time API, instead you use the functions in the example code.
6+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright (c) 2006-2020 Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
#include "mbed.h"
6+
7+
8+
int main()
9+
{
10+
set_time(1256729737); // Set RTC time to Wed, 28 Oct 2009 11:35:37
11+
12+
while (true) {
13+
time_t seconds = time(NULL);
14+
15+
printf("Time as seconds since January 1, 1970 = %u\n", (unsigned int)seconds);
16+
17+
printf("Time as a basic string = %s", ctime(&seconds));
18+
19+
char buffer[32];
20+
strftime(buffer, 32, "%I:%M %p\n", localtime(&seconds));
21+
printf("Time as a custom formatted string = %s", buffer);
22+
23+
ThisThread::sleep_for(1000);
24+
}
25+
}

0 commit comments

Comments
 (0)