From 5cb9dbd5440b923b1a0aa5986c0f2264b2d3660d Mon Sep 17 00:00:00 2001 From: Cezar Guimaraes Date: Tue, 13 Dec 2022 00:05:12 -0300 Subject: [PATCH] Issue #417: support nil params on os.date() --- _glua-tests/issues.lua | 12 ++++++++++++ oslib.go | 9 +++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/_glua-tests/issues.lua b/_glua-tests/issues.lua index a4b830b6..3c7373a9 100644 --- a/_glua-tests/issues.lua +++ b/_glua-tests/issues.lua @@ -404,3 +404,15 @@ function test() assert(2 % 2 == 0) end test() + +-- issue #417 +function test() + assert(type(os.date(nil)) == "string") + assert(type(os.date(nil, nil)) == "string") + local t = os.time() + assert(os.date(nil, t) == os.date("%c", t)) + assert(os.date(2) == "2") + local ok, msg = pcall(os.date, {}) + assert(not ok and string.find(msg, "string expected, got table", 1, true)) +end +test() diff --git a/oslib.go b/oslib.go index 256c8811..79431b1c 100644 --- a/oslib.go +++ b/oslib.go @@ -108,13 +108,18 @@ func osDate(L *LState) int { t := time.Now() cfmt := "%c" if L.GetTop() >= 1 { - cfmt = L.CheckString(1) + lv := L.Get(1) + if LVCanConvToString(lv) { + cfmt = lv.String() + } else if lv != LNil { + L.TypeError(1, LTString) + } if strings.HasPrefix(cfmt, "!") { t = time.Now().UTC() cfmt = strings.TrimLeft(cfmt, "!") } if L.GetTop() >= 2 { - t = time.Unix(L.CheckInt64(2), 0) + t = time.Unix(L.OptInt64(2, t.Unix()), 0) } if strings.HasPrefix(cfmt, "*t") { ret := L.NewTable()