deep-time 0.1.0-beta.31

High-precision, no-std, no-alloc date-time library, leap-seconds, time scales, relativistic time, and a powerful date & duration parser
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
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
//! Red-team / characterization tests for [`Dt::from_str_parse`].
//!
//! Complements the combinatorial coverage in `date_tests.rs` and the relative
//! phrase suite in `date_relative_tests.rs` by probing:
//! - guardrails (empty / oversize / whitespace)
//! - calendar validity (leap years, ordinal/week bounds)
//! - ambiguous numeric order under Smart / Day / Month / Year
//! - pure-numeric modes (Auto / Legacy / Scientific / UnixTimestamp)
//! - named English dates, ordinals, 12-hour clock quirks
//! - syslog year inference
//! - relative phrases + bare times-of-day
//! - offsets, leap seconds, scale suffixes
//! - separators (unicode dashes, fullwidth digits, JP calendar units)
//! - adversarial / lenient garbage handling
//! - `ParseCfg` knobs (explicit formats, `to_lower`, `relative`)
//! - `str_to_*` convenience helpers
//!
//! Several cases document *current* behavior that is surprising; those are
//! labeled `// CHARACTERIZATION` so they can be revisited deliberately.

#![allow(clippy::all, clippy::pedantic, clippy::restriction, warnings)]

#[cfg(feature = "parse")]
mod tests {
    use deep_time::{Dt, DtErrKind, Mode, Order, ParseCfg, Scale};

    // ── helpers ────────────────────────────────────────────────────────────

    fn def() -> ParseCfg {
        ParseCfg::DEFAULT
    }

    /// Fixed "now": Wednesday 2025-01-15 12:00 UTC
    fn ref_cfg() -> ParseCfg {
        ParseCfg {
            ref_time: Some(Dt::from_ymd(2025, 1, 15, Scale::UTC, 12, 0, 0, 0)),
            ..Default::default()
        }
    }

    fn cfg_order(order: Order) -> ParseCfg {
        ParseCfg {
            order,
            ..Default::default()
        }
    }

    fn cfg_mode(mode: Mode) -> ParseCfg {
        ParseCfg {
            mode,
            ..Default::default()
        }
    }

    fn parse(input: &str, cfg: &ParseCfg) -> Dt {
        Dt::from_str_parse(input, cfg).unwrap_or_else(|e| {
            panic!("expected Ok for {input:?}, got Err({e})");
        })
    }

    fn assert_rfc(input: &str, expected: &str, cfg: &ParseCfg) {
        let dt = parse(input, cfg);
        let actual = dt.to_str_rfc3339_nf(9);
        assert_eq!(actual, expected, "input={input:?}");
    }

    fn assert_err(input: &str, cfg: &ParseCfg) {
        assert!(
            Dt::from_str_parse(input, cfg).is_err(),
            "expected Err for {input:?}"
        );
    }

    fn assert_err_kind(input: &str, cfg: &ParseCfg, kind: DtErrKind) {
        match Dt::from_str_parse(input, cfg) {
            Ok(dt) => panic!(
                "expected Err({kind:?}) for {input:?}, got Ok({})",
                dt.to_str_rfc3339_nf(9)
            ),
            Err(e) => assert_eq!(e.kind(), kind, "input={input:?}, err={e}"),
        }
    }

    // ── 1. Guardrails ──────────────────────────────────────────────────────

    #[test]
    fn guardrails_empty_and_oversize() {
        let cfg = def();
        assert_err_kind("", &cfg, DtErrKind::Empty);
        // STRTIME_SIZE is 512; one past that is InvalidLen.
        assert_err_kind(&"x".repeat(513), &cfg, DtErrKind::InvalidLen);
        // Exactly at the limit is length-ok but content is garbage.
        assert_err(&"x".repeat(512), &cfg);
        assert_err("   ", &cfg);
        assert_err("\t\n", &cfg);
    }

    // ── 2. ISO / calendar validity ─────────────────────────────────────────

    #[test]
    fn iso_happy_paths_and_offsets() {
        let cfg = def();
        assert_rfc("2024-03-15", "2024-03-15T00:00:00Z", &cfg);
        assert_rfc("2024-03-15T14:30:00Z", "2024-03-15T14:30:00Z", &cfg);
        assert_rfc(
            "2024-03-15T14:30:00.123456789Z",
            "2024-03-15T14:30:00.123456789Z",
            &cfg,
        );
        assert_rfc("2024-03-15T14:30:00.5Z", "2024-03-15T14:30:00.5Z", &cfg);
        assert_rfc("20240315T143000", "2024-03-15T14:30:00Z", &cfg);
        // Offset is applied; result is the UTC instant.
        assert_rfc("2024-03-15T14:30:00+01:00", "2024-03-15T13:30:00Z", &cfg);
        assert_rfc("2024-03-15T14:30:00-05:30", "2024-03-15T20:00:00Z", &cfg);
        assert_rfc("2024-03-15 14:30 +0100", "2024-03-15T13:30:00Z", &cfg);
        assert_rfc("2024-03-15 14:30 -0530", "2024-03-15T20:00:00Z", &cfg);
    }

