Skip to content

Commit 7dab47b

Browse files
author
Qinghao Shi
authored
Merge pull request #76 from maciejbocianski/move_time_example
add Time example
2 parents c5f9774 + ae587ea commit 7dab47b

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Time example
2+
3+
You can use the time interface 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. The accepted time epoch is the [Unix Epoch](https://en.wikipedia.org/wiki/Unix_time).
4+
5+
**Tip:** An online converter between human readable time and Unix Epoch time is useful, such as the [EpochConverter](https://www.epochconverter.com/).</span>
6+
7+
If the system is not battery powered, the RTC time resets at each power on, or cold reset. Make sure to either provide battery power to keep the time or to set it each time the device starts. During software or warm reset (for example, watchdog reset) RTC counts without interrupt.
8+
9+
There is no official time API. Instead, use the functions in the example code.
10+
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)