Support running over date change

This commit is contained in:
Fabian 2021-12-01 00:02:16 +01:00
parent df8fba3c30
commit c234999ccc

View File

@ -4,7 +4,7 @@ import json
import os
from collections.abc import MutableMapping
from copy import deepcopy
from datetime import datetime, date, timedelta
from datetime import datetime, date, timedelta, time
from threading import Thread, Event
from typing import List, Tuple, Dict, Union
@ -200,14 +200,15 @@ class LogCommentsData:
# trigger save
self._data[month_str] = month
def add_log_entry(self, task: str):
now = datetime.now()
self._ensure_format(now)
tstr = now.strftime("%H:%M")
def add_log_entry(self, task: str, start_time=None):
if not start_time:
start_time = datetime.now()
self._ensure_format(start_time)
tstr = start_time.strftime("%H:%M")
b64str = base64.b64encode(task.encode("utf-8")).decode("utf-8")
encoded = f"{tstr} {b64str}"
month_str = now.strftime("%Y-%m")
day_str = now.strftime("%d")
month_str = start_time.strftime("%Y-%m")
day_str = start_time.strftime("%d")
month = self._data.setdefault(month_str, {})
month.setdefault(day_str, self._init_day)["log"].append(encoded)
self._data[month_str] = month
@ -269,6 +270,7 @@ class LogCommentsData:
class Log:
def __init__(self, data: LogCommentsData):
self._data = data
self._start_date = date.today()
def cleanup():
self.log("End")
@ -276,15 +278,22 @@ class Log:
atexit.register(cleanup)
def log(self, task):
if self._start_date != date.today():
yesterday = date.today() - timedelta(days=1)
yesterday_log = self.last_log(yesterday)
if yesterday_log:
self._data.add_log_entry("End", datetime.combine(yesterday, time(23, 59)))
self._data.add_log_entry(yesterday_log, datetime.combine(date.today(), time(0, 0)))
self._start_date = date.today()
self._data.add_log_entry(task)
def last_log(self):
log = self._data.get_log(date.today())
def last_log(self, day=date.today()):
log = self._data.get_log(day)
if not log:
return None
if log[-1][1] == "End":
del log[-1]
self._data.set_log(date.today(), log)
self._data.set_log(day, log)
if not log:
return None
return log[-1][1]