use alloc::format;
use alloc::string::String;
use crate::TimeFormat;
const MONTHS: [&str; 12] = [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December",
];
const DAYS: [&str; 7] = [
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
];
pub(crate) fn month_name(m: u32) -> String {
MONTHS
.get((m as usize).wrapping_sub(1))
.map_or_else(|| format!("month {m}"), |s| String::from(*s))
}
pub(crate) fn day_name(w: u32) -> String {
DAYS.get((w % 7) as usize)
.map_or_else(|| format!("day {w}"), |s| String::from(*s))
}
pub(crate) fn clock(hour: u32, minute: u32, fmt: TimeFormat) -> String {
match fmt {
TimeFormat::H24 => format!("{hour:02}:{minute:02}"),
TimeFormat::H12 => {
let period = if hour < 12 { "AM" } else { "PM" };
let h12 = match hour % 12 {
0 => 12,
h => h,
};
format!("{h12:02}:{minute:02} {period}")
}
}
}
pub(crate) fn human_list(items: &[String]) -> String {
match items {
[] => String::new(),
[one] => one.clone(),
[a, b] => format!("{a} and {b}"),
[rest @ .., last] => format!("{} and {last}", rest.join(", ")),
}
}
pub(crate) fn capitalize(s: &str) -> String {
let mut chars = s.chars();
match chars.next() {
None => String::new(),
Some(first) => first.to_uppercase().collect::<String>() + chars.as_str(),
}
}