From e1b242d0df9ced1ed6b01a69a12ac6078265e635 Mon Sep 17 00:00:00 2001 From: Emil Renner Berthing Date: Tue, 31 Jul 2012 18:30:22 +0200 Subject: utils: add now function to wrap gettimeofday --- README.markdown | 4 ++++ lem/utils.c | 21 +++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/README.markdown b/README.markdown index e882ce0..93b057a 100644 --- a/README.markdown +++ b/README.markdown @@ -112,6 +112,10 @@ This sets `utils` to a table with the following functions. If `status` is supplied this will be the exit status of program, otherwise `EXIT_SUCCESS` is used. +* __utils.now()__ + + Return the number of seconds since the epoch (1970-01-01 UTC). + * __utils.sleeper()__ This function returns a new sleeper object. diff --git a/lem/utils.c b/lem/utils.c index d93cf98..ad93c6b 100644 --- a/lem/utils.c +++ b/lem/utils.c @@ -16,6 +16,7 @@ * along with LEM. If not, see . */ +#include #include static int @@ -191,6 +192,22 @@ resume(lua_State *T) return 0; } +static int +now_lua(lua_State *L) +{ + struct timeval tp; + lua_Number ret; + + (void)gettimeofday(&tp, NULL); + + ret = (lua_Number)tp.tv_usec; + ret /= 1.0e6; + ret += (lua_Number)tp.tv_sec; + + lua_pushnumber(L, ret); + return 1; +} + int luaopen_lem_utils(lua_State *L) { @@ -239,5 +256,9 @@ luaopen_lem_utils(lua_State *L) lua_pushcfunction(L, resume); lua_setfield(L, -2, "resume"); + /* set now function */ + lua_pushcfunction(L, now_lua); + lua_setfield(L, -2, "now"); + return 1; } -- cgit v1.2.1