Last active
January 7, 2024 00:53
-
-
Save Nircek/053e6391e81e879c059b24d3a9c9fafe to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
from typing import TypedDict | |
from datetime import datetime, timedelta | |
from json import load | |
class Session(TypedDict): | |
"""One session info.""" | |
duration: int | |
"""Duration of session in seconds""" | |
start: str | |
"""ISO8601 start date""" | |
end: str | |
"""ISO8601 end date""" | |
class Stats(TypedDict): | |
"""[Blade.timetracker](https://marketplace.visualstudio.com/items?itemName=Blade.timetracker)\ | |
-compatible stats file.""" | |
total: int | |
"""Duration of all sessions in seconds""" | |
sessions: list[Session] | |
def load_stats(path: str = ".timetracker") -> Stats: | |
"""Load Stats from compatible file.""" | |
with open(path, encoding="ascii") as f: | |
stats: Stats = load(f) | |
return stats | |
def calculate_today_stats(stats: Stats) -> int: | |
"""Calculate today spent time in seconds from stats.""" | |
spent_seconds = 0 | |
now = datetime.now().astimezone() | |
today444 = now.replace(hour=4, minute=44, second=0, microsecond=0) | |
if today444 > now: | |
today444 -= timedelta(days=1) | |
for session in stats["sessions"]: | |
end_date = datetime.fromisoformat(session["end"]) | |
if end_date < today444: | |
continue | |
spent_seconds += int(session["duration"]) | |
return spent_seconds | |
if __name__ == "__main__": | |
print(timedelta(seconds=calculate_today_stats(load_stats()))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment