sley-core 0.8.0

Core Git primitives for the sley engine: object ids, errors, repo paths, and shared value types.
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
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
//! A faithful port of the subset of git's `date.c` approxidate machinery.
//!
//! Canonical home (moved from sley-cli's `commands/approxidate.rs`): rev's
//! cutoff parsing and several CLI expiry paths need this without a
//! presentation-tier dependency. The cli module re-exports these entry points
//! as a thin shim, so all callers keep their original paths.
//!
//! git parses an expiry-date string with `parse_expiry_date`, which special-cases
//! `never`/`false` (→ 0) and `all`/`now` (→ `TIME_MAX`) and otherwise hands the
//! string to `approxidate_careful`. That routine first tries the strict-ish
//! absolute parser (`parse_date_basic`) and, failing that, falls back to the
//! fuzzy relative/partial parser (`approxidate_str`). We port both paths so that
//! `--type=expiry-date` accepts exactly what git accepts and emits the same
//! canonical Unix timestamp.
//!
//! Timezone handling: git uses the process-local timezone (`mktime`/`localtime_r`)
//! when a date carries no explicit offset. sley's existing date code (e.g. the
//! RFC2822 parser in `am.rs` and the log `--since`/`--until` cutoff parser) does
//! pure UTC civil-date arithmetic; we follow that convention here. Under the
//! upstream test harness (which exports `TZ=UTC`) this matches git bit-for-bit.
//!
//! Several routines mirror git's C control flow line-for-line (explicit
//! `x < lo || x > hi` bound checks, nested `if isdigit(...)` guards inside a
//! separator `match`, `tm_hour % 12` assignments). Keeping that shape makes the
//! port auditable against `date.c`, so the corresponding readability lints are
//! allowed module-wide rather than rewritten away from the source.
#![allow(
    clippy::manual_range_contains,
    clippy::collapsible_if,
    clippy::collapsible_match,
    clippy::assign_op_pattern
)]

use super::{civil_from_days, days_from_civil};
/// git's broken-down time. Unset fields carry the sentinel `-1`, exactly as in
/// git's `struct tm` usage in `date.c`.
#[derive(Clone, Copy)]
struct Tm {
    sec: i64,
    min: i64,
    hour: i64,
    mday: i64,
    mon: i64,
    year: i64, // years since 1900, mirroring `tm_year`
    wday: i64,
}

impl Tm {
    fn unset() -> Self {
        Tm {
            sec: -1,
            min: -1,
            hour: -1,
            mday: -1,
            mon: -1,
            year: -1,
            wday: -1,
        }
    }
}

const MONTH_NAMES: [&str; 12] = [
    "January",
    "February",
    "March",
    "April",
    "May",
    "June",
    "July",
    "August",
    "September",
    "October",
    "November",
    "December",
];

const WEEKDAY_NAMES: [&str; 7] = [
    "Sundays",
    "Mondays",
    "Tuesdays",
    "Wednesdays",
    "Thursdays",
    "Fridays",
    "Saturdays",
];

/// (name, offset-hours-east, dst) — git's `timezone_names` table.
const TIMEZONE_NAMES: &[(&str, i64, i64)] = &[
    ("IDLW", -12, 0),
    ("NT", -11, 0),
    ("CAT", -10, 0),
    ("HST", -10, 0),
    ("YST", -9, 0),
    ("YDT", -9, 1),
    ("PST", -8, 0),
    ("PDT", -8, 1),
    ("MST", -7, 0),
    ("MDT", -7, 1),
    ("CST", -6, 0),
    ("CDT", -6, 1),
    ("EST", -5, 0),
    ("EDT", -5, 1),
    ("AST", -3, 0),
    ("ADT", -3, 1),
    ("WAT", -1, 0),
    ("GMT", 0, 0),
    ("UTC", 0, 0),
    ("Z", 0, 0),
    ("WET", 0, 0),
    ("BST", 0, 1),
    ("MET", 1, 0),
    ("MEST", 1, 1),
    ("MEZ", 1, 0),
    ("MESZ", 1, 1),
    ("CET", 1, 0),
    ("CEST", 1, 1),
    ("EET", 2, 0),
    ("EEST", 2, 1),
    ("MSK", 3, 0),
    ("MSD", 3, 1),
    ("CCT", 8, 0),
    ("JST", 9, 0),
    ("EAST", 10, 0),
    ("EADT", 10, 1),
    ("GST", 10, 0),
    ("NZT", 12, 0),
    ("NZST", 12, 0),
    ("NZDT", 12, 1),
    ("IDLE", 12, 0),
];

const SPECIAL_NAMES: [&str; 8] = [
    "yesterday",
    "noon",
    "midnight",
    "tea",
    "PM",
    "AM",
    "never",
    "now",
];