    #[test]
    fn week_and_ordinal_dates() {
        let cfg = def();
        assert_rfc("2024-W11-4", "2024-03-14T00:00:00Z", &cfg);
        assert_rfc("2024-074", "2024-03-14T00:00:00Z", &cfg);
        assert_rfc("2024074", "2024-03-14T00:00:00Z", &cfg);
        assert_rfc("2024-366", "2024-12-31T00:00:00Z", &cfg); // leap year
        // Compact week+weekday (week 11, Thursday).
        assert_rfc("2024W114", "2024-03-14T00:00:00Z", &cfg);
        assert_rfc("2024W15", "2024-04-08T00:00:00Z", &cfg);
        assert_err("2023-366", &cfg); // non-leap year
        assert_err("2024-000", &cfg);
        assert_err("2024-367", &cfg);
        assert_err("2024-W00", &cfg);
        assert_err("2024-W54", &cfg);
        assert_err("2024-W15-0", &cfg);
        assert_err("2024-W15-8", &cfg);
    }

    #[test]
    fn iso_week_no_weekday() {
        let cfg = def();
        assert_rfc("2024-W11", "2024-03-11T00:00:00Z", &cfg);
        assert_rfc("2024W11", "2024-03-11T00:00:00Z", &cfg);
    }

    #[test]
    fn calendar_bounds_and_leap_years() {
        let cfg = def();
        // Extreme but valid years.
        assert_rfc("0000-01-01", "0000-01-01T00:00:00Z", &cfg);
        assert_rfc("9999-12-31", "9999-12-31T00:00:00Z", &cfg);
        assert_rfc("-0001-01-01", "-0001-01-01T00:00:00Z", &cfg);
        assert_rfc("-2024-03-15", "-2024-03-15T00:00:00Z", &cfg);

        // Gregorian leap rules.
        assert_rfc("2024-02-29", "2024-02-29T00:00:00Z", &cfg);
        assert_rfc("2000-02-29", "2000-02-29T00:00:00Z", &cfg); // century leap
        assert_err("2023-02-29", &cfg);
        assert_err("1900-02-29", &cfg); // century non-leap
        assert_err("2100-02-29", &cfg);

        // Impossible civil dates.
        assert_err("2024-04-31", &cfg);
        assert_err("2024-00-15", &cfg);
        assert_err("2024-13-01", &cfg);
        assert_err("2024-03-00", &cfg);
        assert_err("2024-03-32", &cfg);
        assert_err("0000-00-00", &cfg);
    }

    #[test]
    fn time_component_bounds() {
        let cfg = def();
        assert_rfc(
            "2024-03-15 12:00:00.999999999",
            "2024-03-15T12:00:00.999999999Z",
            &cfg,
        );
        assert_err("2024-03-15 24:00:00", &cfg);
        assert_err("2024-03-15 25:00:00", &cfg);
        assert_err("2024-03-15 12:60:00", &cfg);
        // CHARACTERIZATION: second=60 outside a leap-second context is
        // accepted and clamped/snapped rather than hard-failing.
        let dt = parse("2024-03-15 12:00:60", &cfg);
        assert_eq!(dt.to_ymd().sec(), 59);
    }

    // ── 3. Ambiguous numeric order ─────────────────────────────────────────

    #[test]
    fn order_day_vs_month_vs_year_forced() {
        // Prefer order, then the other two (same chains as Smart).
        assert_rfc("01/02/2003", "2003-02-01T00:00:00Z", &cfg_order(Order::Day));
        assert_rfc(
            "01/02/2003",
            "2003-01-02T00:00:00Z",
            &cfg_order(Order::Month),
        );
        assert_rfc(
            "01/02/2003",
            "2003-02-01T00:00:00Z",
            &cfg_order(Order::Year),
        ); // → day

        assert_rfc("13/01/2003", "2003-01-13T00:00:00Z", &cfg_order(Order::Day));
        assert_rfc(
            "13/01/2003",
            "2003-01-13T00:00:00Z",
            &cfg_order(Order::Month),
        ); // → day
        assert_rfc(
            "01/13/2003",
            "2003-01-13T00:00:00Z",
            &cfg_order(Order::Month),
        );
        assert_rfc("01/13/2003", "2003-01-13T00:00:00Z", &cfg_order(Order::Day)); // → month

        // 2-digit year triples.
        assert_rfc("15/03/24", "2024-03-15T00:00:00Z", &cfg_order(Order::Day));
        assert_rfc("15/03/24", "2024-03-15T00:00:00Z", &cfg_order(Order::Month)); // → day
        assert_rfc("15/03/24", "2015-03-24T00:00:00Z", &cfg_order(Order::Year));
        assert_rfc("01/02/03", "2003-02-01T00:00:00Z", &cfg_order(Order::Day));
        assert_rfc("01/02/03", "2003-01-02T00:00:00Z", &cfg_order(Order::Month));
        assert_rfc("01/02/03", "2001-02-03T00:00:00Z", &cfg_order(Order::Year));
    }

