Formatting
This commit is contained in:
+29
-25
@@ -1,25 +1,29 @@
|
||||
use crate::ast::types::{Value, StaticType, Signature};
|
||||
use crate::ast::environment::Environment;
|
||||
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, true, |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
|
||||
});
|
||||
}
|
||||
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, true, |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
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user