Add timezone support

This commit is contained in:
blankhex committed 2026-10-24 20:36:47 +03:00
1 parent fffc1b0c81
commit a07943e645
8 files changed
+90 -6

No files matched your search

+30 -1
View File
@@ -64,7 +64,10 @@ class FakeBot:
def test_polling():
from meetings_telegram_bot.config import _resolve_timezone
tmp = tempfile.mkdtemp()
tz = _resolve_timezone(os.getenv("TIMEZONE", ""))
cfg = Config(
api_base_url="http://x",
api_login="u",
@@ -72,7 +75,9 @@ def test_polling():
bot_token="t",
chat_id="@g",
state_file=os.path.join(tmp, "state.json"),
timezone=tz,
)
print("polling timezone:", tz or "local")
api = FakeApi([mk(1), mk(2), mk(3)])
tb = FakeBot()
@@ -81,11 +86,15 @@ def test_polling():
assert b.poll_once() == 0 and len(tb.sent) == 0 # first run seeds silently
print("run1 (seed) announced 0 sent 0")
api._m.append(mk(4, title="Design review"))
# Fixture meetings are scheduled 1 hour in the future (see mk()).
new_meeting = mk(4, title="Design review")
api._m.append(new_meeting)
b2 = MeetingsBot(cfg, api=api, bot=tb, store=StateStore(cfg.state_file))
b2.start()
assert b2.poll_once() == 1 and len(tb.sent) == 1 # one new meeting posted
print("run2 (new) announced 1 sent 1")
print("scheduled (UTC): %s -> shown as: %s"
% (new_meeting.scheduled_at.strftime("%Y-%m-%d %H:%M"), tz or "local"))
print("--- message ---")
print(tb.sent[0][1])
print("--- end ---")
@@ -130,6 +139,26 @@ def test_proxy():
print("proxy wiring OK")
def test_timezone():
from datetime import timezone as dt_timezone
from meetings_telegram_bot.config import _resolve_timezone
from meetings_telegram_bot.messages import format_new_meeting
gmt3 = _resolve_timezone("GMT+3")
assert gmt3.utcoffset(None) == timedelta(hours=3)
assert _resolve_timezone("") is None
assert _resolve_timezone("UTC").utcoffset(None) == timedelta(0)
assert _resolve_timezone("UTC+05:30").utcoffset(None) == timedelta(hours=5, minutes=30)
# A fixed instant to check the rendered hour shifts by +3.
sched = datetime(2026, 1, 1, 12, 0, tzinfo=dt_timezone.utc)
text = format_new_meeting(mk(1, sched=sched), tz=gmt3)
assert "15:00" in text, text # 12:00 UTC rendered as 15:00 at GMT+3
assert "GMT+3" in text, text
print("timezone OK")
if __name__ == "__main__":
test_polling()
test_proxy()
test_timezone()