From 1555ac83db824b2cb349ea22b2b7ab797ff9859b Mon Sep 17 00:00:00 2001 From: Benoit Giannangeli Date: Tue, 2 May 2017 08:49:49 +0200 Subject: os.difftime --- src/loslib.js | 27 ++++++++++++++++++++------- tests/loslib.js | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 7 deletions(-) diff --git a/src/loslib.js b/src/loslib.js index 65d7e72..c7f2c13 100644 --- a/src/loslib.js +++ b/src/loslib.js @@ -15,7 +15,7 @@ const setallfields = function(L, time) { setfield(L, "hour", time.getHours()); setfield(L, "day", time.getDate()); setfield(L, "month", time.getMonth()); - setfield(L, "year", time.getYear()); + setfield(L, "year", time.getFullYear()); setfield(L, "wday", time.getDay()); let now = new Date(); setfield(L, "yday", Math.floor((now - (new Date(now.getFullYear(), 0, 0))) / (1000 * 60 * 60 * 24))); @@ -49,11 +49,11 @@ const os_time = function(L) { lauxlib.luaL_checktype(L, 1, lua.LUA_TTABLE); /* make sure table is at the top */ lua.lua_settop(L, 1); t.setSeconds(getfield(L, "sec", 0, 0)); - t.setSeconds(getfield(L, "min", 0, 0)); - t.setSeconds(getfield(L, "hour", 12, 0)); - t.setSeconds(getfield(L, "day", -1, 0)); - t.setSeconds(getfield(L, "month", -1, 1)); - t.setSeconds(getfield(L, "year", -1, 1900)); + t.setMinutes(getfield(L, "min", 0, 0)); + t.setHours(getfield(L, "hour", 12, 0)); + t.setDate(getfield(L, "day", -1, 0)); + t.setMonth(getfield(L, "month", -1, 1)); + t.setFullYear(getfield(L, "year", -1, 1900)); setallfields(L, t); } @@ -61,9 +61,22 @@ const os_time = function(L) { return 1; }; +const l_checktime = function(L, arg) { + let t = lauxlib.luaL_checkinteger(L, arg); + // lauxlib.luaL_argcheck(L, t, arg, lua.to_luastring("time out-of-bounds")); + return t; +}; + +const os_difftime = function(L) { + let t1 = l_checktime(L, 1); + let t2 = l_checktime(L, 2); + lua.lua_pushnumber(L, new Date(t1) - new Date(t2)); + return 1; +}; const syslib = { - "time": os_time + "time": os_time, + "difftime": os_difftime }; // Only with Node diff --git a/tests/loslib.js b/tests/loslib.js index c460919..56c0bbd 100644 --- a/tests/loslib.js +++ b/tests/loslib.js @@ -35,6 +35,38 @@ test('os.time', function (t) { }); +test('os.difftime', function (t) { + let luaCode = ` + local t1 = os.time() + local t2 = os.time() + return os.difftime(t2, t1) + `, L; + + t.plan(3); + + t.doesNotThrow(function () { + + L = lauxlib.luaL_newstate(); + + lauxlib.luaL_openlibs(L); + + lauxlib.luaL_loadstring(L, lua.to_luastring(luaCode)); + + }, "Lua program loaded without error"); + + t.doesNotThrow(function () { + + lua.lua_call(L, 0, -1); + + }, "Lua program ran without error"); + + t.ok( + lua.lua_isnumber(L, -1), + "Correct element(s) on the stack" + ); + +}); + test('os.getenv', function (t) { let luaCode = ` -- cgit v1.2.3-70-g09d2