use chrono::{Datelike as _, Local, Timelike as _};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct LocalDateTime {
pub year: i32,
pub month: u32,
pub day: u32,
pub hour: u32,
pub minute: u32,
pub second: u32,
}
pub fn now() -> LocalDateTime {
let t = Local::now();
LocalDateTime {
year: t.year(),
month: t.month(),
day: t.day(),
hour: t.hour(),
minute: t.minute(),
second: t.second().min(59),
}
}
impl LocalDateTime {
pub fn time_string(self) -> String {
let (hour, meridiem) = match self.hour {
0 => (12, "AM"),
h @ 1..=11 => (h, "AM"),
12 => (12, "PM"),
h => (h - 12, "PM"),
};
format!("{hour}:{:02}:{:02} {meridiem}", self.minute, self.second)
}
}
#[cfg(test)]
mod tests {
use super::*;
fn at(hour: u32, minute: u32, second: u32) -> LocalDateTime {
LocalDateTime {
year: 2026,
month: 1,
day: 1,
hour,
minute,
second,
}
}
#[test]
fn time_string_matches_to_locale_time_string() {
assert_eq!(at(14, 5, 9).time_string(), "2:05:09 PM");
assert_eq!(at(0, 0, 0).time_string(), "12:00:00 AM");
assert_eq!(at(12, 30, 0).time_string(), "12:30:00 PM");
assert_eq!(at(9, 41, 7).time_string(), "9:41:07 AM");
}
#[test]
fn now_is_local_time() {
let n = now();
let local = Local::now();
assert_eq!(
(n.year, n.month, n.day),
(local.year(), local.month(), local.day())
);
assert!(n.hour < 24 && n.minute < 60 && n.second < 60);
}
}