quarb 0.33.0

Query engine for arbors (arboreal graphs - tree-spanned or tree-dominated graphs)
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
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
//! Periods (spec: The Temporal Fragment, The Calendar Reading): a
//! stretch of the UTC calendar named at a grain — a year, a month,
//! a day, an hour, a minute or a second — and the arithmetic of
//! anchoring a time expression's value against one (spec: Time
//! Expressions and the Timeline).
//!
//! The value notation is the one `timex` writes: ISO-8601 at
//! reduced precision for a date (`1899-10-06`, `2025-10`, `1812`),
//! `X` for a field left out (`XXXX-04-08`), `T06:30` for a time of
//! day, a signed ISO duration for a relative (`+P1D`, `-P2M`) or a
//! signed ordinal and an RFC 5545 weekday code (`+1MO`, `-1FR`).

use crate::temporal::{civil_from_days, components, days_from_civil};
use std::cmp::Ordering;

/// A period's precision, coarsest first.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum Grain {
    Year,
    Month,
    Day,
    Hour,
    Minute,
    Second,
}

impl Grain {
    pub fn word(self) -> &'static str {
        match self {
            Grain::Year => "year",
            Grain::Month => "month",
            Grain::Day => "day",
            Grain::Hour => "hour",
            Grain::Minute => "minute",
            Grain::Second => "second",
        }
    }

    pub fn from_word(w: &str) -> Option<Grain> {
        Some(match w {
            "year" => Grain::Year,
            "month" => Grain::Month,
            "day" => Grain::Day,
            "hour" => Grain::Hour,
            "minute" => Grain::Minute,
            "second" => Grain::Second,
            _ => return None,
        })
    }
}

/// A period: its first second on the UTC timeline, and its grain.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct Period {
    pub start: i64,
    pub grain: Grain,
}

fn month_days(y: i64, m: u32) -> u32 {
    match m {
        1 | 3 | 5 | 7 | 8 | 10 | 12 => 31,
        4 | 6 | 9 | 11 => 30,
        2 => {
            if days_from_civil(y + 1, 1, 1) - days_from_civil(y, 1, 1) == 366 {
                29
            } else {
                28
            }
        }
        _ => 0,
    }
}

/// The years a period may name: wide enough for any calendar a
/// text speaks of, narrow enough that no arithmetic here overflows.
const YEARS: std::ops::RangeInclusive<i64> = -999_999..=999_999;

impl Period {
    /// The period of `grain` holding the civil fields; the fields
    /// below the grain are ignored. `None` for a date the calendar
    /// lacks.
    pub fn of(y: i64, mo: u32, d: u32, h: u32, mi: u32, s: u32, grain: Grain) -> Option<Period> {
        let (mo, d, h, mi, s) = match grain {
            Grain::Year => (1, 1, 0, 0, 0),
            Grain::Month => (mo, 1, 0, 0, 0),
            Grain::Day => (mo, d, 0, 0, 0),
            Grain::Hour => (mo, d, h, 0, 0),
            Grain::Minute => (mo, d, h, mi, 0),
            Grain::Second => (mo, d, h, mi, s),
        };
        if !YEARS.contains(&y)
            || !(1..=12).contains(&mo)
            || d < 1
            || d > month_days(y, mo)
            || h > 23
            || mi > 59
            || s > 59
        {
            return None;
        }
        Some(Period {
            start: days_from_civil(y, mo, d) * 86400 + h as i64 * 3600 + mi as i64 * 60 + s as i64,
            grain,
        })
    }

    /// The civil fields of the period's first second.
    pub fn fields(&self) -> (i64, u32, u32, u32, u32, u32) {
        components(self.start)
    }

