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
This commit is contained in:
Ahwei 2026-02-14 10:27:09 +08:00
parent 153c83e340
commit d3f6c95ceb

View File

@ -33,16 +33,11 @@ def _compute_next_run(schedule: CronSchedule, now_ms: int) -> int | None:
from croniter import croniter from croniter import croniter
from zoneinfo import ZoneInfo from zoneinfo import ZoneInfo
base_time = time.time() base_time = time.time()
if schedule.tz: tz = ZoneInfo(schedule.tz) if schedule.tz else datetime.now().astimezone().tzinfo
tz = ZoneInfo(schedule.tz) base_dt = datetime.fromtimestamp(base_time, tz=tz)
base_dt = datetime.fromtimestamp(base_time, tz=tz) cron = croniter(schedule.expr, base_dt)
cron = croniter(schedule.expr, base_dt) next_dt = cron.get_next(datetime)
next_dt = cron.get_next(datetime) return int(next_dt.timestamp() * 1000)
return int(next_dt.timestamp() * 1000)
else:
cron = croniter(schedule.expr, base_time)
next_time = cron.get_next()
return int(next_time * 1000)
except Exception: except Exception:
return None return None