From 437857c7353a5947f2f146ad4d19e2b36b4551e6 Mon Sep 17 00:00:00 2001 From: root Date: Sat, 28 Dec 2024 15:57:56 +0100 Subject: [PATCH] tz-aware --- ics-now.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/ics-now.py b/ics-now.py index ee196cd..9964493 100644 --- a/ics-now.py +++ b/ics-now.py @@ -6,6 +6,7 @@ This script yields JSON info about the current event + the next one. It depends on the nice ics-query https://github.com/niccokunzmann/ics-query """ +import os import datetime import json import sys @@ -13,11 +14,17 @@ from argparse import ArgumentParser from pathlib import Path from subprocess import check_output +import pytz from icalendar import Calendar, Event +TZ = os.getenv('TZ') +if TZ is None: + TZ = check_output('timedatectl show -p Timezone --value'.split()).strip() def parse_dt(s: str) -> datetime.datetime: - return datetime.datetime.strptime(s, "%Y-%m-%dT%H:%M:%S") + dt = datetime.datetime.strptime(s, "%Y-%m-%dT%H:%M:%S") + dt = pytz.timezone(TZ).localize(dt) + return dt def parse_minutes(s: str) -> datetime.timedelta: @@ -28,10 +35,9 @@ class IcsNow: def __init__(self): self.parser = ArgumentParser() self.parser.add_argument("ics", type=Path) - self.parser.add_argument("--tz", default="Europe/Rome") self.parser.add_argument( "--now", - default=datetime.datetime.now(), + default=datetime.datetime.now(pytz.timezone(TZ)), type=parse_dt, help="Simulate different time. Mostly for debug", ) @@ -55,7 +61,7 @@ class IcsNow: self.args.ics_query, cmd, "--tz", - self.args.tz, + TZ, "--component", "VEVENT", *args,