use crate::ast::compiler::optimizer::Purity; use crate::ast::environment::Environment; use crate::ast::types::{Signature, StaticType, Value}; use chrono::{NaiveDate, NaiveDateTime}; pub fn register(env: &Environment) { let date_ty = StaticType::Function(Box::new(Signature { params: StaticType::Tuple(vec![StaticType::Text]), ret: StaticType::DateTime, })); env.register_native("date", date_ty, Purity::Pure, |args| { if let Value::Text(s) = &args[0] { // Try parse YYYY-MM-DD if let Ok(dt) = NaiveDate::parse_from_str(s, "%Y-%m-%d") { let ts = dt .and_hms_opt(0, 0, 0) .unwrap() .and_utc() .timestamp_millis(); return Value::DateTime(ts); } // Try parse YYYY-MM-DD HH:MM:SS if let Ok(dt) = NaiveDateTime::parse_from_str(s, "%Y-%m-%d %H:%M:%S") { return Value::DateTime(dt.and_utc().timestamp_millis()); } } Value::Void }); }