X-Git-Url: https://git.madduck.net/etc/awesome.git/blobdiff_plain/81eaf243f3f28403292343af73e178f79ab5c0b3..cf0cc951c06f50ff9390eca662f13d4887c83114:/src/timetable.lua diff --git a/src/timetable.lua b/src/timetable.lua index 9af7994..df78b15 100644 --- a/src/timetable.lua +++ b/src/timetable.lua @@ -63,13 +63,17 @@ local function increment ( tens , units , base ) return tens , units end +local function unpack_tm ( tm ) + return assert ( tm.year , "year required" ) , + assert ( tm.month , "month required" ) , + assert ( tm.day , "day required" ) , + tm.hour or 12 , + tm.min or 0 , + tm.sec or 0 +end + local function normalise ( tm ) - local sec = tm.sec or 0 - local min = tm.min or 0 - local hour = tm.hour or 12 - local day = assert ( tm.day , "day required" ) - local month = assert ( tm.month , "month required" ) - local year = assert ( tm.year , "year required" ) + local year , month , day , hour , min , sec = unpack_tm ( tm ) min , sec = increment ( min , sec , 60 ) -- TODO: consider leap seconds? hour , min = increment ( hour , min , 60 ) @@ -107,8 +111,29 @@ local function normalise ( tm ) return tm end +local timetable_mt = { +} + +local function cast_timetable ( tm ) + return setmetatable ( tm , timetable_mt ) +end + +local function new_timetable ( year , month , day , hour , min , sec ) + return cast_timetable { + year = year ; + month = month ; + day = day ; + hour = hour ; + min = min ; + sec = sec ; + } +end return { doomsday = doomsday ; normalise = normalise ; + + new = new_timetable ; + cast = cast_timetable ; + timetable_mt = timetable_mt ; }