    #[test]
    fn order_smart_heuristic_signals() {
        let smart = cfg_order(Order::Smart);
        assert_rfc("13/01/2003", "2003-01-13T00:00:00Z", &smart); // first 13–31 → day
        assert_rfc("01/13/2003", "2003-01-13T00:00:00Z", &smart); // 1–12 then 13–31 → month
        assert_rfc("2024.03.15", "2024-03-15T00:00:00Z", &smart); // leading 4-digit year
        assert_rfc("03.15.24", "2024-03-15T00:00:00Z", &smart); // US m.d.yy
        assert_rfc("15/03/24", "2024-03-15T00:00:00Z", &smart); // EU d/m/yy
        assert_rfc("01/02/03", "2003-02-01T00:00:00Z", &smart); // all ≤12 → day fallback
        assert_rfc("05/06/07", "2007-06-05T00:00:00Z", &smart);
        assert_err("3-4-5", &smart);
    }

    // ── 4. Pure-numeric modes ──────────────────────────────────────────────

    #[test]
    fn pure_numeric_years_and_compact_dates() {
        let auto = cfg_mode(Mode::Auto);
        let sci = cfg_mode(Mode::Scientific);
        let leg = cfg_mode(Mode::Legacy);

        // 2-digit year pivot (≤68 → 20xx).
        assert_rfc("24", "2024-01-01T00:00:00Z", &auto);
        // Scientific treats 1–4 digits as literal year.
        assert_rfc("24", "0024-01-01T00:00:00Z", &sci);
        assert_rfc("5", "0005-01-01T00:00:00Z", &sci);
        // Auto/Legacy: 1- and 3-digit years fall through to unix seconds.
        assert_rfc("5", "1970-01-01T00:00:05Z", &auto);
        assert_rfc("202", "1970-01-01T00:03:22Z", &auto);

        assert_rfc("2024", "2024-01-01T00:00:00Z", &auto);
        assert_rfc("240315", "2024-03-15T00:00:00Z", &auto);
        assert_rfc("20240315", "2024-03-15T00:00:00Z", &auto);
        // 6-digit with plausible YYYYMM year → first-of-month.
        assert_rfc("202403", "2024-03-01T00:00:00Z", &auto);
        assert_rfc("202403", "2024-03-01T00:00:00Z", &sci);
        assert_rfc("202403", "2024-03-01T00:00:00Z", &leg);
    }

    #[test]
    fn pure_numeric_ordinal_mjd_jd_by_mode() {
        let auto = cfg_mode(Mode::Auto);
        let sci = cfg_mode(Mode::Scientific);
        let leg = cfg_mode(Mode::Legacy);

        // 5-digit YYDDD ordinal (24-123 → 2024-05-02).
        assert_rfc("24123", "2024-05-02T00:00:00Z", &auto);
        assert_rfc("24123", "2024-05-02T00:00:00Z", &leg);

        // MJD 60400 → 2024-03-31 (Auto integer prefers ordinal when valid;
        // 60400 as YYDDD is invalid so MJD wins; Sci prefers MJD).
        assert_rfc("60400", "2024-03-31T00:00:00Z", &auto);
        assert_rfc("60400", "2024-03-31T00:00:00Z", &sci);
        // Legacy is ordinal-only; 60400 is not a valid YYDDD → unix fallback.
        assert_rfc("60400", "1970-01-01T16:46:40Z", &leg);

        // Fractional MJD.
        assert_rfc("60400.75", "2024-03-31T18:00:00Z", &auto);
        assert_rfc("60400.75", "2024-03-31T18:00:00Z", &sci);

        // 7-digit YYYYDDD ordinal.
        assert_rfc("2024123", "2024-05-02T00:00:00Z", &auto);
        assert_rfc("2024123", "2024-05-02T00:00:00Z", &leg);
        // Sci prefers JD for integer 7-digit (JD noon convention for integers).
        assert_rfc("2024123", "0829-10-05T00:00:00Z", &sci);

        // Famous JD of Unix epoch (fractional, so no +0.5 noon adjust).
        assert_rfc("2440587.5", "1970-01-01T00:00:00Z", &sci);
    }

    #[test]
    fn pure_numeric_unix_timestamps() {
        let auto = cfg_mode(Mode::Auto);
        let unix = cfg_mode(Mode::UnixTimestamp);

        // Seconds / ms / µs / ns of 2025-01-01 00:00:00 UTC.
        assert_rfc("1735689600", "2025-01-01T00:00:00Z", &auto);
        assert_rfc("1735689600", "2025-01-01T00:00:00Z", &unix);
        assert_rfc("1735689600123", "2025-01-01T00:00:00.123Z", &auto);
        assert_rfc("1735689600123456", "2025-01-01T00:00:00.123456Z", &auto);
        assert_rfc(
            "1735689600123456789",
            "2025-01-01T00:00:00.123456789Z",
            &auto,
        );
        assert_rfc("1735689600.5", "2025-01-01T00:00:00.5Z", &auto);

        // Signed unix seconds.
        assert_rfc("0", "1970-01-01T00:00:00Z", &unix);
        assert_rfc("-1", "1969-12-31T23:59:59Z", &unix);

        // Mode::UnixTimestamp forces short numbers into the unix path.
        assert_rfc("2024", "1970-01-01T00:33:44Z", &unix);
        assert_rfc("1000000000", "2001-09-09T01:46:40Z", &unix); // 1e9 sec
        assert_rfc("1000000000000", "2001-09-09T01:46:40Z", &unix); // 1e12 ms
    }

