From c5fd0d922c388f4d773ffcc1debc6541c15dc3ea Mon Sep 17 00:00:00 2001 From: Steve Bennett Date: Wed, 13 Oct 2010 07:19:31 +1000 Subject: Add support for clock clicks, micros, millis Signed-off-by: Steve Bennett --- jim-clock.c | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) (limited to 'jim-clock.c') diff --git a/jim-clock.c b/jim-clock.c index f5f3ed5..67698b6 100644 --- a/jim-clock.c +++ b/jim-clock.c @@ -12,6 +12,7 @@ #include #include #include +#include #include "jim.h" #include "jim-subcmd.h" @@ -80,6 +81,28 @@ static int clock_cmd_seconds(Jim_Interp *interp, int argc, Jim_Obj *const *argv) 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); + + 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); + + return JIM_OK; +} + static const jim_subcmd_type clock_command_table[] = { { .cmd = "seconds", .function = clock_cmd_seconds, @@ -87,6 +110,24 @@ static const jim_subcmd_type clock_command_table[] = { .maxargs = 0, .description = "Returns the current time as seconds since the epoch" }, + { .cmd = "clicks", + .function = clock_cmd_micros, + .minargs = 0, + .maxargs = 0, + .description = "Returns the current time in 'clicks'" + }, + { .cmd = "microseconds", + .function = clock_cmd_micros, + .minargs = 0, + .maxargs = 0, + .description = "Returns the current time in microseconds" + }, + { .cmd = "milliseconds", + .function = clock_cmd_millis, + .minargs = 0, + .maxargs = 0, + .description = "Returns the current time in milliseconds" + }, { .cmd = "format", .args = "seconds ?-format format?", .function = clock_cmd_format, -- cgit v1.1