use std::time::Duration;
use chrono::{DateTime, Local};
pub fn format_message_time(now: DateTime<Local>) -> String {
now.format("%-I:%M %p").to_string()
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ElapsedUnit {
Seconds,
Minutes,
Hours,
}
impl ElapsedUnit {
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::Seconds => "s",
Self::Minutes => "m",
Self::Hours => "h",
}
}
}
#[must_use]
pub fn elapsed_tiers(seconds: u64) -> (u64, ElapsedUnit, Option<(u64, ElapsedUnit)>) {
const MINUTE: u64 = 60;
const HOUR: u64 = 3600;
if seconds < MINUTE {
return (seconds, ElapsedUnit::Seconds, None);
}
if seconds < HOUR {
let rest = seconds % MINUTE;
let remainder = (rest != 0).then_some((rest, ElapsedUnit::Seconds));
return (seconds / MINUTE, ElapsedUnit::Minutes, remainder);
}
let rest = (seconds % HOUR) / MINUTE;
let remainder = (rest != 0).then_some((rest, ElapsedUnit::Minutes));
(seconds / HOUR, ElapsedUnit::Hours, remainder)
}
#[must_use]
pub fn format_turn_elapsed(duration: Duration) -> String {
let (value, unit, remainder) = elapsed_tiers(duration.as_secs());
let mut out = format!("{value}{}", unit.as_str());
if let Some((value, unit)) = remainder {
out.push(' ');
out.push_str(&format!("{value}{}", unit.as_str()));
}
out
}
pub fn message_timestamp_footer(now: DateTime<Local>, elapsed: Option<Duration>) -> String {
match elapsed {
Some(duration) => format!("· {} · {}", format_message_time(now), format_turn_elapsed(duration)),
None => format!("· {}", format_message_time(now)),
}
}
pub fn current_message_timestamp_footer() -> String {
message_timestamp_footer(Local::now(), None)
}
pub fn current_message_timestamp_footer_with_elapsed(elapsed: Option<Duration>) -> String {
message_timestamp_footer(Local::now(), elapsed)
}
#[cfg(test)]
mod tests {
use super::*;
use chrono::TimeZone;
fn local_at(hour: u32, minute: u32) -> DateTime<Local> {
Local
.with_ymd_and_hms(2026, 11, 2, hour, minute, 0)
.single()
.expect("fixture timestamp must be unambiguous")
}
#[test]
fn formats_afternoon_without_leading_zero() {
assert_eq!(format_message_time(local_at(15, 12)), "3:12 PM");
}
#[test]
fn formats_single_digit_morning_hour() {
assert_eq!(format_message_time(local_at(9, 5)), "9:05 AM");
}
#[test]
fn formats_double_digit_morning_hour() {
assert_eq!(format_message_time(local_at(11, 55)), "11:55 AM");
}
#[test]
fn midnight_is_twelve_am_not_zero() {
assert_eq!(format_message_time(local_at(0, 5)), "12:05 AM");
}
#[test]
fn noon_is_twelve_pm_not_zero() {
assert_eq!(format_message_time(local_at(12, 0)), "12:00 PM");
}
#[test]
fn footer_prefixes_time_with_middot() {
assert_eq!(message_timestamp_footer(local_at(15, 12), None), "· 3:12 PM");
}
#[test]
fn footer_appends_elapsed_after_the_clock() {
assert_eq!(message_timestamp_footer(local_at(15, 12), Some(Duration::from_secs(804))), "· 3:12 PM · 13m 24s");
assert_eq!(message_timestamp_footer(local_at(15, 12), Some(Duration::from_secs(3900))), "· 3:12 PM · 1h 5m");
}
#[test]
fn current_footer_with_elapsed_keeps_one_chrome_unit() {
let footer = current_message_timestamp_footer_with_elapsed(Some(Duration::from_secs(59)));
let time = footer.strip_prefix("· ").expect("current footer must carry the middot");
let mut halves = time.split(" · ");
let clock = halves.next().expect("clock half must exist");
assert!(clock.contains(':'), "clock half must show clock time: {footer:?}");
assert!(clock.ends_with("AM") || clock.ends_with("PM"), "clock half must show meridiem: {footer:?}");
assert_eq!(halves.next(), Some("59s"), "elapsed half must follow the clock");
assert!(halves.next().is_none(), "footer must stay one clock plus one duration");
}
#[test]
fn elapsed_symbols_omit_zero_parts() {
assert_eq!(format_turn_elapsed(Duration::from_secs(0)), "0s");
assert_eq!(format_turn_elapsed(Duration::from_secs(59)), "59s");
assert_eq!(format_turn_elapsed(Duration::from_secs(60)), "1m");
assert_eq!(format_turn_elapsed(Duration::from_secs(90)), "1m 30s");
assert_eq!(format_turn_elapsed(Duration::from_secs(3600)), "1h");
assert_eq!(format_turn_elapsed(Duration::from_secs(3900)), "1h 5m");
}
#[test]
fn elapsed_tiers_are_boundary_safe_and_zero_parts_omitted() {
use ElapsedUnit as U;
for (seconds, expected) in [
(0, (0, U::Seconds, None)),
(59, (59, U::Seconds, None)),
(60, (1, U::Minutes, None)),
(90, (1, U::Minutes, Some((30, U::Seconds)))),
(3599, (59, U::Minutes, Some((59, U::Seconds)))),
(3600, (1, U::Hours, None)),
(3900, (1, U::Hours, Some((5, U::Minutes)))),
(86_400, (24, U::Hours, None)),
] {
assert_eq!(elapsed_tiers(seconds), expected, "{seconds}s");
}
}
#[test]
fn current_footer_has_footer_shape() {
let footer = current_message_timestamp_footer();
let time = footer.strip_prefix("· ").expect("current footer must carry the middot");
assert!(time.contains(':'), "current footer must show clock time: {footer:?}");
assert!(time.ends_with("AM") || time.ends_with("PM"), "current footer must show meridiem: {footer:?}");
}
}