    // ── 5. Named English dates ─────────────────────────────────────────────

    #[test]
    fn named_english_and_ordinal_days() {
        let cfg = def();
        let cases = [
            ("15 March 2024", "2024-03-15T00:00:00Z"),
            ("March 15, 2024", "2024-03-15T00:00:00Z"),
            ("15th March 2024", "2024-03-15T00:00:00Z"),
            ("the 15th of March 2024", "2024-03-15T00:00:00Z"),
            ("15 of March 2024", "2024-03-15T00:00:00Z"),
            ("Mar 15 2024", "2024-03-15T00:00:00Z"),
            ("15-Mar-2024", "2024-03-15T00:00:00Z"),
            ("2024 March 15", "2024-03-15T00:00:00Z"),
            ("Thursday, March 14, 2024", "2024-03-14T00:00:00Z"),
            ("1st January 2000", "2000-01-01T00:00:00Z"),
            ("31st December 1999", "1999-12-31T00:00:00Z"),
            ("Feb 29 2024", "2024-02-29T00:00:00Z"),
            ("Sept 15 2024", "2024-09-15T00:00:00Z"),
            ("Sep 15 2024", "2024-09-15T00:00:00Z"),
            ("September 15th, 2024", "2024-09-15T00:00:00Z"),
            (
                "on the 5th of april 2024 at 00:00am",
                "2024-04-05T00:00:00Z",
            ),
        ];
        for (input, expected) in cases {
            assert_rfc(input, expected, &cfg);
        }
        assert_err("Feb 29 2023", &cfg);
    }

    /// Month + year with no day → first of that month.
    #[test]
    fn month_and_year_without_day() {
        let cfg = def();
        let cases = [
            ("2024-03", "2024-03-01T00:00:00Z"),
            ("2024/03", "2024-03-01T00:00:00Z"),
            ("2024/3", "2024-03-01T00:00:00Z"),
            ("202403", "2024-03-01T00:00:00Z"),
            // Month-first numeric (invoice / US style MM/YYYY).
            ("03/2024", "2024-03-01T00:00:00Z"),
            ("3/2024", "2024-03-01T00:00:00Z"),
            ("12/2024", "2024-12-01T00:00:00Z"),
            ("03-2024", "2024-03-01T00:00:00Z"),
            ("March 2024", "2024-03-01T00:00:00Z"),
            ("Mar 2024", "2024-03-01T00:00:00Z"),
            ("March, 2024", "2024-03-01T00:00:00Z"),
            ("2024 March", "2024-03-01T00:00:00Z"),
            ("2024 Mar", "2024-03-01T00:00:00Z"),
            ("january 2000", "2000-01-01T00:00:00Z"),
            ("Dec 1999", "1999-12-01T00:00:00Z"),
            ("Sept 2024", "2024-09-01T00:00:00Z"),
        ];
        for (input, expected) in cases {
            assert_rfc(input, expected, &cfg);
        }
        // Month alone is not enough.
        assert_err("March", &cfg);
        assert_err("Mar", &cfg);
        // Invalid month or ambiguous MM/YY (2-digit year).
        assert_err("13/2024", &cfg);
        assert_err("00/2024", &cfg);
        assert_err("03/24", &cfg);
    }

    #[test]
    fn twelve_hour_clock_and_at_glue() {
        let cfg = def();
        assert_rfc("14 Mar 2024 2:30 PM", "2024-03-14T14:30:00Z", &cfg);
        assert_rfc("14 Mar 2024 2:30PM", "2024-03-14T14:30:00Z", &cfg);
        assert_rfc("14 Mar 2024 at 2:30pm", "2024-03-14T14:30:00Z", &cfg);
    }

    /// Bare hour + meridian with no minutes (`2PM` → 14:00 on the same day).
    #[test]
    fn bare_hour_2pm() {
        let cfg = def();
        assert_rfc("14 Mar 2024 2PM", "2024-03-14T14:00:00Z", &cfg);
        assert_rfc("14 Mar 2024 2pm", "2024-03-14T14:00:00Z", &cfg);
        assert_rfc("14 Mar 2024 2 PM", "2024-03-14T14:00:00Z", &cfg);
        assert_rfc("14 Mar 2024 2AM", "2024-03-14T02:00:00Z", &cfg);
        assert_rfc("14 Mar 2024 12PM", "2024-03-14T12:00:00Z", &cfg);
        assert_rfc("14 Mar 2024 12AM", "2024-03-14T00:00:00Z", &cfg);
        assert_rfc("March 14, 2024 2PM", "2024-03-14T14:00:00Z", &cfg);
        assert_rfc("2024-03-14 2PM", "2024-03-14T14:00:00Z", &cfg);
        assert_rfc("14 Mar 2024 at 2PM", "2024-03-14T14:00:00Z", &cfg);
    }

