deep-time 0.1.0-beta.32

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
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
//! 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
//! - safety (digit-counter overflow at STRTIME_SIZE)
//! - Aho-Corasick substring false positives (month/relative inside words)
//! - relative+absolute digit glue, dual dates, invalid compact→unix fallback
//!
//! 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));
            }
        }
    }

    // ── 17. Safety: digit-counter overflow (STRTIME_SIZE allows ≤512 digits) ─

    /// Regression: `num_digits: u8` used to overflow and panic in debug at 256
    /// digits (and wrap in release). Must never panic for inputs ≤ STRTIME_SIZE.
    #[test]
    fn digit_counter_no_panic_at_strtime_limit() {
        let cfg = def();
        for n in [255usize, 256, 300, 400, 512] {
            let s = "1".repeat(n);
            // Must not panic; Ok or Err are both acceptable outcomes.
            let _ = Dt::from_str_parse(&s, &cfg);
        }
        // Slash-separated digit groups that also push digit count past 255.
        let heavy = format!("1{}", "/2".repeat(200));
        assert!(heavy.len() <= 512, "fixture must fit STRTIME_SIZE");
        let _ = Dt::from_str_parse(&heavy, &cfg);
        // Oversize still rejected by the length guard (no classify path).
        assert_err_kind(&"1".repeat(513), &cfg, DtErrKind::InvalidLen);
    }

    // ── 18. Dictionary hits require alphabetic standalone neighbors ────────

    /// Mid-word dictionary hits are ignored (same rule as the old am/pm guard).
    /// Digit/punct neighbors stay allowed (`14Mar2024`, `2pm`).
    #[test]
    fn substring_token_false_positives_rejected() {
        let cfg = ref_cfg();

        // Mid-word month ignored; remaining absolute date can still parse.
        assert_rfc("junk 2024-03-15", "2024-03-15T00:00:00Z", &cfg);
        assert_rfc("decorate 2024-03-15", "2024-03-15T00:00:00Z", &cfg);
        // Not April/May/August — only the year survives.
        assert_rfc("aprilfool 2024", "2024-01-01T00:00:00Z", &cfg);
        assert_rfc("mayhem 2024", "2024-01-01T00:00:00Z", &cfg);
        assert_rfc("augustine 2024", "2024-01-01T00:00:00Z", &cfg);
        assert_err("marching 15 2024", &cfg);

        assert_err("knowledge", &cfg);
        assert_err("snow", &cfg);
        assert_err("unknown", &cfg);
        assert_err("wagon", &cfg);
        // Not tomorrow/sun/mon — year only once mid-word hit is dropped.
        assert_rfc("tomorrowland 2024", "2024-01-01T00:00:00Z", &cfg);
        assert_rfc("sunflower 2024", "2024-01-01T00:00:00Z", &cfg);
        assert_rfc("monster 2024", "2024-01-01T00:00:00Z", &cfg);
        assert_err("saturn 15 2024", &cfg);

        assert_err("america", &cfg);
        assert_rfc("12am", "2025-01-15T00:00:00Z", &cfg);
        assert_rfc("14 Mar 2024 2pm", "2024-03-14T14:00:00Z", &cfg);

        assert_rfc("jun 2024", "2024-06-01T00:00:00Z", &cfg);
        assert_rfc("June 2024", "2024-06-01T00:00:00Z", &cfg);
        assert_rfc("Sept 15 2024", "2024-09-15T00:00:00Z", &cfg);
        assert_rfc("Sept 2024", "2024-09-01T00:00:00Z", &cfg);
        assert_rfc("tomorrow", "2025-01-16T12:00:00Z", &cfg);
        assert_rfc("coming Friday", "2025-01-17T12:00:00Z", &cfg);
        assert_rfc("14Mar2024", "2024-03-14T00:00:00Z", &cfg);
        assert_rfc("15-Mar-2024", "2024-03-15T00:00:00Z", &cfg);
    }

    // ── 19. Relative parser: glued absolute dates → multi-millennium offsets ─

    /// Leftover bare numbers default to **days**. Hyphenated ISO dates glue to
    /// a single huge day count (`2024-03-15` → 20_240_315 days).
    #[test]
    fn relative_plus_absolute_date_blows_up_years() {
        let cfg = ref_cfg(); // 2025-01-15
        // tomorrow + 20240315 days ≈ year 57441.
        assert_rfc("tomorrow 2024-03-15", "57441-02-22T12:00:00Z", &cfg);
        assert_rfc("in 3 days 2024-03-15", "57441-02-24T12:00:00Z", &cfg);
        assert_rfc("next Monday 2024-03-15", "57441-02-26T12:00:00Z", &cfg);
        // "now" inside "noway" is not standalone → no relative early-out;
        // remaining digits parse as an absolute date (or fail). Mid-word
        // relative no longer produces the multi-millennium offset.
        assert_rfc("noway 2024-03-15", "2024-03-15T00:00:00Z", &cfg);
        // Absolute-first still wins (relative tail ignored / stripped).
        assert_rfc("2024-03-15 tomorrow", "2024-03-15T00:00:00Z", &cfg);
        // Relative + civil clock is fine.
        assert_rfc("tomorrow 15:00", "2025-01-16T15:00:00Z", &cfg);
        // Bare "N days" is intentional relative duration.
        assert_rfc("3 days", "2025-01-18T12:00:00Z", &cfg);
    }

    // ── 20. Dual absolute dates / second date eaten as time ────────────────

    #[test]
    fn dual_absolute_dates_partially_reinterpreted() {
        let cfg = def();
        // Second ISO date partially consumed as time fields.
        // CHARACTERIZATION: not an error.
        assert_rfc("2024-03-15 2025-04-16", "2024-03-16T12:25:00Z", &cfg);
        assert_rfc("15/03/2024 16/04/2025", "2024-03-15T16:00:00Z", &cfg);
        // Two named months is hard-rejected.
        assert_err("March 15 2024 March 16 2025", &cfg);
    }

    // ── 21. Invalid compact forms silently become Unix timestamps ──────────

    #[test]
    fn invalid_compact_numeric_falls_through_to_unix() {
        let cfg = def();
        // Impossible civil compact dates are not hard-errors under Mode::Auto;
        // they fall through to the pure-numeric unix path.
        assert_rfc("20241315", "1970-08-23T06:35:15Z", &cfg); // month 13
        assert_rfc("20240230", "1970-08-23T06:17:10Z", &cfg); // Feb 30
        assert_rfc("99999999", "1973-03-03T09:46:39Z", &cfg);
        // Compact datetime with hour 24 → ms unix, not civil reject.
        assert_rfc("20240315249999", "2611-05-23T21:47:29.999Z", &cfg);
        assert_rfc("20240315243000", "2611-05-23T21:47:23Z", &cfg);
        // 6-digit invalid YYMMDD likewise.
        assert_rfc("123456", "1970-01-02T10:17:36Z", &cfg);
        assert_rfc("991332", "1970-01-12T11:22:12Z", &cfg);
    }

    // ── 22. Mode::Explicit with empty / missing format list ────────────────

    #[test]
    fn explicit_mode_empty_parse_list_falls_through() {
        // CHARACTERIZATION: empty or missing `parse` does *not* force failure;
        // the Auto path still runs. Only a non-empty list that fails hard-stops.
        let empty = ParseCfg {
            mode: Mode::Explicit,
            parse: Some(vec![]),
            ..Default::default()
        };
        assert_rfc("15/03/2024", "2024-03-15T00:00:00Z", &empty);

        let none = ParseCfg {
            mode: Mode::Explicit,
            parse: None,
            ..Default::default()
        };
        assert_rfc("15/03/2024", "2024-03-15T00:00:00Z", &none);

        // Non-empty list that cannot match → hard fail (no Auto fallback).
        let ymd_only = ParseCfg {
            mode: Mode::Explicit,
            parse: Some(vec!["%Y-%m-%d".into()]),
            ..Default::default()
        };
        assert_rfc("2024-03-15", "2024-03-15T00:00:00Z", &ymd_only);
        assert_err("15/03/2024", &ymd_only);
    }

    /// Explicit `%Y` / `%Y-%m` must default missing month/day to 1.
    ///
    /// Regression: the `parse` path used `allow_partial_date = false`, so
    /// year-only formats failed with `Incomplete` even though generated
    /// candidates (and the intended Explicit UX) allow partial dates.
    #[test]
    fn explicit_partial_date_formats_default_missing_fields() {
        let only_year = ParseCfg {
            mode: Mode::Explicit,
            parse: Some(vec!["%Y".into()]),
            ..Default::default()
        };
        assert_rfc("2024", "2024-01-01T00:00:00Z", &only_year);
        assert_rfc("0001", "0001-01-01T00:00:00Z", &only_year);
        assert_rfc("9999", "9999-01-01T00:00:00Z", &only_year);
        // Trailing junk after a complete `%Y` match is still accepted
        // (`fmt_can_end_before_inp`); month/day stay defaulted.
        assert_rfc("2024-03-15", "2024-01-01T00:00:00Z", &only_year);

        let year_month = ParseCfg {
            mode: Mode::Explicit,
            parse: Some(vec!["%Y-%m".into()]),
            ..Default::default()
        };
        assert_rfc("2024-03", "2024-03-01T00:00:00Z", &year_month);
        assert_rfc("2024-12", "2024-12-01T00:00:00Z", &year_month);
        assert_err("15/03/2024", &year_month);

        let two_digit = ParseCfg {
            mode: Mode::Explicit,
            parse: Some(vec!["%y".into()]),
            ..Default::default()
        };
        assert_rfc("24", "2024-01-01T00:00:00Z", &two_digit);
        assert_rfc("99", "1999-01-01T00:00:00Z", &two_digit);

        // Multiple formats: first match wins; partial year still usable.
        let multi = ParseCfg {
            mode: Mode::Explicit,
            parse: Some(vec!["%d/%m/%Y".into(), "%Y".into()]),
            ..Default::default()
        };
        assert_rfc("15/03/2024", "2024-03-15T00:00:00Z", &multi);
        assert_rfc("2024", "2024-01-01T00:00:00Z", &multi);
    }

    // ── 23. Garbage embedding / prefix stripping ───────────────────────────

    #[test]
    fn embedded_date_in_noise_is_accepted() {
        let cfg = def();
        // CHARACTERIZATION: surrounding non-date text is often ignored.
        // Do **not** use from_str_parse as a strict validator for untrusted input.
        assert_rfc("2024-03-15'; DROP TABLE", "2024-03-15T00:00:00Z", &cfg);
        assert_rfc("../../../2024-03-15", "2024-03-15T00:00:00Z", &cfg);
        assert_rfc("{\"date\":\"2024-03-15\"}", "2024-03-15T00:00:00Z", &cfg);
        assert_rfc("<time>2024-03-15</time>", "2024-03-15T00:00:00Z", &cfg);
        assert_rfc("\"2024-03-15\"", "2024-03-15T00:00:00Z", &cfg);
        // Trailing alphabetic glued to date is also stripped.
        assert_rfc("2024-03-15x", "2024-03-15T00:00:00Z", &cfg);
        assert_rfc("x2024-03-15", "2024-03-15T00:00:00Z", &cfg);
        // Trailing Z without time is accepted (Zulu-ish).
        assert_rfc("2024-03-15Z", "2024-03-15T00:00:00Z", &cfg);
        assert_rfc("2024-03-15 junk", "2024-03-15T00:00:00Z", &cfg);
        assert_rfc("garbage 2024-03-15", "2024-03-15T00:00:00Z", &cfg);
    }

    // ── 24. Offset extremes, 12h bounds, leap-second clamp ─────────────────

    #[test]
    fn offset_and_clock_edge_cases() {
        let cfg = def();
        // Offsets well outside civil TZ ranges still apply.
        assert_rfc("2024-03-15T12:00:00+15:00", "2024-03-14T21:00:00Z", &cfg);
        assert_rfc("2024-03-15T12:00:00-13:00", "2024-03-16T01:00:00Z", &cfg);
        assert_rfc("2024-03-15T12:00:00+23:59", "2024-03-14T12:01:00Z", &cfg);
        assert_err("2024-03-15T12:00:00+99:00", &cfg);
        // Hour-only offset (no minutes).
        assert_rfc("2024-03-15T12:00:00+01", "2024-03-15T11:00:00Z", &cfg);

        // Invalid 12-hour hours hard-fail.
        assert_err("2024-03-15 0am", &cfg);
        assert_err("2024-03-15 13pm", &cfg);
        assert_err("2024-03-15 24am", &cfg);
        // 00:00am is accepted as midnight.
        assert_rfc("2024-03-15 00:00am", "2024-03-15T00:00:00Z", &cfg);

        // ISO 24:00 end-of-day is rejected (unlike bare ops "24:00" relative).
        assert_err("2024-03-15T24:00:00Z", &cfg);
    }

    #[test]
    fn leap_second_slot_only_at_day_boundary_on_leap_days() {
        let cfg = def();
        // Real leap-second insertion days keep second=60.
        assert_rfc("2015-06-30T23:59:60Z", "2015-06-30T23:59:60Z", &cfg);
        assert_rfc("2012-06-30T23:59:60Z", "2012-06-30T23:59:60Z", &cfg);
        // Non-leap days clamp 23:59:60 → 23:59:59 (not hard-fail).
        assert_rfc("2024-01-01T23:59:60Z", "2024-01-01T23:59:59Z", &cfg);
        assert_rfc("2024-03-15T23:59:60Z", "2024-03-15T23:59:59Z", &cfg);
        // Mid-day :60 always clamps (never a leap-second site).
        assert_rfc("2024-03-15T12:00:60Z", "2024-03-15T12:00:59Z", &cfg);
    }

    // ── 25. Unicode / control tolerance ────────────────────────────────────

    #[test]
    fn control_and_bidi_prefix_tolerance() {
        let cfg = def();
        // Null / CR-LF / BOM / bidi marks do not block a trailing date.
        assert_rfc("2024-03-15\0", "2024-03-15T00:00:00Z", &cfg);
        assert_rfc("2024-03-15\r\n", "2024-03-15T00:00:00Z", &cfg);
        assert_rfc("\u{feff}2024-03-15", "2024-03-15T00:00:00Z", &cfg);
        assert_rfc("2024\u{202e}03-15", "2024-03-15T00:00:00Z", &cfg);
        assert_rfc("\u{200f}2024-03-15", "2024-03-15T00:00:00Z", &cfg);
        // NBSP and em-space act as separators.
        assert_rfc("2024-03-15\u{00a0}12:00", "2024-03-15T12:00:00Z", &cfg);
        assert_rfc("2024\u{2003}03\u{2003}15", "2024-03-15T00:00:00Z", &cfg);
    }

    // ── 26. Pure-numeric unit mis-detection edges ──────────────────────────

    #[test]
    fn pure_numeric_digit_length_unit_edges() {
        let auto = cfg_mode(Mode::Auto);
        // 9-digit: not 8-digit YYYYMMDD, not 10-digit unix → hour glued to date.
        assert_rfc("202403151", "2024-03-15T01:00:00Z", &auto);
        // 10-digit unix seconds (1e10) → far future.
        assert_rfc("10000000000", "2286-11-20T17:46:40Z", &auto);
        // 11-digit still treated as seconds (not ms).
        assert_rfc("17356896000", "2520-01-08T00:00:00Z", &auto);
        // 12-digit → milliseconds path.
        assert_rfc("100000000000", "1973-03-03T09:46:40Z", &auto);
        // 19-digit → nanoseconds path (i64-max-ish).
        assert_rfc(
            "9223372036854775807",
            "2262-04-11T23:47:16.854775807Z",
            &auto,
        );
        // 2-digit year pivot boundary.
        assert_rfc("68", "2068-01-01T00:00:00Z", &auto);
        assert_rfc("69", "1969-01-01T00:00:00Z", &auto);
        assert_rfc("00", "2000-01-01T00:00:00Z", &auto);
        // Signed year / date.
        assert_rfc("+2024", "2024-01-01T00:00:00Z", &auto);
        assert_rfc("+2024-03-15", "2024-03-15T00:00:00Z", &auto);
        // Very large negative pure-numeric still yields a Dt (far past).
        let neg = format!("-{}", "9".repeat(25));
        let dt = parse(&neg, &auto);
        assert!(dt.to_ymd().yr() < -9999);
    }

    // ── 27. relative=false disables bare TOD as well as phrases ────────────

    #[test]
    fn relative_false_also_blocks_bare_tod() {
        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);
        assert_err("14:00", &cfg);
        assert_err("9am", &cfg);
        assert_err("week", &cfg);
        assert_rfc("2024-03-15", "2024-03-15T00:00:00Z", &cfg);
    }
}