Fix timezone bug

This commit is contained in:
Maximilian Friedersdorff 2025-08-27 13:50:25 +01:00
parent 0c6d1064d5
commit 093da880ca

View file

@ -1,7 +1,9 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
from collections import defaultdict from collections import defaultdict
from collections.abc import Iterable from collections.abc import Iterable
from datetime import datetime, timedelta, timezone, time from datetime import datetime, timedelta
from zoneinfo import ZoneInfo
import json import json
@ -48,40 +50,52 @@ def print_projects(projects) -> None:
) )
for line in sys.stdin: def main():
if line.strip() == "": for line in sys.stdin:
break if line.strip() == "":
input = json.load(sys.stdin)
projects = defaultdict(dict)
for record in input:
record["start"] = datetime.strptime(record["start"], DATE_FORMAT)
if "end" in record:
record["end"] = datetime.strptime(record["end"], DATE_FORMAT)
else:
record["end"] = datetime.now()
record["duration"] = record["end"] - record["start"]
record["project"] = ""
for i, tag in enumerate(record["tags"]):
if len(tag) > 0 and tag[0].isupper():
record["project"] = tag
del record["tags"][i]
break break
if record["project"] not in projects: input = json.load(sys.stdin)
projects[record["project"]] = { projects = defaultdict(dict)
"duration": record["duration"],
"tags": record["tags"], for record in input:
} record["start"] = (
else: datetime.strptime(record["start"], DATE_FORMAT)
projects[record["project"]]["duration"] += record["duration"] .replace(tzinfo=ZoneInfo("UTC"))
projects[record["project"]]["tags"] += record["tags"] .astimezone()
)
if "end" in record:
record["end"] = (
datetime.strptime(record["end"], DATE_FORMAT)
.replace(tzinfo=ZoneInfo("UTC"))
.astimezone()
)
else:
record["end"] = datetime.now(tz=ZoneInfo("UTC")).astimezone()
record["duration"] = record["end"] - record["start"]
record["project"] = ""
for i, tag in enumerate(record["tags"]):
if len(tag) > 0 and tag[0].isupper():
record["project"] = tag
del record["tags"][i]
break
if record["project"] not in projects:
projects[record["project"]] = {
"duration": record["duration"],
"tags": record["tags"],
}
else:
projects[record["project"]]["duration"] += record["duration"]
projects[record["project"]]["tags"] += record["tags"]
print_projects(projects)
print()
print_details(input)
print_projects(projects) if __name__ == "__main__":
print() main()
print_details(input)