trying on production (my real dir :-))
[pauldeden-misc.git] / parse_timelog.py
blobf05f494ef32eca3f36f0a72b4f5209988ab3198d
1 import sys
2 import datetime
4 timelog_filename = sys.argv[1]
5 hourly_rate = float(sys.argv[2])
7 fo = open(timelog_filename)
8 lines = fo.readlines()
9 lines = [item.replace("\n", '') for item in lines]
10 outlines = []
11 for line in lines:
12 if line:
13 outlines.append(line)
14 del lines
16 total = datetime.timedelta()
17 in_working = False
18 start_dt = None
19 num_dates = 0
20 for line in outlines:
21 try:
22 dt = datetime.datetime.strptime(line, "%a %b %d %H:%M:%S %Z %Y")
23 # after here a correctly formatted date is found
24 num_dates += 1
25 if in_working: # found the end datetime
26 in_working = False
27 # calc diff and add to total
28 punch = dt - start_dt
29 print "one punch for %.2f hours" % (punch.seconds/3600.0,)
30 total += punch
31 else: # found the start datetime
32 in_working = True
33 # save start_dt
34 start_dt = dt
35 except ValueError:
36 pass
38 if (num_dates%2) != 0:
39 raise Exception, "Malformed file: There needs to be an even number of datetime entries in the file to generate hours worked."
41 # now we should have the total time worked. Go ahead and print it in hours.
42 hours_worked = total.seconds/3600.0
43 print "%.4f hours worked" % (hours_worked,)
44 invoice_amount = hours_worked * hourly_rate
45 print "At $%s an hour this makes a total of $%.2f to invoice" % (hourly_rate, invoice_amount)