const NUMBER_NAMES: [&str; 11] = [
    "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten",
];

/// (type-name, seconds-per-unit) — git's `typelen` table.
const TYPELEN: [(&str, i64); 5] = [
    ("seconds", 1),
    ("minutes", 60),
    ("hours", 60 * 60),
    ("days", 24 * 60 * 60),
    ("weeks", 7 * 24 * 60 * 60),
];

const TIMESTAMP_MAX: i64 = (((2100 - 1970) * 365 + 32) * 24 * 60 * 60) - 1;

fn is_ascii_digit_byte(b: u8) -> bool {
    b.is_ascii_digit()
}

fn is_ascii_alpha_byte(b: u8) -> bool {
    b.is_ascii_alphabetic()
}

/// git's `tm_to_time_t`: like `mktime` without normalisation, treating the tm as
/// UTC. Returns `None` (git's `-1`) on out-of-range or unset time fields.
fn tm_to_time_t(tm: &Tm) -> Option<i64> {
    const MDAYS: [i64; 12] = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334];
    let year = tm.year - 70;
    let month = tm.mon;
    let mut day = tm.mday;
    if year < 0 || year > 129 {
        return None;
    }
    if month < 0 || month > 11 {
        return None;
    }
    if month < 2 || (year + 2) % 4 != 0 {
        day -= 1;
    }
    if tm.hour < 0 || tm.min < 0 || tm.sec < 0 {
        return None;
    }
    Some(
        (year * 365 + (year + 1) / 4 + MDAYS[month as usize] + day) * 24 * 60 * 60
            + tm.hour * 60 * 60
            + tm.min * 60
            + tm.sec,
    )
}

/// Break a UTC Unix time into a `Tm` (git's `localtime_r`/`gmtime_r` under UTC).
fn time_t_to_tm(time: i64) -> Tm {
    let days = time.div_euclid(86_400);
    let secs_of_day = time.rem_euclid(86_400);
    let (year, month, day) = civil_from_days(days);
    // Day of week: 1970-01-01 was a Thursday (wday 4).
    let wday = (days.rem_euclid(7) + 4).rem_euclid(7);
    Tm {
        sec: secs_of_day % 60,
        min: (secs_of_day / 60) % 60,
        hour: secs_of_day / 3600,
        mday: i64::from(day),
        mon: i64::from(month) - 1,
        year: year - 1900,
        wday,
    }
}

/// Current UTC time as a Unix timestamp.
fn now_unix() -> i64 {
    std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map(|d| d.as_secs().min(i64::MAX as u64) as i64)
        .unwrap_or(0)
}

/// git's `match_string`: case-insensitive prefix match; returns the number of
/// matched leading chars, or 0 on a non-alphanumeric mismatch.
fn match_string(date: &[u8], pat: &str) -> usize {
    // git's `match_string`: walk both strings in lockstep. A longer alnum
    // continuation in `date` after `pat` is exhausted is a mismatch (return 0)
    // so "seventeen" does not match "seven" (t7501 bogus-date cell).
    let pat = pat.as_bytes();
    let mut i = 0;
    while i < date.len() {
        let d = date[i];
        if i < pat.len() {
            let p = pat[i];
            if d.eq_ignore_ascii_case(&p) {
                i += 1;
                continue;
            }
            // Mismatch: non-alnum ends the match; alnum fails entirely.
            if !d.is_ascii_alphanumeric() {
                break;
            }
            return 0;
        }
        // Pattern exhausted: alnum continuation is a non-match (git returns 0).
        if d.is_ascii_alphanumeric() {
            return 0;
        }
        break;
    }
    i
}

fn skip_alpha(date: &[u8]) -> usize {
    let mut i = 1;
    while i < date.len() && is_ascii_alpha_byte(date[i]) {
        i += 1;
    }
    i
}

/// git's `set_time`: validate and store H:M:S. Returns Ok on success.
fn set_time(hour: i64, minute: i64, second: i64, tm: &mut Tm) -> Result<(), ()> {
    if (0..=24).contains(&hour) && (0..60).contains(&minute) && (0..=60).contains(&second) {
        tm.hour = hour;
        tm.min = minute;
        tm.sec = second;
        Ok(())
    } else {
        Err(())
    }
}

