免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 2981 | 回复: 18
打印 上一主题 下一主题

[文本处理] 统计工时 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-07-16 10:30 |只看该作者 |倒序浏览
分析的文件格式如下:
2013-07-15    08:26:42    18:12:00
2013-07-12    08:31:38    18:19:17
2013-07-11    08:23:47    18:20:50
2013-07-10    08:17:37    18:24:54
2013-07-09    08:00:00    17:30:00
2013-07-08    08:27:28    17:41:32
2013-07-05    08:24:26    18:14:06
2013-07-04    08:22:21    20:07:45
2013-07-03    08:20:00    20:22:34
2013-07-02    08:17:52    20:10:30
2013-07-01    08:21:46    17:42:14

规则:
1、早上8点以前不计工时
2、中午12点-13.30不计工时
3、下午17:30-18:00不计工时。
4、要求输出文件中的每天平均工时数。例如:上面就是输出这11天的平均工时数。
请各路大神帮帮忙,谢谢~

论坛徽章:
39
辰龙
日期:2013-08-21 15:45:192015亚冠之广州富力
日期:2015-05-12 16:34:52亥猪
日期:2015-03-03 17:22:00申猴
日期:2015-03-03 17:21:37未羊
日期:2014-10-10 13:45:41戌狗
日期:2014-06-17 09:53:29巨蟹座
日期:2014-06-12 23:17:17双鱼座
日期:2014-06-10 12:42:44寅虎
日期:2014-06-09 12:52:172015亚冠之卡尔希纳萨夫
日期:2015-05-24 15:24:35黄金圣斗士
日期:2015-12-02 17:25:0815-16赛季CBA联赛之吉林
日期:2017-06-24 16:43:52
2 [报告]
发表于 2013-07-16 11:07 |只看该作者
本帖最后由 关阴月飞 于 2013-07-16 11:11 编辑

回复 1# whoislp


lz需要精确到什么级别? 小时? 分钟? 秒?
另外,打卡时间会不会出现跨日期的情况: 比如:
2013-07-15    08:26:42    02:12:00   #这里到了第二天再打的下班卡

论坛徽章:
3
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:51:162015年亚洲杯之阿曼
日期:2015-04-07 20:00:59
3 [报告]
发表于 2013-07-16 11:25 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
0
4 [报告]
发表于 2013-07-16 11:49 |只看该作者
回复 2# 关阴月飞
精确到分就可以了,跨时间的暂时不考虑。

   

论坛徽章:
0
5 [报告]
发表于 2013-07-16 11:55 |只看该作者
回复 3# zooyo
  1. linux testshell/tmp0703> awk -f sss.sh 123.txt
  2. 07:08:10
复制代码
貌似结果不是很正确啊,每天肯定不止这么多。。。

   

论坛徽章:
1
IT运维版块每日发帖之星
日期:2015-12-16 06:20:00
6 [报告]
发表于 2013-07-16 13:38 |只看该作者
用shell写了一个,可能有1分钟左右点误差。
  1. nathanielwen@ubuntu:~$ cat shell
  2. #!/bin/bash

  3. tmp0=08:00:00
  4. tmp1=12:00:00
  5. tmp2=13:30:00
  6. tmp3=17:30:00
  7. tmp4=18:00:00

  8. for((i=1;i<12;i++))
  9. {
  10.         thedate=`sed -n "$i p" time.txt | cut -d' ' -f1`;
  11.         time1=`sed -n "$i p" time.txt | cut -d' ' -f5`;
  12.         time2=`sed -n "$i p" time.txt | cut -d' ' -f9`;

  13.         time3=$(($(date +%s -d "$time1")-$(date +%s -d "$tmp0")));
  14.         if [ $time3 -lt 0 ]
  15.         then
  16.                 time1=$tmp0;
  17.         fi
  18.         #先把8点以前到时间调整为8点

  19.         time3=$(($(date +%s -d "$time2")-$(date +%s -d "$tmp1")));
  20.         if [ $time3 -lt 0 ]
  21.         then
  22.                 thetime=$(($(date +%s -d "$time2")-$(date +%s -d "$time1")));
  23.                 echo $thedate `echo $thetime/3600|bc`:`echo $thetime%3600/60|bc`
  24.                 continue;
  25.         fi
  26.         #第一种情况,在12点之前下班,则直接计算结果
  27.        
  28.         time3=$(($(date +%s -d "$time2")-$(date +%s -d "$tmp2")));
  29.         if [ $time3 -lt 0 ]
  30.         then
  31.                 time2=$tmp1;
  32.                 thetime=$(($(date +%s -d "$time2")-$(date +%s -d "$time1")));
  33.                 echo $thedate `echo $thetime/3600|bc`:`echo $thetime%3600/60|bc`
  34.                 continue;
  35.         fi
  36.         #第二种情况,在12点-13点30下班,则视为在12点下班

  37.         time3=$(($(date +%s -d "$time2")-$(date +%s -d "$tmp3")));
  38.         if [ $time3 -lt 0 ]
  39.         then
  40.                 thetime=$(($(date +%s -d "$time2")-$(date +%s -d "$time1")));
  41.                 thetime=`echo "$thetime-90*60" | bc`;
  42.                 echo $thedate `echo $thetime/3600|bc`:`echo $thetime%3600/60|bc`
  43.                 continue;
  44.         fi
  45.         #第三种情况,在17点30以前下班,则计算结果,减去90分钟

  46.         time3=$(($(date +%s -d "$time2")-$(date +%s -d "$tmp4")));
  47.         if [ $time3 -lt 0 ]
  48.         then
  49.                 time2=$tmp3;
  50.                 thetime=$(($(date +%s -d "$time2")-$(date +%s -d "$time1")));
  51.                 thetime=`echo "$thetime-30*60" | bc`;
  52.                 echo $thedate `echo $thetime/3600|bc`:`echo $thetime%3600/60|bc`
  53.                 continue;
  54.         fi
  55.         #第四种情况,在17点30-18点下班,则视为在17点30下班

  56.        
  57.         thetime=$(($(date +%s -d "$time2")-$(date +%s -d "$time1")));
  58.         thetime=`echo "$thetime-120*60" | bc`;
  59.         echo $thedate `echo $thetime/3600|bc`:`echo $thetime%3600/60|bc`
  60.         #第五种情况,在18点之后下班,则减去120分钟
  61. }
