Last active
March 8, 2021 17:17
-
-
Save devilholk/c4b1fc3fe09b6c742120a8a1a8f1a32f to your computer and use it in GitHub Desktop.
Primitive time tally tool
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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