MCPcopy Create free account
hub / github.com/NodeDB-Lab/nodedb / try_eval

Function try_eval

nodedb-query/src/functions/datetime.rs:8–179  ·  view source on GitHub ↗
(name: &str, args: &[Value])

Source from the content-addressed store, hash-verified

6use nodedb_types::Value;
7
8pub(super) fn try_eval(name: &str, args: &[Value]) -> Option<Value> {
9 let v = match name {
10 "now" | "current_timestamp" => {
11 let dt = nodedb_types::NdbDateTime::now();
12 Value::DateTime(dt)
13 }
14 "datetime" | "to_datetime" => args
15 .first()
16 .and_then(|v| match v {
17 Value::String(s) => {
18 nodedb_types::NdbDateTime::parse(s).map(|dt| Value::String(dt.to_iso8601()))
19 }
20 Value::Integer(micros) => Some(Value::String(
21 nodedb_types::NdbDateTime::from_micros(*micros).to_iso8601(),
22 )),
23 Value::Float(f) => Some(Value::String(
24 nodedb_types::NdbDateTime::from_micros(*f as i64).to_iso8601(),
25 )),
26 Value::DateTime(dt) | Value::NaiveDateTime(dt) => {
27 Some(Value::String(dt.to_iso8601()))
28 }
29 _ => None,
30 })
31 .unwrap_or(Value::Null),
32 "unix_secs" | "epoch_secs" => args
33 .first()
34 .and_then(|v| v.as_str())
35 .and_then(nodedb_types::NdbDateTime::parse)
36 .map_or(Value::Null, |dt| Value::Integer(dt.unix_secs())),
37 "unix_millis" | "epoch_millis" => args
38 .first()
39 .and_then(|v| v.as_str())
40 .and_then(nodedb_types::NdbDateTime::parse)
41 .map_or(Value::Null, |dt| Value::Integer(dt.unix_millis())),
42 "extract" | "date_part" => {
43 let part = args.first().and_then(|v| v.as_str()).unwrap_or("");
44 let dt = args
45 .get(1)
46 .and_then(|v| v.as_str())
47 .and_then(nodedb_types::NdbDateTime::parse);
48 match dt {
49 Some(dt) => {
50 let c = dt.components();
51 let val: i64 = match part.to_lowercase().as_str() {
52 "year" | "y" => c.year as i64,
53 "month" | "mon" => c.month as i64,
54 "day" | "d" => c.day as i64,
55 "hour" | "h" => c.hour as i64,
56 "minute" | "min" | "m" => c.minute as i64,
57 "second" | "sec" | "s" => c.second as i64,
58 "microsecond" | "us" => c.microsecond as i64,
59 "epoch" => dt.unix_secs(),
60 "dow" | "dayofweek" => {
61 let days = dt.micros / 86_400_000_000;
62 (days + 4) % 7
63 }
64 _ => return Some(Value::Null),
65 };

Callers

nothing calls this directly

Calls 15

nowFunction · 0.85
to_value_numberFunction · 0.85
value_to_display_stringFunction · 0.85
eval_time_bucketFunction · 0.85
firstMethod · 0.80
to_iso8601Method · 0.80
unix_secsMethod · 0.80
unix_millisMethod · 0.80
add_durationMethod · 0.80
sub_durationMethod · 0.80
duration_sinceMethod · 0.80
as_secs_f64Method · 0.80

Tested by

no test coverage detected