diff --git a/doc/user/stored-procedures.xml b/doc/user/stored-procedures.xml index 6a6b5da7742656c930d7e27b62c6f18ff1ce0840..66936a135b4b7790be966988d85d904c8eb11af7 100644 --- a/doc/user/stored-procedures.xml +++ b/doc/user/stored-procedures.xml @@ -831,6 +831,31 @@ lua box.dostring('local f = function(key) t=box.select(0, 0, key); if t ~= nil t </para> </listitem> </varlistentry> + <varlistentry> + <term> + <emphasis role="lua">box.time()</emphasis> + </term> + <listitem> + <para> + Returns current system time (in seconds) as a Lua + number. The time is taken from the event loop + clock, which makes this call very cheap, + but still useful for constructing artificial + tuple keys. + </para> + </listitem> + </varlistentry> + <varlistentry> + <term> + <emphasis role="lua">box.time64()</emphasis> + </term> + <listitem> + <para> + Returns current system time (in seconds) as a 64-bit + integer. The time is taken from the event loop clock. + </para> + </listitem> + </varlistentry> <varlistentry> <term> <emphasis role="lua">box.uuid()</emphasis> diff --git a/src/lua/init.m b/src/lua/init.m index da0f150d97f41d7a0b6f731f64196a9ab7fa42ce..dcf45fa6545937beb9082b0bda81d3b572e1c99b 100644 --- a/src/lua/init.m +++ b/src/lua/init.m @@ -49,6 +49,9 @@ #include "lua/stat.h" #include "lua/uuid.h" +#include <sys/time.h> +#include <sys/types.h> + #include TARANTOOL_CONFIG /** @@ -474,12 +477,32 @@ lbox_unpack(struct lua_State *L) #undef CHECK_SIZE } +/** Report libev time (cheap). */ +static int +lbox_time(struct lua_State *L) +{ + lua_pushnumber(L, ev_now()); + return 1; +} + +/** Report libev time as 64-bit integer */ +static int +lbox_time64(struct lua_State *L) +{ + luaL_pushnumber64(L, (u64) ( ev_now() * 1000000 + 0.5 ) ); + return 1; +} + + + /** * descriptor for box methods */ static const struct luaL_reg boxlib[] = { {"pack", lbox_pack}, {"unpack", lbox_unpack}, + {"time", lbox_time}, + {"time64", lbox_time64}, {NULL, NULL} }; @@ -1275,10 +1298,14 @@ lbox_pcall(struct lua_State *L) static int lbox_tonumber64(struct lua_State *L) { - uint64_t result = tarantool_lua_tointeger64(L, -1); + if (lua_gettop(L) != 1) + luaL_error(L, "tonumber64: wrong number of arguments"); + uint64_t result = tarantool_lua_tointeger64(L, 1); return luaL_pushnumber64(L, result); } + + /** * A helper to register a single type metatable. */ diff --git a/test/box/lua.result b/test/box/lua.result index 7309ff0e4fb7b379dc35f880bdcdd34965ebce02..fad4d96661d197ebad07fe4ac9d93dd583726fb3 100644 --- a/test/box/lua.result +++ b/test/box/lua.result @@ -13,31 +13,33 @@ lua print(' lua says: hello') lua for n in pairs(box) do print(' - box.', n) end --- - box.fiber - - box.select_reverse_range + - box.space + - box.time64 - box.uuid - - box.ipc + - box.select_limit - box.delete - - box.replace - - box.space - box.cfg + - box.replace - box.on_reload_configuration + - box.bless_space + - box.time - box.select_range - box.insert - - box.bless_space - box.counter - - box.info - box.auto_increment - - box.uuid_hex + - box.info - box.update - - box.slab - - box.process + - box.uuid_hex + - box.select_reverse_range - box.dostring - - box.index + - box.process - box.select + - box.slab + - box.stat - box.flags - box.unpack - - box.stat - - box.select_limit + - box.index + - box.ipc - box.pack ... lua box.pack()