Add time::is::leap_year() function (#4671)

This commit is contained in:
Dave MacLeod 2024-09-04 04:30:59 +09:00 committed by GitHub
parent 6818ed6985
commit 4431809711
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 59 additions and 1 deletions

View file

@ -361,6 +361,7 @@ pub fn synchronous(
"time::from::millis" => time::from::millis,
"time::from::secs" => time::from::secs,
"time::from::unix" => time::from::unix,
"time::is::leap_year" => time::is::leap_year,
//
"type::array" => r#type::array,
"type::bool" => r#type::bool,
@ -735,6 +736,7 @@ pub async fn idiom(
"format" => time::format,
"group" => time::group,
"hour" => time::hour,
"is_leap_year" => time::is::leap_year,
"micros" => time::micros,
"millis" => time::millis,
"minute" => time::minute,

View file

@ -2,6 +2,7 @@ use super::run;
use crate::fnc::script::modules::impl_module_def;
mod from;
mod is;
#[non_exhaustive]
pub struct Package;
@ -33,5 +34,6 @@ impl_module_def!(
"week" => run,
"yday" => run,
"year" => run,
"from" => (from::Package)
"from" => (from::Package),
"is" => (is::Package)
);

View 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
);

View file

@ -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 {
use crate::err::Error;

View file

@ -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::secs") => 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::bool") => PathKind::Function,

View file

@ -362,6 +362,7 @@
"time::format("
"time::group("
"time::hour("
"time::is::leap_year("
"time::max("
"time::min("
"time::minute("

View file

@ -360,6 +360,7 @@
"time::format("
"time::group("
"time::hour("
"time::is::leap_year("
"time::max("
"time::min("
"time::minute("

View file

@ -4434,6 +4434,34 @@ async fn function_time_hour() -> Result<(), Error> {
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]
async fn function_time_min() -> Result<(), Error> {
let sql = r#"