jan-cli 0.24.0

YAML-defined CLI trees with progressive help, optional exec aliases, merged extra specs, and SQLite audit logging keyed by git branch
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
//! Cron expression matching for `jan cron`.
//!
//! Supports five-, six-, and seven-field expressions:
//!
//! - **5 fields** — minute hour dom month dow (fires at second 0 within the minute)
//! - **6 fields** — second minute hour dom month dow (fires at decisecond 0 within the second)
//! - **7 fields** — decisecond second minute hour dom month dow (100 ms ticks; decisecond 0–9)
//!
//! Field tokens: `*`, `N`, `A,B`, `A-B`, `*/S`, `A-B/S`. Day-of-week uses the crontab
//! convention: 0 or 7 = Sunday … 6 = Saturday.

use anyhow::{bail, Result};
use chrono::{DateTime, Datelike, Duration, Local, NaiveDateTime, TimeZone, Timelike, Weekday};

/// Daemon tick interval (100 ms).
pub const TICK_MS: u64 = 100;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct CivilTime {
    pub minute: u32,
    pub hour: u32,
    pub day: u32,
    pub month: u32,
    /// Crontab day-of-week: 0/7 = Sunday … 6 = Saturday.
    pub dow: u32,
}

/// Local time aligned to a 100 ms daemon tick.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct TickTime {
    /// Tenths of a second within the current second (0–9).
    pub decisecond: u32,
    pub second: u32,
    pub minute: u32,
    pub hour: u32,
    pub day: u32,
    pub month: u32,
    pub dow: u32,
}

impl CivilTime {
    pub fn now_local() -> Self {
        Self::from_chrono(Local::now())
    }

    pub fn from_chrono<T: Datelike + Timelike>(dt: T) -> Self {
        Self {
            minute: dt.minute(),
            hour: dt.hour(),
            day: dt.day(),
            month: dt.month(),
            dow: weekday_to_cron(dt.weekday()),
        }
    }
}

impl TickTime {
    pub fn now_local() -> Self {
        Self::from_chrono(Local::now())
    }

    pub fn from_chrono<T: Datelike + Timelike>(dt: T) -> Self {
        let nanos = dt.nanosecond();
        Self {
            decisecond: nanos / 100_000_000,
            second: dt.second(),
            minute: dt.minute(),
            hour: dt.hour(),
            day: dt.day(),
            month: dt.month(),
            dow: weekday_to_cron(dt.weekday()),
        }
    }

}

fn weekday_to_cron(wd: Weekday) -> u32 {
    match wd {
        Weekday::Sun => 0,
        Weekday::Mon => 1,
        Weekday::Tue => 2,
        Weekday::Wed => 3,
        Weekday::Thu => 4,
        Weekday::Fri => 5,
        Weekday::Sat => 6,
    }
}

/// Parse `YYYY-MM-DD HH:MM` (24h, local civil time) into [`CivilTime`].
pub fn parse_at(s: &str) -> Result<CivilTime> {
    let s = s.trim();
    let naive = NaiveDateTime::parse_from_str(s, "%Y-%m-%d %H:%M")
        .or_else(|_| NaiveDateTime::parse_from_str(s, "%Y-%m-%dT%H:%M"))
        .map_err(|e| anyhow::anyhow!("invalid --at `{s}` (expected YYYY-MM-DD HH:MM): {e}"))?;
    Ok(CivilTime::from_chrono(naive))
}

