From d3f6c95cebaf17d04f0d04655a98c0e795777bb1 Mon Sep 17 00:00:00 2001 From: Ahwei Date: Sat, 14 Feb 2026 10:27:09 +0800 Subject: [PATCH] refactor(cron): simplify timezone logic and merge conditional branches With tz: Use the specified timezone (e.g., "Asia/Shanghai"). Without tz: Use the local timezone (datetime.now().astimezone().tzinfo) instead of defaulting to UTC --- nanobot/cron/service.py | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/nanobot/cron/service.py b/nanobot/cron/service.py index 6fea4de..4da845a 100644 --- a/nanobot/cron/service.py +++ b/nanobot/cron/service.py @@ -33,16 +33,11 @@ def _compute_next_run(schedule: CronSchedule, now_ms: int) -> int | None: from croniter import croniter from zoneinfo import ZoneInfo base_time = time.time() - if schedule.tz: - tz = ZoneInfo(schedule.tz) - base_dt = datetime.fromtimestamp(base_time, tz=tz) - cron = croniter(schedule.expr, base_dt) - next_dt = cron.get_next(datetime) - return int(next_dt.timestamp() * 1000) - else: - cron = croniter(schedule.expr, base_time) - next_time = cron.get_next() - return int(next_time * 1000) + tz = ZoneInfo(schedule.tz) if schedule.tz else datetime.now().astimezone().tzinfo + base_dt = datetime.fromtimestamp(base_time, tz=tz) + cron = croniter(schedule.expr, base_dt) + next_dt = cron.get_next(datetime) + return int(next_dt.timestamp() * 1000) except Exception: return None