Add SQL time::timezone() function

Related to #1428
This commit is contained in:
Tobie Morgan Hitchcock 2023-01-08 20:56:59 +00:00
parent 23be3353be
commit f5739cba48
4 changed files with 11 additions and 12 deletions

View file

@ -198,6 +198,7 @@ pub fn synchronous(ctx: &Context<'_>, name: &str, args: Vec<Value>) -> Result<Va
"time::now" => time::now, "time::now" => time::now,
"time::round" => time::round, "time::round" => time::round,
"time::second" => time::second, "time::second" => time::second,
"time::timezone" => time::timezone,
"time::unix" => time::unix, "time::unix" => time::unix,
"time::wday" => time::wday, "time::wday" => time::wday,
"time::week" => time::week, "time::week" => time::week,

View file

@ -28,6 +28,7 @@ impl ModuleDef for Package {
module.add("now")?; module.add("now")?;
module.add("round")?; module.add("round")?;
module.add("secs")?; module.add("secs")?;
module.add("timezone")?;
module.add("unix")?; module.add("unix")?;
module.add("wday")?; module.add("wday")?;
module.add("week")?; 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("now", Func::from(|v: Any| run("time::now", v.0)))?;
module.set("round", Func::from(|v: Any| run("time::round", 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("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("unix", Func::from(|v: Any| run("time::unix", v.0)))?;
module.set("wday", Func::from(|v: Any| run("time::wday", 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)))?; 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("now", Func::from(|v: Any| run("time::now", v.0)))?;
default.set("round", Func::from(|v: Any| run("time::round", 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("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("unix", Func::from(|v: Any| run("time::unix", v.0)))?;
default.set("wday", Func::from(|v: Any| run("time::wday", 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)))?; default.set("week", Func::from(|v: Any| run("time::week", v.0)))?;

View file

@ -1,9 +1,10 @@
use crate::err::Error; use crate::err::Error;
use crate::sql::datetime::Datetime; use crate::sql::datetime::Datetime;
use crate::sql::value::Value; use crate::sql::value::Value;
use chrono::prelude::*; use chrono::offset::TimeZone;
use chrono::Datelike; use chrono::Datelike;
use chrono::DurationRound; use chrono::DurationRound;
use chrono::Local;
use chrono::Timelike; use chrono::Timelike;
use chrono::Utc; use chrono::Utc;
@ -13,7 +14,6 @@ pub fn day((datetime,): (Option<Value>,)) -> Result<Value, Error> {
None => Datetime::default(), None => Datetime::default(),
Some(_) => return Ok(Value::None), Some(_) => return Ok(Value::None),
}; };
Ok(date.day().into()) Ok(date.day().into())
} }
@ -76,7 +76,6 @@ pub fn hour((datetime,): (Option<Value>,)) -> Result<Value, Error> {
None => Datetime::default(), None => Datetime::default(),
Some(_) => return Ok(Value::None), Some(_) => return Ok(Value::None),
}; };
Ok(date.hour().into()) Ok(date.hour().into())
} }
@ -86,7 +85,6 @@ pub fn minute((datetime,): (Option<Value>,)) -> Result<Value, Error> {
None => Datetime::default(), None => Datetime::default(),
Some(_) => return Ok(Value::None), Some(_) => return Ok(Value::None),
}; };
Ok(date.minute().into()) Ok(date.minute().into())
} }
@ -96,7 +94,6 @@ pub fn month((datetime,): (Option<Value>,)) -> Result<Value, Error> {
None => Datetime::default(), None => Datetime::default(),
Some(_) => return Ok(Value::None), Some(_) => return Ok(Value::None),
}; };
Ok(date.month().into()) Ok(date.month().into())
} }
@ -106,7 +103,6 @@ pub fn nano((datetime,): (Option<Value>,)) -> Result<Value, Error> {
None => Datetime::default(), None => Datetime::default(),
Some(_) => return Ok(Value::None), Some(_) => return Ok(Value::None),
}; };
Ok(date.timestamp_nanos().into()) Ok(date.timestamp_nanos().into())
} }
@ -133,17 +129,19 @@ pub fn second((datetime,): (Option<Value>,)) -> Result<Value, Error> {
None => Datetime::default(), None => Datetime::default(),
Some(_) => return Ok(Value::None), Some(_) => return Ok(Value::None),
}; };
Ok(date.second().into()) Ok(date.second().into())
} }
pub fn timezone(_: ()) -> Result<Value, Error> {
Ok(Local::now().offset().to_string().into())
}
pub fn unix((datetime,): (Option<Value>,)) -> Result<Value, Error> { pub fn unix((datetime,): (Option<Value>,)) -> Result<Value, Error> {
let date = match datetime { let date = match datetime {
Some(Value::Datetime(v)) => v, Some(Value::Datetime(v)) => v,
None => Datetime::default(), None => Datetime::default(),
Some(_) => return Ok(Value::None), Some(_) => return Ok(Value::None),
}; };
Ok(date.timestamp().into()) Ok(date.timestamp().into())
} }
@ -153,7 +151,6 @@ pub fn wday((datetime,): (Option<Value>,)) -> Result<Value, Error> {
None => Datetime::default(), None => Datetime::default(),
Some(_) => return Ok(Value::None), Some(_) => return Ok(Value::None),
}; };
Ok(date.weekday().number_from_monday().into()) Ok(date.weekday().number_from_monday().into())
} }
@ -163,7 +160,6 @@ pub fn week((datetime,): (Option<Value>,)) -> Result<Value, Error> {
None => Datetime::default(), None => Datetime::default(),
Some(_) => return Ok(Value::None), Some(_) => return Ok(Value::None),
}; };
Ok(date.iso_week().week().into()) Ok(date.iso_week().week().into())
} }
@ -173,7 +169,6 @@ pub fn yday((datetime,): (Option<Value>,)) -> Result<Value, Error> {
None => Datetime::default(), None => Datetime::default(),
Some(_) => return Ok(Value::None), Some(_) => return Ok(Value::None),
}; };
Ok(date.ordinal().into()) Ok(date.ordinal().into())
} }
@ -183,6 +178,5 @@ pub fn year((datetime,): (Option<Value>,)) -> Result<Value, Error> {
None => Datetime::default(), None => Datetime::default(),
Some(_) => return Ok(Value::None), Some(_) => return Ok(Value::None),
}; };
Ok(date.year().into()) Ok(date.year().into())
} }

View file

@ -441,6 +441,7 @@ fn function_time(i: &str) -> IResult<&str, &str> {
tag("time::now"), tag("time::now"),
tag("time::round"), tag("time::round"),
tag("time::second"), tag("time::second"),
tag("time::timezone"),
tag("time::unix"), tag("time::unix"),
tag("time::wday"), tag("time::wday"),
tag("time::week"), tag("time::week"),