[13] | 1 | #!/usr/bin/env python
|
---|
| 2 |
|
---|
| 3 | import sys
|
---|
| 4 | import time
|
---|
| 5 |
|
---|
| 6 | TIME_FORMAT= "%H:%M"
|
---|
| 7 | HOURS_DAY = 8
|
---|
| 8 | SECS_DAY = HOURS_DAY * 60 * 60
|
---|
| 9 |
|
---|
| 10 | def usage(name):
|
---|
| 11 | msg = """
|
---|
| 12 | time_sum.py: Sum dates in proper hour time
|
---|
| 13 |
|
---|
| 14 | Params: Worked hours
|
---|
| 15 | Syntax: $ ./time_sum 6:45 13:48 12:44 8:28 14:43
|
---|
| 16 | 56:28 min. = 7 days, remaining: 0:28 (8 hours/day)
|
---|
| 17 | """
|
---|
| 18 | print(msg)
|
---|
| 19 | exit(1)
|
---|
| 20 |
|
---|
| 21 | def isTimeFormat(input):
|
---|
| 22 | try:
|
---|
| 23 | time.strptime(input, TIME_FORMAT)
|
---|
| 24 | return True
|
---|
| 25 | except ValueError:
|
---|
| 26 | return False
|
---|
| 27 |
|
---|
| 28 | totalSecs = 0
|
---|
| 29 |
|
---|
| 30 | if __name__ == '__main__':
|
---|
| 31 |
|
---|
| 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)
|
---|