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

Function set_date

components/drivers/rtc/dev_rtc.c:146–182  ·  view source on GitHub ↗

* Set system date(time not modify, local timezone). * * @param rt_uint32_t year e.g: 2012. * @param rt_uint32_t month e.g: 12 (1~12). * @param rt_uint32_t day e.g: 31. * * @return rt_err_t if set success, return RT_EOK. */

Source from the content-addressed store, hash-verified

144 * @return rt_err_t if set success, return RT_EOK.
145 */
146rt_err_t set_date(rt_uint32_t year, rt_uint32_t month, rt_uint32_t day)
147{
148 time_t now, old_timestamp = 0;
149 struct tm tm_new = {0};
150 rt_err_t ret = -RT_ERROR;
151
152 if (_rtc_device == RT_NULL)
153 {
154 _rtc_device = rt_device_find("rtc");
155 if (_rtc_device == RT_NULL)
156 {
157 return -RT_ERROR;
158 }
159 }
160
161 /* get current time */
162 ret = rt_device_control(_rtc_device, RT_DEVICE_CTRL_RTC_GET_TIME, &old_timestamp);
163 if (ret != RT_EOK)
164 {
165 return ret;
166 }
167
168 /* converts calendar time into local time. */
169 localtime_r(&old_timestamp, &tm_new);
170
171 /* update date. */
172 tm_new.tm_year = year - 1900;
173 tm_new.tm_mon = month - 1; /* tm_mon: 0~11 */
174 tm_new.tm_mday = day;
175
176 /* converts the local time into the calendar time. */
177 now = mktime(&tm_new);
178
179 /* update to RTC device. */
180 ret = rt_device_control(_rtc_device, RT_DEVICE_CTRL_RTC_SET_TIME, &now);
181 return ret;
182}
183
184/**
185 * Set system time(date not modify, local timezone).

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_dateFunction · 0.85
msh_rtc_set_dateFunction · 0.85
msh_rtc_set_dateFunction · 0.85
msh_rtc_set_dateFunction · 0.85
msh_rtc_set_dateFunction · 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