MCPcopy Create free account
hub / github.com/RT-Thread/rt-thread / set_time

Function set_time

components/drivers/rtc/dev_rtc.c:193–229  ·  view source on GitHub ↗

* Set system time(date not modify, local timezone). * * @param rt_uint32_t hour e.g: 0~23. * @param rt_uint32_t minute e.g: 0~59. * @param rt_uint32_t second e.g: 0~59. * * @return rt_err_t if set success, return RT_EOK. */

Source from the content-addressed store, hash-verified

191 * @return rt_err_t if set success, return RT_EOK.
192 */
193rt_err_t set_time(rt_uint32_t hour, rt_uint32_t minute, rt_uint32_t second)
194{
195 time_t now, old_timestamp = 0;
196 struct tm tm_new = {0};
197 rt_err_t ret = -RT_ERROR;
198
199 if (_rtc_device == RT_NULL)
200 {
201 _rtc_device = rt_device_find("rtc");
202 if (_rtc_device == RT_NULL)
203 {
204 return -RT_ERROR;
205 }
206 }
207
208 /* get current time */
209 ret = rt_device_control(_rtc_device, RT_DEVICE_CTRL_RTC_GET_TIME, &old_timestamp);
210 if (ret != RT_EOK)
211 {
212 return ret;
213 }
214
215 /* converts calendar time into local time. */
216 localtime_r(&old_timestamp, &tm_new);
217
218 /* update time. */
219 tm_new.tm_hour = hour;
220 tm_new.tm_min = minute;
221 tm_new.tm_sec = second;
222
223 /* converts the local time into the calendar time. */
224 now = mktime(&tm_new);
225
226 /* update to RTC device. */
227 ret = rt_device_control(_rtc_device, RT_DEVICE_CTRL_RTC_SET_TIME, &now);
228 return ret;
229}
230
231/**
232 * Set timestamp(UTC).

Callers 15

test_rtc_setFunction · 0.85
test_rtc_alarmFunction · 0.85
rtc_sampleFunction · 0.85
rtc_sampleFunction · 0.85
test_rtcFunction · 0.85
rtc_sampleFunction · 0.85
rtc_sampleFunction · 0.85
msh_rtc_set_timeFunction · 0.85
msh_rtc_set_timeFunction · 0.85
msh_rtc_set_timeFunction · 0.85
msh_rtc_set_timeFunction · 0.85
msh_rtc_set_timeFunction · 0.85

Calls 4

rt_device_findFunction · 0.85
rt_device_controlFunction · 0.85
localtime_rFunction · 0.85
mktimeFunction · 0.85

Tested by 5

test_rtc_setFunction · 0.68
test_rtc_alarmFunction · 0.68
rtc_sampleFunction · 0.68
test_rtcFunction · 0.68
rtc_testFunction · 0.68