Skip to content

add Time example #76

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions APIs_Platform/time_HelloWorld/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Time example

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).

**Tip:** An online converter between human readable time and Unix Epoch time is useful, such as the [EpochConverter](https://www.epochconverter.com/).</span>

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.

There is no official time API. Instead, use the functions in the example code.

25 changes: 25 additions & 0 deletions APIs_Platform/time_HelloWorld/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* 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);
}
}