ReMe/reme/utils/cron_utils.py
jinliyl b476690c60
fix(config): default to the system timezone (#778)
* Default the application timezone to the system timezone

* Use an explicit timezone in the retention boundary test

* fix(timezone): handle TZ overrides and DST scheduling

* refactor(docker)!: use TZ for the default timezone

BREAKING CHANGE: Use TZ in place of REME_TIMEZONE for Docker deployments. An explicit application timezone takes precedence.

* fix(timezone)!: use IANA names and current cron scheduling

Require croniter 7.0, remove legacy timezone formats and scheduler branches, and preserve triggers across partial-hour DST rollback.

* fix(timezone): preserve final rollback triggers and validate local TZ

* fix(timezone): preserve native rules for local scheduling

* fix(timezone): separate named defaults from active host clock rules

* fix(timezone): share clock rules across scheduling and local dates
2026-11-25 17:59:34 +08:00

31 lines
1.3 KiB
Python

"""Next-fire calculations shared by the Cron and proactive runtimes."""
from datetime import datetime, timedelta
def next_cron_time(expression: str, now: datetime) -> datetime:
"""Find the earliest future trigger, including times repeated by a clock rollback."""
from croniter import CroniterBadDateError, croniter
try:
next_fire = croniter(expression, now).get_next(datetime)
except CroniterBadDateError:
next_fire = None
# An exhausted wall-clock schedule may still have an occurrence in the second fold.
next_offset = (next_fire or now.replace(fold=1)).utcoffset()
offset_change = next_offset - now.utcoffset()
if offset_change < timedelta():
# A rollback can repeat an earlier wall time before croniter's first result.
repeated = croniter(expression, now.replace(tzinfo=None) + offset_change)
while True:
try:
candidate = repeated.get_next(datetime).replace(tzinfo=now.tzinfo, fold=1)
except CroniterBadDateError:
break
if next_fire is not None and candidate.timestamp() >= next_fire.timestamp():
break
if candidate.timestamp() > now.timestamp():
return candidate
if next_fire is None:
raise CroniterBadDateError("failed to find next date")
return next_fire