如何在lua中实现2倍对象之间的差异?

如何在lua中实现2倍对象之间的差异?,lua,Lua,我正在尝试用Lua编写一些代码。 代码应该完成以下任务:分析包含大量行的.log文件, 每行有3列:第一列为时间字符串格式00:00:00, 第二个是[id],第三个是操作字符串,如登录、注销等。 我需要做的是根据每个workerid的登录和注销操作,打印出每天的平均小时数。 以下是我的解决方案: function averageHoursPerWorker() fh,err = io.open("log2.log") if err ~= nil then print (err)

我正在尝试用Lua编写一些代码。 代码应该完成以下任务:分析包含大量行的.log文件, 每行有3列:第一列为时间字符串格式00:00:00, 第二个是[id],第三个是操作字符串,如登录、注销等。 我需要做的是根据每个workerid的登录和注销操作,打印出每天的平均小时数。 以下是我的解决方案:

function averageHoursPerWorker()
fh,err = io.open("log2.log")
if err ~= nil then
        print (err)
        return
        end
numOfDaysPerWorkerArray = {}
amountOfHoursPerWorkerArray = {}
logInsPerWorker = {}
logOutsPerWorker = {}
hour = ''
worker = ''
action = ''

while true do
        stuff = fh.read(fh)
        if stuff == nil then break end
        lynetab = {}
        for i in string.gmatch(stuff,"(%S+)") do
                lynetab[#lynetab+1] = i
                -- print (i)
                end
        --here, lynatab contains one line. this is the logic
        hour = lynetab[1]
        action = lynetab[3]
        worker = lynetab[2]
        if action == 'log-in' then --save the certain worker log-in hour
            logInsPerWorker[worker] = hour
        end
        if action == 'log-out' then
            logOutsPerWorker[worker] = hour --save the certain worker log-out hour
            if not (logInsPerWorker[worker] == 0) then --this worker already logged in
                if amountOfHoursPerWorkerArray[worker] == nil then --initialize worker's hours%days counter
                    amountOfHoursPerWorkerArray[worker] = 0
                    numOfDaysPerWorkerArray[worker] = 0
                end
                -- print(math.floor(os.difftime(makeTimeStamp(logOutsPerWorker[worker]),makeTimeStamp(logInsPerWorker[worker]))))

                --add this hours to worker's hours counter and add one day to days counter of this worker
                --i know that the results are wrong- this is because the - operator did not do his job...
                --i had no more time to figure it out but i do believe it's something pretty simple to solve.
                -- print(GetTimeDifference(logOutsPerWorker[worker],logInsPerWorker[worker]))
                amountOfHoursPerWorkerArray[worker] = amountOfHoursPerWorkerArray[worker] + makeTimeStamp(logOutsPerWorker[worker])-makeTimeStamp(logInsPerWorker[worker])
                numOfDaysPerWorkerArray[worker] = numOfDaysPerWorkerArray[worker] + 1
                logInsPerWorker[worker] = 0
            end
        end
end
--print the average hours for day per worker
for k,v in pairs(amountOfHoursPerWorkerArray) do
    print(k,tonumber(os.date('%H',v))/numOfDaysPerWorkerArray[k])
end
end
我认为问题在于减号运算的准时性,所以我有以下功能:

function makeTimeStamp(dateString)
local pattern = "(%d+):(%d+):(%d+)"
local xhour, xminute, 
    xseconds = dateString:match(pattern)
local convertedTimestamp = os.time{ year=0,month=0,day=0,
    hour = xhour, minute = xminute, second = xseconds}
return convertedTimestamp
end
因为这对我不起作用-我也有这个功能,它起到了不同的作用:

function GetTimeDifference(intialTime,finalTime)
initialHour=tonumber(string.sub(intialTime,1,2)) *3600
initialMinute=tonumber(string.sub(intialTime,4,5))*60
initialSecond=tonumber(string.sub(intialTime,7,8))

finalHour=tonumber(string.sub(finalTime,1,2))*3600
finalMinute=tonumber(string.sub(finalTime,4,5))*60
finalSecond=tonumber(string.sub(finalTime,7,8))

totalInitialTime=initialHour+initialMinute+initialSecond
totalFinalTime=finalHour+finalMinute+finalSecond
local duration=totalFinalTime-totalInitialTime

formatedDuration="00:00:00"
if(duration<10) then
    formatedDuration="00:00:0"..duration
elseif(duration>9 and duration<60) then
    formatedDuration="00:00:"..duration
elseif(duration>59 and duration<=3600 ) then
    --minutes handler
    intermediateCalc=(duration/60)
    i,j=string.find(tostring(intermediateCalc),".")
    if(i==nil and j==nil) then
      formatedDuration="00:0"..intermediateCalc
    else
       min=string.sub(tostring(intermediateCalc),i,j)
       if(tonumber(min)<10) then
        formatedDuration="00:0"..min
       else
        formatedDuration="00:"..min
      end
    end

    newSeconds=duration%60
    if(newSeconds<10) then
        formatedDuration=formatedDuration..":0"
                ..newSeconds
    else
        formatedDuration=formatedDuration..":"
            ..newSeconds
    end
else
    --hour handler

    newMinutes=(finalMinute-initialMinute)/60
    if(newMinutes<0) then
      newMinutes=newMinutes*-1
    end

    if(newMinutes<10) then
        newMinutes="0"..newMinutes
    end

    newSeconds=(finalSecond-initialSecond)
    if(newSeconds<0) then
      newSeconds=newSeconds*-1
    end

    if(newSeconds<10) then
        newSeconds="0"..newSeconds
    end

    formatedDuration=(finalHour-initialHour)/3600
     ..":"..newMinutes..":"..newSeconds
end
return formatedDuration
end

仍然不工作。。。请帮忙

我假设您使用的是基于24小时的时钟值,并且人们在同一天结账,因为您没有指定数据中的天数。假设这样,我将使用字符串匹配模式,然后将值修改为要使用的数字。 使用string.format为输出生成相同的结构可能如下所示:

function calculateWorktime(time_login, time_logout)
    login = {time_login:match("(%d+):(%d+):(%d+)")}
    logout = {time_logout:match("(%d+):(%d+):(%d+)")}

    in_h, out_h = tonumber(login[1]), tonumber(logout[1])
    in_m, out_m = tonumber(login[2]), tonumber(logout[2])
    in_s, out_s = tonumber(login[3]), tonumber(logout[3])

    if out_s < in_s then
        out_s = out_s + 60
        out_m = out_m - 1
    end

    if out_m < in_m then
        out_m = out_m + 60
        out_h = out_h - 1
    end

    worked_h = out_h - in_h
    worked_m = out_m - in_m
    worked_s = out_s - in_s

    worked_time = ("%02d:%02d:%02d"):format(worked_h, worked_m, worked_s)

    return worked_time
end

这将生成14:25:40,登录时间为08:10:32,注销时间为22:36:12

什么不起作用?具体点。我相信您可以在代码中找到一个特定的位置,它没有完成它应该做的事情。解释它对一些示例输入所做的操作以及它所做的操作。os.time{year=0该年超出了允许的范围,因此os.time不起作用