前言
刚入职不知道工时要平均每天满8小时,并且看不到每天的实际工时,一气之下花了一个中午写了个脚本计算每天的工时,分享一下,不同地区的兄弟需要修改一下午休和晚饭时间才能使用。
文件位置
把welink上 上下班时间输入work.csv的文档里面,代码放入worktime.py里面
work.csv的格式如下:
第一列是日期,第二列是上班时间,第三列是下班时间
代码
将代码放入worktime.py文件就行
import csv
from datetime import *
with open('./work.csv','r') as myFile:
lines = csv.reader(myFile)
zhongwu1 = "12:30:00"
zhongwu2 = "14:00:00"
wanshang1 = "17:30:00"
wanshang2 = "18:00:00"
zhongwu1 = datetime.strptime(zhongwu1,"%H:%M:%S")
zhongwu2 = datetime.strptime(zhongwu2,"%H:%M:%S")
wanshang1 = datetime.strptime(wanshang1,"%H:%M:%S")
wanshang2 = datetime.strptime(wanshang2,"%H:%M:%S")
sum = datetime.strptime("00:00:00","%H:%M:%S")
count = 0
for line in lines:
count = count + 1
shangban = datetime.strptime(line[1],"%H:%M:%S")
xiaban = datetime.strptime(line[2],"%H:%M:%S")
shangwu = zhongwu1 - shangban
if xiaban<wanshang2:
xiawu = xiaban - zhongwu2
else:
xiawu = wanshang1 - zhongwu2 + xiaban - wanshang2
gongshi = shangwu+xiawu
sum = sum + gongshi
print(line[0],"工时为:",gongshi)
all = (sum.day-1)*24*60*60+sum.hour*60*60+sum.minute*60+sum.second
aver = all/count
avh = aver//3600
avs = aver%60
avm = (aver%3600 - avs)/60
print("平均每天工作:",int(avh),":",int(avm),":",int(avs))
运行结果
有兴趣的兄弟可以加上写文件啥的写会,我是没有这个需求,对我来说能看到平均时间有没有满8小时就行了。