    /// The first second after the period.
    pub fn end(&self) -> i64 {
        let (y, mo, ..) = self.fields();
        match self.grain {
            Grain::Year => days_from_civil(y + 1, 1, 1) * 86400,
            Grain::Month => {
                let (y, mo) = if mo == 12 { (y + 1, 1) } else { (y, mo + 1) };
                days_from_civil(y, mo, 1) * 86400
            }
            Grain::Day => self.start + 86400,
            Grain::Hour => self.start + 3600,
            Grain::Minute => self.start + 60,
            Grain::Second => self.start + 1,
        }
    }

    /// The period of a coarser (or the same) grain holding this
    /// one; a finer grain has none.
    pub fn at(&self, grain: Grain) -> Option<Period> {
        if grain > self.grain {
            return None;
        }
        let (y, mo, d, h, mi, s) = self.fields();
        Period::of(y, mo, d, h, mi, s, grain)
    }

    /// An instant at the grain it prints at: the day for a midnight
    /// instant with no offset, the second otherwise.
    pub fn of_instant(secs: i64, nanos: u32, offset_min: Option<i16>) -> Period {
        let midnight = secs.rem_euclid(86400) == 0 && nanos == 0 && offset_min.is_none();
        Period {
            start: secs,
            grain: if midnight { Grain::Day } else { Grain::Second },
        }
    }

    /// Periods compare as intervals of the timeline. The grains
    /// nest, so two periods are disjoint (ordered by the timeline)
    /// or nested (equal).
    pub fn interval_cmp(&self, other: &Period) -> Ordering {
        if self.end() <= other.start {
            Ordering::Less
        } else if other.end() <= self.start {
            Ordering::Greater
        } else {
            Ordering::Equal
        }
    }

    /// ISO-8601 at reduced precision, at the period's own grain.
    pub fn format(&self) -> String {
        let (y, mo, d, h, mi, s) = self.fields();
        let year = if y < 0 {
            format!("-{:04}", -y)
        } else {
            format!("{y:04}")
        };
        match self.grain {
            Grain::Year => year,
            Grain::Month => format!("{year}-{mo:02}"),
            Grain::Day => format!("{year}-{mo:02}-{d:02}"),
            Grain::Hour => format!("{year}-{mo:02}-{d:02}T{h:02}"),
            Grain::Minute => format!("{year}-{mo:02}-{d:02}T{h:02}:{mi:02}"),
            Grain::Second => format!("{year}-{mo:02}-{d:02}T{h:02}:{mi:02}:{s:02}"),
        }
    }

    /// Parse ISO-8601 at reduced precision: `1812`, `2025-10`,
    /// `1899-10-06`, `1899-10-06T14`, `1899-10-06T14:30`,
    /// `1899-10-06T14:30:05` (a trailing `Z` allowed on the timed
    /// forms). A year is four digits or more, with an optional
    /// leading `-`.
    pub fn parse(text: &str) -> Option<Period> {
        let (date, time) = match text.split_once('T') {
            Some((d, t)) => (d, Some(t.strip_suffix('Z').unwrap_or(t))),
            None => (text, None),
        };
        let (neg, date) = match date.strip_prefix('-') {
            Some(rest) => (true, rest),
            None => (false, date),
        };
        let mut parts = date.split('-');
        let year = parts.next()?;
        if year.len() < 4 || year.len() > 6 {
            return None;
        }
        let y = digits(year)? as i64;
        let y = if neg { -y } else { y };
        let mo = parts.next();
        let d = parts.next();
        if parts.next().is_some() {
            return None;
        }
        let two = |p: &str| if p.len() == 2 { digits(p) } else { None };
        let (mo_n, d_n) = (
            match mo {
                Some(p) => Some(two(p)?),
                None => None,
            },
            match d {
                Some(p) => Some(two(p)?),
                None => None,
            },
        );
        let Some(time) = time else {
            return match (mo_n, d_n) {
                (None, _) => Period::of(y, 1, 1, 0, 0, 0, Grain::Year),
                (Some(mo), None) => Period::of(y, mo, 1, 0, 0, 0, Grain::Month),
                (Some(mo), Some(d)) => Period::of(y, mo, d, 0, 0, 0, Grain::Day),
            };
        };
        let (mo, d) = (mo_n?, d_n?);
        let (h, mi, s, grain) = parse_clock(time)?;
        Period::of(y, mo, d, h, mi, s, grain)
    }
}

