Skip to content

Instantly share code, notes, and snippets.

@devilholk
Last active March 8, 2021 17:17
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 devilholk/c4b1fc3fe09b6c742120a8a1a8f1a32f to your computer and use it in GitHub Desktop.
Save devilholk/c4b1fc3fe09b6c742120a8a1a8f1a32f to your computer and use it in GitHub Desktop.
Primitive time tally tool
import time
def get_day_minutes(t):
s = time.strptime(t, '%H:%M')
return s.tm_hour * 60 + s.tm_min
def calc_duration(start_time, stop_time):
'Returns how many minutes have passed between two times expressed as HH:MM'
'Wraps around for midnight'
'Does not track DST'
duration = get_day_minutes(stop_time) - get_day_minutes(start_time)
if duration < 0: #midnight wrap around
return duration + 24 * 60
return duration
def fmt_minutes(minutes):
'Formats a number of minutes as HH:MM'
return f'{minutes // 60}:{str(minutes % 60).zfill(2)}'
print("EOF (Ctrl+D) when done, SIGINT (Ctrl+C) to restart input from last valid input")
totals = dict()
while True:
try:
print('From:', end=' '); start_time = input()
print('To:', end=' '); stop_time = input()
print('Activity:', end=' '); activity = input()
except EOFError:
break
except KeyboardInterrupt:
print("\nAborted input, try again")
continue
try:
duration = calc_duration(start_time, stop_time)
except ValueError as e:
print(f'\nInvalid input, try again ({e})')
continue
print()
print(f'Duration: {fmt_minutes(duration)}')
print()
totals[activity] = totals.get(activity, 0) + duration
print()
print('-'*50)
print('Totals')
print('-'*50)
for activity, duration in totals.items():
print(f'\t{activity}\t{fmt_minutes(duration)}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment