From f6a9b5adb12fe92ebaaa20d9707d7945406ae533 Mon Sep 17 00:00:00 2001 From: daurnimator Date: Sun, 22 Apr 2018 21:00:58 +1000 Subject: src/loslib.js: Fix off-by-one error in month field of os.time() --- src/loslib.js | 2 +- test/loslib.test.js | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/loslib.js b/src/loslib.js index c282175..d20873d 100644 --- a/src/loslib.js +++ b/src/loslib.js @@ -73,7 +73,7 @@ const setallfields = function(L, time, utc) { setfield(L, "min", !utc ? time.getMinutes() : time.getUTCMinutes()); setfield(L, "hour", !utc ? time.getHours() : time.getUTCHours()); setfield(L, "day", !utc ? time.getDate() : time.getUTCDate()); - setfield(L, "month", !utc ? time.getMonth() : time.getUTCMonth()); + setfield(L, "month", (!utc ? time.getMonth() : time.getUTCMonth()) + 1); setfield(L, "year", !utc ? time.getFullYear() : time.getUTCFullYear()); setfield(L, "wday", !utc ? time.getDay() : time.getUTCDay()); let now = new Date(); diff --git a/test/loslib.test.js b/test/loslib.test.js index 5f06a06..abb5ef2 100644 --- a/test/loslib.test.js +++ b/test/loslib.test.js @@ -105,6 +105,27 @@ test('os.date normalisation', () => { }); +test('os.time normalisation of table', () => { + let L = lauxlib.luaL_newstate(); + if (!L) throw Error("failed to create lua state"); + + let luaCode = ` + local t = { + day = 20, + month = 2, + year = 2018 + } + os.time(t) + assert(t.day == 20, "unmodified day") + assert(t.month == 2, "unmodified month") + assert(t.year == 2018, "unmodified year") + `; + lualib.luaL_openlibs(L); + expect(lauxlib.luaL_loadstring(L, to_luastring(luaCode))).toBe(lua.LUA_OK); + lua.lua_call(L, 0, 0); +}); + + test('os.getenv', () => { let L = lauxlib.luaL_newstate(); if (!L) throw Error("failed to create lua state"); -- cgit v1.2.3-54-g00ecf