Mistake on this page?
Report an issue in GitHub or email us
mbed_rtc_time.h
1 /* mbed Microcontroller Library
2  * Copyright (c) 2006-2019 ARM Limited
3  * SPDX-License-Identifier: Apache-2.0
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #ifndef __MBED_RTC_TIME_H__
19 #define __MBED_RTC_TIME_H__
20 
21 #include <time.h>
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
27 /** \addtogroup platform-public-api */
28 /** @{*/
29 
30 /**
31  * \defgroup platform_rtc_time rtc_time functions
32  * @{
33  */
34 
35 /* Timeval definition for non GCC_ARM toolchains */
36 #if !defined(__GNUC__) || defined(__CC_ARM) || defined(__clang__)
37 struct timeval {
38  time_t tv_sec;
39  int32_t tv_usec;
40 };
41 #endif
42 
43 /** Implementation of the C time.h functions
44  *
45  * Provides mechanisms to set and read the current time, based
46  * on the microcontroller Real-Time Clock (RTC), plus some
47  * standard C manipulation and formatting functions.
48  *
49  * Example:
50  * @code
51  * #include "mbed.h"
52  *
53  * int main() {
54  * set_time(1256729737); // Set RTC time to Wed, 28 Oct 2009 11:35:37
55  *
56  * while (true) {
57  * time_t seconds = time(NULL);
58  *
59  * printf("Time as seconds since January 1, 1970 = %u\n", (unsigned int)seconds);
60  *
61  * printf("Time as a basic string = %s", ctime(&seconds));
62  *
63  * char buffer[32];
64  * strftime(buffer, 32, "%I:%M %p\n", localtime(&seconds));
65  * printf("Time as a custom formatted string = %s", buffer);
66  *
67  * wait(1);
68  * }
69  * }
70  * @endcode
71  */
72 
73 /** Set the current time
74  *
75  * Initializes and sets the time of the microcontroller Real-Time Clock (RTC)
76  * to the time represented by the number of seconds since January 1, 1970
77  * (the UNIX timestamp).
78  *
79  * @param t Number of seconds since January 1, 1970 (the UNIX timestamp)
80  *
81  * @note Synchronization level: Thread safe
82  *
83  * Example:
84  * @code
85  * #include "mbed.h"
86  *
87  * int main() {
88  * set_time(1256729737); // Set time to Wed, 28 Oct 2009 11:35:37
89  * }
90  * @endcode
91  */
92 void set_time(time_t t);
93 
94 /** Attach an external RTC to be used for the C time functions
95  *
96  * @note Synchronization level: Thread safe
97  *
98  * @param read_rtc pointer to function which returns current UNIX timestamp
99  * @param write_rtc pointer to function which sets current UNIX timestamp, can be NULL
100  * @param init_rtc pointer to function which initializes RTC, can be NULL
101  * @param isenabled_rtc pointer to function which returns if the RTC is enabled, can be NULL
102  */
103 void attach_rtc(time_t (*read_rtc)(void), void (*write_rtc)(time_t), void (*init_rtc)(void), int (*isenabled_rtc)(void));
104 
105 /** Standard lib retarget, get time since Epoch
106  *
107  * @param tv Structure containing time_t seconds and useconds_t microseconds. Due to
108  * separate target specific RTC implementations only the seconds component is used.
109  * @param tz DEPRECATED IN THE STANDARD: This parameter is left in for legacy code. It is
110  * not used.
111  * @return 0 on success, -1 on a failure.
112  * @note Synchronization level: Thread safe
113  *
114  */
115 int gettimeofday(struct timeval *tv, void *tz);
116 
117 /** Standard lib retarget, set time since Epoch
118  *
119  * @param tv Structure containing time_t seconds and useconds_t microseconds. Due to
120  * separate target specific RTC implementations only the seconds component is used.
121  * @param tz DEPRECATED IN THE STANDARD: This parameter is left in for legacy code. It is
122  * not used.
123  * @return Time in seconds on success, -1 on a failure.
124  * @note Synchronization level: Thread safe
125  *
126  */
127 int settimeofday(const struct timeval *tv, const struct timezone *tz);
128 
129 #ifdef __cplusplus
130 }
131 #endif
132 
133 /** @}*/
134 /** @}*/
135 
136 #endif /* __MBED_RTC_TIME_H__ */
void set_time(time_t t)
Implementation of the C time.h functions.
int gettimeofday(struct timeval *tv, void *tz)
Standard lib retarget, get time since Epoch.
int settimeofday(const struct timeval *tv, const struct timezone *tz)
Standard lib retarget, set time since Epoch.
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.
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.