/// git's `set_date` for the absolute path. `now_tm`/`now` enable the future-date
/// refusal heuristic; pass `None` to disable it (the `num > 70` branch).
fn set_date(
    year: i64,
    month: i64,
    day: i64,
    now_tm: Option<&Tm>,
    now: i64,
    tm: &mut Tm,
) -> Result<bool, ()> {
    // Returns Ok(true) when set_date returned git's `1` (year unknown, no now_tm),
    // Ok(false) on success (git's `0`), Err on git's `-1`.
    if (1..13).contains(&month) && (1..32).contains(&day) {
        let mut r = *tm;
        let use_check = now_tm.is_some();
        r.mon = month - 1;
        r.mday = day;
        if year == -1 {
            match now_tm {
                None => return Ok(true),
                Some(now_tm) => r.year = now_tm.year,
            }
        } else if (1970..2100).contains(&year) {
            r.year = year - 1900;
        } else if (71..100).contains(&year) {
            r.year = year;
        } else if year < 38 {
            r.year = year + 100;
        } else {
            return Err(());
        }
        if !use_check {
            tm.mon = r.mon;
            tm.mday = r.mday;
            if year != -1 {
                tm.year = r.year;
            }
            return Ok(false);
        }
        let specified = tm_to_time_t(&r);
        if let Some(specified) = specified {
            if now + 10 * 24 * 3600 < specified {
                return Err(());
            }
        }
        tm.mon = r.mon;
        tm.mday = r.mday;
        if year != -1 {
            tm.year = r.year;
        }
        Ok(false)
    } else {
        Err(())
    }
}

fn is_date_known(tm: &Tm) -> bool {
    tm.year != -1 && tm.mon != -1 && tm.mday != -1
}

/// git's `match_multi_number`. `date`/`end` are byte offsets into the full
/// string; returns the number of consumed bytes (0 = no match).
fn match_multi_number(
    num: i64,
    c: u8,
    full: &[u8],
    start_off: usize,
    tm: &mut Tm,
    now: i64,
) -> usize {
    // `start_off` is the index of the separator `c` in `full`; git's `end`
    // pointer sits there and num2 is parsed from end+1.
    let (num2, mut cursor) = parse_long(full, start_off + 1);
    let mut num3: i64 = -1;
    if cursor < full.len()
        && full[cursor] == c
        && cursor + 1 < full.len()
        && is_ascii_digit_byte(full[cursor + 1])
    {
        let (n3, c3) = parse_long(full, cursor + 1);
        num3 = n3;
        cursor = c3;
    }

    let date_start = {
        // git computes `end - date`; `date` is the start of the first number.
        // We track that as the position right before the first digit run. The
        // caller passes the number's own start via `num_start` implicitly: we
        // recompute by scanning back is awkward, so the caller hands `full`
        // already sliced from the first digit. Here `0` is the first digit.
        0usize
    };
    let _ = date_start;

    match c {
        b':' => {
            let num3v = if num3 < 0 { 0 } else { num3 };
            if set_time(num, num2, num3v, tm).is_ok() {
                // Optional fractional seconds after %H:%M:%S when a date is known.
                if cursor < full.len()
                    && full[cursor] == b'.'
                    && cursor + 1 < full.len()
                    && is_ascii_digit_byte(full[cursor + 1])
                    && is_date_known(tm)
                {
                    let (_n, c4) = parse_long(full, cursor + 1);
                    cursor = c4;
                }
                cursor
            } else {
                0
            }
        }
        b'-' | b'/' | b'.' => {
            let now = if now == 0 { now_unix() } else { now };
            let now_tm = time_t_to_tm(now);
            let refuse_future = Some(&now_tm);

            if num > 70 {
                if set_date(num, num2, num3, None, now, tm).is_ok() {
                    return cursor;
                }
                if set_date(num, num3, num2, None, now, tm).is_ok() {
                    return cursor;
                }
            }
            if c != b'.' && set_date(num3, num, num2, refuse_future, now, tm).is_ok() {
                return cursor;
            }
            if set_date(num3, num2, num, refuse_future, now, tm).is_ok() {
                return cursor;
            }
            if c == b'.' && set_date(num3, num, num2, refuse_future, now, tm).is_ok() {
                return cursor;
            }
            0
        }
        _ => cursor,
    }
}

/// Parse a base-10 integer at `off`, returning (value, next-offset).
fn parse_long(s: &[u8], off: usize) -> (i64, usize) {
    let mut i = off;
    let neg = i < s.len() && s[i] == b'-';
    if neg {
        i += 1;
    }
    let mut val: i64 = 0;
    while i < s.len() && is_ascii_digit_byte(s[i]) {
        val = val.saturating_mul(10).saturating_add((s[i] - b'0') as i64);
        i += 1;
    }
    (if neg { -val } else { val }, i)
}

/// Parse an unsigned base-10 integer at `off`, returning (value, next-offset).
fn parse_uint(s: &[u8], off: usize) -> (i64, usize) {
    let mut i = off;
    let mut val: i64 = 0;
    while i < s.len() && is_ascii_digit_byte(s[i]) {
        val = val.saturating_mul(10).saturating_add((s[i] - b'0') as i64);
        i += 1;
    }
    (val, i)
}

