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

Function date

components/drivers/rtc/dev_rtc.c:280–371  ·  view source on GitHub ↗

* get date and time or set (local timezone) [year month day hour min sec] */

Source from the content-addressed store, hash-verified

278 * get date and time or set (local timezone) [year month day hour min sec]
279 */
280static void date(int argc, char **argv)
281{
282 time_t now = (time_t)0;
283
284 if (argc == 1)
285 {
286 struct timeval tv = { 0 };
287 int32_t tz_offset_sec = 0;
288 uint32_t abs_tz_offset_sec = 0U;
289
290#if defined(RT_LIBC_USING_LIGHT_TZ_DST)
291 tz_offset_sec = rt_tz_get();
292#endif /* RT_LIBC_USING_LIGHT_TZ_DST */
293
294 gettimeofday(&tv, RT_NULL);
295 now = tv.tv_sec;
296
297 abs_tz_offset_sec = tz_offset_sec > 0 ? tz_offset_sec : -tz_offset_sec;
298 /* output current time */
299 rt_kprintf("local time: %.*s", 25U, ctime(&now));
300 rt_kprintf("timestamps: %ld\n", (long)tv.tv_sec);
301 rt_kprintf("timezone: UTC%c%02d:%02d:%02d\n",
302 tz_offset_sec > 0 ? '+' : '-', abs_tz_offset_sec / 3600U, abs_tz_offset_sec % 3600U / 60U, abs_tz_offset_sec % 3600U % 60U);
303 }
304 else if (argc >= 7)
305 {
306 /* set time and date */
307 struct tm tm_new = {0};
308 time_t old = (time_t)0;
309 rt_err_t err;
310
311 tm_new.tm_year = atoi(argv[1]) - 1900;
312 tm_new.tm_mon = atoi(argv[2]) - 1; /* .tm_min's range is [0-11] */
313 tm_new.tm_mday = atoi(argv[3]);
314 tm_new.tm_hour = atoi(argv[4]);
315 tm_new.tm_min = atoi(argv[5]);
316 tm_new.tm_sec = atoi(argv[6]);
317 if (tm_new.tm_year <= 0)
318 {
319 rt_kprintf("year is out of range [1900-]\n");
320 return;
321 }
322 if (tm_new.tm_mon > 11) /* .tm_min's range is [0-11] */
323 {
324 rt_kprintf("month is out of range [1-12]\n");
325 return;
326 }
327 if (tm_new.tm_mday == 0 || tm_new.tm_mday > 31)
328 {
329 rt_kprintf("day is out of range [1-31]\n");
330 return;
331 }
332 if (tm_new.tm_hour > 23)
333 {
334 rt_kprintf("hour is out of range [0-23]\n");
335 return;
336 }
337 if (tm_new.tm_min > 59)

Callers

nothing calls this directly

Calls 7

rt_tz_getFunction · 0.85
gettimeofdayFunction · 0.85
rt_kprintfFunction · 0.85
ctimeFunction · 0.85
mktimeFunction · 0.85
get_timestampFunction · 0.70
set_timestampFunction · 0.70

Tested by

no test coverage detected