aboutsummaryrefslogtreecommitdiff
path: root/jim-clock.c
diff options
context:
space:
mode:
authorSteve Bennett <steveb@workware.net.au>2022-12-01 10:13:57 +1000
committerSteve Bennett <steveb@workware.net.au>2023-02-13 10:44:10 +1000
commita5ea6b096e9e9f9913ad860a847e3757580dd9e4 (patch)
treec3e317177151e3ad83e996ee198b28aff4765b03 /jim-clock.c
parent4b26f0ec1fa38bcfc8b2bd500a9bef10372bb140 (diff)
downloadjimtcl-a5ea6b096e9e9f9913ad860a847e3757580dd9e4.zip
jimtcl-a5ea6b096e9e9f9913ad860a847e3757580dd9e4.tar.gz
jimtcl-a5ea6b096e9e9f9913ad860a847e3757580dd9e4.tar.bz2
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 <steveb@workware.net.au>
Diffstat (limited to 'jim-clock.c')
-rw-r--r--jim-clock.c23
1 files changed, 9 insertions, 14 deletions
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' */