add accumulation of captured time
This commit is contained in:
6
main.py
6
main.py
@@ -1,6 +1,6 @@
|
|||||||
#!/usr/bin/python3
|
#!/usr/bin/python3
|
||||||
|
|
||||||
from util.str_to_datetime import datetime
|
from util.str_to_datetime import str_to_datetime as datetime
|
||||||
|
|
||||||
from model.TimeSlotContainer import time_slot_container as tsc
|
from model.TimeSlotContainer import time_slot_container as tsc
|
||||||
from model.TimeSlot import TimeSlot
|
from model.TimeSlot import TimeSlot
|
||||||
@@ -35,3 +35,7 @@ if __name__ == "__main__":
|
|||||||
|
|
||||||
elif args.command == "add":
|
elif args.command == "add":
|
||||||
tsc.add_time_slot(args.name, datetime(args.start), datetime(args.end))
|
tsc.add_time_slot(args.name, datetime(args.start), datetime(args.end))
|
||||||
|
|
||||||
|
elif args.command == "acc":
|
||||||
|
d = tsc.accumulate_duration(args.query)
|
||||||
|
print(f"{d.seconds // 3600}:{d.seconds % 3600 // 60}")
|
||||||
|
|||||||
@@ -42,3 +42,8 @@ class TimeSlot():
|
|||||||
|
|
||||||
def end_now(self):
|
def end_now(self):
|
||||||
self.end = datetime.now()
|
self.end = datetime.now()
|
||||||
|
|
||||||
|
def duration(self) -> datetime.timedelta:
|
||||||
|
end = self.end
|
||||||
|
if not end: end = datetime.now()
|
||||||
|
return self.end - self.start
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
from datetime import date, datetime
|
import re
|
||||||
|
|
||||||
|
from datetime import date, datetime, timedelta
|
||||||
from model.DataStore import DataStore
|
from model.DataStore import DataStore
|
||||||
from model.TimeSlot import TimeSlot
|
from model.TimeSlot import TimeSlot
|
||||||
|
|
||||||
@@ -43,5 +45,12 @@ class TimeSlotContainer:
|
|||||||
def delete_time_slot(self, ts: TimeSlot):
|
def delete_time_slot(self, ts: TimeSlot):
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
|
def accumulate_duration(self, query: str) -> timedelta:
|
||||||
|
pattern = re.compile(query)
|
||||||
|
return sum([ts.duration() for ts in self._ds.get_all_time_slots()
|
||||||
|
if pattern.search(ts.name) ], start=timedelta(0))
|
||||||
|
|
||||||
time_slot_container = TimeSlotContainer(DataStore("data.json"))
|
try:
|
||||||
|
time_slot_container = TimeSlotContainer(DataStore("data.json"))
|
||||||
|
except FileNotFoundError:
|
||||||
|
time_slot_container = TimeSlotContainer(DataStore("data.json", create=True))
|
||||||
|
|||||||
@@ -27,4 +27,8 @@ def create_parser():
|
|||||||
parser_end = subparsers.add_parser("end", help="Ending the current event.")
|
parser_end = subparsers.add_parser("end", help="Ending the current event.")
|
||||||
parser_end.add_argument("-e", "--end", help="End datetime")
|
parser_end.add_argument("-e", "--end", help="End datetime")
|
||||||
|
|
||||||
|
# Create the parser for the "acc" command
|
||||||
|
parser_end = subparsers.add_parser("acc", help="Accumulate the duration of events.")
|
||||||
|
parser_end.add_argument("query", help="The regex matching for the name of the event.")
|
||||||
|
|
||||||
return parser
|
return parser
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
from datetime import datetime as dt
|
from datetime import datetime as dt
|
||||||
|
|
||||||
|
|
||||||
def datetime(s: str) -> dt:
|
def str_to_datetime(s: str) -> dt:
|
||||||
s = s.strip()
|
s = s.strip()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|||||||
Reference in New Issue
Block a user