From 148014c53ee125c8867a20feda2c3df8381f9460 Mon Sep 17 00:00:00 2001 From: Qrius Date: Thu, 26 Sep 2024 00:11:05 +0200 Subject: Add macro for formatting RFC3339-timestamps --- src/macro_processor/macro_processor.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'src') diff --git a/src/macro_processor/macro_processor.rs b/src/macro_processor/macro_processor.rs index 7eb41cf..a5b6a59 100644 --- a/src/macro_processor/macro_processor.rs +++ b/src/macro_processor/macro_processor.rs @@ -218,6 +218,20 @@ fn smp_builtin_indent( Ok(String::new()) } +#[cfg(feature = "time")] +fn smp_builtin_format_time( + _smp: &mut MacroProcessor, + macro_name: &str, + args: &mut [String], +) -> Result { + if args.len() < 2 { + return Ok(macro_name.to_string()); + } + let dt = chrono::DateTime::parse_from_rfc3339(&args[1]) + .map_err(|_| SMPError::UnknownError(87, None))?; + Ok(format!("{}", dt.format(&args[0]))) +} + /// Types of macros, this is to make it easy to store both functions and strings #[derive(Clone)] pub enum MacroType { @@ -296,6 +310,11 @@ impl MacroProcessor { MacroType::Function(smp_builtin_indent), ); self.define_macro(String::from("expr"), MacroType::Function(smp_builtin_expr)); + #[cfg(feature = "time")] + self.define_macro( + String::from("format_time"), + MacroType::Function(smp_builtin_format_time), + ); // format('Result id %d', 3282) } -- cgit v1.2.3