Skip to content

scheduling

scheduling

Pure recurrence calculation for declared score schedules.

Classes

Functions:

parse_interval_seconds

parse_interval_seconds(value)

Parse a positive, whitespace-free interval declaration into seconds.

Supported unit suffixes are seconds (s), minutes (m), hours (h), and days (d). Decimal values are accepted.

Source code in src/marianne/core/scheduling.py
def parse_interval_seconds(value: str) -> float:
    """Parse a positive, whitespace-free interval declaration into seconds.

    Supported unit suffixes are seconds (``s``), minutes (``m``), hours
    (``h``), and days (``d``). Decimal values are accepted.
    """
    match = _INTERVAL_PATTERN.fullmatch(value)
    if match is None:
        raise ValueError("Interval must be a positive s/m/h/d duration")

    quantity = float(value[:-1])
    if quantity <= 0:
        raise ValueError("Interval must be a positive s/m/h/d duration")
    seconds = quantity * _INTERVAL_SECONDS[match.group("unit")]
    if not isfinite(quantity) or not isfinite(seconds):
        raise ValueError("Interval must be finite and datetime-representable")
    try:
        duration = timedelta(seconds=seconds)
    except OverflowError as exc:
        raise ValueError("Interval must be datetime-representable") from exc
    if duration <= timedelta(0):
        raise ValueError("Interval must be datetime-representable")
    return seconds

next_due_at

next_due_at(config, after, *, interval_anchor=None)

Return the first scheduled UTC instant strictly later than after.

Cron expressions calculate from an aware datetime in the configured IANA zone, preserving that zone's daylight-saving rules. Interval expressions advance from the prior scheduled start when supplied, avoiding schedule drift caused by delayed dispatch.

Source code in src/marianne/core/scheduling.py
def next_due_at(
    config: ScheduleConfig,
    after: datetime,
    *,
    interval_anchor: datetime | None = None,
) -> datetime:
    """Return the first scheduled UTC instant strictly later than ``after``.

    Cron expressions calculate from an aware datetime in the configured IANA
    zone, preserving that zone's daylight-saving rules. Interval expressions
    advance from the prior scheduled start when supplied, avoiding schedule
    drift caused by delayed dispatch.
    """
    _require_aware(after, name="after")

    if config.cron is not None:
        base = after
        if config.timezone is not None:
            base = after.astimezone(ZoneInfo(config.timezone))
        next_time = cast(datetime, croniter(config.cron, base).get_next(datetime))
        return next_time.astimezone(UTC)

    if config.interval is None:
        raise ValueError("Schedule requires an interval or cron declaration")

    anchor = interval_anchor or after
    _require_aware(anchor, name="interval_anchor")
    duration = timedelta(seconds=parse_interval_seconds(config.interval))
    if duration <= timedelta(0):
        raise ValueError("Interval must be datetime-representable")

    anchor_utc = anchor.astimezone(UTC)
    after_utc = after.astimezone(UTC)
    if anchor_utc > after_utc:
        next_time = anchor_utc + duration
    else:
        periods = (after_utc - anchor_utc) // duration + 1
        next_time = anchor_utc + duration * periods
    return next_time.astimezone(UTC)