clocktoy/time.lua

40 lines
799 B
Lua
Raw Normal View History

local MAX_MINUTES = 60 * 12
local Time = {}
Time.__index = Time
local function newTimeRaw(rawMinutes)
local t = {
rawminutes = rawMinutes % MAX_MINUTES
}
return setmetatable(t, Time)
end
function Time:new(hour, minute)
return newTimeRaw((hour or 12) % 12 * 60 + (minute or 0))
end
function Time:get()
local h = math.floor(self.rawminutes / 60 % 12)
if h == 0 then h = 12 end
local m = self.rawminutes % 60
return h, m
end
function Time:getHoursSinceTwelve()
local h = self.rawminutes / 60
if h < 1 then h = h + 12 end
return h
end
function Time:addMinutes(minutes)
return newTimeRaw(self.rawminutes + minutes)
end
function Time:__tostring()
local h, m = self:get()
return string.format("%2d:%02d", h, m)
end
return Time