Guide API
Skip to main content

calc_next_expires_at

Function calc_next_expires_at 

Source
pub fn calc_next_expires_at(expires_at: u64, now: u64, period: u64) -> u64
Expand description

Catch-up-aware reschedule for periodic timers.

// Pre: expires_at <= now && period > 0
return (1 + (now - expires_at + period) / period) * period
           + expires_at - period;

The arithmetic advances expires_at to the next grid point strictly after now, skipping any missed periods. It is not a simple expires_at + period — it aligns to the period grid even when many periods have elapsed.

§Overflow analysis

With u64 ms the product (1 + …) * period is at least 2 * period. The subsequent + expires_at - period subtracts one period back, so the final result equals product + expires_at - period. Because product >= period, product + expires_at - period >= expires_at, and wrapping cannot occur for realistic ms timestamps. The - period must execute last (not expires_at - period + product) to avoid underflow when expires_at < period.

§Panics

Panics in debug mode (and is undefined in release) if period == 0. Callers must guarantee period > 0 and expires_at <= now.