/// Parse `--at` for tick-level matching (`YYYY-MM-DD HH:MM:SS` or `YYYY-MM-DD HH:MM:SS.d`).
pub fn parse_at_tick(s: &str) -> Result<TickTime> {
    let s = s.trim();
    if let Ok(naive) = NaiveDateTime::parse_from_str(s, "%Y-%m-%d %H:%M:%S")
        .or_else(|_| NaiveDateTime::parse_from_str(s, "%Y-%m-%dT%H:%M:%S"))
    {
        return Ok(TickTime::from_chrono(naive));
    }
    if let Some((base, ds)) = s.rsplit_once('.') {
        let naive = NaiveDateTime::parse_from_str(base.trim(), "%Y-%m-%d %H:%M:%S")
            .or_else(|_| NaiveDateTime::parse_from_str(base.trim(), "%Y-%m-%dT%H:%M:%S"))
            .map_err(|e| {
                anyhow::anyhow!("invalid --at `{s}` (expected YYYY-MM-DD HH:MM:SS[.d]): {e}")
            })?;
        let ds = ds.trim();
        if ds.len() != 1 || !ds.chars().all(|c| c.is_ascii_digit()) {
            bail!("invalid decisecond in --at `{s}` (expected single digit 0-9)");
        }
        let decisecond: u32 = ds.parse().unwrap();
        if decisecond > 9 {
            bail!("decisecond in --at `{s}` must be 0-9");
        }
        let mut t = TickTime::from_chrono(naive);
        t.decisecond = decisecond;
        return Ok(t);
    }
    bail!("invalid --at `{s}` (expected YYYY-MM-DD HH:MM[:SS[.d]])");
}

#[derive(Debug, Clone)]
struct Field {
    values: Vec<u32>,
}

impl Field {
    fn matches(&self, value: u32) -> bool {
        self.values.contains(&value)
    }
}

