Skip to content

Instantly share code, notes, and snippets.

@felipe-prenholato
Created February 16, 2012 17:31
Show Gist options
  • Save felipe-prenholato/1846621 to your computer and use it in GitHub Desktop.
Save felipe-prenholato/1846621 to your computer and use it in GitHub Desktop.
~$ ./sum_hours.py tracking.txt
08/02/2012 21:00 23:59 -> 2:59:00 hours
09/02/2012 00:00 00:40 -> 0:40:00 hours
Group hours: 3:39:00
09/02/2012 21:00 23:59 -> 2:59:00 hours
09/02/2012 00:00 01:40 -> 1:40:00 hours
Group hours: 4:39:00
09/02/2012 22:30 23:59 -> 1:29:00 hours
10/02/2012 00:00 01:40 -> 1:40:00 hours
Group hours: 3:09:00
10/02/2012 22:58 23:59 -> 1:01:00 hours
11/02/2012 00:00 05:40 -> 5:40:00 hours
Group hours: 6:41:00
11/02/2012 19:00 23:59 -> 4:59:00 hours
12/02/2012 00:00 04:40 -> 4:40:00 hours
Group hours: 9:39:00
12/02/2012 16:00 18:00 -> 2:00:00 hours
12/02/2012 22:00 23:59 -> 1:59:00 hours
13/02/2012 00:00 02:00 -> 2:00:00 hours
Group hours: 5:59:00
13/02/2012 23:00 23:59 -> 0:59:00 hours
14/02/2012 00:00 00:40 -> 0:40:00 hours
Group hours: 1:39:00
Group hours: 0:00:00
Total hours: 1 day, 11:25:00
#!/usr/bin/env python
# from a file with entries like that
#
# 08/02/2012 09:00 23:59
# 09/02/2012 00:00 00:40
#
# 09/02/2012 09:00 23:59
# 09/02/2012 00:00 01:40
#
# 09/02/2012 22:30 23:59
# 10/02/2012 00:00 01:40
#
# read and get 'worked' ours grouping by non blank lines
#
# pass file as argument
#
import sys, os
import datetime
from pprint import pprint
def process(tracking_file):
f = open(os.path.realpath(tracking_file),'r')
lines = ['\n'] + f.readlines()
groups = []
for i,line in enumerate(lines):
if i == 0 or line == '\n':
if i != 0:
groups.append(this_group)
this_group = []
else:
this_group.append(line)
hours = []
dformat = '%d/%m/%Y %H:%M'
for group in groups:
this_hours = []
for entry in group:
date,ini_time,end_time = entry.replace('\n','').split(' ')
ini_time =datetime.datetime.strptime(date+' '+ini_time,dformat)
end_time =datetime.datetime.strptime(date+' '+end_time,dformat)
this_hours.append(end_time - ini_time)
print '%s -> %s hours' % (entry.replace('\n',''),this_hours[-1])
print 'Group hours: %s' % sum(this_hours,datetime.timedelta())
print
hours.append(sum(this_hours,datetime.timedelta()))
print 'Total hours: %s' % sum(hours,datetime.timedelta())
if __name__ == '__main__':
try:
tracking_file = sys.argv[1]
except IndexError:
print "Shoud receive a file as parameter!"
sys.exit(1)
if os.path.exists(tracking_file) and os.path.isfile(tracking_file):
process(tracking_file)
else:
print "The parameter isn't a file!"
sys.exit(1)
08/02/2012 21:00 23:59
09/02/2012 00:00 00:40
09/02/2012 21:00 23:59
09/02/2012 00:00 01:40
09/02/2012 22:30 23:59
10/02/2012 00:00 01:40
10/02/2012 22:58 23:59
11/02/2012 00:00 05:40
11/02/2012 19:00 23:59
12/02/2012 00:00 04:40
12/02/2012 16:00 18:00
12/02/2012 22:00 23:59
13/02/2012 00:00 02:00
13/02/2012 23:00 23:59
14/02/2012 00:00 00:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment