From a5ea6b096e9e9f9913ad860a847e3757580dd9e4 Mon Sep 17 00:00:00 2001 From: Steve Bennett Date: Thu, 1 Dec 2022 10:13:57 +1000 Subject: clock millis, time: now use monotonic raw time if possible Instead of using all time, these commands now use a monotonically increasing system timer so that they are not affected by time (e.g. ntp) adjustments. (But not on Windows since it doesn't work reliably) Fixes #240 Signed-off-by: Steve Bennett --- jim-clock.c | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) (limited to 'jim-clock.c') diff --git a/jim-clock.c b/jim-clock.c index 0ca3e7b..43d7f74 100644 --- a/jim-clock.c +++ b/jim-clock.c @@ -151,37 +151,32 @@ static int clock_cmd_scan(Jim_Interp *interp, int argc, Jim_Obj *const *argv) static int clock_cmd_seconds(Jim_Interp *interp, int argc, Jim_Obj *const *argv) { - Jim_SetResultInt(interp, time(NULL)); + Jim_SetResultInt(interp, Jim_GetTimeUsec(CLOCK_REALTIME) / 1000000); + return JIM_OK; +} +static int clock_cmd_clicks(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + Jim_SetResultInt(interp, Jim_GetTimeUsec(CLOCK_MONOTONIC_RAW)); return JIM_OK; } static int clock_cmd_micros(Jim_Interp *interp, int argc, Jim_Obj *const *argv) { - struct timeval tv; - - gettimeofday(&tv, NULL); - - Jim_SetResultInt(interp, (jim_wide) tv.tv_sec * 1000000 + tv.tv_usec); - + Jim_SetResultInt(interp, Jim_GetTimeUsec(CLOCK_REALTIME)); return JIM_OK; } static int clock_cmd_millis(Jim_Interp *interp, int argc, Jim_Obj *const *argv) { - struct timeval tv; - - gettimeofday(&tv, NULL); - - Jim_SetResultInt(interp, (jim_wide) tv.tv_sec * 1000 + tv.tv_usec / 1000); - + Jim_SetResultInt(interp, Jim_GetTimeUsec(CLOCK_REALTIME) / 1000); return JIM_OK; } static const jim_subcmd_type clock_command_table[] = { { "clicks", NULL, - clock_cmd_micros, + clock_cmd_clicks, 0, 0, /* Description: Returns the current time in 'clicks' */ -- cgit v1.1