diff --git a/52/jread/logtimes.py b/52/jread/logtimes.py new file mode 100644 index 00000000..a36d5e5f --- /dev/null +++ b/52/jread/logtimes.py @@ -0,0 +1,30 @@ +import os +import sys +import re + +from datetime import datetime + +DATETIME_REGEX = re.compile(r'.*(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}).*') +SHUTDOWN_EVENT = 'Shutdown initiated' + +with open(os.path.join(sys.path[0], "messages.log"), "r") as f: + loglines = f.readlines() + + +def convert_to_datetime(line): + match = re.match(DATETIME_REGEX, line) + return datetime(int(match.group(1)), int(match.group(2)), int(match.group(3)), int(match.group(4)), + int(match.group(5)), int(match.group(6))) if match else None + + +def time_between_shutdowns(loglines): + start = None + end = None + for line in loglines: + if SHUTDOWN_EVENT in line: + if dt := convert_to_datetime(line): + if start: + end = dt + else: + start = dt + return end - start if end and start else None diff --git a/52/jread/pomodoro_timer.py b/52/jread/pomodoro_timer.py new file mode 100644 index 00000000..8748998b --- /dev/null +++ b/52/jread/pomodoro_timer.py @@ -0,0 +1,45 @@ +import time + +from datetime import datetime +from datetime import timedelta + +# Note - this Pomodoro timer uses seconds instead of minutes for practical reasons - a real Pomodoro timer would use +# minutes instead + +# Start time is now +start = datetime.now() + +# Run timer for this duration +duration = timedelta(seconds=60) + +# Stop the timer once duration is reached +stop = start + duration + +# Take breaks at this frequency +break_frequency = timedelta(seconds=25) + +# Breaks are this duration +break_duration = timedelta(seconds=5) + +# Work status - working or break +work_status = 'working' + +# When to take the first break (transition from work to break) +next_transition = start + break_frequency + +# Seconds counter +secs = 0 + +print(f'Starting pomodoro timer [start_time={start}] [stop_time={stop}] [break_frequency={break_frequency}] ' + f'[first_break={next_transition}') + +# Run the timer from now until the stop time designated above +while datetime.now() < stop: + # If at a transition time (ignore milliseconds), then change the status from working => break or break => working + # and set the next transition time (+5 if transitioning to break, +25 if transitioning to work) + if str(datetime.now()).split('.')[0] == str(next_transition).split('.')[0]: + next_transition = datetime.now() + (break_duration if work_status == 'working' else break_frequency) + work_status = 'break' if work_status == 'working' else 'working' + secs += 1 + print(f'[{secs:03}] Work status={work_status}') + time.sleep(1) diff --git a/52/jread/test_logtimes.py b/52/jread/test_logtimes.py new file mode 100644 index 00000000..a94b4f35 --- /dev/null +++ b/52/jread/test_logtimes.py @@ -0,0 +1,18 @@ +from datetime import datetime, timedelta + +from logtimes import loglines, convert_to_datetime, time_between_shutdowns + + +def test_convert_to_datetime(): + line1 = 'ERROR 2014-07-03T23:24:31 supybot Invalid user dictionary file' + line2 = 'INFO 2015-10-03T10:12:51 supybot Shutdown initiated.' + line3 = 'INFO 2016-09-03T02:11:22 supybot Shutdown complete.' + assert convert_to_datetime(line1) == datetime(2014, 7, 3, 23, 24, 31) + assert convert_to_datetime(line2) == datetime(2015, 10, 3, 10, 12, 51) + assert convert_to_datetime(line3) == datetime(2016, 9, 3, 2, 11, 22) + + +def test_time_between_events(): + diff = time_between_shutdowns(loglines) + assert type(diff) == timedelta + assert str(diff) == '0:03:31'