void ros_walltime(uint32_t& sec, uint32_t& nsec)
{
#if !defined(_WIN32)
#if HAS_CLOCK_GETTIME
timespec start;
clock_gettime(CLOCK_REALTIME, &start);
if (start.tv_sec < 0 || start.tv_sec > std::numeric_limits<uint32_t>::max())
throw std::runtime_error("Timespec is out of dual 32-bit range");
sec = start.tv_sec;
nsec = start.tv_nsec;
#else
struct timeval timeofday;
gettimeofday(&timeofday,NULL);
if (timeofday.tv_sec < 0 || timeofday.tv_sec > std::numeric_limits<uint32_t>::max())
throw std::runtime_error("Timeofday is out of dual signed 32-bit range");
sec = timeofday.tv_sec;
nsec = timeofday.tv_usec * 1000;
#endif
#else
uint64_t now_s = 0;
uint64_t now_ns = std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
normalizeSecNSec(now_s, now_ns);
sec = (uint32_t)now_s;
nsec = (uint32_t)now_ns;
#endif
}
ros::Time 源码分析
于 2024-11-11 11:24:23 首次发布