diff --git a/lib/src/fnc/mod.rs b/lib/src/fnc/mod.rs index b9749237..4eb62a08 100644 --- a/lib/src/fnc/mod.rs +++ b/lib/src/fnc/mod.rs @@ -198,6 +198,7 @@ pub fn synchronous(ctx: &Context<'_>, name: &str, args: Vec) -> Result time::now, "time::round" => time::round, "time::second" => time::second, + "time::timezone" => time::timezone, "time::unix" => time::unix, "time::wday" => time::wday, "time::week" => time::week, diff --git a/lib/src/fnc/script/modules/surrealdb/functions/time.rs b/lib/src/fnc/script/modules/surrealdb/functions/time.rs index 0681c1a3..b45edcbf 100644 --- a/lib/src/fnc/script/modules/surrealdb/functions/time.rs +++ b/lib/src/fnc/script/modules/surrealdb/functions/time.rs @@ -28,6 +28,7 @@ impl ModuleDef for Package { module.add("now")?; module.add("round")?; module.add("secs")?; + module.add("timezone")?; module.add("unix")?; module.add("wday")?; module.add("week")?; @@ -48,6 +49,7 @@ impl ModuleDef for Package { module.set("now", Func::from(|v: Any| run("time::now", v.0)))?; module.set("round", Func::from(|v: Any| run("time::round", v.0)))?; module.set("secs", Func::from(|v: Any| run("time::secs", v.0)))?; + module.set("timezone", Func::from(|v: Any| run("time::timezone", v.0)))?; module.set("unix", Func::from(|v: Any| run("time::unix", v.0)))?; module.set("wday", Func::from(|v: Any| run("time::wday", v.0)))?; module.set("week", Func::from(|v: Any| run("time::week", v.0)))?; @@ -65,6 +67,7 @@ impl ModuleDef for Package { default.set("now", Func::from(|v: Any| run("time::now", v.0)))?; default.set("round", Func::from(|v: Any| run("time::round", v.0)))?; default.set("secs", Func::from(|v: Any| run("time::secs", v.0)))?; + default.set("timezone", Func::from(|v: Any| run("time::timezone", v.0)))?; default.set("unix", Func::from(|v: Any| run("time::unix", v.0)))?; default.set("wday", Func::from(|v: Any| run("time::wday", v.0)))?; default.set("week", Func::from(|v: Any| run("time::week", v.0)))?; diff --git a/lib/src/fnc/time.rs b/lib/src/fnc/time.rs index 99d93c8a..2f1196f7 100644 --- a/lib/src/fnc/time.rs +++ b/lib/src/fnc/time.rs @@ -1,9 +1,10 @@ use crate::err::Error; use crate::sql::datetime::Datetime; use crate::sql::value::Value; -use chrono::prelude::*; +use chrono::offset::TimeZone; use chrono::Datelike; use chrono::DurationRound; +use chrono::Local; use chrono::Timelike; use chrono::Utc; @@ -13,7 +14,6 @@ pub fn day((datetime,): (Option,)) -> Result { None => Datetime::default(), Some(_) => return Ok(Value::None), }; - Ok(date.day().into()) } @@ -76,7 +76,6 @@ pub fn hour((datetime,): (Option,)) -> Result { None => Datetime::default(), Some(_) => return Ok(Value::None), }; - Ok(date.hour().into()) } @@ -86,7 +85,6 @@ pub fn minute((datetime,): (Option,)) -> Result { None => Datetime::default(), Some(_) => return Ok(Value::None), }; - Ok(date.minute().into()) } @@ -96,7 +94,6 @@ pub fn month((datetime,): (Option,)) -> Result { None => Datetime::default(), Some(_) => return Ok(Value::None), }; - Ok(date.month().into()) } @@ -106,7 +103,6 @@ pub fn nano((datetime,): (Option,)) -> Result { None => Datetime::default(), Some(_) => return Ok(Value::None), }; - Ok(date.timestamp_nanos().into()) } @@ -133,17 +129,19 @@ pub fn second((datetime,): (Option,)) -> Result { None => Datetime::default(), Some(_) => return Ok(Value::None), }; - Ok(date.second().into()) } +pub fn timezone(_: ()) -> Result { + Ok(Local::now().offset().to_string().into()) +} + pub fn unix((datetime,): (Option,)) -> Result { let date = match datetime { Some(Value::Datetime(v)) => v, None => Datetime::default(), Some(_) => return Ok(Value::None), }; - Ok(date.timestamp().into()) } @@ -153,7 +151,6 @@ pub fn wday((datetime,): (Option,)) -> Result { None => Datetime::default(), Some(_) => return Ok(Value::None), }; - Ok(date.weekday().number_from_monday().into()) } @@ -163,7 +160,6 @@ pub fn week((datetime,): (Option,)) -> Result { None => Datetime::default(), Some(_) => return Ok(Value::None), }; - Ok(date.iso_week().week().into()) } @@ -173,7 +169,6 @@ pub fn yday((datetime,): (Option,)) -> Result { None => Datetime::default(), Some(_) => return Ok(Value::None), }; - Ok(date.ordinal().into()) } @@ -183,6 +178,5 @@ pub fn year((datetime,): (Option,)) -> Result { None => Datetime::default(), Some(_) => return Ok(Value::None), }; - Ok(date.year().into()) } diff --git a/lib/src/sql/function.rs b/lib/src/sql/function.rs index 47111588..568c0a00 100644 --- a/lib/src/sql/function.rs +++ b/lib/src/sql/function.rs @@ -441,6 +441,7 @@ fn function_time(i: &str) -> IResult<&str, &str> { tag("time::now"), tag("time::round"), tag("time::second"), + tag("time::timezone"), tag("time::unix"), tag("time::wday"), tag("time::week"),