2
0
mirror of https://github.com/koreader/koreader synced 2024-11-16 06:12:56 +00:00
koreader/frontend/ui/timeval.lua

159 lines
3.5 KiB
Lua
Raw Normal View History

2017-09-10 22:01:54 +00:00
--[[--
A simple module to module to compare and do arithmetic with time values.
@usage
local TimeVal = require("ui/timeval")
local tv_start = TimeVal:now()
-- Do some stuff.
2017-09-11 11:17:52 +00:00
-- You can add and subtract `TimeVal` objects.
2017-09-10 22:01:54 +00:00
local tv_duration = TimeVal:now() - tv_start
-- If you need more precision (like 2.5 s),
-- you can add the milliseconds to the seconds.
local tv_duration_seconds_float = tv_duration.sec + tv_duration.usec/1000000
]]
local dbg = require("dbg")
2014-06-04 09:22:45 +00:00
local util = require("ffi/util")
2017-09-10 22:01:54 +00:00
--[[--
TimeVal object.
@table TimeVal
@int sec floored number of seconds
@int usec remaining number of milliseconds
]]
2013-10-18 20:38:07 +00:00
local TimeVal = {
2014-03-13 13:52:43 +00:00
sec = 0,
usec = 0,
2012-11-16 23:54:01 +00:00
}
2017-09-10 22:01:54 +00:00
--[[--
Creates a new TimeVal object.
@usage
local timev = TimeVal:new{
sec = 10,
usec = 10000,
}
@treturn TimeVal
]]
2016-02-16 02:08:04 +00:00
function TimeVal:new(from_o)
local o = from_o or {}
2014-03-13 13:52:43 +00:00
if o.sec == nil then
o.sec = 0
end
if o.usec == nil then
o.usec = 0
elseif o.usec > 1000000 then
2014-06-08 11:56:42 +00:00
o.sec = o.sec + math.floor(o.usec/1000000)
2014-03-13 13:52:43 +00:00
o.usec = o.usec % 1000000
end
setmetatable(o, self)
self.__index = self
return o
2012-11-16 23:54:01 +00:00
end
function TimeVal:__lt(time_b)
2014-03-13 13:52:43 +00:00
if self.sec < time_b.sec then
return true
elseif self.sec > time_b.sec then
return false
else
-- self.sec == time_b.sec
if self.usec < time_b.usec then
return true
else
return false
end
end
2012-11-16 23:54:01 +00:00
end
function TimeVal:__le(time_b)
2014-03-13 13:52:43 +00:00
if self.sec < time_b.sec then
return true
elseif self.sec > time_b.sec then
return false
else
-- self.sec == time_b.sec
if self.usec > time_b.usec then
return false
else
return true
end
end
2012-11-16 23:54:01 +00:00
end
function TimeVal:__eq(time_b)
2014-03-13 13:52:43 +00:00
if self.sec == time_b.sec and self.usec == time_b.usec then
return true
else
return false
end
2012-11-16 23:54:01 +00:00
end
function TimeVal:__sub(time_b)
2014-03-13 13:52:43 +00:00
local diff = TimeVal:new{}
2012-11-16 23:54:01 +00:00
2014-03-13 13:52:43 +00:00
diff.sec = self.sec - time_b.sec
diff.usec = self.usec - time_b.usec
2012-11-16 23:54:01 +00:00
2014-03-13 13:52:43 +00:00
if diff.sec < 0 and diff.usec > 0 then
diff.sec = diff.sec + 1
diff.usec = diff.usec - 1000000
elseif diff.sec > 0 and diff.usec < 0 then
diff.sec = diff.sec - 1
diff.usec = diff.usec + 1000000
end
2012-11-16 23:54:01 +00:00
2014-03-13 13:52:43 +00:00
return diff
2012-11-16 23:54:01 +00:00
end
dbg:guard(TimeVal, '__sub',
function(self, time_b)
assert(self.sec > time_b.sec or (self.sec == time_b.sec and self.usec >= time_b.usec),
"Subtract the first timeval from the latest, not vice versa.")
end)
2012-11-16 23:54:01 +00:00
function TimeVal:__add(time_b)
2014-03-13 13:52:43 +00:00
local sum = TimeVal:new{}
2012-11-16 23:54:01 +00:00
2014-03-13 13:52:43 +00:00
sum.sec = self.sec + time_b.sec
sum.usec = self.usec + time_b.usec
if sum.usec > 1000000 then
sum.usec = sum.usec - 1000000
sum.sec = sum.sec + 1
end
2012-11-16 23:54:01 +00:00
2014-03-13 13:52:43 +00:00
if sum.sec < 0 and sum.usec > 0 then
sum.sec = sum.sec + 1
sum.usec = sum.usec - 1000000
elseif sum.sec > 0 and sum.usec < 0 then
sum.sec = sum.sec - 1
sum.usec = sum.usec + 1000000
end
2012-11-16 23:54:01 +00:00
2014-03-13 13:52:43 +00:00
return sum
2012-11-16 23:54:01 +00:00
end
2017-09-10 22:01:54 +00:00
--[[--
Creates a new TimeVal object based on the current time.
@usage
local TimeVal = require("ui/timeval")
local tv_start = TimeVal:now()
-- Do some stuff.
-- You can add and substract `TimeVal` objects.
local tv_duration = TimeVal:now() - tv_start
@treturn TimeVal
]]
2012-11-16 23:54:01 +00:00
function TimeVal:now()
2014-03-13 13:52:43 +00:00
local sec, usec = util.gettime()
return TimeVal:new{sec = sec, usec = usec}
2012-11-16 23:54:01 +00:00
end
2013-10-18 20:38:07 +00:00
return TimeVal