* 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. */
| 191 | * @return rt_err_t if set success, return RT_EOK. |
| 192 | */ |
| 193 | rt_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). |