use chrono::{NaiveDate, NaiveDateTime, NaiveTime};
pub const DATE_FORMAT: &str = "%b. %-d, %Y";
pub const TIME_FORMAT: &str = "%H:%M";
pub const DATETIME_FORMAT: &str = "%b. %-d, %Y, %-I:%M %p";
#[must_use]
pub fn date_format(date: &NaiveDate) -> String {
date.format(DATE_FORMAT).to_string()
}
#[must_use]
pub fn time_format(time: &NaiveTime) -> String {
time.format(TIME_FORMAT).to_string()
}
#[must_use]
pub fn datetime_format(dt: &NaiveDateTime) -> String {
dt.format(DATETIME_FORMAT).to_string()
}
#[cfg(test)]
mod tests {
use super::{
DATE_FORMAT, DATETIME_FORMAT, TIME_FORMAT, date_format, datetime_format, time_format,
};
use chrono::{NaiveDate, NaiveTime};
#[test]
fn date_format_uses_expected_default_pattern() {
let date = NaiveDate::from_ymd_opt(2024, 10, 7).unwrap();
assert_eq!(DATE_FORMAT, "%b. %-d, %Y");
assert_eq!(date_format(&date), "Oct. 7, 2024");
}
#[test]
fn time_format_uses_24_hour_clock() {
let time = NaiveTime::from_hms_opt(14, 30, 0).unwrap();
assert_eq!(TIME_FORMAT, "%H:%M");
assert_eq!(time_format(&time), "14:30");
}
#[test]
fn datetime_format_combines_date_and_time() {
let dt = NaiveDate::from_ymd_opt(2024, 10, 7)
.unwrap()
.and_hms_opt(14, 30, 0)
.unwrap();
assert_eq!(DATETIME_FORMAT, "%b. %-d, %Y, %-I:%M %p");
assert_eq!(datetime_format(&dt), "Oct. 7, 2024, 2:30 PM");
}
}