fn nodate(tm: &Tm) -> bool {
    (tm.year & tm.mon & tm.mday & tm.hour & tm.min & tm.sec) < 0
}

fn maybeiso8601(tm: &Tm) -> bool {
    tm.hour == -1 && tm.min == 0 && tm.sec == 0
}

/// git's `match_alpha`. `date` is the remaining byte slice; returns bytes consumed.
fn match_alpha(date: &[u8], tm: &mut Tm, offset: &mut i64) -> usize {
    for (i, name) in MONTH_NAMES.iter().enumerate() {
        let m = match_string(date, name);
        if m >= 3 {
            tm.mon = i as i64;
            return m;
        }
    }
    for (i, name) in WEEKDAY_NAMES.iter().enumerate() {
        let m = match_string(date, name);
        if m >= 3 {
            tm.wday = i as i64;
            return m;
        }
    }
    for (name, off, dst) in TIMEZONE_NAMES.iter() {
        let m = match_string(date, name);
        if m >= 3 || m == name.len() {
            let off = off + dst;
            if *offset == -1 {
                *offset = 60 * off;
            }
            return m;
        }
    }
    if match_string(date, "PM") == 2 {
        tm.hour = (tm.hour % 12) + 12;
        return 2;
    }
    if match_string(date, "AM") == 2 {
        tm.hour = tm.hour % 12;
        return 2;
    }
    if date.first() == Some(&b'T')
        && date.len() > 1
        && is_ascii_digit_byte(date[1])
        && tm.hour == -1
    {
        tm.min = 0;
        tm.sec = 0;
        return 1;
    }
    skip_alpha(date)
}

/// git's `match_digit`. `date` is the remaining byte slice; returns bytes consumed.
fn match_digit(date: &[u8], tm: &mut Tm, offset: &mut i64, tm_gmt: &mut bool) -> usize {
    let (num, end) = parse_uint(date, 0);

    // Seconds since 1970 for any number with more than 8 digits.
    if num >= 100_000_000 && nodate(tm) {
        *tm = time_t_to_tm(num);
        *tm_gmt = true;
        return end;
    }

    // num[-.:/]num[same]num
    if end < date.len() {
        match date[end] {
            b':' | b'.' | b'/' | b'-' => {
                if end + 1 < date.len() && is_ascii_digit_byte(date[end + 1]) {
                    let m = match_multi_number(num, date[end], date, end, tm, 0);
                    if m != 0 {
                        return m;
                    }
                }
            }
            _ => {}
        }
    }

    // Number of consecutive digits from the start.
    let mut n = 0usize;
    while n < date.len() && is_ascii_digit_byte(date[n]) {
        n += 1;
    }

    // 8-digit YYYYmmDD or 6-digit HHMMSS.
    if n == 8 || n == 6 {
        let num1 = num / 10000;
        let num2 = (num % 10000) / 100;
        let num3 = num % 100;
        let mut cursor = end;
        if n == 8 {
            let _ = set_date(num1, num2, num3, None, now_unix(), tm);
        } else if n == 6 && set_time(num1, num2, num3, tm).is_ok() {
            if cursor < date.len()
                && date[cursor] == b'.'
                && cursor + 1 < date.len()
                && is_ascii_digit_byte(date[cursor + 1])
            {
                let (_v, c) = parse_uint(date, cursor + 1);
                cursor = c;
            }
        }
        return cursor;
    }

    // Reduced-precision ISO-8601 time: HHMM or HH.
    if maybeiso8601(tm) {
        let mut num1 = num;
        let mut num2 = 0;
        if n == 4 {
            num1 = num / 100;
            num2 = num % 100;
        }
        if (n == 4 || n == 2) && !nodate(tm) && set_time(num1, num2, 0, tm).is_ok() {
            return n;
        }
        tm.min = -1;
        tm.sec = -1;
    }

    // Four-digit year or timezone.
    if n == 4 {
        if num <= 1400 && *offset == -1 {
            let minutes = num % 100;
            let hours = num / 100;
            *offset = hours * 60 + minutes;
        } else if num > 1900 && num < 2100 {
            tm.year = num - 1900;
        }
        return n;
    }

    if n > 2 {
        return n;
    }

    // Day-of-month precedence for 1..31.
    if num > 0 && num < 32 && tm.mday < 0 {
        tm.mday = num;
        return n;
    }

    // Two-digit year.
    if n == 2 && tm.year < 0 {
        if num < 10 && tm.mday >= 0 {
            tm.year = num + 100;
            return n;
        }
        if num >= 70 {
            tm.year = num;
            return n;
        }
    }

    if num > 0 && num < 13 && tm.mon < 0 {
        tm.mon = num - 1;
    }

    n
}

/// git's `match_tz`. `date` starts at the sign; returns bytes consumed.
fn match_tz(date: &[u8], offp: &mut i64) -> usize {
    let (mut hour, end) = parse_uint(date, 1);
    let n = end - 1;
    let mut min = 0i64;
    let mut cursor = end;

    if n == 4 {
        min = hour % 100;
        hour /= 100;
    } else if n != 2 {
        min = 99;
    } else if cursor < date.len() && date[cursor] == b':' {
        let (m, c) = parse_uint(date, cursor + 1);
        min = m;
        if c - 1 != 5 {
            min = 99;
        }
        cursor = c;
    }

    if min < 60 && hour < 24 {
        let mut off = hour * 60 + min;
        if date[0] == b'-' {
            off = -off;
        }
        *offp = off;
    }
    cursor
}

/// git's `match_object_header_date`: parse "<stamp> +HHMM" only.
fn match_object_header_date(date: &[u8]) -> Option<(i64, i64)> {
    if date.is_empty() || !is_ascii_digit_byte(date[0]) {
        return None;
    }
    let (stamp, end) = parse_uint(date, 0);
    if end >= date.len() || date[end] != b' ' || stamp == i64::MAX {
        return None;
    }
    if end + 1 >= date.len() || (date[end + 1] != b'+' && date[end + 1] != b'-') {
        return None;
    }
    let sign_idx = end + 1;
    let (ofs_raw, ofs_end) = parse_uint(date, sign_idx + 1);
    // git requires exactly 4 digits and end at NUL or newline.
    if ofs_end - (sign_idx + 1) != 4 {
        return None;
    }
    if ofs_end != date.len() && date[ofs_end] != b'\n' {
        return None;
    }
    let mut ofs = (ofs_raw / 100) * 60 + (ofs_raw % 100);
    if date[sign_idx] == b'-' {
        ofs = -ofs;
    }
    Some((stamp, ofs))
}

/// git's `parse_date_basic`: the strict-ish absolute parser. Returns the Unix
/// timestamp on success, `None` on failure.
fn parse_date_basic(date: &[u8]) -> Option<i64> {
    parse_date_basic_full(date).map(|(timestamp, _offset)| timestamp)
}

/// git's `parse_date_basic`, returning both the absolute UTC `timestamp` and the
/// parsed timezone `offset` in minutes (git's `*offset`). Mirrors git's
/// `parse_date` shape, which a commit/tag author/committer date needs: the
/// object line stores `<utc_seconds> <+HHMM>` where the seconds are timezone-
/// normalised and the offset is carried alongside for display.
fn parse_date_basic_full(date: &[u8]) -> Option<(i64, i64)> {
    let mut tm = Tm::unset();
    let mut tm_gmt = false;
    let mut offset: i64 = -1;

    if date.first() == Some(&b'@') {
        if let Some((stamp, ofs)) = match_object_header_date(&date[1..]) {
            return Some((stamp, ofs));
        }
    }

    let mut i = 0usize;
    while i < date.len() {
        let c = date[i];
        if c == 0 || c == b'\n' {
            break;
        }
        let rest = &date[i..];
        let mut m = 0usize;
        if is_ascii_alpha_byte(c) {
            m = match_alpha(rest, &mut tm, &mut offset);
        } else if is_ascii_digit_byte(c) {
            m = match_digit(rest, &mut tm, &mut offset, &mut tm_gmt);
        } else if (c == b'-' || c == b'+') && i + 1 < date.len() && is_ascii_digit_byte(date[i + 1])
        {
            m = match_tz(rest, &mut offset);
        }
        if m == 0 {
            m = 1;
        }
        i += m;
    }

    let mut timestamp = tm_to_time_t(&tm)?;

    if offset == -1 {
        // git falls back to mktime() to derive the local offset. Under our UTC
        // convention the local offset is 0, so the timestamp stands as-is.
        offset = 0;
    }

    if !tm_gmt {
        if offset > 0 && offset * 60 > timestamp {
            return None;
        }
        if offset < 0 && -offset * 60 > TIMESTAMP_MAX - timestamp {
            return None;
        }
        timestamp -= offset * 60;
    }

    Some((timestamp, offset))
}

/// git's `update_tm`: fill in unset date fields from `now`, then apply a relative
/// `sec` offset. Uses UTC civil arithmetic in place of mktime/localtime_r.
fn update_tm(tm: &mut Tm, now: &Tm, sec: i64) -> i64 {
    if tm.mday < 0 {
        tm.mday = now.mday;
    }
    if tm.mon < 0 {
        tm.mon = now.mon;
    }
    if tm.year < 0 {
        tm.year = now.year;
        if tm.mon > now.mon {
            tm.year -= 1;
        }
    }
    // update_tm's callers guarantee mday/mon are filled from `now` (or parsed
    // into 1..=31 / 1..=12) before this arithmetic, so the casts cannot fail;
    // the fallbacks keep the function total without changing any real value.
    let month = u32::try_from(tm.mon + 1).unwrap_or(1);
    let day = u32::try_from(tm.mday).unwrap_or(1);
    let days = days_from_civil(tm.year + 1900, month, day);
    let base = days * 86_400 + tm.hour.max(0) * 3600 + tm.min.max(0) * 60 + tm.sec.max(0);
    let n = base - sec;
    *tm = time_t_to_tm(n);
    n
}

/// git's `pending_number`: assume a trailing number is a day, else month, else year.
fn pending_number(tm: &mut Tm, num: &mut i64) {
    let number = *num;
    if number != 0 {
        *num = 0;
        if tm.mday < 0 && number < 32 {
            tm.mday = number;
        } else if tm.mon < 0 && number < 13 {
            tm.mon = number - 1;
        } else if tm.year < 0 {
            if number > 1969 && number < 2100 {
                tm.year = number - 1900;
            } else if number > 69 && number < 100 {
                tm.year = number;
            } else if number < 38 {
                tm.year = 100 + number;
            }
        }
    }
}

/// git's specials dispatch (`date_yesterday`, `date_noon`, …).
fn apply_special(name: &str, tm: &mut Tm, now: &Tm, num: &mut i64) {
    let date_time = |tm: &mut Tm, now: &Tm, hour: i64| {
        if tm.hour < hour {
            update_tm(tm, now, 24 * 60 * 60);
        }
        tm.hour = hour;
        tm.min = 0;
        tm.sec = 0;
    };
    match name {
        "yesterday" => {
            *num = 0;
            update_tm(tm, now, 24 * 60 * 60);
        }
        "noon" => {
            pending_number(tm, num);
            date_time(tm, now, 12);
        }
        "midnight" => {
            pending_number(tm, num);
            date_time(tm, now, 0);
        }
        "tea" => {
            pending_number(tm, num);
            date_time(tm, now, 17);
        }
        "PM" => {
            let n = *num;
            *num = 0;
            let mut hour = tm.hour;
            if n != 0 {
                hour = n;
                tm.min = 0;
                tm.sec = 0;
            }
            tm.hour = (hour % 12) + 12;
        }
        "AM" => {
            let n = *num;
            *num = 0;
            let mut hour = tm.hour;
            if n != 0 {
                hour = n;
                tm.min = 0;
                tm.sec = 0;
            }
            tm.hour = hour % 12;
        }
        "never" => {
            *tm = time_t_to_tm(0);
            *num = 0;
        }
        "now" => {
            *num = 0;
            update_tm(tm, now, 0);
        }
        _ => {}
    }
}

/// git's `approxidate_alpha`. Returns bytes consumed.
fn approxidate_alpha(
    date: &[u8],
    tm: &mut Tm,
    now: &Tm,
    num: &mut i64,
    touched: &mut bool,
) -> usize {
    let mut end = 1usize;
    while end < date.len() && is_ascii_alpha_byte(date[end]) {
        end += 1;
    }

    for (i, name) in MONTH_NAMES.iter().enumerate() {
        if match_string(date, name) >= 3 {
            tm.mon = i as i64;
            *touched = true;
            return end;
        }
    }

    for name in SPECIAL_NAMES.iter() {
        if match_string(date, name) == name.len() {
            apply_special(name, tm, now, num);
            *touched = true;
            return end;
        }
    }

    if *num == 0 {
        for (i, name) in NUMBER_NAMES.iter().enumerate().skip(1) {
            if match_string(date, name) == name.len() {
                *num = i as i64;
                *touched = true;
                return end;
            }
        }
        if match_string(date, "last") == 4 {
            *num = 1;
            *touched = true;
        }
        return end;
    }

    for (name, length) in TYPELEN.iter() {
        let m = match_string(date, name);
        if m >= name.len() - 1 {
            update_tm(tm, now, length * *num);
            *num = 0;
            *touched = true;
            return end;
        }
    }

    for (i, name) in WEEKDAY_NAMES.iter().enumerate() {
        if match_string(date, name) >= 3 {
            let mut n = *num - 1;
            *num = 0;
            let mut diff = tm.wday - i as i64;
            if diff <= 0 {
                n += 1;
            }
            diff += 7 * n;
            update_tm(tm, now, diff * 24 * 60 * 60);
            *touched = true;
            return end;
        }
    }

    if match_string(date, "months") >= 5 {
        update_tm(tm, now, 0);
        let mut n = tm.mon - *num;
        *num = 0;
        while n < 0 {
            n += 12;
            tm.year -= 1;
        }
        tm.mon = n;
        *touched = true;
        return end;
    }

    if match_string(date, "years") >= 4 {
        update_tm(tm, now, 0);
        tm.year -= *num;
        *num = 0;
        *touched = true;
        return end;
    }

    end
}

