Add time::is::leap_year() function (#4671)
This commit is contained in:
parent
6818ed6985
commit
4431809711
8 changed files with 59 additions and 1 deletions
core/src
sdk
|
@ -361,6 +361,7 @@ pub fn synchronous(
|
||||||
"time::from::millis" => time::from::millis,
|
"time::from::millis" => time::from::millis,
|
||||||
"time::from::secs" => time::from::secs,
|
"time::from::secs" => time::from::secs,
|
||||||
"time::from::unix" => time::from::unix,
|
"time::from::unix" => time::from::unix,
|
||||||
|
"time::is::leap_year" => time::is::leap_year,
|
||||||
//
|
//
|
||||||
"type::array" => r#type::array,
|
"type::array" => r#type::array,
|
||||||
"type::bool" => r#type::bool,
|
"type::bool" => r#type::bool,
|
||||||
|
@ -735,6 +736,7 @@ pub async fn idiom(
|
||||||
"format" => time::format,
|
"format" => time::format,
|
||||||
"group" => time::group,
|
"group" => time::group,
|
||||||
"hour" => time::hour,
|
"hour" => time::hour,
|
||||||
|
"is_leap_year" => time::is::leap_year,
|
||||||
"micros" => time::micros,
|
"micros" => time::micros,
|
||||||
"millis" => time::millis,
|
"millis" => time::millis,
|
||||||
"minute" => time::minute,
|
"minute" => time::minute,
|
||||||
|
|
|
@ -2,6 +2,7 @@ use super::run;
|
||||||
use crate::fnc::script::modules::impl_module_def;
|
use crate::fnc::script::modules::impl_module_def;
|
||||||
|
|
||||||
mod from;
|
mod from;
|
||||||
|
mod is;
|
||||||
|
|
||||||
#[non_exhaustive]
|
#[non_exhaustive]
|
||||||
pub struct Package;
|
pub struct Package;
|
||||||
|
@ -33,5 +34,6 @@ impl_module_def!(
|
||||||
"week" => run,
|
"week" => run,
|
||||||
"yday" => run,
|
"yday" => run,
|
||||||
"year" => run,
|
"year" => run,
|
||||||
"from" => (from::Package)
|
"from" => (from::Package),
|
||||||
|
"is" => (is::Package)
|
||||||
);
|
);
|
||||||
|
|
11
core/src/fnc/script/modules/surrealdb/functions/time/is.rs
Normal file
11
core/src/fnc/script/modules/surrealdb/functions/time/is.rs
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
use super::run;
|
||||||
|
use crate::fnc::script::modules::impl_module_def;
|
||||||
|
|
||||||
|
#[non_exhaustive]
|
||||||
|
pub struct Package;
|
||||||
|
|
||||||
|
impl_module_def!(
|
||||||
|
Package,
|
||||||
|
"time::is",
|
||||||
|
"leap_year" => run
|
||||||
|
);
|
|
@ -239,6 +239,18 @@ pub fn year((val,): (Option<Datetime>,)) -> Result<Value, Error> {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub mod is {
|
||||||
|
use crate::err::Error;
|
||||||
|
use crate::sql::{Datetime, Value};
|
||||||
|
|
||||||
|
pub fn leap_year((val,): (Option<Datetime>,)) -> Result<Value, Error> {
|
||||||
|
Ok(match val {
|
||||||
|
Some(v) => v.naive_utc().date().leap_year().into(),
|
||||||
|
None => Datetime::default().naive_utc().date().leap_year().into(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub mod from {
|
pub mod from {
|
||||||
|
|
||||||
use crate::err::Error;
|
use crate::err::Error;
|
||||||
|
|
|
@ -368,6 +368,7 @@ pub(crate) static PATHS: phf::Map<UniCase<&'static str>, PathKind> = phf_map! {
|
||||||
UniCase::ascii("time::from::nanos") => PathKind::Function,
|
UniCase::ascii("time::from::nanos") => PathKind::Function,
|
||||||
UniCase::ascii("time::from::secs") => PathKind::Function,
|
UniCase::ascii("time::from::secs") => PathKind::Function,
|
||||||
UniCase::ascii("time::from::unix") => PathKind::Function,
|
UniCase::ascii("time::from::unix") => PathKind::Function,
|
||||||
|
UniCase::ascii("time::is::leap_year") => PathKind::Function,
|
||||||
//
|
//
|
||||||
UniCase::ascii("type::array") => PathKind::Function,
|
UniCase::ascii("type::array") => PathKind::Function,
|
||||||
UniCase::ascii("type::bool") => PathKind::Function,
|
UniCase::ascii("type::bool") => PathKind::Function,
|
||||||
|
|
|
@ -362,6 +362,7 @@
|
||||||
"time::format("
|
"time::format("
|
||||||
"time::group("
|
"time::group("
|
||||||
"time::hour("
|
"time::hour("
|
||||||
|
"time::is::leap_year("
|
||||||
"time::max("
|
"time::max("
|
||||||
"time::min("
|
"time::min("
|
||||||
"time::minute("
|
"time::minute("
|
||||||
|
|
|
@ -360,6 +360,7 @@
|
||||||
"time::format("
|
"time::format("
|
||||||
"time::group("
|
"time::group("
|
||||||
"time::hour("
|
"time::hour("
|
||||||
|
"time::is::leap_year("
|
||||||
"time::max("
|
"time::max("
|
||||||
"time::min("
|
"time::min("
|
||||||
"time::minute("
|
"time::minute("
|
||||||
|
|
|
@ -4434,6 +4434,34 @@ async fn function_time_hour() -> Result<(), Error> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn function_time_is_leap_year() -> Result<(), Error> {
|
||||||
|
let sql = r#"
|
||||||
|
RETURN time::is::leap_year();
|
||||||
|
RETURN time::is::leap_year(d"1987-06-22T08:30:45Z");
|
||||||
|
RETURN time::is::leap_year(d"1988-06-22T08:30:45Z");
|
||||||
|
RETURN d'2024-09-03T02:33:15.349397Z'.is_leap_year();
|
||||||
|
"#;
|
||||||
|
let mut test = Test::new(sql).await?;
|
||||||
|
//
|
||||||
|
let tmp = test.next()?.result?;
|
||||||
|
assert!(tmp.is_bool());
|
||||||
|
//
|
||||||
|
let tmp = test.next()?.result?;
|
||||||
|
let val = Value::from(false);
|
||||||
|
assert_eq!(tmp, val);
|
||||||
|
//
|
||||||
|
let tmp = test.next()?.result?;
|
||||||
|
let val = Value::from(true);
|
||||||
|
assert_eq!(tmp, val);
|
||||||
|
//
|
||||||
|
let tmp = test.next()?.result?;
|
||||||
|
let val = Value::from(true);
|
||||||
|
assert_eq!(tmp, val);
|
||||||
|
//
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn function_time_min() -> Result<(), Error> {
|
async fn function_time_min() -> Result<(), Error> {
|
||||||
let sql = r#"
|
let sql = r#"
|
||||||
|
|
Loading…
Reference in a new issue