4 releases

0.1.3 May 1, 2023
0.1.2 Jan 26, 2023
0.1.1 Jan 25, 2023
0.1.0 Jan 25, 2023

#90 in Date and time

Download history 76/week @ 2023-02-14 230/week @ 2023-02-21 402/week @ 2023-02-28 154/week @ 2023-03-07 246/week @ 2023-03-14 265/week @ 2023-03-21 391/week @ 2023-03-28 254/week @ 2023-04-04 444/week @ 2023-04-11 292/week @ 2023-04-18 244/week @ 2023-04-25 513/week @ 2023-05-02 439/week @ 2023-05-09 289/week @ 2023-05-16 378/week @ 2023-05-23 381/week @ 2023-05-30

1,601 downloads per month
Used in atuin

MIT license

22KB
404 lines

runtime-format

Formatting, but processed at runtime.

use runtime_format::{FormatArgs, FormatKey, FormatKeyError};
use core::fmt;

impl FormatKey for DateTime {
    fn fmt(&self, key: &str, f: &mut fmt::Formatter<'_>) -> Result<(), FormatKeyError> {
        use core::fmt::Write;
        match key {
            "year"    => write!(f, "{}", self.year()).map_err(FormatKeyError::Fmt),
            "month"   => write!(f, "{}", self.short_month_name()).map_err(FormatKeyError::Fmt),
            "day"     => write!(f, "{}", self.day()).map_err(FormatKeyError::Fmt),
            "hours"   => write!(f, "{}", self.hours()).map_err(FormatKeyError::Fmt),
            "minutes" => write!(f, "{}", self.minutes()).map_err(FormatKeyError::Fmt),
            "seconds" => write!(f, "{}", self.seconds()).map_err(FormatKeyError::Fmt),
            _ => Err(FormatKeyError::UnknownKey),
        }
    }
}
let now = DateTime::now();
let fmt = "{month} {day} {year} {hours}:{minutes}:{seconds}";
let args = FormatArgs::new(fmt, &now);

// Outputs "Jan 25 2023 16:27:53"
println!("{args}");

Dependencies

~68KB