fn digits(s: &str) -> Option<u32> {
    if s.is_empty() || !s.bytes().all(|b| b.is_ascii_digit()) {
        return None;
    }
    s.parse().ok()
}

/// `HH`, `HH:MM` or `HH:MM:SS`, with the grain written.
fn parse_clock(time: &str) -> Option<(u32, u32, u32, Grain)> {
    let mut parts = time.split(':');
    let two = |p: Option<&str>| match p {
        Some(p) if p.len() == 2 => digits(p).map(Some),
        Some(_) => None,
        None => Some(None),
    };
    let h = two(parts.next())??;
    let mi = two(parts.next())?;
    let s = two(parts.next())?;
    if parts.next().is_some() || h > 23 || mi.is_some_and(|m| m > 59) || s.is_some_and(|s| s > 59) {
        return None;
    }
    Some(match (mi, s) {
        (None, _) => (h, 0, 0, Grain::Hour),
        (Some(mi), None) => (h, mi, 0, Grain::Minute),
        (Some(mi), Some(s)) => (h, mi, s, Grain::Second),
    })
}

/// A relative expression's offset, as `timex` writes it.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Offset {
    /// A signed ISO-8601 duration in calendar units (`+P1D`,
    /// `-P2M`, `+PT5M`): the fields are counts, the sign is one
    /// for all.
    Span {
        sign: i64,
        years: i64,
        months: i64,
        days: i64,
        hours: i64,
        minutes: i64,
        seconds: i64,
    },
    /// A signed ordinal and a weekday (`+1MO`: the first Monday
    /// after; `-1FR`: the last Friday before). `weekday` counts
    /// from Monday = 0.
    Weekday { nth: i64, weekday: u32 },
}

/// The RFC 5545 weekday codes, from Monday.
pub const WEEKDAY_CODES: [&str; 7] = ["MO", "TU", "WE", "TH", "FR", "SA", "SU"];

impl Offset {
    /// Parse `+P1D`, `-P1Y2M`, `+PT5M`, `+P2W`, `+1MO`, `-2FR`. The
    /// sign is required: a bare `P2W` is a duration, not an offset.
    pub fn parse(text: &str) -> Option<Offset> {
        let (sign, rest) = match text.as_bytes().first()? {
            b'+' => (1, &text[1..]),
            b'-' => (-1, &text[1..]),
            _ => return None,
        };
        if let Some(body) = rest.strip_prefix('P') {
            let (years, months, days, hours, minutes, seconds) = parse_span_fields(body)?;
            return Some(Offset::Span {
                sign,
                years,
                months,
                days,
                hours,
                minutes,
                seconds,
            });
        }
        let split = rest.find(|c: char| !c.is_ascii_digit())?;
        let (n, code) = rest.split_at(split);
        let nth = digits(n)? as i64;
        let weekday = WEEKDAY_CODES.iter().position(|c| *c == code)? as u32;
        if nth == 0 {
            return None;
        }
        Some(Offset::Weekday {
            nth: sign * nth,
            weekday,
        })
    }

    /// The finest calendar unit a span names (weeks count as days).
    fn finest(&self) -> Grain {
        match *self {
            Offset::Weekday { .. } => Grain::Day,
            Offset::Span {
                years,
                months,
                days,
                hours,
                minutes,
                seconds,
                ..
            } => {
                if seconds != 0 {
                    Grain::Second
                } else if minutes != 0 {
                    Grain::Minute
                } else if hours != 0 {
                    Grain::Hour
                } else if days != 0 {
                    Grain::Day
                } else if months != 0 {
                    Grain::Month
                } else {
                    let _ = years;
                    Grain::Year
                }
            }
        }
    }
}

