* 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. */
| 144 | * @return rt_err_t if set success, return RT_EOK. |
| 145 | */ |
| 146 | rt_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). |