Hello. I come up with writing this small lib. Its easy to use, I will present it below.
How it works? It gets time range (from hour to hour) and check if current time is in this range.
It may contain small error, but if you have some skils you can easily fix this.
Usage:
How it works? It gets time range (from hour to hour) and check if current time is in this range.
It may contain small error, but if you have some skils you can easily fix this.
Code:
TimeRange = {}
function TimeRange:new(time1, time2)
local obj = {}
obj.timestart = {hours = time1.hours, minutes = time1.minutes}
obj.timeend = {hours = time2.hours, minutes = time2.minutes}
self.__index = self
return setmetatable(obj,self)
end
function TimeRange:changeToMinutes(t)
return t.hours*60 + t.minutes
end
function TimeRange:isBetween()
local timeinseconds = self:changeToMinutes(getTibiaTime())
local timestart = self:changeToMinutes(self.timestart)
local timeend = self:changeToMinutes(self.timeend)
local t1 = math.min(timestart, timeend)
local t2 = math.max(timestart, timeend)
print(t1,t2,timeinseconds)
if(t1 == timestart) then
print("t1 tstart")
return (timeinseconds >= t1 and timeinseconds <= t2)
elseif (t1 == timeend) then
print("t2 tstart")
return not((timeinseconds > t1 and timeinseconds < t2))
end
return false
end
Usage:
Code:
local timeSample =TimeRange:new({hours = 1, minutes = 21},{hours = 1, minutes = 50})
print("Is current time in range? " .. tostring(timeSample:isBetween()))