    // ── 6. Syslog / year-less dates ────────────────────────────────────────

    #[test]
    fn syslog_year_inference_from_ref_time() {
        // ref = 2025-01-15. Dates more than 2 days in the future snap to
        // the previous year (classic Dec-in-Jan syslog behaviour).
        let cfg = ref_cfg();
        assert_rfc("Mar  5 10:23:45", "2024-03-05T10:23:45Z", &cfg);
        assert_rfc("Dec 31 23:59:59", "2024-12-31T23:59:59Z", &cfg);
        assert_rfc("Jun 16 12:00:00", "2024-06-16T12:00:00Z", &cfg);
        // Jan 1 2025 is still "this year" relative to Jan 15.
        assert_rfc("Jan  1 00:00:00", "2025-01-01T00:00:00Z", &cfg);
    }

    // ── 7. Relative phrases & bare times ───────────────────────────────────

    #[test]
    fn relative_core_phrases() {
        let cfg = ref_cfg(); // Wed 2025-01-15 12:00
        let cases = [
            ("tomorrow", "2025-01-16T12:00:00Z"),
            ("yesterday", "2025-01-14T12:00:00Z"),
            ("today", "2025-01-15T12:00:00Z"),
            ("now", "2025-01-15T12:00:00Z"),
            ("next Monday", "2025-01-20T12:00:00Z"),
            ("last Friday", "2025-01-10T12:00:00Z"),
            ("this Monday", "2025-01-13T12:00:00Z"),
            ("coming Friday", "2025-01-17T12:00:00Z"),
            ("in 3 days", "2025-01-18T12:00:00Z"),
            ("2 weeks ago", "2025-01-01T12:00:00Z"),
            ("next Monday at 14:00", "2025-01-20T14:00:00Z"),
            ("tomorrow at 9am", "2025-01-16T09:00:00Z"),
            ("next year", "2026-01-15T12:00:00Z"),
            ("last year", "2024-01-15T12:00:00Z"),
            ("Monday next", "2025-01-20T12:00:00Z"),
            ("14:00 next Monday", "2025-01-20T14:00:00Z"),
            ("at 14:00", "2025-01-15T14:00:00Z"),
        ];
        for (input, expected) in cases {
            assert_rfc(input, expected, &cfg);
        }
    }

    #[test]
    fn relative_partial_and_surprising() {
        let cfg = ref_cfg();
        assert_rfc("the day after tomorrow", "2025-01-17T12:00:00Z", &cfg);
        assert_rfc("day after tomorrow", "2025-01-17T12:00:00Z", &cfg);
        assert_rfc("the day before yesterday", "2025-01-13T12:00:00Z", &cfg);
        assert_rfc("day before yesterday", "2025-01-13T12:00:00Z", &cfg);
        // Bare week/month (+ ignored "a"/"in") default to quantity 1.
        assert_rfc("in a week", "2025-01-22T12:00:00Z", &cfg);
        assert_rfc("a week", "2025-01-22T12:00:00Z", &cfg);
        assert_rfc("week", "2025-01-22T12:00:00Z", &cfg);
        assert_rfc("week ago", "2025-01-08T12:00:00Z", &cfg);
        assert_rfc("a month ago", "2024-12-15T12:00:00Z", &cfg);
        assert_rfc("in a month", "2025-02-15T12:00:00Z", &cfg);
        // CHARACTERIZATION: bare relative tokens alone act as "now".
        assert_rfc("next", "2025-01-15T12:00:00Z", &cfg);
        assert_rfc("ago", "2025-01-15T12:00:00Z", &cfg);

        assert_err("noon", &cfg);
        assert_err("midnight", &cfg);
        // Bare hour + meridiem (no colon) is TOD on the ref day.
        assert_rfc("9am", "2025-01-15T09:00:00Z", &cfg);
        assert_rfc("9 am", "2025-01-15T09:00:00Z", &cfg);
        assert_rfc("9pm", "2025-01-15T21:00:00Z", &cfg);
        assert_rfc("12am", "2025-01-15T00:00:00Z", &cfg);
        assert_rfc("12pm", "2025-01-15T12:00:00Z", &cfg);
    }

