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