From 605ba4adea51af2580f1ab94fd6372e873c108e7 Mon Sep 17 00:00:00 2001 From: 0xNyk <0xNyk@users.noreply.github.com> Date: Wed, 11 Mar 2026 08:38:24 -0700 Subject: [PATCH] fix(cron): interpret naive timestamps as local time in due-job checks MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Legacy cron job rows may store next_run_at without timezone info. _ensure_aware() previously stamped the Hermes-configured tz directly via replace(tzinfo=...), which shifts absolute time when system-local tz differs from Hermes tz — causing overdue jobs to appear not due. Now: naive datetimes are interpreted as system-local wall time first, then converted to Hermes tz. Aware datetimes are normalized to Hermes tz for consistency. Cherry-picked from PR #807, rebased onto current main. Fixes #806 Co-authored-by: 0xNyk <0xNyk@users.noreply.github.com> --- cron/jobs.py | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/cron/jobs.py b/cron/jobs.py index 0c062cfea..6cbb168f0 100644 --- a/cron/jobs.py +++ b/cron/jobs.py @@ -168,16 +168,22 @@ def parse_schedule(schedule: str) -> Dict[str, Any]: def _ensure_aware(dt: datetime) -> datetime: - """Make a naive datetime tz-aware using the configured timezone. + """Return a timezone-aware datetime in Hermes configured timezone. - Handles backward compatibility: timestamps stored before timezone support - are naive (server-local). We assume they were in the same timezone as - the current configuration so comparisons work without crashing. + Backward compatibility: + - Older stored timestamps may be naive. + - Naive values are interpreted as *system-local wall time* (the timezone + `datetime.now()` used when they were created), then converted to the + configured Hermes timezone. + + This preserves relative ordering for legacy naive timestamps across + timezone changes and avoids false not-due results. """ + target_tz = _hermes_now().tzinfo if dt.tzinfo is None: - tz = _hermes_now().tzinfo - return dt.replace(tzinfo=tz) - return dt + local_tz = datetime.now().astimezone().tzinfo + return dt.replace(tzinfo=local_tz).astimezone(target_tz) + return dt.astimezone(target_tz) def compute_next_run(schedule: Dict[str, Any], last_run_at: Optional[str] = None) -> Optional[str]: