Skip to content

Commit

Permalink
Issue yuin#417: support nil params on os.date()
Browse files Browse the repository at this point in the history
  • Loading branch information
cezarguimaraes committed Dec 13, 2022
1 parent 3323424 commit 5cb9dbd
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
12 changes: 12 additions & 0 deletions _glua-tests/issues.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()
9 changes: 7 additions & 2 deletions oslib.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 5cb9dbd

Please sign in to comment.