如何区分lua中2次对象之间的差异?

问题描述 投票:0回答:1

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

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

仍然无法正常运行,请帮助!

lua
1个回答
1
投票
我假设您要使用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”]
© www.soinside.com 2019 - 2024. All rights reserved.