use core::fmt;
use crate::Date;
use crate::Time;
use crate::Weekday;
#[expect(
missing_copy_implementations,
reason = "This type isn't `Copy` on purpose as embedded systems might not handle 64-bit operations efficiently."
)]
#[derive(PartialEq, Eq, Hash, PartialOrd, Ord, Clone)]
pub struct DateTime {
pub date: Date,
pub time: Time,
}
impl DateTime {
#[must_use]
pub fn new(date: Date, time: Time) -> Self {
Self { date, time }
}
#[must_use]
pub fn year(&self) -> u16 {
self.date.year
}
#[must_use]
pub fn month(&self) -> u8 {
self.date.month
}
#[must_use]
pub fn day(&self) -> u8 {
self.date.day
}
#[must_use]
pub fn hour(&self) -> u8 {
self.time.hour
}
#[must_use]
pub fn minute(&self) -> u8 {
self.time.minute
}
#[must_use]
pub fn second(&self) -> u8 {
self.time.second
}
#[must_use]
pub fn weekday(&self) -> Weekday {
self.date.weekday()
}
}
impl fmt::Debug for DateTime {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(&self, f)
}
}
impl fmt::Display for DateTime {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{} {}", self.date, self.time)
}
}
#[cfg(feature = "defmt")]
impl defmt::Format for DateTime {
fn format(&self, fmt: defmt::Formatter<'_>) {
defmt::write!(fmt, "{} {}", self.date, self.time);
}
}
#[cfg(feature = "ufmt")]
impl ufmt::uDebug for DateTime {
fn fmt<W>(&self, fmt: &mut ufmt::Formatter<'_, W>) -> Result<(), W::Error>
where
W: ufmt::uWrite + ?Sized,
{
ufmt::uDisplay::fmt(&self, fmt)
}
}
#[cfg(feature = "ufmt")]
impl ufmt::uDisplay for DateTime {
fn fmt<W>(&self, fmt: &mut ufmt::Formatter<'_, W>) -> Result<(), W::Error>
where
W: ufmt::uWrite + ?Sized,
{
ufmt::uwrite!(fmt, "{} {}", self.date, self.time)
}
}