/// The integer fields of an ISO-8601 duration body (after `P`):
/// `nY nM nW nD` and, after `T`, `nH nM nS`. Weeks fold into days.
/// At least one field is required.
fn parse_span_fields(body: &str) -> Option<(i64, i64, i64, i64, i64, i64)> {
    let (date, time) = match body.split_once('T') {
        Some((d, t)) => (d, Some(t)),
        None => (body, None),
    };
    let mut out = [0i64; 6]; // Y M D h m s
    let mut any = false;
    let mut read = |part: &str, units: &[(char, usize, i64)]| -> Option<()> {
        let mut num = String::new();
        let mut next = 0; // units come in order, each at most once
        for c in part.chars() {
            if c.is_ascii_digit() {
                num.push(c);
                continue;
            }
            let at = units[next..].iter().position(|(u, ..)| *u == c)? + next;
            let (_, slot, scale) = units[at];
            let n: i64 = num.parse().ok()?;
            out[slot] = out[slot].checked_add(n.checked_mul(scale)?)?;
            num.clear();
            next = at + 1;
            any = true;
        }
        num.is_empty().then_some(())
    };
    read(date, &[('Y', 0, 1), ('M', 1, 1), ('W', 2, 7), ('D', 2, 1)])?;
    if let Some(time) = time {
        if time.is_empty() {
            return None;
        }
        read(time, &[('H', 3, 1), ('M', 4, 1), ('S', 5, 1)])?;
    }
    any.then_some((out[0], out[1], out[2], out[3], out[4], out[5]))
}

impl Period {
    /// Move by an offset (spec: Anchoring one expression). A span
    /// moves by calendar units and keeps the grain — except that a
    /// period finer than the day drops to the day when the offset
    /// names no unit finer than the day (`the next day` after
    /// 20:35 is a day: the text states no hour). A day the target
    /// month lacks becomes that month's last day. An offset finer
    /// than the grain has no result. A weekday offset needs the
    /// day or finer, and yields a day.
    pub fn shift(&self, offset: &Offset) -> Option<Period> {
        if offset.finest() > self.grain {
            return None;
        }
        let (y, mo, d, h, mi, s) = self.fields();
        match *offset {
            Offset::Weekday { nth, weekday } => {
                let day = self.start.div_euclid(86400);
                // Monday = 0, as the codes count.
                let today = (day + 3).rem_euclid(7);
                let target = weekday as i64;
                let day = if nth > 0 {
                    let ahead = (target - today).rem_euclid(7);
                    let ahead = if ahead == 0 { 7 } else { ahead };
                    day + ahead + (nth - 1) * 7
                } else {
                    let back = (today - target).rem_euclid(7);
                    let back = if back == 0 { 7 } else { back };
                    day - back - (-nth - 1) * 7
                };
                let (y, mo, d) = civil_from_days(day);
                Period::of(y, mo, d, 0, 0, 0, Grain::Day)
            }
            Offset::Span {
                sign,
                years,
                months,
                days,
                hours,
                minutes,
                seconds,
            } => {
                let total = (y * 12 + (mo as i64 - 1))
                    .checked_add(sign.checked_mul(years.checked_mul(12)?.checked_add(months)?)?)?;
                let (y, mo) = (total.div_euclid(12), total.rem_euclid(12) as u32 + 1);
                if !YEARS.contains(&y) {
                    return None;
                }
                let d = d.min(month_days(y, mo));
                let grain = if self.grain > Grain::Day && offset.finest() <= Grain::Day {
                    Grain::Day
                } else {
                    self.grain
                };
                let base = Period::of(y, mo, d, h, mi, s, grain)?;
                let delta = days
                    .checked_mul(86400)?
                    .checked_add(hours.checked_mul(3600)?)?
                    .checked_add(minutes.checked_mul(60)?)?
                    .checked_add(seconds)?;
                let start = base.start.checked_add(sign.checked_mul(delta)?)?;
                let moved = Period { start, grain };
                YEARS.contains(&moved.fields().0).then_some(moved)
            }
        }
    }

