55import time
66import unicodedata
77from datetime import datetime , timedelta
8+ from zoneinfo import ZoneInfo
89
910import requests
1011
2021
2122# ── config ───────────────────────────────────────────────────────────────────
2223
24+ def _load_timezone (tz_name : str ) -> ZoneInfo :
25+ try :
26+ return ZoneInfo (tz_name )
27+ except Exception :
28+ log .warning ("Invalid TZ value %r — falling back to 'Europe/Malta'." , tz_name )
29+ return ZoneInfo ("Europe/Malta" )
30+
31+
2332def load_config () -> dict :
2433 required = ["CYBERPASS_USERNAME" , "CYBERPASS_PASSWORD" , "TELEGRAM_BOT_TOKEN" , "TELEGRAM_CHAT_ID" ]
2534 cfg = {
@@ -33,6 +42,7 @@ def load_config() -> dict:
3342 "window_start" : os .environ .get ("NOTIFY_WINDOW_START" , "13:30" ),
3443 "window_end" : os .environ .get ("NOTIFY_WINDOW_END" , "14:30" ),
3544 "notify_days" : [int (d ) for d in os .environ .get ("NOTIFY_DAYS" , "0,1,2,3,4" ).split ("," )],
45+ "timezone" : _load_timezone (os .environ .get ("TZ" , "Europe/Malta" )),
3646 }
3747 missing = [k for k in required if not os .environ .get (k )]
3848 if missing :
@@ -59,7 +69,7 @@ def parse_time(t: str) -> tuple[int, int]:
5969
6070
6171def in_notify_window (cfg : dict ) -> bool :
62- now = datetime .now ()
72+ now = datetime .now (tz = cfg [ "timezone" ] )
6373 if now .weekday () not in cfg ["notify_days" ]:
6474 return False
6575 start_h , start_m = parse_time (cfg ["window_start" ])
@@ -71,7 +81,7 @@ def in_notify_window(cfg: dict) -> bool:
7181
7282def seconds_until_next_window (cfg : dict ) -> float :
7383 """Return seconds until the next notify window opens."""
74- now = datetime .now ()
84+ now = datetime .now (tz = cfg [ "timezone" ] )
7585 start_h , start_m = parse_time (cfg ["window_start" ])
7686
7787 for day_offset in range (8 ): # check up to a week ahead
@@ -152,7 +162,7 @@ def main() -> None:
152162 while True :
153163 try :
154164 # Reset notification flag on new day
155- today = datetime .now ().strftime ("%Y-%m-%d" )
165+ today = datetime .now (tz = cfg [ "timezone" ] ).strftime ("%Y-%m-%d" )
156166 if notified_date and notified_date != today :
157167 notified_date = None
158168
0 commit comments