From 55d0e718c8f30795d80f92731a5517e52d1dccdd Mon Sep 17 00:00:00 2001 From: Aaron Lindsay Date: Sat, 11 Feb 2017 07:28:15 -0500 Subject: [PATCH] lua: Add ability to add/subtract dates --- date_lua.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/date_lua.go b/date_lua.go index 1e1acb0..1f7d2a6 100644 --- a/date_lua.go +++ b/date_lua.go @@ -19,6 +19,8 @@ func luaRegisterDates(L *lua.LState) { L.SetField(mt, "__eq", L.NewFunction(luaDate__eq)) L.SetField(mt, "__lt", L.NewFunction(luaDate__lt)) L.SetField(mt, "__le", L.NewFunction(luaDate__le)) + L.SetField(mt, "__add", L.NewFunction(luaDate__add)) + L.SetField(mt, "__sub", L.NewFunction(luaDate__sub)) L.SetField(mt, "__metatable", lua.LString("protected")) } @@ -146,3 +148,23 @@ func luaDate__le(L *lua.LState) int { return 1 } + +func luaDate__add(L *lua.LState) int { + a := luaCheckTime(L, 1) + b := luaCheckTime(L, 2) + + date := a.AddDate(b.Year(), int(b.Month()), b.Day()) + L.Push(TimeToLua(L, &date)) + + return 1 +} + +func luaDate__sub(L *lua.LState) int { + a := luaCheckTime(L, 1) + b := luaCheckTime(L, 2) + + date := a.AddDate(-b.Year(), -int(b.Month()), -b.Day()) + L.Push(TimeToLua(L, &date)) + + return 1 +}