Skip to content

Commit 4ce6b2f

Browse files
committed
[libc][time] 对posix相关函数 获取时间结果的判断逻辑做出调整
1 parent 62ff77d commit 4ce6b2f

File tree

1 file changed

+35
-24
lines changed
  • components/libc/compilers/common

1 file changed

+35
-24
lines changed

components/libc/compilers/common/time.c

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -465,38 +465,42 @@ RTM_EXPORT(difftime);
465465
RTM_EXPORT(strftime);
466466

467467
#ifdef RT_USING_POSIX
468+
469+
#ifdef RT_USING_RTC
468470
static volatile struct timeval _timevalue;
469471
static int _rt_clock_time_system_init()
470472
{
471-
time_t time;
473+
time_t time = 0;
472474
rt_tick_t tick;
473475
rt_device_t device;
474476

475-
time = 0;
476-
477-
#ifdef RT_USING_RTC
478477
device = rt_device_find("rtc");
479478
if (device != RT_NULL)
480479
{
481480
/* get realtime seconds */
482-
rt_device_control(device, RT_DEVICE_CTRL_RTC_GET_TIME, &time);
481+
if(rt_device_control(device, RT_DEVICE_CTRL_RTC_GET_TIME, &time) == RT_EOK)
482+
{
483+
/* get tick */
484+
tick = rt_tick_get();
485+
_timevalue.tv_usec = (tick%RT_TICK_PER_SECOND) * MICROSECOND_PER_TICK;
486+
_timevalue.tv_sec = time - tick/RT_TICK_PER_SECOND - 1;
487+
return 0;
488+
}
483489
}
484-
#else
485-
LOG_W("Cannot find a RTC device to provide time!");
486-
#endif
487-
488-
/* get tick */
489-
tick = rt_tick_get();
490-
491-
_timevalue.tv_usec = (tick%RT_TICK_PER_SECOND) * MICROSECOND_PER_TICK;
492-
_timevalue.tv_sec = time - tick/RT_TICK_PER_SECOND - 1;
493490

494-
return 0;
491+
_timevalue.tv_usec = 0;
492+
_timevalue.tv_sec = 0;
493+
return -1;
495494
}
496495
INIT_COMPONENT_EXPORT(_rt_clock_time_system_init);
496+
#endif /* RT_USING_RTC */
497497

498498
int clock_getres(clockid_t clockid, struct timespec *res)
499499
{
500+
#ifndef RT_USING_RTC
501+
LOG_W("Cannot find a RTC device to save time!");
502+
return -1;
503+
#else
500504
int ret = 0;
501505

502506
if (res == RT_NULL)
@@ -526,11 +530,16 @@ int clock_getres(clockid_t clockid, struct timespec *res)
526530
}
527531

528532
return ret;
533+
#endif /* RT_USING_RTC */
529534
}
530535
RTM_EXPORT(clock_getres);
531536

532537
int clock_gettime(clockid_t clockid, struct timespec *tp)
533538
{
539+
#ifndef RT_USING_RTC
540+
LOG_W("Cannot find a RTC device to save time!");
541+
return -1;
542+
#else
534543
int ret = 0;
535544

536545
if (tp == RT_NULL)
@@ -571,19 +580,23 @@ int clock_gettime(clockid_t clockid, struct timespec *tp)
571580
}
572581

573582
return ret;
583+
#endif /* RT_USING_RTC */
574584
}
575585
RTM_EXPORT(clock_gettime);
576586

577587
int clock_settime(clockid_t clockid, const struct timespec *tp)
578588
{
589+
#ifndef RT_USING_RTC
590+
LOG_W("Cannot find a RTC device to save time!");
591+
return -1;
592+
#else
579593
int second;
580594
rt_tick_t tick;
581595
rt_device_t device;
582596

583597
if ((clockid != CLOCK_REALTIME) || (tp == RT_NULL))
584598
{
585599
rt_set_errno(EINVAL);
586-
587600
return -1;
588601
}
589602

@@ -596,21 +609,19 @@ int clock_settime(clockid_t clockid, const struct timespec *tp)
596609
_timevalue.tv_usec = MICROSECOND_PER_SECOND - (tick % RT_TICK_PER_SECOND) * MICROSECOND_PER_TICK;
597610
_timevalue.tv_sec = second - tick/RT_TICK_PER_SECOND - 1;
598611

599-
#ifdef RT_USING_RTC
600612
/* update for RTC device */
601613
device = rt_device_find("rtc");
602614
if (device != RT_NULL)
603615
{
604616
/* set realtime seconds */
605-
rt_device_control(device, RT_DEVICE_CTRL_RTC_SET_TIME, &second);
617+
if(rt_device_control(device, RT_DEVICE_CTRL_RTC_SET_TIME, &second) == RT_EOK)
618+
{
619+
return 0;
620+
}
606621
}
607-
else
608-
#else
609-
LOG_W("Cannot find a RTC device to save time!");
610-
#endif
611-
return -1;
612622

613-
return 0;
623+
return -1;
624+
#endif /* RT_USING_RTC */
614625
}
615626
RTM_EXPORT(clock_settime);
616627

0 commit comments

Comments
 (0)