From 5dff054a4d1b9f92858057861d6b04a5d6095915 Mon Sep 17 00:00:00 2001
From: Konstantin Osipov <kostja@tarantool.org>
Date: Thu, 6 Dec 2012 21:32:26 +0400
Subject: [PATCH] Add box.time() and box.time64() needed by queues.

---
 doc/user/stored-procedures.xml | 25 +++++++++++++++++++++++++
 src/lua/init.m                 | 29 ++++++++++++++++++++++++++++-
 test/box/lua.result            | 26 ++++++++++++++------------
 3 files changed, 67 insertions(+), 13 deletions(-)

diff --git a/doc/user/stored-procedures.xml b/doc/user/stored-procedures.xml
index 6a6b5da774..66936a135b 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 da0f150d97..dcf45fa654 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 7309ff0e4f..fad4d96661 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()
-- 
GitLab