use core::fmt::Display;

use embassy_time::Duration;

pub struct FormattedDuration<'a>(&'a Duration);

impl Display for FormattedDuration<'_> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        let &FormattedDuration(duration) = self;
        f.write_fmt(format_args!(
            "{:01}.{:06}",
            duration.as_secs(),
            duration.as_micros() % 1_000_000
        ))
    }
}

pub trait DurationExt {
    fn display_as_secs(&self) -> FormattedDuration<'_>;
}

impl DurationExt for Duration {
    fn display_as_secs(&self) -> FormattedDuration<'_> {
        FormattedDuration(self)
    }
}