    #[test]
    fn bare_time_of_day_uses_ref_date() {
        let cfg = ref_cfg();
        assert_rfc("17:00", "2025-01-15T17:00:00Z", &cfg);
        assert_rfc("14:30", "2025-01-15T14:30:00Z", &cfg);
        assert_rfc("2:30 PM", "2025-01-15T14:30:00Z", &cfg);
        assert_rfc("9:00 am", "2025-01-15T09:00:00Z", &cfg);
        assert_rfc("00:00", "2025-01-15T00:00:00Z", &cfg);
        assert_rfc("15:30:45", "2025-01-15T15:30:45Z", &cfg);
        assert_rfc("9:30:00", "2025-01-15T09:30:00Z", &cfg);
        assert_rfc("12:00", "2025-01-15T12:00:00Z", &cfg);
        assert_rfc("12:00:00", "2025-01-15T12:00:00Z", &cfg);
        assert_rfc("12:00 am", "2025-01-15T00:00:00Z", &cfg);
        assert_rfc("12:00 pm", "2025-01-15T12:00:00Z", &cfg);
        // Ops H:MM / H:MM:SS when hour is not a civil clock.
        assert_rfc("24:00", "2025-01-16T12:00:00Z", &cfg);
        assert_rfc("72:30", "2025-01-18T12:30:00Z", &cfg);
        assert_rfc("25:00:00", "2025-01-16T13:00:00Z", &cfg);
        assert_rfc("72:30 ago", "2025-01-12T11:30:00Z", &cfg);
    }

    #[test]
    fn invalid_clock_fields_do_not_glue_to_days() {
        let cfg = ref_cfg();
        // Invalid ops duration → Err (not digit-glue into multi-day offsets).
        assert_err("24:60", &cfg);
        assert_err("25:99", &cfg);
        assert_err("at 25:00", &cfg);
        assert_err("in 3 days at 25:00", &cfg);
        assert_err("tomorrow at 25:00", &cfg);
        assert_err("tomorrow at 99:00", &cfg);
        assert_rfc("in 3 days at 14:00", "2025-01-18T14:00:00Z", &cfg);
        assert_rfc("at 14:00", "2025-01-15T14:00:00Z", &cfg);
    }

    #[test]
    fn relative_cfg_false_disables_relative_phrases() {
        let cfg = ParseCfg {
            relative: false,
            ref_time: Some(Dt::from_ymd(2025, 1, 15, Scale::UTC, 12, 0, 0, 0)),
            ..Default::default()
        };
        assert_err("tomorrow", &cfg);
        assert_err("in 3 days", &cfg);
        // Absolute dates still parse.
        assert_rfc("2024-03-15", "2024-03-15T00:00:00Z", &cfg);
    }

    // ── 8. Scales & leap seconds ───────────────────────────────────────────

    #[test]
    fn scale_suffixes_and_leap_seconds() {
        let cfg = def();
        // Scale suffix is recognized; RFC3339 view is the civil form on that
        // scale projected through the usual conversion path.
        for s in [
            "2024-03-15T12:00:00 TAI",
            "2024-03-15T12:00:00 TT",
            "2024-03-15T12:00:00 UTC",
            "2024-03-15T12:00:00 GPS",
        ] {
            assert!(
                Dt::from_str_parse(s, &cfg).is_ok(),
                "scale suffix should parse: {s}"
            );
        }

        // Real positive leap-second instants.
        assert_rfc("2015-06-30T23:59:60", "2015-06-30T23:59:60Z", &cfg);
        assert_rfc("1972-06-30T23:59:60", "1972-06-30T23:59:60Z", &cfg);
        assert_rfc("2016-12-31T23:59:60", "2016-12-31T23:59:60Z", &cfg);
        assert_err("2015-06-30T23:59:61", &cfg);
        // CHARACTERIZATION: `23:59:60` is accepted even on days that are not
        // historical leap-second insertions (lenient leap-second slot).
        assert_rfc("2012-06-30T23:59:60", "2012-06-30T23:59:60Z", &cfg);
    }

    // ── 9. Separators, unicode, fullwidth ──────────────────────────────────

    #[test]
    fn separators_and_unicode_digits() {
        let cfg = def();
        // Leading/trailing ASCII whitespace is fine.
        assert_rfc("  2024-03-15  ", "2024-03-15T00:00:00Z", &cfg);
        // Unicode dashes used as separators.
        assert_rfc("2024\u{2010}03\u{2010}15", "2024-03-15T00:00:00Z", &cfg); // hyphen
        assert_rfc("2024\u{2013}03\u{2013}15", "2024-03-15T00:00:00Z", &cfg); // en-dash
        assert_rfc("2024\u{2014}03\u{2014}15", "2024-03-15T00:00:00Z", &cfg); // em-dash
        assert_rfc("2024_03_15", "2024-03-15T00:00:00Z", &cfg);
        assert_rfc("2024,03,15", "2024-03-15T00:00:00Z", &cfg);
        // Fullwidth digits.
        assert_rfc("2024-03-15", "2024-03-15T00:00:00Z", &cfg);
        assert_rfc("15/03/2024", "2024-03-15T00:00:00Z", &cfg);
        // Japanese calendar units.
        assert_rfc("2024年3月15日", "2024-03-15T00:00:00Z", &cfg);
        // Zero-width space prefix is tolerated.
        assert_rfc("\u{200b}2024-03-15", "2024-03-15T00:00:00Z", &cfg);
        // Tab between date and time is treated like a separator (normalised
        // to a hyphen in classify) and the time is still recovered.
        assert_rfc("2024-03-15\t14:30", "2024-03-15T14:30:00Z", &cfg);
    }

