Skip to content

Commit 135bf99

Browse files
authored
Merge pull request #3 from 3zero2/copilot/add-tzdata-to-dockerfile
Fix container timestamps: install tzdata and make datetime calls timezone-aware
2 parents 83e7ddf + 68a2eb5 commit 135bf99

2 files changed

Lines changed: 15 additions & 3 deletions

File tree

Dockerfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
FROM python:3.12-slim
22

3+
RUN apt-get update && apt-get install -y --no-install-recommends tzdata && rm -rf /var/lib/apt/lists/*
4+
35
WORKDIR /app
46

57
COPY requirements.txt .

cybernotify.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import time
66
import unicodedata
77
from datetime import datetime, timedelta
8+
from zoneinfo import ZoneInfo
89

910
import requests
1011

@@ -20,6 +21,14 @@
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+
2332
def 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

6171
def 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

7282
def 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

Comments
 (0)