Mbed OS Reference
Loading...
Searching...
No Matches
mbed_rtc_time.h
Go to the documentation of this file.
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/**
24 * @file
25 *
26 * @brief Implementation of the C time.h functions
27 *
28 * Provides mechanisms to set and read the current time, based
29 * on the microcontroller Real-Time Clock (RTC), plus some
30 * standard C manipulation and formatting functions.
31 *
32 * Example:
33 * @code
34 * #include "mbed.h"
35 *
36 * int main() {
37 * set_time(1256729737); // Set RTC time to Wed, 28 Oct 2009 11:35:37
38 *
39 * while (true) {
40 * time_t seconds = time(NULL);
41 *
42 * printf("Time as seconds since January 1, 1970 = %u\n", (unsigned int)seconds);
43 *
44 * printf("Time as a basic string = %s", ctime(&seconds));
45 *
46 * char buffer[32];
47 * strftime(buffer, 32, "%I:%M %p\n", localtime(&seconds));
48 * printf("Time as a custom formatted string = %s", buffer);
49 *
50 * ThisThread::sleep_for(1000);
51 * }
52 * }
53 * @endcode
54 */
55
56#ifdef __cplusplus
57extern "C" {
58#endif
59
60/** \addtogroup platform-public-api */
61/** @{*/
62
63/**
64 * \defgroup platform_rtc_time rtc_time functions
65 * @{
66 */
67
68/* Timeval definition for non GCC_ARM toolchains,
69 * Note: The GNU libc defines _TIMEVAL_DEFINED and the newlib defines __timeval_defined,
70 * thus the double-check and double-define
71 */
72#if !defined(__timeval_defined) && !defined(_TIMEVAL_DEFINED)
73#define __timeval_defined 1
74#define _TIMEVAL_DEFINED
75struct timeval {
76 time_t tv_sec;
77 int32_t tv_usec;
78};
79#endif
80
81/** Set the current time
82 *
83 * Initializes and sets the time of the microcontroller Real-Time Clock (RTC)
84 * to the time represented by the number of seconds since January 1, 1970
85 * (the UNIX timestamp).
86 *
87 * @param t Number of seconds since January 1, 1970 (the UNIX timestamp)
88 *
89 * @note Synchronization level: Thread safe
90 *
91 * Example:
92 * @code
93 * #include "mbed.h"
94 *
95 * int main() {
96 * set_time(1256729737); // Set time to Wed, 28 Oct 2009 11:35:37
97 * }
98 * @endcode
99 */
100void set_time(time_t t);
101
102/** Attach an external RTC to be used for the C time functions
103 *
104 * @note Synchronization level: Thread safe
105 *
106 * @param read_rtc pointer to function which returns current UNIX timestamp
107 * @param write_rtc pointer to function which sets current UNIX timestamp, can be NULL
108 * @param init_rtc pointer to function which initializes RTC, can be NULL
109 * @param isenabled_rtc pointer to function which returns if the RTC is enabled, can be NULL
110 */
111void attach_rtc(time_t (*read_rtc)(void), void (*write_rtc)(time_t), void (*init_rtc)(void), int (*isenabled_rtc)(void));
112
113/** Standard lib retarget, get time since Epoch
114 *
115 * @param tv Structure containing time_t seconds and useconds_t microseconds. Due to
116 * separate target specific RTC implementations only the seconds component is used.
117 * @param tz DEPRECATED IN THE STANDARD: This parameter is left in for legacy code. It is
118 * not used.
119 * @return 0 on success, -1 on a failure.
120 * @note Synchronization level: Thread safe
121 *
122 */
123int gettimeofday(struct timeval *tv, void *tz);
124
125/** Standard lib retarget, set time since Epoch
126 *
127 * @param tv Structure containing time_t seconds and useconds_t microseconds. Due to
128 * separate target specific RTC implementations only the seconds component is used.
129 * @param tz DEPRECATED IN THE STANDARD: This parameter is left in for legacy code. It is
130 * not used.
131 * @return Time in seconds on success, -1 on a failure.
132 * @note Synchronization level: Thread safe
133 *
134 */
135int settimeofday(const struct timeval *tv, const struct timezone *tz);
136
137#ifdef __cplusplus
138}
139#endif
140
141/** @}*/
142/** @}*/
143
144#endif /* __MBED_RTC_TIME_H__ */
int gettimeofday(struct timeval *tv, void *tz)
Standard lib retarget, get time since Epoch.
void set_time(time_t t)
Set the current time.
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.