    // ── 10. Compact datetime layouts ───────────────────────────────────────

    #[test]
    fn compact_datetime_layouts() {
        let cfg = def();
        let cases = [
            ("20240315143045", "2024-03-15T14:30:45Z"),
            ("240315143045", "2024-03-15T14:30:45Z"),
            ("20240315 143045", "2024-03-15T14:30:45Z"),
            ("2024-03-15 1430", "2024-03-15T14:30:00Z"),
            ("20240315T14:30:45", "2024-03-15T14:30:45Z"),
            ("2024-03-15T143045", "2024-03-15T14:30:45Z"),
            ("2024031514:30:45", "2024-03-15T14:30:45Z"),
            ("2024-03", "2024-03-01T00:00:00Z"),
            ("202403", "2024-03-01T00:00:00Z"),
            ("2024/03", "2024-03-01T00:00:00Z"),
            ("03/2024", "2024-03-01T00:00:00Z"),
        ];
        for (input, expected) in cases {
            assert_rfc(input, expected, &cfg);
        }
    }

    // ── 11. HTTP / RFC 2822-ish ────────────────────────────────────────────

    #[test]
    fn http_and_rfc2822_style() {
        let cfg = def();
        assert_rfc(
            "Thu, 14 Mar 2024 15:30:45 GMT",
            "2024-03-14T15:30:45Z",
            &cfg,
        );
        assert_rfc(
            "Thu, 14 Mar 2024 15:30:45 +0000",
            "2024-03-14T15:30:45Z",
            &cfg,
        );
        assert_rfc("14 Mar 2024 15:30:45 GMT", "2024-03-14T15:30:45Z", &cfg);
    }

    // ── 12. Adversarial / lenient garbage ──────────────────────────────────

    #[test]
    fn adversarial_hard_rejects() {
        let cfg = ref_cfg();
        for s in [
            "not a date",
            "null",
            "NaN",
            "Infinity",
            "yes",
            "no",
            "true",
            "false",
            "----",
            "Mar",
            "March",
            "Monday",
            "1/2/3/4",
            "0x2024",
            "1e9",
            "123abc",
            "999999999999999999999999999999",
        ] {
            assert_err(s, &cfg);
        }
    }

    #[test]
    fn adversarial_lenient_accepts() {
        let cfg = ref_cfg();
        // ISO-ish time with leading T is treated as time-of-day on ref date.
        assert_rfc("T14:30:00", "2025-01-15T14:30:00Z", &cfg);
        // CHARACTERIZATION: trailing / leading non-date tokens are stripped
        // when a valid date remains.
        assert_rfc("2024-03-15T14:30:00Z garbage", "2024-03-15T14:30:00Z", &cfg);
        assert_rfc("garbage 2024-03-15", "2024-03-15T00:00:00Z", &cfg);
        // Trailing T connector with no time still yields midnight.
        assert_rfc("2024-03-15T", "2024-03-15T00:00:00Z", &cfg);
        // Quotes / brackets / parens around a date are ignored.
        assert_rfc("\"2024-03-15\"", "2024-03-15T00:00:00Z", &cfg);
        assert_rfc("'2024-03-15'", "2024-03-15T00:00:00Z", &cfg);
        assert_rfc("[2024-03-15]", "2024-03-15T00:00:00Z", &cfg);
        assert_rfc("(2024-03-15)", "2024-03-15T00:00:00Z", &cfg);
        // CHARACTERIZATION: alphabetic noise + digits can fall through to a
        // pure-numeric unix-seconds path (abc123 → 123 seconds past epoch).
        assert_rfc("abc123", "1970-01-01T00:02:03Z", &cfg);
        // CHARACTERIZATION: scientific-notation-looking tokens can be
        // reinterpreted as date pieces (2.5e10 → 2010-05-02).
        assert_rfc("2.5e10", "2010-05-02T00:00:00Z", &cfg);
    }

    #[test]
    fn iana_zone_bracket_requires_tz_feature() {
        let cfg = def();
        // Without jiff-tz*, bracketed IANA zones are not applied / rejected.
        #[cfg(not(any(feature = "jiff-tz", feature = "jiff-tz-bundle")))]
        {
            assert_err("2024-03-15T14:30:00[Europe/Paris]", &cfg);
            assert_err("2024-03-15T14:30:00+01:00[Europe/Paris]", &cfg);
        }
        #[cfg(any(feature = "jiff-tz", feature = "jiff-tz-bundle"))]
        {
            // With TZ support the zoned form should at least parse.
            assert!(
                Dt::from_str_parse("2024-03-15T14:30:00+01:00[Europe/Paris]", &cfg).is_ok()
                    || Dt::from_str_parse("2024-03-15T14:30:00[Europe/Paris]", &cfg).is_ok()
            );
        }
    }

    // ── 13. ParseCfg knobs ─────────────────────────────────────────────────

