Skip to content

Your lightweight open-source personal agent.

fix(cron): add timezone support for accurate next run time calculation

Ahwei153c83e340c5
diff --git a/nanobot/cron/service.py b/nanobot/cron/service.py
index d1965a9ec..6fea4de70 100644
--- a/nanobot/cron/service.py
+++ b/nanobot/cron/service.py
@@ -4,6 +4,7 @@ import asyncio
import json
import time
import uuid
+from datetime import datetime
from pathlib import Path
from typing import Any, Callable, Coroutine
@@ -30,9 +31,18 @@ def _compute_next_run(schedule: CronSchedule, now_ms: int) -> int | None:
if schedule.kind == "cron" and schedule.expr:
try:
from croniter import croniter
- cron = croniter(schedule.expr, time.time())
- next_time = cron.get_next()
- return int(next_time * 1000)
+ 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)
except Exception:
return None