Add SQL is::datetime() function

Closes #1301
This commit is contained in:
Tobie Morgan Hitchcock 2023-01-09 09:48:46 +00:00
parent cca04de5c4
commit 2949ea7f4d
4 changed files with 11 additions and 0 deletions

View file

@ -1,5 +1,6 @@
use crate::err::Error; use crate::err::Error;
use crate::sql::value::Value; use crate::sql::value::Value;
use chrono::NaiveDateTime;
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use regex::Regex; use regex::Regex;
use semver::Version; use semver::Version;
@ -25,6 +26,11 @@ pub fn ascii((arg,): (String,)) -> Result<Value, Error> {
Ok(arg.is_ascii().into()) Ok(arg.is_ascii().into())
} }
#[inline]
pub fn datetime((arg, fmt): (String, String)) -> Result<Value, Error> {
Ok(NaiveDateTime::parse_from_str(&arg, &fmt).is_ok().into())
}
#[inline] #[inline]
pub fn domain((arg,): (String,)) -> Result<Value, Error> { pub fn domain((arg,): (String,)) -> Result<Value, Error> {
Ok(addr::parse_domain_name(arg.as_str()).is_ok().into()) Ok(addr::parse_domain_name(arg.as_str()).is_ok().into())

View file

@ -99,6 +99,7 @@ pub fn synchronous(ctx: &Context<'_>, name: &str, args: Vec<Value>) -> Result<Va
"is::alphanum" => is::alphanum, "is::alphanum" => is::alphanum,
"is::alpha" => is::alpha, "is::alpha" => is::alpha,
"is::ascii" => is::ascii, "is::ascii" => is::ascii,
"is::datetime" => is::datetime,
"is::domain" => is::domain, "is::domain" => is::domain,
"is::email" => is::email, "is::email" => is::email,
"is::hexadecimal" => is::hexadecimal, "is::hexadecimal" => is::hexadecimal,

View file

@ -21,6 +21,7 @@ impl ModuleDef for Package {
module.add("alphanum")?; module.add("alphanum")?;
module.add("alpha")?; module.add("alpha")?;
module.add("ascii")?; module.add("ascii")?;
module.add("datetime")?;
module.add("domain")?; module.add("domain")?;
module.add("email")?; module.add("email")?;
module.add("hexadecimal")?; module.add("hexadecimal")?;
@ -38,6 +39,7 @@ impl ModuleDef for Package {
module.set("alphanum", Func::from(|v: Any| run("is::alphanum", v.0)))?; module.set("alphanum", Func::from(|v: Any| run("is::alphanum", v.0)))?;
module.set("alpha", Func::from(|v: Any| run("is::alpha", v.0)))?; module.set("alpha", Func::from(|v: Any| run("is::alpha", v.0)))?;
module.set("ascii", Func::from(|v: Any| run("is::ascii", v.0)))?; module.set("ascii", Func::from(|v: Any| run("is::ascii", v.0)))?;
module.set("datetime", Func::from(|v: Any| run("is::datetime", v.0)))?;
module.set("domain", Func::from(|v: Any| run("is::domain", v.0)))?; module.set("domain", Func::from(|v: Any| run("is::domain", v.0)))?;
module.set("email", Func::from(|v: Any| run("is::email", v.0)))?; module.set("email", Func::from(|v: Any| run("is::email", v.0)))?;
module.set("hexadecimal", Func::from(|v: Any| run("is::hexadecimal", v.0)))?; module.set("hexadecimal", Func::from(|v: Any| run("is::hexadecimal", v.0)))?;
@ -52,6 +54,7 @@ impl ModuleDef for Package {
default.set("alphanum", Func::from(|v: Any| run("is::alphanum", v.0)))?; default.set("alphanum", Func::from(|v: Any| run("is::alphanum", v.0)))?;
default.set("alpha", Func::from(|v: Any| run("is::alpha", v.0)))?; default.set("alpha", Func::from(|v: Any| run("is::alpha", v.0)))?;
default.set("ascii", Func::from(|v: Any| run("is::ascii", v.0)))?; default.set("ascii", Func::from(|v: Any| run("is::ascii", v.0)))?;
default.set("datetime", Func::from(|v: Any| run("is::datetime", v.0)))?;
default.set("domain", Func::from(|v: Any| run("is::domain", v.0)))?; default.set("domain", Func::from(|v: Any| run("is::domain", v.0)))?;
default.set("email", Func::from(|v: Any| run("is::email", v.0)))?; default.set("email", Func::from(|v: Any| run("is::email", v.0)))?;
default.set("hexadecimal", Func::from(|v: Any| run("is::hexadecimal", v.0)))?; default.set("hexadecimal", Func::from(|v: Any| run("is::hexadecimal", v.0)))?;

View file

@ -310,6 +310,7 @@ fn function_is(i: &str) -> IResult<&str, &str> {
tag("is::alphanum"), tag("is::alphanum"),
tag("is::alpha"), tag("is::alpha"),
tag("is::ascii"), tag("is::ascii"),
tag("is::datetime"),
tag("is::domain"), tag("is::domain"),
tag("is::email"), tag("is::email"),
tag("is::hexadecimal"), tag("is::hexadecimal"),