    #[test]
    fn explicit_mode_only_tries_listed_formats() {
        let cfg = ParseCfg {
            mode: Mode::Explicit,
            parse: Some(vec!["%Y-%m-%d".into()]),
            ..Default::default()
        };
        assert_rfc("2024-03-15", "2024-03-15T00:00:00Z", &cfg);
        // Completely different layout is rejected (no Auto fallback).
        assert_err("15/03/2024", &cfg);
        assert_err("March 15, 2024", &cfg);
        // CHARACTERIZATION: `%Y-%m-%d` succeeds and the trailing time is
        // ignored / absorbed by the lower-level `from_str` path, so this
        // does *not* hard-fail under Explicit.
        assert_rfc("2024-03-15 12:00", "2024-03-15T00:00:00Z", &cfg);
    }

    #[test]
    fn explicit_formats_then_fallback_when_not_explicit_mode() {
        let cfg = ParseCfg {
            mode: Mode::Auto,
            parse: Some(vec!["%Y-%m-%d".into()]),
            ..Default::default()
        };
        // Listed format works.
        assert_rfc("2024-03-15", "2024-03-15T00:00:00Z", &cfg);
        // Fallback still handles other layouts.
        assert_rfc("15/03/2024", "2024-03-15T00:00:00Z", &cfg);
    }

    #[test]
    fn to_lower_false_requires_already_lowercase_names() {
        let cfg = ParseCfg {
            to_lower: false,
            ..Default::default()
        };
        // Numeric ISO is case-insensitive in practice.
        assert_rfc("2024-03-15", "2024-03-15T00:00:00Z", &cfg);
        // Title-case month names fail when to_lower is off.
        assert_err("March 15, 2024", &cfg);
        assert_rfc("march 15, 2024", "2024-03-15T00:00:00Z", &cfg);
    }

    // ── 14. Helper wrappers ────────────────────────────────────────────────

    #[test]
    fn str_to_helpers_agree_with_from_str_parse() {
        let cfg = def();
        let s = "2024-03-15T12:00:00Z";
        let dt = parse(s, &cfg);

        assert_eq!(Dt::str_to_attos(s, &cfg), Some(dt.to_attos()));
        assert_eq!(Dt::str_to_ms(s, &cfg), Some(dt.to_ms().0));
        assert_eq!(Dt::str_to_ns(s, &cfg), Some(dt.to_ns().0));
        assert_eq!(
            Dt::str_to_unix_ms(s, &cfg),
            Some(dt.to_scale_and_diff(Dt::UNIX_EPOCH, false).to_ms().0)
        );
        assert_eq!(
            Dt::str_to_unix_ns(s, &cfg),
            Some(dt.to_scale_and_diff(Dt::UNIX_EPOCH, false).to_ns().0)
        );

        assert_eq!(Dt::str_to_attos("not-a-date", &cfg), None);
        assert_eq!(Dt::str_to_unix_ms("not-a-date", &cfg), None);
    }

    // ── 15. Round-trip smoke ───────────────────────────────────────────────

    #[test]
    fn rfc3339_roundtrip_smoke() {
        let cfg = def();
        for s in [
            "2024-03-15T00:00:00Z",
            "2024-03-15T14:30:45.123456789Z",
            "1970-01-01T00:00:00Z",
            "2000-01-01T12:00:00Z",
            "-0001-06-15T00:00:00Z",
        ] {
            let dt = parse(s, &cfg);
            let again = dt.to_str_rfc3339_nf(9);
            let dt2 = parse(&again, &cfg);
            assert_eq!(dt.to_attos(), dt2.to_attos(), "roundtrip {s} → {again}");
        }
    }

    // ── 16. Smart order matrix on classic pitfalls ─────────────────────────

    #[test]
    fn smart_order_matrix_classic_pitfalls() {
        // input → Smart, Day, Month, Year (prefer + fallback).
        let rows: &[(&str, &str, &str, &str, &str)] = &[
            (
                "01/02/2003",
                "2003-02-01T00:00:00Z",
                "2003-02-01T00:00:00Z",
                "2003-01-02T00:00:00Z",
                "2003-02-01T00:00:00Z",
            ),
            (
                "02/01/2003",
                "2003-01-02T00:00:00Z",
                "2003-01-02T00:00:00Z",
                "2003-02-01T00:00:00Z",
                "2003-01-02T00:00:00Z",
            ),
            (
                "12/11/10",
                "2010-11-12T00:00:00Z",
                "2010-11-12T00:00:00Z",
                "2010-12-11T00:00:00Z",
                "2012-11-10T00:00:00Z",
            ),
            (
                "15.03.24",
                "2024-03-15T00:00:00Z",
                "2024-03-15T00:00:00Z",
                "2024-03-15T00:00:00Z",
                "2015-03-24T00:00:00Z",
            ),
        ];

        for &(input, smart, day, month, year) in rows {
            for (order, expected) in [
                (Order::Smart, smart),
                (Order::Day, day),
                (Order::Month, month),
                (Order::Year, year),
            ] {
                assert_rfc(input, expected, &cfg_order(order));
            }
        }
    }
}