Skip to main content

atom_engine/filters/
date.rs

1use chrono::{DateTime, Utc};
2use serde_json::Value;
3use std::collections::HashMap;
4
5use super::FilterResult;
6
7pub fn date_format(value: &Value, args: &HashMap<String, Value>) -> FilterResult {
8    let fmt = args
9        .get("format")
10        .and_then(|v| v.as_str())
11        .unwrap_or("%Y-%m-%d");
12
13    let result = if let Some(s) = value.as_str() {
14        if let Ok(dt) = DateTime::parse_from_rfc3339(s) {
15            dt.format(fmt).to_string()
16        } else {
17            s.to_string()
18        }
19    } else if let Some(n) = value.as_i64() {
20        if let Some(dt) = DateTime::<Utc>::from_timestamp(n, 0) {
21            dt.format(fmt).to_string()
22        } else {
23            n.to_string()
24        }
25    } else {
26        return Ok(value.clone());
27    };
28
29    Ok(Value::String(result))
30}