source: stamper/bin/time_sum@ 63:76140cb24ebd

Last change on this file since 63:76140cb24ebd was 14:1231814a7b4b, checked in by Óscar M. Lage <info@…>, 10 years ago

Micro-refactor

  • Property exe set to *
File size: 1.2 KB
Line 
1#!/usr/bin/env python
2
3import sys
4import time
5
6TIME_FORMAT= "%H:%M"
7HOURS_DAY = 8
8SECS_DAY = HOURS_DAY * 60 * 60
9
10def usage(name):
11 msg = """
12time_sum.py: Sum dates in proper hour time
13
14Params: Worked hours
15Syntax: $ ./time_sum 6:45 13:48 12:44 8:28 14:43
1656:28 min. = 7 days, remaining: 0:28 (8 hours/day)
17"""
18 print(msg)
19 exit(1)
20
21def isTimeFormat(input):
22 try:
23 time.strptime(input, TIME_FORMAT)
24 return True
25 except ValueError:
26 return False
27
28
29if __name__ == '__main__':
30
31 totalSecs = 0
32 for param in sys.argv[1:]:
33 if not isTimeFormat(param):
34 print("Invalid hour format: %s" % param)
35 help()
36 exit(1)
37 timeParts = [int(s) for s in param.split(':')]
38 totalSecs += (timeParts[0] * 60 + timeParts[1]) * 60
39
40 totalDays, remaining = divmod(totalSecs, SECS_DAY)
41 remainingMin, remainingSec = divmod(remaining, (60))
42 remainingHr, remainingMin = divmod(remainingMin, (60))
43
44 totalSecs, sec = divmod(totalSecs, 60)
45 hr, min = divmod(totalSecs, 60)
46 print "%d:%02d hours. = %d days, remaining: %d:%02d (%d hours/day)" % (
47 hr, min, totalDays, remainingHr, remainingMin, HOURS_DAY)
Note: See TracBrowser for help on using the repository browser.