计算在线会议的总时间

Calculating the total time in an online meeting

假设有一个会议,会议记录保存在一个CSV文件中。如何编写 bash script/awk 脚本来找出员工在线的总时间。一个员工可以离开并重新加入会议,所有his/her在线时间都应该计算。

我的做法如下,但卡在了如何比较一条记录与所有其他记录,以及添加每对加入和离开的人的总时间。

#!/bin/bash

inputFile=
startTime=$(date -u -d  +"%s")
endTime=$(date -u -d  +"%s")

awk 'BEGIN{ FS=","; totalTime=0; }

        {
            for (rows=1; rows <= NR; rows++) {
             #I am stuck here on how to compare a record with each and every record
                     if ((==?? && =="Joined") && (==?? && =="Left")) {
                     totalTime=$($(date -u -d  +"%s")-$(date -u -d  +"%s"))
       print  "," $totalTime +"%H:%M:%S"
 }' $inputFile

会议的start_time和end_time在命令行给出如:

$ ./script.sh input.csv 10:00:00 13:00:00

输出如下所示:(可以存储在输出文件中)

Bob,    00:30:00
John,    01:02:00

CSV文件内容如下:

    Employee_name, Joined/Left, Time  
    John, joined, 10:00:00  
    Bob, joined, 10:01:00  
    James, joined, 10:00:30  
    Bob, left, 10:20:00  
    Bob, joined, 10:35:00  
    Bob, left, 11:40:00  
    James, left, 11:40:00  
    John, left, 10:41:00
    Bob, joined, 11:45:00  
    
$ cat tst.awk
BEGIN { FS=" *, *"; OFS=", " }
NR==1 { next }
 in joined {
    jt = time2secs(joined[])
    lt = time2secs()
    totSecs[] += (lt - jt)
    delete joined[]
    next
}
{ joined[] =  }
END {
    for (name in totSecs) {
        print name, secs2time(totSecs[name])
    }
}

function time2secs(time,        t) {
    split(time,t,/:/)
    return (t[1]*60 + t[2])*60 + t[3]
}

function secs2time(secs,        h,m,s) {
    h = int(secs / (60*60))
    m = int((secs - (h*60*60)) / 60)
    s = int(secs % 60)
    return sprintf("%02d:%02d:%02d", h, m, s)
}

.

$ awk -f tst.awk file
James, 01:39:30
Bob, 01:24:00
John, 00:41:00

如果您需要考虑 DST 转换、leap-seconds、通宵(或持续多天)的会议、会议结束时人们仍在会议中,或者您未在问题中显示的任何其他内容- 留作练习 :-).