pub(super) const ABSENT: &str = "—";
pub(super) fn human_size(bytes: u64) -> String {
const UNITS: [&str; 6] = ["KiB", "MiB", "GiB", "TiB", "PiB", "EiB"];
if bytes < 1024 {
return format!("{bytes} B");
}
let mut divisor = 1024_u64;
let mut unit = 0;
while unit + 1 < UNITS.len() && bytes / divisor >= 1024 {
divisor *= 1024;
unit += 1;
}
let whole = bytes / divisor;
if whole >= 10 {
return format!("{whole} {}", UNITS[unit]);
}
let tenths = (bytes % divisor) * 10 / divisor;
format!("{whole}.{tenths} {}", UNITS[unit])
}
pub(super) fn civil_date(unix_seconds: i64) -> Option<String> {
let formatted = http_date(unix_seconds)?;
let day = formatted.get(5..7)?;
let month_name = formatted.get(8..11)?;
let month = MONTHS.iter().position(|name| *name == month_name)?;
let year = formatted.get(12..16)?;
Some(format!("{year}-{:02}-{day}", month + 1))
}
pub(super) fn http_date(unix_seconds: i64) -> Option<String> {
const LATEST_FORMATTABLE: u64 = 253_402_300_799;
let seconds = u64::try_from(unix_seconds).ok()?;
if seconds > LATEST_FORMATTABLE {
return None;
}
let instant = std::time::UNIX_EPOCH.checked_add(std::time::Duration::from_secs(seconds))?;
Some(httpdate::fmt_http_date(instant))
}
const MONTHS: [&str; 12] = [
"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
];
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn sizes_below_a_kibibyte_are_plain_byte_counts() {
assert_eq!(human_size(0), "0 B");
assert_eq!(human_size(1), "1 B");
assert_eq!(human_size(812), "812 B");
assert_eq!(human_size(1023), "1023 B");
}
#[test]
fn sizes_gain_one_decimal_below_ten_and_lose_it_above() {
assert_eq!(human_size(1024), "1.0 KiB");
assert_eq!(human_size(1536), "1.5 KiB");
assert_eq!(human_size(4250), "4.1 KiB");
assert_eq!(human_size(10 * 1024), "10 KiB");
assert_eq!(human_size(1024 * 1024), "1.0 MiB");
}
#[test]
fn the_largest_representable_size_still_formats() {
assert_eq!(human_size(u64::MAX), "15 EiB");
}
#[test]
fn a_timestamp_becomes_an_iso_date_in_utc() {
assert_eq!(civil_date(0).as_deref(), Some("1970-01-01"));
assert_eq!(civil_date(1_788_870_896).as_deref(), Some("2026-09-08"));
}
#[test]
fn an_unrepresentable_timestamp_yields_no_date_rather_than_panicking() {
assert_eq!(civil_date(-1), None);
assert_eq!(
civil_date(i64::MAX),
None,
"must not panic in a page render"
);
assert_eq!(
civil_date(253_402_300_800),
None,
"one second past year 9999"
);
assert!(
civil_date(253_402_300_799).is_some(),
"the last formattable instant"
);
}
#[test]
fn every_month_round_trips_through_the_name_table() {
for (index, expected) in [
(0_i64, "1970-01-01"),
(2_678_400, "1970-02-01"),
(5_097_600, "1970-03-01"),
(7_776_000, "1970-04-01"),
(10_368_000, "1970-05-01"),
(13_046_400, "1970-06-01"),
(15_638_400, "1970-07-01"),
(18_316_800, "1970-08-01"),
(20_995_200, "1970-09-01"),
(23_587_200, "1970-10-01"),
(26_265_600, "1970-11-01"),
(28_857_600, "1970-12-01"),
] {
assert_eq!(civil_date(index).as_deref(), Some(expected));
}
}
}