Mistake on this page?
Report an issue in GitHub or email us
Data Structures | Functions
rtc_time functions

Data Structures

struct  timeval
 

Functions

void set_time (time_t t)
 Implementation of the C time.h functions. More...
 
void attach_rtc (time_t(*read_rtc)(void), void(*write_rtc)(time_t), void(*init_rtc)(void), int(*isenabled_rtc)(void))
 Attach an external RTC to be used for the C time functions. More...
 
int gettimeofday (struct timeval *tv, void *tz)
 Standard lib retarget, get time since Epoch. More...
 
int settimeofday (const struct timeval *tv, const struct timezone *tz)
 Standard lib retarget, set time since Epoch. More...
 

Detailed Description

Function Documentation

void attach_rtc ( time_t(*)(void)  read_rtc,
void(*)(time_t)  write_rtc,
void(*)(void)  init_rtc,
int(*)(void)  isenabled_rtc 
)

Attach an external RTC to be used for the C time functions.

Note
Synchronization level: Thread safe
Parameters
read_rtcpointer to function which returns current UNIX timestamp
write_rtcpointer to function which sets current UNIX timestamp, can be NULL
init_rtcpointer to function which initializes RTC, can be NULL
isenabled_rtcpointer to function which returns if the RTC is enabled, can be NULL
int gettimeofday ( struct timeval tv,
void *  tz 
)

Standard lib retarget, get time since Epoch.

Parameters
tvStructure containing time_t seconds and useconds_t microseconds. Due to separate target specific RTC implementations only the seconds component is used.
tzDEPRECATED IN THE STANDARD: This parameter is left in for legacy code. It is not used.
Returns
0 on success, -1 on a failure.
Note
Synchronization level: Thread safe
void set_time ( time_t  t)

Implementation of the C time.h functions.

Provides mechanisms to set and read the current time, based on the microcontroller Real-Time Clock (RTC), plus some standard C manipulation and formatting functions.

Example:

1 #include "mbed.h"
2 
3 int main() {
4  set_time(1256729737); // Set RTC time to Wed, 28 Oct 2009 11:35:37
5 
6  while (true) {
7  time_t seconds = time(NULL);
8 
9  printf("Time as seconds since January 1, 1970 = %u\n", (unsigned int)seconds);
10 
11  printf("Time as a basic string = %s", ctime(&seconds));
12 
13  char buffer[32];
14  strftime(buffer, 32, "%I:%M %p\n", localtime(&seconds));
15  printf("Time as a custom formatted string = %s", buffer);
16 
17  wait(1);
18  }
19 }

Set the current time

Initializes and sets the time of the microcontroller Real-Time Clock (RTC) to the time represented by the number of seconds since January 1, 1970 (the UNIX timestamp).

Parameters
tNumber of seconds since January 1, 1970 (the UNIX timestamp)
Note
Synchronization level: Thread safe

Example:

1 #include "mbed.h"
2 
3 int main() {
4  set_time(1256729737); // Set time to Wed, 28 Oct 2009 11:35:37
5 }
int settimeofday ( const struct timeval tv,
const struct timezone *  tz 
)

Standard lib retarget, set time since Epoch.

Parameters
tvStructure containing time_t seconds and useconds_t microseconds. Due to separate target specific RTC implementations only the seconds component is used.
tzDEPRECATED IN THE STANDARD: This parameter is left in for legacy code. It is not used.
Returns
Time in seconds on success, -1 on a failure.
Note
Synchronization level: Thread safe
Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.