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
from collections import defaultdict
from collections.abc import Iterable
from datetime import datetime, timedelta, timezone, time
from datetime import datetime, timedelta
from zoneinfo import ZoneInfo
import json
@ -48,40 +50,52 @@ def print_projects(projects) -> None:
)
for line in sys.stdin:
if line.strip() == "":
break
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]
def main():
for line in sys.stdin:
if line.strip() == "":
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"]
input = json.load(sys.stdin)
projects = defaultdict(dict)
for record in input:
record["start"] = (
datetime.strptime(record["start"], DATE_FORMAT)
.replace(tzinfo=ZoneInfo("UTC"))
.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)
print()
print_details(input)
if __name__ == "__main__":
main()