Skip to content

Instantly share code, notes, and snippets.

@HacKanCuBa
Last active August 19, 2020 20:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HacKanCuBa/51798d2c8b4ba6dd5955ba86a8018e9c to your computer and use it in GitHub Desktop.
Save HacKanCuBa/51798d2c8b4ba6dd5955ba86a8018e9c to your computer and use it in GitHub Desktop.
Calculate and sum time differences easily
# Copyright © 2020 HacKan
# This work is free. You can redistribute it and/or modify it under the
# terms of the Do What The Fuck You Want To Public License, Version 2,
# as published by Sam Hocevar. See http://www.wtfpl.net/ for more details.
#
# This software is provided as-is. You are free to use, share, modify
# and share modifications under the terms of that license. Attribution
# is not required to share but is appreciated.
"""Calculate and sum time differences easily.
Requires Python 3.5+
"""
import re
from datetime import timedelta
from math import ceil
from typing import Optional
def timedelta_from_string(string: str) -> Optional[timedelta]:
"""Convert a string matching a time pattern to a timedelta if possible."""
time_pattern = re.compile(r'^(\d{1,2})[\D]?(\d{2})$') # type: re.Pattern
time_match = time_pattern.match(string) # type: Optional[re.Match[str]]
if not time_match:
return None
hours, minutes = map(int, time_match.groups()) # type: int
time_part = timedelta(hours=hours, minutes=minutes) # type: timedelta
return time_part
print(
'Time difference sum calculator.',
'For time ranges: input time ranges one part at a time: every two ',
'consecutive inputs is considered a range. Then leave empty to calculate.',
'The minute value is rounded up when displaying results.',
'The input format is roughly HH:MM or H:MM (actually the separator may be ',
'anything or nothing, not just ":").',
sep='\n',
)
total_seconds = 0 # type: int
prev_time_part = None # type: Optional[timedelta]
print('Time ranges')
while True:
raw_time = input(
'Input time (leave empty to continue): ',
)
if not raw_time:
if not prev_time_part:
break # Exit
print('Missing time range part')
continue
time_part = timedelta_from_string(raw_time) # type: Optional[timedelta]
if not time_part:
print('Wrong input format')
continue
if prev_time_part:
if time_part > prev_time_part:
difference = time_part - prev_time_part
else:
difference = prev_time_part - time_part
total_seconds += difference.total_seconds()
prev_time_part = None
else:
prev_time_part = time_part
print()
print('Time lapses')
while True:
raw_time = input(
'Input time (leave empty to continue): ',
)
if not raw_time:
break # Exit
time_part = timedelta_from_string(raw_time) # type: Optional[timedelta]
if not time_part:
print('Wrong input format')
continue
total_seconds += time_part.total_seconds()
print()
print(
'Total time:',
int(total_seconds // 3600),
'h',
ceil((total_seconds % 3600) / 60),
'm',
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment