pub fn is_protocol_timestamp(s: &str) -> bool {
let b = s.as_bytes();
if b.len() < 20 {
return false;
}
if b[4] != b'-' || b[7] != b'-' || b[10] != b'T' || b[13] != b':' || b[16] != b':' {
return false;
}
if !b[..4].iter().all(u8::is_ascii_digit) {
return false;
}
let Some(month) = two_digits(&b[5..7]) else {
return false;
};
let Some(day) = two_digits(&b[8..10]) else {
return false;
};
let Some(hour) = two_digits(&b[11..13]) else {
return false;
};
let Some(minute) = two_digits(&b[14..16]) else {
return false;
};
let Some(second) = two_digits(&b[17..19]) else {
return false;
};
let year: u32 = s[..4].parse().unwrap_or(0);
if !(1..=12).contains(&month) || day < 1 || day > days_in_month(year, month) {
return false;
}
if hour > 23 || minute > 59 || second > 60 {
return false;
}
match &b[19..] {
[b'Z'] => true,
[b'.', rest @ ..] => {
let Some((last, digits)) = rest.split_last() else {
return false;
};
*last == b'Z' && !digits.is_empty() && digits.iter().all(u8::is_ascii_digit)
}
_ => false,
}
}
pub fn format_protocol_timestamp(unix_seconds: i64) -> String {
let days = unix_seconds.div_euclid(SECONDS_PER_DAY);
let second_of_day = unix_seconds.rem_euclid(SECONDS_PER_DAY);
let (year, month, day) = civil_from_days(days);
let hour = second_of_day / 3_600;
let minute = (second_of_day % 3_600) / 60;
let second = second_of_day % 60;
format!("{year:04}-{month:02}-{day:02}T{hour:02}:{minute:02}:{second:02}Z")
}
const SECONDS_PER_DAY: i64 = 86_400;
fn civil_from_days(days: i64) -> (i64, i64, i64) {
let z = days + 719_468;
let era = z.div_euclid(146_097);
let day_of_era = z.rem_euclid(146_097); let year_of_era =
(day_of_era - day_of_era / 1_460 + day_of_era / 36_524 - day_of_era / 146_096) / 365; let year = year_of_era + era * 400;
let day_of_year = day_of_era - (365 * year_of_era + year_of_era / 4 - year_of_era / 100); let month_prime = (5 * day_of_year + 2) / 153; let day = day_of_year - (153 * month_prime + 2) / 5 + 1; let month = if month_prime < 10 {
month_prime + 3
} else {
month_prime - 9
}; let year = if month <= 2 { year + 1 } else { year };
(year, month, day)
}
fn two_digits(pair: &[u8]) -> Option<u32> {
if pair.len() == 2 && pair.iter().all(u8::is_ascii_digit) {
Some((pair[0] - b'0') as u32 * 10 + (pair[1] - b'0') as u32)
} else {
None
}
}
fn days_in_month(year: u32, month: u32) -> u32 {
match month {
1 | 3 | 5 | 7 | 8 | 10 | 12 => 31,
4 | 6 | 9 | 11 => 30,
2 if is_leap_year(year) => 29,
2 => 28,
_ => 0,
}
}
fn is_leap_year(year: u32) -> bool {
(year.is_multiple_of(4) && !year.is_multiple_of(100)) || year.is_multiple_of(400)
}
pub const DIGEST_ALGORITHMS: &[&str] = &["sha256"];
pub fn is_well_formed_digest(s: &str) -> bool {
let Some((algorithm, hex)) = s.split_once(':') else {
return false;
};
let expected_hex_len = match algorithm {
"sha256" => 64,
_ => return false,
};
hex.len() == expected_hex_len
&& hex
.bytes()
.all(|c| c.is_ascii_digit() || (b'a'..=b'f').contains(&c))
}
#[cfg(test)]
mod tests {
use super::*;
fn digest(hex: &str) -> String {
format!("sha256:{hex}")
}
#[test]
fn formats_known_instants() {
assert_eq!(format_protocol_timestamp(0), "1970-01-01T00:00:00Z");
assert_eq!(format_protocol_timestamp(1), "1970-01-01T00:00:01Z");
assert_eq!(format_protocol_timestamp(86_399), "1970-01-01T23:59:59Z");
assert_eq!(format_protocol_timestamp(86_400), "1970-01-02T00:00:00Z");
assert_eq!(
format_protocol_timestamp(1_784_000_000),
"2026-07-14T03:33:20Z"
);
}
#[test]
fn formats_leap_days_and_century_rules() {
assert_eq!(
format_protocol_timestamp(951_782_400),
"2000-02-29T00:00:00Z"
);
assert_eq!(
format_protocol_timestamp(-2_203_891_200),
"1900-03-01T00:00:00Z"
);
assert_eq!(
format_protocol_timestamp(1_709_164_800),
"2024-02-29T00:00:00Z"
);
}
#[test]
fn formats_instants_before_the_epoch_without_rounding_the_day_up() {
assert_eq!(format_protocol_timestamp(-1), "1969-12-31T23:59:59Z");
assert_eq!(format_protocol_timestamp(-86_400), "1969-12-31T00:00:00Z");
}
#[test]
fn everything_it_formats_is_a_valid_protocol_timestamp() {
let mut instants = vec![-2_203_891_200, -86_401, -1, 0, 951_782_400, 1_784_000_000];
let mut t = -62_135_596_800; while t < 4_102_444_800 {
instants.push(t);
t += 999_999_937; }
for instant in instants {
let formatted = format_protocol_timestamp(instant);
assert!(
is_protocol_timestamp(&formatted),
"format_protocol_timestamp({instant}) produced `{formatted}`, which the validator rejects"
);
}
}
#[test]
fn accepts_the_canonical_timestamp_spelling() {
assert!(is_protocol_timestamp("2026-07-20T18:00:00Z"));
assert!(is_protocol_timestamp("1970-01-01T00:00:00Z"));
assert!(is_protocol_timestamp("2026-12-31T23:59:59Z"));
}
#[test]
fn accepts_fractional_seconds_of_any_precision() {
assert!(is_protocol_timestamp("2026-07-20T18:00:00.1Z"));
assert!(is_protocol_timestamp("2026-07-20T18:00:00.123Z"));
assert!(is_protocol_timestamp("2026-07-20T18:00:00.123456789Z"));
}
#[test]
fn rejects_prose_which_is_the_bug_this_check_exists_for() {
assert!(!is_protocol_timestamp("last tuesday"));
assert!(!is_protocol_timestamp(""));
assert!(!is_protocol_timestamp("2026-07-20"));
}
#[test]
fn rejects_non_utc_spellings_of_a_valid_instant() {
assert!(!is_protocol_timestamp("2026-07-20T18:00:00+02:00"));
assert!(!is_protocol_timestamp("2026-07-20T18:00:00-05:00"));
assert!(!is_protocol_timestamp("2026-07-20t18:00:00Z"));
assert!(!is_protocol_timestamp("2026-07-20 18:00:00Z"));
assert!(!is_protocol_timestamp("2026-07-20T18:00:00z"));
}
#[test]
fn rejects_out_of_range_components() {
assert!(!is_protocol_timestamp("2026-13-01T00:00:00Z")); assert!(!is_protocol_timestamp("2026-00-01T00:00:00Z")); assert!(!is_protocol_timestamp("2026-07-32T00:00:00Z")); assert!(!is_protocol_timestamp("2026-07-00T00:00:00Z")); assert!(!is_protocol_timestamp("2026-07-20T24:00:00Z")); assert!(!is_protocol_timestamp("2026-07-20T00:60:00Z")); }
#[test]
fn honors_month_lengths_and_leap_years() {
assert!(is_protocol_timestamp("2026-01-31T00:00:00Z"));
assert!(!is_protocol_timestamp("2026-04-31T00:00:00Z"));
assert!(!is_protocol_timestamp("2026-02-29T00:00:00Z")); assert!(is_protocol_timestamp("2024-02-29T00:00:00Z")); assert!(is_protocol_timestamp("2000-02-29T00:00:00Z")); assert!(!is_protocol_timestamp("1900-02-29T00:00:00Z")); }
#[test]
fn accepts_a_leap_second() {
assert!(is_protocol_timestamp("2016-12-31T23:59:60Z"));
assert!(!is_protocol_timestamp("2016-12-31T23:59:61Z"));
}
#[test]
fn rejects_a_malformed_fractional_part() {
assert!(!is_protocol_timestamp("2026-07-20T18:00:00.Z")); assert!(!is_protocol_timestamp("2026-07-20T18:00:00.12")); assert!(!is_protocol_timestamp("2026-07-20T18:00:00.1a2Z")); }
#[test]
fn accepts_a_well_formed_sha256_digest() {
assert!(is_well_formed_digest(&digest(&"a".repeat(64))));
assert!(is_well_formed_digest(&digest(
&"0123456789abcdef".repeat(4)
)));
}
#[test]
fn rejects_the_placeholder_digest_the_repo_used_in_examples() {
assert!(!is_well_formed_digest("sha256:abc"));
}
#[test]
fn rejects_uppercase_hex_so_comparison_never_yields_a_false_mismatch() {
let upper = format!("sha256:{}", "A".repeat(64));
assert!(!is_well_formed_digest(&upper));
}
#[test]
fn rejects_a_missing_or_unknown_algorithm_prefix() {
assert!(!is_well_formed_digest(&"a".repeat(64))); assert!(!is_well_formed_digest(&format!("md5:{}", "a".repeat(32))));
assert!(!is_well_formed_digest(&format!(
"sha512:{}",
"a".repeat(64)
)));
}
#[test]
fn rejects_non_hex_characters_of_the_right_length() {
assert!(!is_well_formed_digest(&digest(&"g".repeat(64))));
}
#[test]
fn the_declared_algorithm_list_matches_what_the_validator_accepts() {
for algorithm in DIGEST_ALGORITHMS {
let candidate = format!("{algorithm}:{}", "a".repeat(64));
assert!(
is_well_formed_digest(&candidate),
"{algorithm} is advertised but not accepted"
);
}
}
}