From 5c7dc5df4cc83bad6200c4041d32ff0c6862b84c Mon Sep 17 00:00:00 2001 From: Aaron Lindsay Date: Sat, 11 Feb 2017 07:27:52 -0500 Subject: [PATCH] lua: Add ability to query date year, month, day --- date_lua.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/date_lua.go b/date_lua.go index b24a594..1e1acb0 100644 --- a/date_lua.go +++ b/date_lua.go @@ -14,6 +14,7 @@ func luaRegisterDates(L *lua.LState) { L.SetGlobal("date", mt) L.SetField(mt, "new", L.NewFunction(luaDateNew)) L.SetField(mt, "now", L.NewFunction(luaDateNow)) + L.SetField(mt, "__index", L.NewFunction(luaDate__index)) L.SetField(mt, "__tostring", L.NewFunction(luaDate__tostring)) L.SetField(mt, "__eq", L.NewFunction(luaDate__eq)) L.SetField(mt, "__lt", L.NewFunction(luaDate__lt)) @@ -93,6 +94,24 @@ func luaDateNow(L *lua.LState) int { return 1 } +func luaDate__index(L *lua.LState) int { + d := luaCheckTime(L, 1) + field := L.CheckString(2) + + switch field { + case "Year", "year": + L.Push(lua.LNumber(d.Year())) + case "Month", "month": + L.Push(lua.LNumber(float64(d.Month()))) + case "Day", "day": + L.Push(lua.LNumber(float64(d.Day()))) + default: + L.ArgError(2, "unexpected date attribute: "+field) + } + + return 1 +} + func luaDate__tostring(L *lua.LState) int { a := luaCheckTime(L, 1)