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

#133 in Value formatting

Download history 882/week @ 2024-07-23 697/week @ 2024-07-30 630/week @ 2024-08-06 536/week @ 2024-08-13 659/week @ 2024-08-20 744/week @ 2024-08-27 1167/week @ 2024-09-03 819/week @ 2024-09-10 1055/week @ 2024-09-17 1310/week @ 2024-09-24 1829/week @ 2024-10-01 1237/week @ 2024-10-08 1511/week @ 2024-10-15 1838/week @ 2024-10-22 1036/week @ 2024-10-29 1350/week @ 2024-11-05

6,019 downloads per month
Used in 7 crates (4 directly)

MIT license

23KB
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

~76KB