计算在线会议的总时间 [英] Calculating the total time in an online meeting

查看:174
本文介绍了计算在线会议的总时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设有一个会议并将会议记录保存在CSV文件中.如何编写bash脚本/awk脚本以找出员工在线的总时间.一名员工可以离开并重新参加会议,应该计算他/她的所有在线时间.

Suppose there was a meeting and the meeting record is saved in a CSV file. How to write a bash script/awk script to find out the total amount of time for which an employee stayed online. One employee may leave and rejoin the meeting, all his/her online time should be calculated.

我所做的工作如下,但是在如何比较一条记录和所有其他记录,以及将一个人的每对加入和离开的对的总时间上有所了解.

What I did is as follows, but got stuck on how to compare one record with all other record, and add the total time of each joined and left pairs of a person.

#!/bin/bash

inputFile=$1
startTime=$(date -u -d $2 +"%s")
endTime=$(date -u -d $3 +"%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 (($1==?? && $2=="Joined") && ($1==?? && $2=="Left")) {
                     totalTime=$($(date -u -d $3 +"%s")-$(date -u -d $3 +"%s"))
       print $1 "," $totalTime +"%H:%M:%S"
 }' $inputFile

会议的开始时间和结束时间在命令行中给出,例如:

The start_time and end_time of the meeting are given at command line such as:

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

输出看起来像这样:(可以存储在输出文件中)

The output look like this: (Can be stored in an output file)

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 }
$1 in joined {
    jt = time2secs(joined[$1])
    lt = time2secs($3)
    totSecs[$1] += (lt - jt)
    delete joined[$1]
    next
}
{ joined[$1] = $3 }
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秒,会议整夜(或连续数天),会议结束时人们仍在会议中,或者您的问题中没有显示的其他任何内容-剩下的作为练习:-).

If you need to consider DST transitions, leap-seconds, meetings going overnight (or for multiple days), people still being in the meeting when it ends, or anything else you haven't shown in your question - that is left as an exercise :-).

这篇关于计算在线会议的总时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