fn parse_field(raw: &str, min: u32, max: u32) -> Result<Field> {
    let raw = raw.trim();
    if raw.is_empty() {
        bail!("empty cron field");
    }
    let mut values = Vec::new();
    for part in raw.split(',') {
        let part = part.trim();
        if part.is_empty() {
            bail!("empty cron field list entry");
        }
        let (range_part, step) = match part.split_once('/') {
            Some((r, s)) => {
                let step: u32 = s
                    .parse()
                    .map_err(|_| anyhow::anyhow!("invalid cron step `{s}`"))?;
                if step == 0 {
                    bail!("cron step must be > 0");
                }
                (r, step)
            }
            None => (part, 1u32),
        };
        let (start, end) = if range_part == "*" {
            (min, max)
        } else if let Some((a, b)) = range_part.split_once('-') {
            let start: u32 = a
                .parse()
                .map_err(|_| anyhow::anyhow!("invalid cron range start `{a}`"))?;
            let end: u32 = b
                .parse()
                .map_err(|_| anyhow::anyhow!("invalid cron range end `{b}`"))?;
            (start, end)
        } else {
            let n: u32 = range_part
                .parse()
                .map_err(|_| anyhow::anyhow!("invalid cron value `{range_part}`"))?;
            (n, n)
        };
        if start > end || start < min || end > max {
            bail!("cron field `{part}` out of range {min}-{max}");
        }
        let mut v = start;
        while v <= end {
            values.push(v);
            v = v.saturating_add(step);
            if step == 0 {
                break;
            }
        }
    }
    values.sort_unstable();
    values.dedup();
    Ok(Field { values })
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CronGranularity {
    Minute,
    Second,
    Decisecond,
}

#[derive(Debug, Clone)]
pub struct CronExpr {
    granularity: CronGranularity,
    decisecond: Option<Field>,
    second: Option<Field>,
    minute: Field,
    hour: Field,
    day: Field,
    month: Field,
    dow: Field,
}

/// How far ahead to search for the next matching tick (~1 year of 100 ms ticks).
const NEXT_SEARCH_TICKS: i64 = 366 * 24 * 60 * 60 * 10;
const NEXT_SEARCH_SECONDS: i64 = 366 * 24 * 60 * 60;
const NEXT_SEARCH_MINUTES: i64 = 366 * 24 * 60;

impl CronExpr {
    fn step_and_limit(&self) -> i64 {
        match self.granularity {
            CronGranularity::Minute => NEXT_SEARCH_MINUTES,
            CronGranularity::Second => NEXT_SEARCH_SECONDS,
            CronGranularity::Decisecond => NEXT_SEARCH_TICKS,
        }
    }

    fn align_for_search(&self, from: DateTime<Local>) -> DateTime<Local> {
        match self.granularity {
            CronGranularity::Minute => from
                .with_second(0)
                .and_then(|d| d.with_nanosecond(0))
                .unwrap_or(from),
            CronGranularity::Second => from.with_nanosecond(0).unwrap_or(from),
            CronGranularity::Decisecond => align_tick_datetime(from),
        }
    }

    pub fn parse(expr: &str) -> Result<Self> {
        let expr = expr.trim();
        if expr.is_empty() {
            bail!("empty cron expression");
        }
        let expanded = match expr {
            "@yearly" | "@annually" => "0 0 1 1 *",
            "@monthly" => "0 0 1 * *",
            "@weekly" => "0 0 * * 0",
            "@daily" | "@midnight" => "0 0 * * *",
            "@hourly" => "0 * * * *",
            "@every_second" => "* * * * * *",
            "@every_100ms" => "* * * * * * *",
            other => other,
        };
        let parts: Vec<&str> = expanded.split_whitespace().collect();
        match parts.len() {
            5 => {
                let minute = parse_field(parts[0], 0, 59)?;
                let hour = parse_field(parts[1], 0, 23)?;
                let day = parse_field(parts[2], 1, 31)?;
                let month = parse_field(parts[3], 1, 12)?;
                let mut dow = parse_field(parts[4], 0, 7)?;
                normalize_dow(&mut dow);
                Ok(Self {
                    granularity: CronGranularity::Minute,
                    decisecond: None,
                    second: None,
                    minute,
                    hour,
                    day,
                    month,
                    dow,
                })
            }
            6 => {
                let second = parse_field(parts[0], 0, 59)?;
                let minute = parse_field(parts[1], 0, 59)?;
                let hour = parse_field(parts[2], 0, 23)?;
                let day = parse_field(parts[3], 1, 31)?;
                let month = parse_field(parts[4], 1, 12)?;
                let mut dow = parse_field(parts[5], 0, 7)?;
                normalize_dow(&mut dow);
                Ok(Self {
                    granularity: CronGranularity::Second,
                    decisecond: None,
                    second: Some(second),
                    minute,
                    hour,
                    day,
                    month,
                    dow,
                })
            }
            7 => {
                let decisecond = parse_field(parts[0], 0, 9)?;
                let second = parse_field(parts[1], 0, 59)?;
                let minute = parse_field(parts[2], 0, 59)?;
                let hour = parse_field(parts[3], 0, 23)?;
                let day = parse_field(parts[4], 1, 31)?;
                let month = parse_field(parts[5], 1, 12)?;
                let mut dow = parse_field(parts[6], 0, 7)?;
                normalize_dow(&mut dow);
                Ok(Self {
                    granularity: CronGranularity::Decisecond,
                    decisecond: Some(decisecond),
                    second: Some(second),
                    minute,
                    hour,
                    day,
                    month,
                    dow,
                })
            }
            n => bail!(
                "cron expression `{expr}` must have 5, 6, or 7 fields \
                 (minute|second|decisecond granularity), got {n}"
            ),
        }
    }

    pub fn granularity(&self) -> CronGranularity {
        self.granularity
    }

    pub fn matches_tick(&self, t: &TickTime) -> bool {
        match self.granularity {
            CronGranularity::Minute => {
                if t.decisecond != 0 || t.second != 0 {
                    return false;
                }
            }
            CronGranularity::Second => {
                if t.decisecond != 0 {
                    return false;
                }
                if let Some(second) = &self.second {
                    if !second.matches(t.second) {
                        return false;
                    }
                }
            }
            CronGranularity::Decisecond => {
                if let Some(decisecond) = &self.decisecond {
                    if !decisecond.matches(t.decisecond) {
                        return false;
                    }
                }
                if let Some(second) = &self.second {
                    if !second.matches(t.second) {
                        return false;
                    }
                }
            }
        }
        self.minute.matches(t.minute)
            && self.hour.matches(t.hour)
            && self.day.matches(t.day)
            && self.month.matches(t.month)
            && self.dow.matches(t.dow)
    }

    /// Next fire at or after `from`, stepping at this expression's granularity.
    pub fn next_after(&self, from: DateTime<Local>) -> Option<DateTime<Local>> {
        let limit = self.step_and_limit();
        let start = self.align_for_search(from);
        for offset in 0..limit {
            let candidate = match self.granularity {
                CronGranularity::Minute => start + Duration::minutes(offset),
                CronGranularity::Second => start + Duration::seconds(offset),
                CronGranularity::Decisecond => {
                    start + Duration::milliseconds(offset * TICK_MS as i64)
                }
            };
            if self.matches_tick(&TickTime::from_chrono(candidate)) {
                return Some(candidate);
            }
        }
        None
    }

    /// Next tick at or after `from` aligned to this expression's granularity.
    pub fn next_after_tick(&self, from: DateTime<Local>) -> Option<TickTime> {
        self.next_after(from).map(TickTime::from_chrono)
    }
}

fn normalize_dow(dow: &mut Field) {
    if dow.values.contains(&7) && !dow.values.contains(&0) {
        dow.values.push(0);
        dow.values.sort_unstable();
    }
}

fn align_tick_datetime(from: DateTime<Local>) -> DateTime<Local> {
    let nanos = from.nanosecond();
    let aligned_nanos = (nanos / 100_000_000) * 100_000_000;
    from.with_nanosecond(aligned_nanos).unwrap_or(from)
}

/// True if any expression in `exprs` matches `t`.
pub fn any_match(exprs: &[String], t: &CivilTime) -> Result<bool> {
    any_match_tick(exprs, &t.to_tick())
}

fn trait_to_tick(c: &CivilTime) -> TickTime {
    TickTime {
        decisecond: 0,
        second: 0,
        minute: c.minute,
        hour: c.hour,
        day: c.day,
        month: c.month,
        dow: c.dow,
    }
}

impl CivilTime {
    fn to_tick(&self) -> TickTime {
        trait_to_tick(self)
    }
}

/// True if any expression in `exprs` matches `t`.
pub fn any_match_tick(exprs: &[String], t: &TickTime) -> Result<bool> {
    for e in exprs {
        let parsed = CronExpr::parse(e)?;
        if parsed.matches_tick(t) {
            return Ok(true);
        }
    }
    Ok(false)
}

/// Earliest next fire among `exprs` at or after `from`.
pub fn next_any(exprs: &[String], from: DateTime<Local>) -> Result<Option<DateTime<Local>>> {
    let mut best: Option<DateTime<Local>> = None;
    for e in exprs {
        let parsed = CronExpr::parse(e)?;
        if let Some(n) = parsed.next_after(from) {
            best = Some(match best {
                Some(b) if b <= n => b,
                _ => n,
            });
        }
    }
    Ok(best)
}

/// Earliest next tick among `exprs` at or after `from`.
pub fn next_any_tick(exprs: &[String], from: DateTime<Local>) -> Result<Option<TickTime>> {
    let mut best: Option<TickTime> = None;
    for e in exprs {
        let parsed = CronExpr::parse(e)?;
        if let Some(n) = parsed.next_after_tick(from) {
            best = Some(match best {
                Some(b) if tick_le(&b, &n) => b,
                _ => n,
            });
        }
    }
    Ok(best)
}

fn tick_le(a: &TickTime, b: &TickTime) -> bool {
    (a.month, a.day, a.hour, a.minute, a.second, a.decisecond)
        <= (b.month, b.day, b.hour, b.minute, b.second, b.decisecond)
}

/// Format `when` as local `YYYY-MM-DD HH:MM`.
pub fn format_absolute(when: DateTime<Local>) -> String {
    when.format("%Y-%m-%d %H:%M").to_string()
}

/// Format a tick as local civil time with optional sub-minute precision.
pub fn format_absolute_tick(t: &TickTime, from: DateTime<Local>) -> String {
    if t.decisecond == 0 && t.second == 0 {
        format!(
            "{:04}-{:02}-{:02} {:02}:{:02}",
            from.year(),
            t.month,
            t.day,
            t.hour,
            t.minute
        )
    } else if t.decisecond == 0 {
        format!(
            "{:04}-{:02}-{:02} {:02}:{:02}:{:02}",
            from.year(),
            t.month,
            t.day,
            t.hour,
            t.minute,
            t.second
        )
    } else {
        format!(
            "{:04}-{:02}-{:02} {:02}:{:02}:{:02}.{}",
            from.year(),
            t.month,
            t.day,
            t.hour,
            t.minute,
            t.second,
            t.decisecond
        )
    }
}

/// Human relative span from `from` to `when` (e.g. `now`, `in 5m`, `in 2h 15m`, `in 3d 4h`).
pub fn format_relative(from: DateTime<Local>, when: DateTime<Local>) -> String {
    let delta = when.signed_duration_since(from);
    let millis = delta.num_milliseconds();
    if millis <= 0 {
        return "now".to_string();
    }
    if millis < 1000 {
        return format!("in {}ms", millis);
    }
    let secs = delta.num_seconds();
    if secs <= 0 {
        return "now".to_string();
    }
    let mins_total = (secs + 59) / 60;
    if mins_total < 1 {
        return format!("in {secs}s");
    }
    let days = mins_total / (24 * 60);
    let hours = (mins_total % (24 * 60)) / 60;
    let mins = mins_total % 60;
    let mut parts = Vec::new();
    if days > 0 {
        parts.push(format!("{days}d"));
    }
    if hours > 0 {
        parts.push(format!("{hours}h"));
    }
    if mins > 0 && days == 0 {
        parts.push(format!("{mins}m"));
    } else if mins > 0 && hours > 0 {
        parts.push(format!("{mins}m"));
    } else if parts.is_empty() {
        parts.push(format!("{mins}m"));
    }
    format!("in {}", parts.join(" "))
}

pub fn format_relative_tick(from: DateTime<Local>, when: &TickTime) -> String {
    let when_dt = Local
        .with_ymd_and_hms(
            from.year(),
            when.month,
            when.day,
            when.hour,
            when.minute,
            when.second,
        )
        .single()
        .unwrap_or(from)
        .with_nanosecond(when.decisecond * 100_000_000)
        .unwrap_or(from);
    format_relative(from, when_dt)
}

#[cfg(test)]
mod tests {
    use super::*;
    use chrono::TimeZone;

    fn t(minute: u32, hour: u32, day: u32, month: u32, dow: u32) -> TickTime {
        CivilTime {
            minute,
            hour,
            day,
            month,
            dow,
        }
        .to_tick()
    }

    fn tick(ds: u32, sec: u32, min: u32, hr: u32, day: u32, mon: u32, dow: u32) -> TickTime {
        TickTime {
            decisecond: ds,
            second: sec,
            minute: min,
            hour: hr,
            day,
            month: mon,
            dow,
        }
    }

    #[test]
    fn every_minute() {
        let c = CronExpr::parse("* * * * *").unwrap();
        assert!(c.matches_tick(&t(0, 0, 1, 1, 0)));
        assert!(c.matches_tick(&tick(0, 0, 0, 0, 1, 1, 0)));
        assert!(!c.matches_tick(&tick(0, 1, 0, 0, 1, 1, 0)));
    }

    #[test]
    fn every_second() {
        let c = CronExpr::parse("* * * * * *").unwrap();
        assert_eq!(c.granularity(), CronGranularity::Second);
        assert!(c.matches_tick(&tick(0, 0, 0, 0, 1, 1, 0)));
        assert!(c.matches_tick(&tick(0, 59, 0, 0, 1, 1, 0)));
        assert!(!c.matches_tick(&tick(1, 0, 0, 0, 1, 1, 0)));
    }

    #[test]
    fn every_100ms() {
        let c = CronExpr::parse("* * * * * * *").unwrap();
        assert_eq!(c.granularity(), CronGranularity::Decisecond);
        assert!(c.matches_tick(&tick(0, 0, 0, 0, 1, 1, 0)));
        assert!(c.matches_tick(&tick(9, 0, 0, 0, 1, 1, 0)));
    }

    #[test]
    fn specific_time() {
        let c = CronExpr::parse("30 10 * * *").unwrap();
        assert!(c.matches_tick(&t(30, 10, 5, 3, 2)));
        assert!(!c.matches_tick(&t(31, 10, 5, 3, 2)));
        assert!(!c.matches_tick(&t(30, 11, 5, 3, 2)));
    }

    #[test]
    fn step_hours() {
        let c = CronExpr::parse("0 */6 * * *").unwrap();
        assert!(c.matches_tick(&t(0, 0, 1, 1, 0)));
        assert!(c.matches_tick(&t(0, 6, 1, 1, 0)));
        assert!(c.matches_tick(&t(0, 18, 1, 1, 0)));
        assert!(!c.matches_tick(&t(0, 7, 1, 1, 0)));
    }

    #[test]
    fn dow_sunday_aliases() {
        let c = CronExpr::parse("0 0 * * 7").unwrap();
        assert!(c.matches_tick(&t(0, 0, 1, 1, 0)));
        assert!(!c.matches_tick(&t(0, 0, 1, 1, 1)));
    }

    #[test]
    fn nickname_hourly() {
        let c = CronExpr::parse("@hourly").unwrap();
        assert!(c.matches_tick(&t(0, 15, 1, 1, 0)));
        assert!(!c.matches_tick(&t(1, 15, 1, 1, 0)));
    }

    #[test]
    fn nickname_every_second() {
        let c = CronExpr::parse("@every_second").unwrap();
        assert!(c.matches_tick(&tick(0, 42, 10, 15, 1, 1, 0)));
    }

    #[test]
    fn parse_at_string() {
        let ct = parse_at("2026-08-05 10:30").unwrap();
        assert_eq!(ct.hour, 10);
        assert_eq!(ct.minute, 30);
        assert_eq!(ct.day, 5);
        assert_eq!(ct.month, 8);
    }

    #[test]
    fn parse_at_tick_string() {
        let tt = parse_at_tick("2026-08-05 10:30:45.3").unwrap();
        assert_eq!(tt.hour, 10);
        assert_eq!(tt.minute, 30);
        assert_eq!(tt.second, 45);
        assert_eq!(tt.decisecond, 3);
    }

    #[test]
    fn next_after_same_day() {
        let c = CronExpr::parse("30 10 * * *").unwrap();
        let from = Local.with_ymd_and_hms(2026, 8, 5, 9, 0, 0).unwrap();
        let next = c.next_after(from).unwrap();
        assert_eq!(format_absolute(next), "2026-08-05 10:30");
    }

    #[test]
    fn next_after_rolls_to_tomorrow() {
        let c = CronExpr::parse("30 10 * * *").unwrap();
        let from = Local.with_ymd_and_hms(2026, 8, 5, 11, 0, 0).unwrap();
        let next = c.next_after(from).unwrap();
        assert_eq!(format_absolute(next), "2026-08-06 10:30");
    }

    #[test]
    fn format_relative_parts() {
        let from = Local.with_ymd_and_hms(2026, 8, 5, 10, 0, 0).unwrap();
        let when = Local.with_ymd_and_hms(2026, 8, 5, 10, 0, 0).unwrap();
        assert_eq!(format_relative(from, when), "now");
        let when = Local.with_ymd_and_hms(2026, 8, 5, 12, 15, 0).unwrap();
        assert_eq!(format_relative(from, when), "in 2h 15m");
    }
}