/// git's `approxidate_digit`. `date` is the remaining slice; returns bytes consumed.
fn approxidate_digit(date: &[u8], tm: &mut Tm, num: &mut i64, now: i64) -> usize {
    let (number, end) = parse_uint(date, 0);

    if end < date.len() {
        match date[end] {
            b':' | b'.' | b'/' | b'-' => {
                if end + 1 < date.len() && is_ascii_digit_byte(date[end + 1]) {
                    let m = match_multi_number(number, date[end], date, end, tm, now);
                    if m != 0 {
                        return m;
                    }
                }
            }
            _ => {}
        }
    }

    // Accept zero-padding only for small numbers ("Dec 02", never "Dec 0002").
    if date.first() != Some(&b'0') || end <= 2 {
        *num = number;
    }
    end
}

/// git's `approxidate_str`: the fuzzy relative/partial parser.
fn approxidate_str(date: &[u8], now: i64, error_ret: &mut bool) -> i64 {
    let mut number: i64 = 0;
    let mut touched = false;
    let now_tm = time_t_to_tm(now);
    let mut tm = now_tm;
    tm.year = -1;
    tm.mon = -1;
    tm.mday = -1;

    let mut i = 0usize;
    while i < date.len() {
        let c = date[i];
        if c == 0 {
            break;
        }
        if is_ascii_digit_byte(c) {
            pending_number(&mut tm, &mut number);
            let consumed = approxidate_digit(&date[i..], &mut tm, &mut number, now);
            touched = true;
            i += consumed.max(1);
            continue;
        }
        if is_ascii_alpha_byte(c) {
            let consumed =
                approxidate_alpha(&date[i..], &mut tm, &now_tm, &mut number, &mut touched);
            i += consumed.max(1);
            continue;
        }
        i += 1;
    }
    pending_number(&mut tm, &mut number);
    if !touched {
        *error_ret = true;
    }
    update_tm(&mut tm, &now_tm, 0)
}

/// git's `approxidate_careful`: try the absolute parser, fall back to fuzzy.
/// Returns `(timestamp, had_error)`.
fn approxidate_careful(date: &[u8]) -> (i64, bool) {
    if let Some(ts) = parse_date_basic(date) {
        return (ts, false);
    }
    let mut error_ret = false;
    let ts = approxidate_str(date, now_unix(), &mut error_ret);
    (ts, error_ret)
}

/// Parse a commit/tag author or committer date the way git's `parse_date` does
/// for `GIT_AUTHOR_DATE`/`GIT_COMMITTER_DATE` and `--date=`, returning the
/// absolute UTC `seconds` and the canonical `+HHMM` timezone string git would
/// store on the object line.
///
/// The fast path is the already-canonical raw form (`<seconds> +HHMM`, optional
/// leading `@`), which round-trips without going through the civil-date parser;
/// otherwise git's `parse_date_basic` handles the human/ISO/RFC formats the test
/// suite (and users) feed in (`2005-04-07T22:13:13`, `Thu, 7 Apr 2005 ...`, etc).
pub fn parse_commit_date(date: &str) -> Option<(i64, String)> {
    let trimmed = date.trim();
    // Raw form `<seconds> <+HHMM>` (optionally `@`-prefixed) — preserve the tz
    // string verbatim so a caller-supplied offset survives untouched.
    if let Some((raw_secs, raw_tz)) = split_raw_seconds_tz(trimmed) {
        return Some((raw_secs, raw_tz));
    }
    // Absolute / ISO / RFC forms first (git's `parse_date` → `parse_date_basic`).
    if let Some((seconds, offset)) = parse_date_basic_full(trimmed.as_bytes()) {
        return Some((seconds, format_tz_offset(offset)));
    }
    // Fuzzy relative / partial forms — git's `parse_force_date` falls back to
    // `approxidate_careful` and stores `@<timestamp>` (no explicit zone). Under
    // sley's UTC-convention local clock that zone is `+0000`, which is also what
    // the test suite pins via `TZ=UTC` / test-lib.
    let (ts, had_error) = approxidate_careful(trimmed.as_bytes());
    if had_error {
        return None;
    }
    Some((ts, "+0000".to_string()))
}