复制代码
  1. nathanielwen@ubuntu:~$ ./shell
  2. 2013-07-15 7:45
  3. 2013-07-12 7:47
  4. 2013-07-11 7:57
  5. 2013-07-10 8:7
  6. 2013-07-09 7:59
  7. 2013-07-08 8:32
  8. 2013-07-05 7:49
  9. 2013-07-04 9:45
  10. 2013-07-03 10:2
  11. 2013-07-02 9:52
  12. 2013-07-01 8:38
复制代码

论坛徽章:
3
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:51:162015年亚洲杯之阿曼
日期:2015-04-07 20:00:59
7 [报告]
发表于 2013-07-16 14:00 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
0
8 [报告]
发表于 2013-07-16 15:21 |只看该作者
回复 7# zooyo
赶脚很强大,多谢~


   

论坛徽章:
3
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:51:162015年亚洲杯之阿曼
日期:2015-04-07 20:00:59
9 [报告]
发表于 2013-07-16 15:36 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
13
丑牛
日期:2013-08-16 15:08:22技术图书徽章
日期:2013-11-26 10:13:40双鱼座
日期:2013-11-08 15:03:26戌狗
日期:2013-11-08 13:52:30技术图书徽章
日期:2013-11-05 14:06:30戌狗
日期:2013-10-31 11:45:42CU十二周年纪念徽章
日期:2013-10-24 15:41:34天秤座
日期:2013-10-11 14:55:08子鼠
日期:2013-09-26 19:36:35水瓶座
日期:2013-09-26 17:44:56午马
日期:2013-08-26 10:24:23丑牛
日期:2013-08-19 14:43:22
10 [报告]
发表于 2013-07-16 16:49 |只看该作者
py版的
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-

  3. def ati(s):
  4.     return int(s.split(':')[0]) * 3600 + int(s.split(':')[1]) * 60 + int(s.split(':')[2])
  5. def bti(s,t):
  6.     print"%s,%02d:%02d:%02d"%(s,t/3600,t%3600/60,t%60)

  7. for line in open('file'):
  8.     sum,h=0,3600
  9.     start,end1,end2=8*h,17.5*h,18*h
  10.     s,t=line.split()[1],line.split()[2]
  11.     x,y=ati(s),ati(t)
  12.     if x < start:
  13.         x=start
  14.     if y >= end1 and y <= end2:
  15.         sum=end1-x-1.5*h
  16.     if y > end2:
  17.         sum=y-x-1.5*h-0.5*h
  18.     bti(line.split()[0],sum)
复制代码
[root@ py]$ python 4.py
2013-07-15,07:45:18
2013-07-12,07:47:39
2013-07-11,07:57:03
2013-07-10,08:07:17
2013-07-09,08:00:00
2013-07-08,07:32:32
2013-07-05,07:49:40
2013-07-04,09:45:24
2013-07-03,10:02:34
2013-07-02,09:52:38
2013-07-01,07:38:14
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP