2013-10-18 20:38:07 +00:00
|
|
|
local TimeVal = {
|
2012-11-16 23:54:01 +00:00
|
|
|
sec = 0,
|
|
|
|
usec = 0,
|
|
|
|
}
|
|
|
|
|
|
|
|
function TimeVal:new(o)
|
|
|
|
local o = o or {}
|
|
|
|
if o.sec == nil then
|
|
|
|
o.sec = 0
|
|
|
|
end
|
|
|
|
if o.usec == nil then
|
|
|
|
o.usec = 0
|
2012-11-17 18:41:26 +00:00
|
|
|
elseif o.usec > 1000000 then
|
|
|
|
o.sec = o.sec + maht.floor(o.usec/1000000)
|
|
|
|
o.usec = o.usec % 1000000
|
2012-11-16 23:54:01 +00:00
|
|
|
end
|
|
|
|
setmetatable(o, self)
|
|
|
|
self.__index = self
|
|
|
|
return o
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function TimeVal:__lt(time_b)
|
|
|
|
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
|
|
|
|
end
|
|
|
|
|
|
|
|
function TimeVal:__le(time_b)
|
|
|
|
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
|
|
|
|
end
|
|
|
|
|
|
|
|
function TimeVal:__eq(time_b)
|
|
|
|
if self.sec == time_b.sec and self.usec == time_b.usec then
|
|
|
|
return true
|
|
|
|
else
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function TimeVal:__sub(time_b)
|
2013-03-02 14:24:21 +00:00
|
|
|
local diff = TimeVal:new{}
|
2012-11-16 23:54:01 +00:00
|
|
|
|
|
|
|
diff.sec = self.sec - time_b.sec
|
|
|
|
diff.usec = self.usec - time_b.usec
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
return diff
|
|
|
|
end
|
|
|
|
|
|
|
|
function TimeVal:__add(time_b)
|
2013-03-02 14:24:21 +00:00
|
|
|
local sum = TimeVal:new{}
|
2012-11-16 23:54:01 +00:00
|
|
|
|
|
|
|
sum.sec = self.sec + time_b.sec
|
|
|
|
sum.usec = self.usec + time_b.usec
|
|
|
|
if sum.usec > 1000000 then
|
2012-11-23 06:04:56 +00:00
|
|
|
sum.usec = sum.usec - 1000000
|
2012-11-16 23:54:01 +00:00
|
|
|
sum.sec = sum.sec + 1
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
return sum
|
|
|
|
end
|
|
|
|
|
|
|
|
function TimeVal:now()
|
|
|
|
local sec, usec = util.gettime()
|
|
|
|
return TimeVal:new{sec = sec, usec = usec}
|
|
|
|
end
|
2013-10-18 20:38:07 +00:00
|
|
|
|
|
|
|
return TimeVal
|