use std::fmt;
use std::future::ready;
use std::str::FromStr;
use std::sync::Arc;
use std::sync::atomic::{AtomicI64, Ordering};
use std::time::{Duration, Instant};
use jiff::civil::{Date, DateTime};
use jiff::tz::{AmbiguousOffset, TimeZone};
use jiff::{SignedDuration, Timestamp, ToSpan};
use super::datetime::{UtcDateTime, ZonedDateTime};
use super::schedule::Schedule;
use crate::kernel::{Ctx, Effect};
const MONTH_NAMES: [&str; 12] = [
"JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC",
];
const DAY_NAMES: [&str; 7] = ["SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"];
const SEARCH_YEARS: i16 = 9;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct CronParseError {
pub expression: String,
pub reason: String,
}
impl fmt::Display for CronParseError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"invalid cron expression {:?}: {}",
self.expression, self.reason
)
}
}
impl std::error::Error for CronParseError {}
#[derive(Clone)]
pub struct Cron {
source: Arc<str>,
seconds: u64,
minutes: u64,
hours: u64,
days: u64,
months: u64,
weekdays: u64,
dom_any: bool,
dow_any: bool,
zone: TimeZone,
}
impl fmt::Debug for Cron {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Cron")
.field("expression", &&*self.source)
.field("zone", &self.zone.iana_name().unwrap_or("<fixed>"))
.finish()
}
}
impl fmt::Display for Cron {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.source)
}
}
impl FromStr for Cron {
type Err = CronParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Self::parse(s)
}
}
impl Cron {
pub fn parse(expression: &str) -> Result<Self, CronParseError> {
let err = |reason: String| CronParseError {
expression: expression.to_string(),
reason,
};
let text = expression.trim();
let lower = text.to_ascii_lowercase();
let expanded = match lower.as_str() {
"@yearly" | "@annually" => "0 0 1 1 *",
"@monthly" => "0 0 1 * *",
"@weekly" => "0 0 * * 0",
"@daily" | "@midnight" => "0 0 * * *",
"@hourly" => "0 * * * *",
s if s.starts_with('@') => return Err(err(format!("unknown macro {text:?}"))),
_ => text,
};
let fields: Vec<&str> = expanded.split_whitespace().collect();
let (second, rest) = match fields.len() {
5 => ("0", &fields[..]),
6 => (fields[0], &fields[1..]),
n => return Err(err(format!("expected 5 or 6 fields, found {n}"))),
};
let weekdays = field(rest[4], "day-of-week", 0, 7, &DAY_NAMES, 0, true).map_err(err)?;
Ok(Self {
source: Arc::from(text),
seconds: field(second, "second", 0, 59, &[], 0, false).map_err(err)?,
minutes: field(rest[0], "minute", 0, 59, &[], 0, false).map_err(err)?,
hours: field(rest[1], "hour", 0, 23, &[], 0, false).map_err(err)?,
days: field(rest[2], "day-of-month", 1, 31, &[], 0, true).map_err(err)?,
months: field(rest[3], "month", 1, 12, &MONTH_NAMES, 1, false).map_err(err)?,
weekdays: (weekdays & 0x7f) | ((weekdays >> 7) & 1),
dom_any: rest[2].starts_with(['*', '?']),
dow_any: rest[4].starts_with(['*', '?']),
zone: TimeZone::UTC,
})
}
pub fn in_zone(mut self, zone: TimeZone) -> Self {
self.zone = zone;
self
}
pub fn zone(&self) -> &TimeZone {
&self.zone
}
pub fn expression(&self) -> &str {
&self.source
}
pub fn matches(&self, at: &ZonedDateTime) -> bool {
let dt = at.0.timestamp().to_zoned(self.zone.clone()).datetime();
self.day_matches(dt.date()) && self.time_after(dt) == Some(dt)
}
pub fn next_after(&self, after: UtcDateTime) -> Option<ZonedDateTime> {
let after = after.0;
let local = after.to_zoned(self.zone.clone()).datetime();
let mut from = local
.date()
.at(local.hour(), local.minute(), local.second(), 0)
.checked_add(1.second())
.ok()?;
loop {
let dt = self.next_civil(from)?;
if let Some(t) = self.resolve(dt, after) {
return Some(ZonedDateTime(t.to_zoned(self.zone.clone())));
}
from = dt.checked_add(1.second()).ok()?;
}
}
pub fn upcoming(&self, after: UtcDateTime) -> impl Iterator<Item = ZonedDateTime> + '_ {
std::iter::successors(self.next_after(after), move |z| {
self.next_after(UtcDateTime(z.0.timestamp()))
})
}
fn resolve(&self, dt: DateTime, after: Timestamp) -> Option<Timestamp> {
let amb = self.zone.to_ambiguous_timestamp(dt);
match amb.offset() {
AmbiguousOffset::Fold { .. } => [amb.earlier().ok(), amb.later().ok()]
.into_iter()
.flatten()
.find(|t| *t > after),
_ => amb.compatible().ok().filter(|t| *t > after),
}
}
fn next_civil(&self, from: DateTime) -> Option<DateTime> {
let limit = from.year().saturating_add(SEARCH_YEARS);
let mut date = from.date();
let mut time = Some(from);
while date.year() <= limit {
if !has(self.months, date.month()) {
date = date.first_of_month().checked_add(1.month()).ok()?;
time = None;
continue;
}
if self.day_matches(date) {
let start = time.unwrap_or_else(|| date.at(0, 0, 0, 0));
if let Some(dt) = self.time_after(start) {
return Some(dt);
}
}
date = date.tomorrow().ok()?;
time = None;
}
None
}
fn day_matches(&self, date: Date) -> bool {
let dom = has(self.days, date.day());
let dow = has(self.weekdays, date.weekday().to_sunday_zero_offset());
if self.dom_any || self.dow_any {
dom && dow
} else {
dom || dow
}
}
fn time_after(&self, start: DateTime) -> Option<DateTime> {
let (h0, m0, s0) = (start.hour(), start.minute(), start.second());
for h in h0..24 {
if !has(self.hours, h) {
continue;
}
let first_m = if h == h0 { m0 } else { 0 };
for m in first_m..60 {
if !has(self.minutes, m) {
continue;
}
let first_s = if h == h0 && m == m0 { s0 } else { 0 };
if let Some(s) = first_set(self.seconds, first_s) {
return Some(start.date().at(h, m, s, 0));
}
}
}
None
}
}
fn has(mask: u64, v: i8) -> bool {
(0..64).contains(&v) && mask & (1 << v) != 0
}
fn first_set(mask: u64, from: i8) -> Option<i8> {
let from = u32::try_from(from).ok().filter(|f| *f < 64)?;
let rest = mask & (u64::MAX << from);
(rest != 0).then(|| rest.trailing_zeros() as i8)
}
fn field(
text: &str,
name: &str,
min: u32,
max: u32,
names: &[&str],
name_base: u32,
question: bool,
) -> Result<u64, String> {
let value = |s: &str| -> Result<u32, String> {
let v = match s.parse::<u32>() {
Ok(v) => v,
Err(_) => names
.iter()
.position(|n| n.eq_ignore_ascii_case(s))
.map(|i| i as u32 + name_base)
.ok_or_else(|| format!("{name} field: {s:?} is not a value"))?,
};
if (min..=max).contains(&v) {
Ok(v)
} else {
Err(format!("{name} field: {v} is outside {min}-{max}"))
}
};
let mut mask = 0u64;
for part in text.split(',') {
let (range, step) = match part.split_once('/') {
Some((r, s)) => match s.parse::<u32>() {
Ok(step) if step > 0 => (r, Some(step)),
_ => return Err(format!("{name} field: bad step {s:?}")),
},
None => (part, None),
};
let (lo, hi) = if range == "*" || (question && range == "?") {
(min, max)
} else if let Some((a, b)) = range.split_once('-') {
(value(a)?, value(b)?)
} else if range.is_empty() {
return Err(format!("{name} field: empty item in {text:?}"));
} else {
let v = value(range)?;
(v, if step.is_some() { max } else { v })
};
if lo > hi {
return Err(format!("{name} field: range {range:?} is backwards"));
}
let mut v = lo;
while v <= hi {
mask |= 1 << v;
v += step.unwrap_or(1);
}
}
Ok(mask)
}
#[derive(Clone, Default)]
pub struct WallClock {
anchor: Option<Arc<Anchor>>,
}
struct Anchor {
instant: Instant,
at: Timestamp,
jumped: AtomicI64,
}
impl fmt::Debug for WallClock {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match &self.anchor {
None => f.write_str("WallClock::System"),
Some(a) => f
.debug_struct("WallClock::Anchored")
.field("at", &a.at)
.field("jumped_ns", &a.jumped.load(Ordering::Relaxed))
.finish(),
}
}
}
impl WallClock {
pub fn system() -> Self {
Self { anchor: None }
}
pub fn anchored(instant: Instant, at: UtcDateTime) -> Self {
Self {
anchor: Some(Arc::new(Anchor {
instant,
at: at.0,
jumped: AtomicI64::new(0),
})),
}
}
pub fn starting_at<E, R>(at: UtcDateTime) -> Effect<Self, E, R>
where
E: Send + 'static,
R: Send + Sync + 'static,
{
Effect::from_ctx(move |ctx: Ctx<R>| ready(Ok(Self::anchored(ctx.fiber().executor().now(), at))))
}
pub fn at(&self, now: Instant) -> UtcDateTime {
let Some(a) = &self.anchor else {
return UtcDateTime(Timestamp::now());
};
let elapsed = now.saturating_duration_since(a.instant);
let jumped = SignedDuration::from_nanos(a.jumped.load(Ordering::SeqCst));
UtcDateTime(
a.at
.checked_add(elapsed)
.and_then(|t| t.checked_add(jumped))
.unwrap_or(Timestamp::MAX),
)
}
pub fn now<E, R>(&self) -> Effect<UtcDateTime, E, R>
where
E: Send + 'static,
R: Send + Sync + 'static,
{
let clock = self.clone();
Effect::from_ctx(move |ctx: Ctx<R>| ready(Ok(clock.at(ctx.fiber().executor().now()))))
}
pub fn jump(&self, by: Duration) {
let a = self
.anchor
.as_ref()
.expect("WallClock::jump: the system clock cannot be moved");
let ns = i64::try_from(by.as_nanos()).unwrap_or(i64::MAX);
a.jumped.fetch_add(ns, Ordering::SeqCst);
}
}
pub(crate) fn delay_until(now: UtcDateTime, next: &ZonedDateTime) -> Duration {
let d = next.0.timestamp().as_duration() - now.0.as_duration();
Duration::try_from(d).unwrap_or(Duration::ZERO)
}
impl<In: 'static> Schedule<Option<ZonedDateTime>, In> {
pub fn cron(cron: Cron, clock: WallClock) -> Self {
Self::from_fn(
|| (),
move |now, _, ()| {
let wall = clock.at(now);
match cron.next_after(wall) {
Some(next) => {
let d = delay_until(wall, &next);
((), Some(next), Some(d))
}
None => ((), None, None),
}
},
)
}
}
#[cfg(test)]
mod tests {
use super::*;
fn utc(s: &str) -> UtcDateTime {
UtcDateTime(s.parse().expect("timestamp"))
}
fn next(expr: &str, after: &str) -> Option<String> {
Cron::parse(expr)
.expect("parse")
.next_after(utc(after))
.map(|z| z.0.timestamp().to_string())
}
#[test]
fn fields_steps_lists_and_names() {
assert_eq!(
next("*/15 * * * *", "2026-01-01T00:07:00Z").as_deref(),
Some("2026-01-01T00:15:00Z")
);
assert_eq!(
next("0 9-17/4 * * MON-fri", "2026-01-01T10:00:00Z").as_deref(), Some("2026-01-01T13:00:00Z")
);
assert_eq!(
next("0 0 1 jan,JUL *", "2026-02-01T00:00:00Z").as_deref(),
Some("2026-07-01T00:00:00Z")
);
assert_eq!(
next("*/10 * * * * *", "2026-01-01T00:00:05.5Z").as_deref(),
Some("2026-01-01T00:00:10Z")
);
assert_eq!(
next("@weekly", "2026-01-01T00:00:00Z").as_deref(),
Some("2026-01-04T00:00:00Z")
);
assert_eq!(
next("0 0 * * 7", "2026-01-01T00:00:00Z").as_deref(),
Some("2026-01-04T00:00:00Z")
);
}
#[test]
fn restricted_day_fields_are_or_ed() {
assert_eq!(
next("0 0 15 * 1", "2026-01-01T00:00:00Z").as_deref(),
Some("2026-01-05T00:00:00Z")
);
assert_eq!(
next("0 0 15 * */1", "2026-01-01T00:00:00Z").as_deref(),
Some("2026-01-15T00:00:00Z")
);
assert_eq!(
next("0 0 15 * ?", "2026-01-01T00:00:00Z").as_deref(),
Some("2026-01-15T00:00:00Z")
);
}
#[test]
fn leap_day_and_never() {
assert_eq!(
next("0 0 29 2 *", "2026-01-01T00:00:00Z").as_deref(),
Some("2028-02-29T00:00:00Z")
);
assert_eq!(next("0 0 30 2 *", "2026-01-01T00:00:00Z"), None);
}
#[test]
fn parse_errors() {
for bad in [
"",
"* * * *",
"* * * * * * *",
"60 * * * *",
"* 24 * * *",
"* * 0 * *",
"* * * 13 *",
"* * * * 8",
"5-1 * * * *",
"*/0 * * * *",
"a * * * *",
"1,,2 * * * *",
"@reboot",
"? * * * *",
] {
let e = Cron::parse(bad).expect_err(bad);
assert_eq!(e.expression, bad);
assert!(e.to_string().contains("invalid cron expression"), "{e}");
}
}
#[test]
fn matches_display_and_debug() {
let c: Cron = "0 30 9 * * *".parse().expect("parse");
assert_eq!(c.to_string(), "0 30 9 * * *");
assert_eq!(c.expression(), "0 30 9 * * *");
assert!(format!("{c:?}").contains("UTC"));
let at = utc("2026-05-05T09:30:00Z").to_zoned(TimeZone::UTC);
assert!(c.matches(&at));
assert!(!c.matches(&utc("2026-05-05T09:30:01Z").to_zoned(TimeZone::UTC)));
let fixed = c.in_zone(crate::timezone::offset(60));
assert!(format!("{fixed:?}").contains("<fixed>"));
assert!(!fixed.matches(&at));
}
#[test]
fn upcoming_is_in_order() {
let c = Cron::parse("@hourly").expect("parse");
let got: Vec<String> = c
.upcoming(utc("2026-01-01T00:30:00Z"))
.take(3)
.map(|z| z.0.timestamp().to_string())
.collect();
assert_eq!(
got,
[
"2026-01-01T01:00:00Z",
"2026-01-01T02:00:00Z",
"2026-01-01T03:00:00Z"
]
);
}
#[test]
fn wall_clock_anchor_and_jump() {
let t0 = Instant::now();
let clock = WallClock::anchored(t0, utc("2026-01-01T00:00:00Z"));
assert_eq!(clock.at(t0), utc("2026-01-01T00:00:00Z"));
assert_eq!(
clock.at(t0 + Duration::from_secs(60)),
utc("2026-01-01T00:01:00Z")
);
clock.jump(Duration::from_secs(3600));
assert_eq!(clock.at(t0), utc("2026-01-01T01:00:00Z"));
assert!(format!("{clock:?}").contains("Anchored"));
assert!(format!("{:?}", WallClock::system()).contains("System"));
}
#[test]
#[should_panic(expected = "system clock cannot be moved")]
fn system_clock_cannot_jump() {
WallClock::system().jump(Duration::from_secs(1));
}
}