summaryrefslogtreecommitdiff
path: root/src/macro_processor
diff options
context:
space:
mode:
authorQrius <[email protected]>2024-09-26 00:11:05 +0200
committerQrius <[email protected]>2024-09-26 00:11:05 +0200
commit148014c53ee125c8867a20feda2c3df8381f9460 (patch)
treed506ea6b3cb3569de664f98037f0b5eb78121c50 /src/macro_processor
parentdec7cf62faf74695e78940ba5a4073dc8cfc3552 (diff)
downloadskaldpress-148014c53ee125c8867a20feda2c3df8381f9460.tar.gz
skaldpress-148014c53ee125c8867a20feda2c3df8381f9460.zip
Add macro for formatting RFC3339-timestamps
Diffstat (limited to 'src/macro_processor')
-rw-r--r--src/macro_processor/macro_processor.rs19
1 files changed, 19 insertions, 0 deletions
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<String, SMPError> {
+ 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)
}