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)