    /// This day at a time of day (`T06`, `T06:30`, `T06:30:15`): the
    /// day or finer is needed; the result has the grain written.
    pub fn at_time(&self, time: &str) -> Option<Period> {
        if self.grain < Grain::Day {
            return None;
        }
        let (h, mi, s, grain) = parse_clock(time.strip_prefix('T')?)?;
        let (y, mo, d, ..) = self.fields();
        Period::of(y, mo, d, h, mi, s, grain)
    }

    /// Complete a date with fields left out (`XXXX-04-08`,
    /// `XXXX-10`, `XXXX-XX-08`) from this period: the missing
    /// fields are this period's, which must carry them. With
    /// `forward`, the first such date at or after this period.
    pub fn complete(&self, partial: &str, forward: bool) -> Option<Period> {
        let mut parts = partial.split('-');
        let (year, mo, d) = (parts.next()?, parts.next(), parts.next());
        if parts.next().is_some() {
            return None;
        }
        let field = |p: &str, width: usize| -> Option<Option<u32>> {
            if p.len() != width {
                None
            } else if p.bytes().all(|b| b == b'X') {
                Some(None)
            } else {
                digits(p).map(Some)
            }
        };
        let year = field(year, 4)?;
        let mo = match mo {
            Some(p) => field(p, 2)?,
            None => return None, // a bare year leaves nothing out
        };
        let d = match d {
            Some(p) => Some(field(p, 2)??),
            None => None,
        };
        if year.is_some() && mo.is_some() {
            return None; // nothing left out: a complete date
        }
        let grain = if d.is_some() {
            Grain::Day
        } else {
            Grain::Month
        };
        if mo.is_none() && self.grain < Grain::Month {
            return None;
        }
        let (ay, amo, ..) = self.fields();
        let build = |y: i64, m: u32| Period::of(y, m, d.unwrap_or(1), 0, 0, 0, grain);
        let (mut y, mut m) = (year.map_or(ay, |y| y as i64), mo.unwrap_or(amo));
        if !forward {
            return build(y, m);
        }
        // The first such date at or after this period: step the
        // field the expression left out (the month where it names
        // none, else the year) until the date is not behind. A day
        // some months or years lack (the 31st, 29 February) is
        // looked for a little further.
        for _ in 0..(12 * 8) {
            if let Some(p) = build(y, m)
                && p.interval_cmp(self) != Ordering::Less
            {
                return Some(p);
            }
            if mo.is_none() {
                m += 1;
                if m > 12 {
                    m = 1;
                    if year.is_some() {
                        return None;
                    }
                    y += 1;
                }
            } else if year.is_none() {
                y += 1;
            } else {
                return None;
            }
        }
        None
    }
}

/// What a `timex` value text is, by its shape.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Shape {
    /// A complete date: its own period.
    Complete(Period),
    /// A relative offset, a time of day, or a date with fields left
    /// out: valued against a point.
    Anchored,
    /// A duration, or text in none of the notations.
    None,
}

/// Classify a value text.
pub fn shape(value: &str) -> Shape {
    if Offset::parse(value).is_some() {
        return Shape::Anchored;
    }
    if let Some(t) = value.strip_prefix('T') {
        return if parse_clock(t).is_some() {
            Shape::Anchored
        } else {
            Shape::None
        };
    }
    if value.contains('X') {
        return Shape::Anchored;
    }
    match Period::parse(value) {
        Some(p) => Shape::Complete(p),
        None => Shape::None,
    }
}

