summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEmil Renner Berthing <esmil@mailme.dk>2012-07-31 18:30:22 +0200
committerEmil Renner Berthing <esmil@mailme.dk>2012-07-31 18:47:58 +0200
commite1b242d0df9ced1ed6b01a69a12ac6078265e635 (patch)
tree107364369cabb52ded03bdb8016c78400f0db7b0
parent526c253d57b2daed8a122d56a0532dc1e7e83b40 (diff)
downloadlem-e1b242d0df9ced1ed6b01a69a12ac6078265e635.tar.gz
lem-e1b242d0df9ced1ed6b01a69a12ac6078265e635.tar.xz
lem-e1b242d0df9ced1ed6b01a69a12ac6078265e635.zip
utils: add now function to wrap gettimeofday
-rw-r--r--README.markdown4
-rw-r--r--lem/utils.c21
2 files changed, 25 insertions, 0 deletions
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 <http://www.gnu.org/licenses/>.
*/
+#include <sys/time.h>
#include <lem.h>
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;
}