Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.