Skip to content

Instantly share code, notes, and snippets.

@robinchew
Created August 4, 2019 18:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robinchew/881911977bcb36667a85678546220643 to your computer and use it in GitHub Desktop.
Save robinchew/881911977bcb36667a85678546220643 to your computer and use it in GitHub Desktop.
import re
import sys
from decimal import Decimal
def in_seconds(time_str):
hour, minute = map(int, time_str.split(':'))
return hour * 60 * 60 + minute * 60
def seconds_to_hours(seconds):
return seconds / 60 / 60
def minus_reverse(a, b):
return b - a
def no_zero_decimal(num):
if num.is_integer():
return str(int(num))
return str(num)
with open(sys.argv[1]) as f:
s = f.read()
line_hours = [
seconds_to_hours(sum(
minus_reverse(*(in_seconds(time_str) for time_str in rng.split('-')))
for rng in re.compile('-'.join(['[0-9]{1,2}:[0-9]{2}'] * 2)).findall(line)))
for line in re.compile(' *- *').sub('-', s).splitlines()
]
for line, hours in zip(s.splitlines(), line_hours):
if hours == 0:
print(line)
else:
print(f'{line} ({no_zero_decimal(hours)} hrs)')
dollars = Decimal(sys.argv[2]) * sum(map(Decimal, line_hours))
print()
print(f'Total hours: {sum(line_hours)} hrs = ${dollars}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment