time Hello World

Fork of time_HelloWorld by Mbed

Use

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.

API

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

Committer:
mbedAustin
Date:
Fri Mar 27 20:18:01 2015 +0000
Revision:
2:c8b4159048f0
Parent:
0:b3b93997a0a6
Child:
5:0c6401d671c6
Added license to main.c file.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbedAustin 2:c8b4159048f0 1 /* mbed Example Program
mbedAustin 2:c8b4159048f0 2 * Copyright (c) 2006-2014 ARM Limited
mbedAustin 2:c8b4159048f0 3 *
mbedAustin 2:c8b4159048f0 4 * Licensed under the Apache License, Version 2.0 (the "License");
mbedAustin 2:c8b4159048f0 5 * you may not use this file except in compliance with the License.
mbedAustin 2:c8b4159048f0 6 * You may obtain a copy of the License at
mbedAustin 2:c8b4159048f0 7 *
mbedAustin 2:c8b4159048f0 8 * http://www.apache.org/licenses/LICENSE-2.0
mbedAustin 2:c8b4159048f0 9 *
mbedAustin 2:c8b4159048f0 10 * Unless required by applicable law or agreed to in writing, software
mbedAustin 2:c8b4159048f0 11 * distributed under the License is distributed on an "AS IS" BASIS,
mbedAustin 2:c8b4159048f0 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
mbedAustin 2:c8b4159048f0 13 * See the License for the specific language governing permissions and
mbedAustin 2:c8b4159048f0 14 * limitations under the License.
mbedAustin 2:c8b4159048f0 15 */
mbed_official 0:b3b93997a0a6 16 #include "mbed.h"
mbed_official 0:b3b93997a0a6 17
mbed_official 0:b3b93997a0a6 18 int main() {
mbed_official 0:b3b93997a0a6 19 set_time(1256729737); // Set RTC time to Wed, 28 Oct 2009 11:35:37
mbed_official 0:b3b93997a0a6 20
mbed_official 0:b3b93997a0a6 21 while (true) {
mbed_official 0:b3b93997a0a6 22 time_t seconds = time(NULL);
mbed_official 0:b3b93997a0a6 23
mbed_official 0:b3b93997a0a6 24 printf("Time as seconds since January 1, 1970 = %d\n", seconds);
mbed_official 0:b3b93997a0a6 25
mbed_official 0:b3b93997a0a6 26 printf("Time as a basic string = %s", ctime(&seconds));
mbed_official 0:b3b93997a0a6 27
mbed_official 0:b3b93997a0a6 28 char buffer[32];
mbed_official 0:b3b93997a0a6 29 strftime(buffer, 32, "%I:%M %p\n", localtime(&seconds));
mbed_official 0:b3b93997a0a6 30 printf("Time as a custom formatted string = %s", buffer);
mbed_official 0:b3b93997a0a6 31
mbed_official 0:b3b93997a0a6 32 wait(1);
mbed_official 0:b3b93997a0a6 33 }
mbed_official 0:b3b93997a0a6 34 }