diff --git a/libraries/AP_HAL_SITL/system.cpp b/libraries/AP_HAL_SITL/system.cpp index 1e4a59be20..81c5b4941b 100644 --- a/libraries/AP_HAL_SITL/system.cpp +++ b/libraries/AP_HAL_SITL/system.cpp @@ -18,12 +18,19 @@ using HALSITL::Scheduler; namespace AP_HAL { static struct { - struct timeval start_time; + uint64_t start_time_ns; } state; +static uint64_t ts_to_nsec(struct timespec &ts) +{ + return ts.tv_sec*1000000000ULL + ts.tv_nsec; +} + void init() { - gettimeofday(&state.start_time, nullptr); + struct timespec ts {}; + clock_gettime(CLOCK_MONOTONIC, &ts); + state.start_time_ns = ts_to_nsec(ts); } #if defined(__CYGWIN__) || defined(__CYGWIN64__) || defined(CYGWIN_BUILD) @@ -170,12 +177,9 @@ uint64_t micros64() return stopped_usec; } - struct timeval tp; - gettimeofday(&tp, nullptr); - uint64_t ret = 1.0e6 * ((tp.tv_sec + (tp.tv_usec * 1.0e-6)) - - (state.start_time.tv_sec + - (state.start_time.tv_usec * 1.0e-6))); - return ret; + struct timespec ts; + clock_gettime(CLOCK_MONOTONIC, &ts); + return uint64_div1000(ts_to_nsec(ts) - state.start_time_ns); } uint64_t millis64()