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

#124 in Value formatting

Download history 1044/week @ 2024-09-22 2138/week @ 2024-09-29 1143/week @ 2024-10-06 1487/week @ 2024-10-13 1886/week @ 2024-10-20 1235/week @ 2024-10-27 1333/week @ 2024-11-03 1409/week @ 2024-11-10 2214/week @ 2024-11-17 2232/week @ 2024-11-24 1368/week @ 2024-12-01 1013/week @ 2024-12-08 589/week @ 2024-12-15 1116/week @ 2024-12-22 1014/week @ 2024-12-29 851/week @ 2025-01-05

3,634 downloads per month
Used in 8 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

~79KB