[ctime]解决clock_gettime计算出来的nsec超过1sec的问题

This commit is contained in:
goldengrandpa 2023-06-01 10:52:33 +08:00 committed by GitHub
parent 1638241297
commit e88a19467e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 6 additions and 1 deletions

View File

@ -677,8 +677,13 @@ int clock_gettime(clockid_t clockid, struct timespec *tp)
level = rt_hw_interrupt_disable(); level = rt_hw_interrupt_disable();
tick = rt_tick_get(); /* get tick */ tick = rt_tick_get(); /* get tick */
tp->tv_sec = _timevalue.tv_sec + tick / RT_TICK_PER_SECOND; tp->tv_sec = _timevalue.tv_sec + tick / RT_TICK_PER_SECOND;
tp->tv_nsec = (_timevalue.tv_usec + (tick % RT_TICK_PER_SECOND) * MICROSECOND_PER_TICK) * 1000; tp->tv_nsec = (_timevalue.tv_usec + (tick % RT_TICK_PER_SECOND) * MICROSECOND_PER_TICK) * 1000U;
rt_hw_interrupt_enable(level); rt_hw_interrupt_enable(level);
if (tp->tv_nsec > 1000000000ULL)
{
tp->tv_nsec %= 1000000000ULL;
tp->tv_sec += 1;
}
} }
break; break;
#endif #endif