计算行之间经过的时间

时间:2013-01-29 11:20:35

标签: elapsedtime

 |   uploadedby   |     uploaddate     |
    Gracey Vinas  | 2012-04-20 20:16:00
    Gracey Vinas  | 2012-04-20 20:25:00
    Gracey Vinas  | 2012-04-20 20:35:00
    Gracey Vinas  | 2012-04-20 20:39:00
    Gracey Vinas  | 2012-04-20 22:07:00
    Gracey Vinas  | 2012-04-21 00:04:00
    Gracey Vinas  | 2012-04-21 01:14:00
    Gracey Vinas  | 2012-04-23 17:56:00
    Gracey Vinas  | 2012-04-23 18:06:00
    Gracey Vinas  | 2012-04-23 18:21:00
    Gracey Vinas  | 2012-04-23 19:04:00
    Gracey Vinas  | 2012-04-23 19:11:00
    Gracey Vinas  | 2012-04-23 19:24:00
    Gracey Vinas  | 2012-04-23 20:08:00
    Gracey Vinas  | 2012-04-23 20:22:00
    Gracey Vinas  | 2012-04-23 21:00:00
    Gracey Vinas  | 2012-04-23 22:04:00
    Gracey Vinas  | 2012-04-23 22:17:00
    Gracey Vinas  | 2012-04-23 22:29:00
    Gracey Vinas  | 2012-04-23 23:02:00
    Gracey Vinas  | 2012-04-23 23:48:00
    Gracey Vinas  | 2012-04-24 00:23:00
    Gracey Vinas  | 2012-04-24 01:54:00
    Gracey Vinas  | 2012-04-24 17:13:00
    Gracey Vinas  | 2012-04-24 17:32:00
    Gracey Vinas  | 2012-04-24 17:38:00
    Gracey Vinas  | 2012-04-24 17:45:00
    Gracey Vinas  | 2012-04-24 17:54:00

如何在msql中按日期获取每次上传的平均已用时间。例如(2012-04-20每次上传的平均经过时间是(第1行和第2行的时间差异(9分钟)+第2行和第3行的时间差异(10分钟)+第3行和第3行的时间差异4(4分钟)+第4行和第5行的时差(92分钟)/ 4 =平均经过时间为28.75分钟。

1 个答案:

答案 0 :(得分:0)

您没有指定语言,因此有一个python解决方案:

from re import search
from itertools import groupby
from operator import itemgetter
from datetime import datetime

elapsed = []
averages = []
with open("log.txt","r") as f:
    for line in f:
        date = search("\d{4}-\d{2}-\d{2}",line)
        time = search("\d{2}:\d{2}:\d{2}",line)
        if (date and time):
            elapsed.append((date.group(),datetime.strptime(time.group(),'%H:%M:%S')))
    for date,times in groupby(elapsed,itemgetter(0)):
        times = list(times)
        averages.append((date,(times[-1][1]-times[0][1]).seconds/60./(len(times)-1)))

for avg in averages:
    print 'On date %s average minutes downloading is %.2f' % avg

log.txt文件中数据的输出为:

On date 2012-04-20 average minutes downloading is 27.75
On date 2012-04-21 average minutes downloading is 70.00
On date 2012-04-23 average minutes downloading is 27.08
On date 2012-04-24 average minutes downloading is 175.17