/// Recognise git's already-canonical `<seconds> <+HHMM>` raw date (with an
/// optional leading `@`), returning the integer seconds and the tz string as
/// written. Anything else returns `None` so the full parser runs.
fn split_raw_seconds_tz(date: &str) -> Option<(i64, String)> {
    let mut parts = date.split_whitespace();
    let secs = parts.next()?;
    let tz = parts.next()?;
    if parts.next().is_some() {
        return None;
    }
    let secs = secs.strip_prefix('@').unwrap_or(secs);
    let seconds = secs.parse::<i64>().ok()?;
    let tz_bytes = tz.as_bytes();
    if tz_bytes.len() == 5
        && matches!(tz_bytes[0], b'+' | b'-')
        && tz_bytes[1..].iter().all(u8::is_ascii_digit)
    {
        Some((seconds, tz.to_string()))
    } else {
        None
    }
}

/// Render a tz offset in minutes as git's `+HHMM`/`-HHMM`.
fn format_tz_offset(offset_minutes: i64) -> String {
    let sign = if offset_minutes < 0 { '-' } else { '+' };
    let abs = offset_minutes.abs();
    format!("{sign}{:02}{:02}", abs / 60, abs % 60)
}

/// git's `parse_expiry_date`: the public entry point. Returns `Some(timestamp)`
/// git's `approxidate_careful`: parse an absolute-or-relative date string to a
/// unix timestamp, returning `None` when it does not canonicalise. Unlike
/// [`parse_expiry_date`], it does NOT special-case "now"/"all" (the caller owns
/// those sentinels); "now" here resolves to the current time.
pub fn parse_approxidate(date: &str) -> Option<i64> {
    let (ts, had_error) = approxidate_careful(date.as_bytes());
    if had_error { None } else { Some(ts) }
}

/// when the value canonicalises, `None` when it does not (git's `errors != 0`).
pub fn parse_expiry_date(date: &str) -> Option<i64> {
    if date == "never" || date == "false" {
        return Some(0);
    }
    if date == "all" || date == "now" {
        // git's TIME_MAX; expiry-date is unsigned, so this is u64::MAX.
        return Some(u64::MAX as i64);
    }
    let (ts, had_error) = approxidate_careful(date.as_bytes());
    if had_error { None } else { Some(ts) }
}

/// Render a parsed expiry-date timestamp the way git prints it. git stores the
/// result in an unsigned `timestamp_t` and prints it with `%"PRItime"`, so the
/// "now"/"all" sentinel renders as `u64::MAX`.
pub fn format_expiry_date(date: &str) -> Option<String> {
    parse_expiry_date(date).map(|ts| {
        if ts == i64::MIN {
            // unreachable in practice; keep total.
            "0".to_string()
        } else if date == "all" || date == "now" {
            u64::MAX.to_string()
        } else {
            // Negative timestamps are not expected from expiry-date; clamp.
            (ts as i128).to_string()
        }
    })
}

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

    #[test]
    fn absolute_asctime_form() {
        // Under the UTC convention this matches git's `1275666415`.
        assert_eq!(
            parse_expiry_date("Fri Jun 4 15:46:55 2010"),
            Some(1_275_666_415)
        );
    }

    #[test]
    fn slash_form_with_pm() {
        assert_eq!(
            parse_expiry_date("2017/11/11 11:11:11PM"),
            Some(1_510_441_871)
        );
    }

    #[test]
    fn slash_form_space_pm() {
        assert_eq!(
            parse_expiry_date("2017/11/10 09:08:07 PM"),
            Some(1_510_348_087)
        );
    }

    #[test]
    fn never_and_now() {
        assert_eq!(parse_expiry_date("never"), Some(0));
        assert_eq!(parse_expiry_date("false"), Some(0));
        assert_eq!(parse_expiry_date("now"), Some(u64::MAX as i64));
        assert_eq!(
            format_expiry_date("now").as_deref(),
            Some("18446744073709551615")
        );
    }

    #[test]
    fn relative_values_parse() {
        // `1M` and `10` are relative; we only require that they parse (git's
        // list --type=expiry-date shows them, value depends on "now").
        assert!(parse_expiry_date("1M").is_some());
        assert!(parse_expiry_date("10").is_some());
    }

    #[test]
    fn rejects_non_dates() {
        assert!(parse_expiry_date("abc").is_none());
        assert!(parse_expiry_date("True").is_none());
        assert!(parse_expiry_date("red").is_none());
        assert!(parse_expiry_date("Blue").is_none());
        assert!(parse_expiry_date("~/dir").is_none());
        assert!(parse_expiry_date(":(optional)no-such-path").is_none());
    }

    #[test]
    fn asctime_with_tz() {
        let r = parse_commit_date("Thu Apr 7 22:13:13 2005 +0000");
        assert!(r.is_some(), "expected parse");
        // 1112911993 is 2005-04-07 22:13:13 UTC
        assert_eq!(r.expect("valid approxidate").0, 1_112_911_993);
    }
}