mitsuhiko / minijinja

MiniJinja is a powerful but minimal dependency template engine for Rust compatible with Jinja/Jinja2
https://docs.rs/minijinja/
Apache License 2.0
1.46k stars 83 forks source link

Error { kind: InvalidOperation, detail: "filter requires time, but only received a date", name: "<string>", line: 1 } #397

Closed nbittich closed 7 months ago

nbittich commented 7 months ago

Description

Trying to use the datetimeformat filter with default format and timezone.

[What happened]

Trying to use the datetimeformat filter using default format & timezone. The timestamp below comes from the example in chrono doc (https://docs.rs/chrono/latest/chrono/naive/struct.NaiveDateTime.html#method.from_timestamp_millis)

Reproduction steps

  1. Add the dependencies (using latest, 1.0.11):
minijinja = { workspace = true, default-features = false, features = [

  "builtins",
  "macros",
  "speedups",
  "urlencode",
] }
minijinja-contrib = { workspace = true, features = ["datetime", "timezone", "time-tz", "time"] }
chrono = { workspace = true, features = ["serde"] }
  1. Make a function to create the jinja engine:

fn get_jinja_engine<'a>() -> Result<&'a Environment<'static>, Box<dyn Error>> {
    let engine = {
        if JINJA_ENGINE.get().is_none() {
            let mut env = Environment::new();
            env.add_global("TIMEZONE", "Europe/Brussels");
            env.add_global("DATETIME_FORMAT", "[day]/[month]/[year] [hour]:[minute]");
            env.add_global("DATE_FORMAT", "[day]/[month]/[year]");

            minijinja_contrib::add_to_environment(&mut env);
            JINJA_ENGINE
                .set(env)
                .map_err(|_env| "could not setup jinja".to_string())?;
        }
        JINJA_ENGINE
            .get()
            .ok_or("could not extract engine from cell")
    }?;
    Ok(engine)
}
  1. Make a simple unit test:
    #[tokio::test]
    async fn test_date_and_time() {
        #[derive(Serialize, Deserialize)]
        struct Whatever {
            dt: NaiveDateTime,
            d: NaiveDate,
        }

        let ctx = Whatever {
            dt: NaiveDateTime::from_timestamp_millis(1662921288000).unwrap(),
            d: NaiveDate::from_ymd_opt(2024, 1, 1).unwrap(),
        };

         assert_eq!(
                  "01/09/2022 18:34",
                  engine
                      .render_str(r#"{{ dt|datetimeformat }}"#, ctx)
                      .unwrap()
              );
    }
}

Additional helpful information:

What did you expect

I should get the date format like e.g 01/09/2022 18:34

mitsuhiko commented 7 months ago

Duplicate of #396