/// Value a `timex` value text against an anchor (spec: Anchoring
/// one expression): a complete date passes through; an offset, a
/// time of day and a date with fields left out are read against
/// the anchor; a duration and anything else are `None`.
pub fn anchor(value: &str, anchor: Option<&Period>, forward: bool) -> Option<Period> {
    if let Some(offset) = Offset::parse(value) {
        return anchor?.shift(&offset);
    }
    if value.starts_with('T') {
        return anchor?.at_time(value);
    }
    if value.contains('X') {
        return anchor?.complete(value, forward);
    }
    Period::parse(value)
}

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

    fn p(s: &str) -> Period {
        Period::parse(s).unwrap_or_else(|| panic!("{s}"))
    }

    #[test]
    fn reduced_precision_round_trips() {
        for s in [
            "1812",
            "2025-10",
            "1899-10-06",
            "1899-10-06T14",
            "1899-10-06T14:30",
            "1899-10-06T14:30:05",
            "0476",
            "-0750",
        ] {
            assert_eq!(p(s).format(), s);
        }
        assert_eq!(p("1899-10-06T14:30Z").format(), "1899-10-06T14:30");
        for bad in [
            "18",
            "1899-13",
            "1899-02-29",
            "1899-10-6",
            "1899-10-06T24",
            "",
            "abc",
            "1899-10-06T",
        ] {
            assert!(Period::parse(bad).is_none(), "{bad}");
        }
        assert_eq!(p("1900-02").end() - p("1900-02").start, 28 * 86400);
        assert_eq!(p("2000-02").end() - p("2000-02").start, 29 * 86400);
    }

    #[test]
    fn periods_are_disjoint_or_nested() {
        use Ordering::*;
        assert_eq!(p("2025-10").interval_cmp(&p("2025-10-06")), Equal);
        assert_eq!(p("2025-10-06").interval_cmp(&p("2025-10")), Equal);
        assert_eq!(p("2025-10-06").interval_cmp(&p("2025-10-07")), Less);
        assert_eq!(p("2025").interval_cmp(&p("2024-12-31T23:59")), Greater);
        assert_eq!(p("2025-10-06T14:30").at(Grain::Month), Some(p("2025-10")));
        assert_eq!(p("2025-10").at(Grain::Day), None);
    }

    #[test]
    fn offsets() {
        let a = |value: &str, at: &str| anchor(value, Some(&p(at)), false).map(|r| r.format());
        // a span moves by calendar units and keeps the grain
        assert_eq!(a("+P2M", "1899-10-06").as_deref(), Some("1899-12-06"));
        assert_eq!(a("+P1D", "1899-10-06").as_deref(), Some("1899-10-07"));
        assert_eq!(a("-P2W", "1899-10-06").as_deref(), Some("1899-09-22"));
        assert_eq!(a("+P1Y", "2025-10").as_deref(), Some("2026-10"));
        assert_eq!(a("+P3M", "2025-11").as_deref(), Some("2026-02"));
        assert_eq!(a("-P1Y", "1812").as_deref(), Some("1811"));
        assert_eq!(
            a("+PT5M", "1899-10-06T23:58").as_deref(),
            Some("1899-10-07T00:03")
        );
        // finer than the day, the anchor drops to the day unless the
        // offset names a finer unit
        assert_eq!(a("+P1D", "1897-05-03T20:35").as_deref(), Some("1897-05-04"));
        assert_eq!(a("+P2M", "1897-05-03T20:35").as_deref(), Some("1897-07-03"));
        assert_eq!(a("+P0D", "1897-05-03T20:35").as_deref(), Some("1897-05-03"));
        assert_eq!(
            a("+PT1H", "1897-05-03T20:35").as_deref(),
            Some("1897-05-03T21:35")
        );
        assert_eq!(
            a("+P1DT1H", "1897-05-03T20:35").as_deref(),
            Some("1897-05-04T21:35")
        );
        // the month's last day where the target month lacks the day
        assert_eq!(a("+P1M", "1899-01-31").as_deref(), Some("1899-02-28"));
        assert_eq!(a("+P1M", "1900-01-31").as_deref(), Some("1900-02-28"));
        assert_eq!(a("+P1M", "2000-01-31").as_deref(), Some("2000-02-29"));
        // an offset finer than the anchor has no result
        assert_eq!(a("+P1D", "1812"), None);
        assert_eq!(a("+PT1H", "1899-10-06"), None);
        assert_eq!(a("+P1M", "1812"), None);
        // weekdays: 1899-10-06 was a Friday
        assert_eq!(a("+1MO", "1899-10-06").as_deref(), Some("1899-10-09"));
        assert_eq!(a("+1FR", "1899-10-06").as_deref(), Some("1899-10-13"));
        assert_eq!(a("-1FR", "1899-10-06").as_deref(), Some("1899-09-29"));
        assert_eq!(a("-1TH", "1899-10-06").as_deref(), Some("1899-10-05"));
        assert_eq!(a("+2MO", "1899-10-06T14:30").as_deref(), Some("1899-10-16"));
        assert_eq!(a("+1MO", "1899-10"), None);
        // a time of day
        assert_eq!(
            a("T06:30", "1899-10-06").as_deref(),
            Some("1899-10-06T06:30")
        );
        assert_eq!(
            a("T18", "1899-10-06T09:00").as_deref(),
            Some("1899-10-06T18")
        );
        assert_eq!(a("T06:30", "1899-10"), None);
        // a complete date passes through; a duration is nothing
        assert_eq!(a("1846-04-08", "1899-10-06").as_deref(), Some("1846-04-08"));
        assert_eq!(a("P2W", "1899-10-06"), None);
        assert_eq!(anchor("+P1D", None, false), None);
        assert_eq!(
            anchor("1812", None, false).map(|r| r.format()).as_deref(),
            Some("1812")
        );
        assert!(Offset::parse("P1D").is_none() && Offset::parse("+0MO").is_none());
        assert!(Offset::parse("+P").is_none() && Offset::parse("+P1DT").is_none());
        assert!(Offset::parse("+P1M1Y").is_none());
    }

    #[test]
    fn fields_left_out() {
        let c =
            |value: &str, at: &str, fwd: bool| anchor(value, Some(&p(at)), fwd).map(|r| r.format());
        assert_eq!(
            c("XXXX-04-08", "1846-03-20", false).as_deref(),
            Some("1846-04-08")
        );
        assert_eq!(c("XXXX-10", "1899", false).as_deref(), Some("1899-10"));
        assert_eq!(
            c("XXXX-XX-08", "1846-03-20", false).as_deref(),
            Some("1846-03-08")
        );
        assert_eq!(c("XXXX-XX-08", "1846", false), None);
        // forward: the first such date at or after the point
        assert_eq!(
            c("XXXX-04-08", "1846-12-20", true).as_deref(),
            Some("1847-04-08")
        );
        assert_eq!(
            c("XXXX-04-08", "1846-04-08", true).as_deref(),
            Some("1846-04-08")
        );
        assert_eq!(
            c("XXXX-04-08", "1846-12-20", false).as_deref(),
            Some("1846-04-08")
        );
        assert_eq!(
            c("XXXX-XX-08", "1846-12-20", true).as_deref(),
            Some("1847-01-08")
        );
        assert_eq!(
            c("XXXX-02-29", "1897-03-01", true).as_deref(),
            Some("1904-02-29")
        );
        assert_eq!(
            c("XXXX-XX-31", "1899-04-02", true).as_deref(),
            Some("1899-05-31")
        );
        assert_eq!(c("XXXX-02-30", "1899-01-01", true), None);
        assert_eq!(shape("XXXX-04-08"), Shape::Anchored);
        assert_eq!(shape("P2W"), Shape::None);
        assert_eq!(shape("1812"), Shape::Complete(p("1812")));
    }
}