]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: lua: add "now" time function
authorThierry Fournier <tfournier@arpalert.org>
Thu, 21 Jan 2016 08:46:15 +0000 (09:46 +0100)
committerWilly Tarreau <w@1wt.eu>
Fri, 12 Feb 2016 10:08:53 +0000 (11:08 +0100)
This function returns the current time in the Lua.

doc/lua-api/index.rst
src/hlua_fcn.c

index 7a1f44edb819adb4a340e85340ad56123c29f7ea..d41451f53d26f7887d2140f700a75f3decf8d48c 100644 (file)
@@ -270,6 +270,20 @@ Core class
   :param string filename: the filename that reference the map entries.
   :param string key: the key which will be deleted.
 
+.. js:function:: core.now()
+
+  **context**: body, init, task, action
+
+  This function returns the current time. The time returned is fixed by the
+  HAProxy core and assures than the hour will be monotnic and that the system
+  call 'gettimeofday' will not be called too. The time is refreshed between each
+  Lua execution or resume, so two consecutive call to the function "now" will
+  probably returns the same result.
+
+  :returns: an array which contains two entries "sec" and "usec". "sec"
+    contains the current at the epoch format, and "usec" contains the
+    current microseconds.
+
 .. js:function:: core.msleep(milliseconds)
 
   **context**: body, init, task, action
index 60780319fda35be0c9c3a2a3f9b64920ad1621dd..b4df09a5248c5c923436ad60f0010b8ae9b901ca 100644 (file)
@@ -7,6 +7,21 @@
 #include <lua.h>
 #include <lualib.h>
 
+#include <common/time.h>
+
+/* This function return the current date at epoch format in milliseconds. */
+int hlua_now(lua_State *L)
+{
+       lua_newtable(L);
+       lua_pushstring(L, "sec");
+       lua_pushinteger(L, now.tv_sec);
+       lua_rawset(L, -3);
+       lua_pushstring(L, "usec");
+       lua_pushinteger(L, now.tv_usec);
+       lua_rawset(L, -3);
+       return 1;
+}
+
 static void hlua_array_add_fcn(lua_State *L, const char *name,
                                int (*function)(lua_State *L))
 {
@@ -17,5 +32,6 @@ static void hlua_array_add_fcn(lua_State *L, const char *name,
 
 int hlua_fcn_reg_core_fcn(lua_State *L)
 {
-       return 0;
+       hlua_array_add_fcn(L, "now", hlua_now);
+       return 1;
 }