2022-05-14 12:35:08 +00:00
|
|
|
use crate::ctx::Context;
|
2022-01-13 17:37:46 +00:00
|
|
|
use crate::err::Error;
|
|
|
|
use crate::sql::datetime::Datetime;
|
|
|
|
use crate::sql::value::Value;
|
2022-01-18 22:23:46 +00:00
|
|
|
use chrono::prelude::*;
|
2022-01-13 17:37:46 +00:00
|
|
|
use chrono::Datelike;
|
|
|
|
use chrono::DurationRound;
|
|
|
|
use chrono::Timelike;
|
|
|
|
use chrono::Utc;
|
|
|
|
|
2022-05-14 12:35:08 +00:00
|
|
|
pub fn day(_: &Context, mut args: Vec<Value>) -> Result<Value, Error> {
|
2022-01-13 17:37:46 +00:00
|
|
|
match args.len() {
|
|
|
|
0 => Ok(Utc::now().day().into()),
|
|
|
|
_ => match args.remove(0) {
|
2022-05-05 05:01:00 +00:00
|
|
|
Value::Datetime(v) => Ok(v.day().into()),
|
2022-01-13 17:37:46 +00:00
|
|
|
_ => Ok(Value::None),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-14 12:35:08 +00:00
|
|
|
pub fn floor(_: &Context, mut args: Vec<Value>) -> Result<Value, Error> {
|
2022-01-13 17:37:46 +00:00
|
|
|
match args.remove(0) {
|
|
|
|
Value::Datetime(v) => match args.remove(0) {
|
2022-05-05 06:10:05 +00:00
|
|
|
Value::Duration(w) => match chrono::Duration::from_std(*w) {
|
2022-05-05 05:01:00 +00:00
|
|
|
Ok(d) => match v.duration_trunc(d) {
|
2022-01-13 17:37:46 +00:00
|
|
|
Ok(v) => Ok(v.into()),
|
|
|
|
_ => Ok(Value::None),
|
|
|
|
},
|
|
|
|
_ => Ok(Value::None),
|
|
|
|
},
|
|
|
|
_ => Ok(Value::None),
|
|
|
|
},
|
|
|
|
_ => Ok(Value::None),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-14 12:35:08 +00:00
|
|
|
pub fn group(_: &Context, mut args: Vec<Value>) -> Result<Value, Error> {
|
2022-01-18 22:23:46 +00:00
|
|
|
match args.remove(0) {
|
|
|
|
Value::Datetime(v) => match args.remove(0) {
|
2022-05-05 04:30:32 +00:00
|
|
|
Value::Strand(g) => match g.as_str() {
|
2022-05-05 05:01:00 +00:00
|
|
|
"year" => Ok(Utc.ymd(v.year(), 1, 1).and_hms(0, 0, 0).into()),
|
|
|
|
"month" => Ok(Utc.ymd(v.year(), v.month(), 1).and_hms(0, 0, 0).into()),
|
2022-01-18 22:23:46 +00:00
|
|
|
"day" => Ok(Utc
|
2022-05-05 05:01:00 +00:00
|
|
|
.ymd(v.year(), v.month(), v.day())
|
2022-01-18 22:23:46 +00:00
|
|
|
.and_hms(0, 0, 0)
|
|
|
|
.into()),
|
|
|
|
"hour" => Ok(Utc
|
2022-05-05 05:01:00 +00:00
|
|
|
.ymd(v.year(), v.month(), v.day())
|
|
|
|
.and_hms(v.hour(), 0, 0)
|
2022-01-18 22:23:46 +00:00
|
|
|
.into()),
|
|
|
|
"minute" => Ok(Utc
|
2022-05-05 05:01:00 +00:00
|
|
|
.ymd(v.year(), v.month(), v.day())
|
|
|
|
.and_hms(v.hour(), v.minute(), 0)
|
2022-01-18 22:23:46 +00:00
|
|
|
.into()),
|
|
|
|
"second" => Ok(Utc
|
2022-05-05 05:01:00 +00:00
|
|
|
.ymd(v.year(), v.month(), v.day())
|
|
|
|
.and_hms(v.hour(), v.minute(), v.second())
|
2022-01-18 22:23:46 +00:00
|
|
|
.into()),
|
2022-03-06 10:58:59 +00:00
|
|
|
_ => Err(Error::InvalidArguments {
|
2022-01-18 22:23:46 +00:00
|
|
|
name: String::from("time::group"),
|
|
|
|
message: String::from("The second argument must be a string, and can be one of 'year', 'month', 'day', 'hour', 'minute', or 'second'."),
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
_ => Ok(Value::None),
|
|
|
|
},
|
|
|
|
_ => Ok(Value::None),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-14 12:35:08 +00:00
|
|
|
pub fn hour(_: &Context, mut args: Vec<Value>) -> Result<Value, Error> {
|
2022-01-13 17:37:46 +00:00
|
|
|
match args.len() {
|
|
|
|
0 => Ok(Utc::now().hour().into()),
|
|
|
|
_ => match args.remove(0) {
|
2022-05-05 05:01:00 +00:00
|
|
|
Value::Datetime(v) => Ok(v.hour().into()),
|
2022-01-13 17:37:46 +00:00
|
|
|
_ => Ok(Value::None),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-14 12:35:08 +00:00
|
|
|
pub fn mins(_: &Context, mut args: Vec<Value>) -> Result<Value, Error> {
|
2022-01-13 17:37:46 +00:00
|
|
|
match args.len() {
|
|
|
|
0 => Ok(Utc::now().minute().into()),
|
|
|
|
_ => match args.remove(0) {
|
2022-05-05 05:01:00 +00:00
|
|
|
Value::Datetime(v) => Ok(v.minute().into()),
|
2022-01-13 17:37:46 +00:00
|
|
|
_ => Ok(Value::None),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-14 12:35:08 +00:00
|
|
|
pub fn month(_: &Context, mut args: Vec<Value>) -> Result<Value, Error> {
|
2022-01-13 17:37:46 +00:00
|
|
|
match args.len() {
|
2022-09-19 01:35:54 +00:00
|
|
|
0 => Ok(Utc::now().month().into()),
|
2022-01-13 17:37:46 +00:00
|
|
|
_ => match args.remove(0) {
|
2022-09-19 01:35:54 +00:00
|
|
|
Value::Datetime(v) => Ok(v.month().into()),
|
2022-01-13 17:37:46 +00:00
|
|
|
_ => Ok(Value::None),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-14 12:35:08 +00:00
|
|
|
pub fn nano(_: &Context, mut args: Vec<Value>) -> Result<Value, Error> {
|
2022-01-13 17:37:46 +00:00
|
|
|
match args.len() {
|
|
|
|
0 => Ok(Utc::now().timestamp_nanos().into()),
|
|
|
|
_ => match args.remove(0) {
|
2022-05-05 05:01:00 +00:00
|
|
|
Value::Datetime(v) => Ok(v.timestamp_nanos().into()),
|
2022-01-13 17:37:46 +00:00
|
|
|
_ => Ok(Value::None),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-14 12:35:08 +00:00
|
|
|
pub fn now(_: &Context, _: Vec<Value>) -> Result<Value, Error> {
|
2022-01-13 17:37:46 +00:00
|
|
|
Ok(Datetime::default().into())
|
|
|
|
}
|
|
|
|
|
2022-05-14 12:35:08 +00:00
|
|
|
pub fn round(_: &Context, mut args: Vec<Value>) -> Result<Value, Error> {
|
2022-01-13 17:37:46 +00:00
|
|
|
match args.remove(0) {
|
|
|
|
Value::Datetime(v) => match args.remove(0) {
|
2022-05-05 06:10:05 +00:00
|
|
|
Value::Duration(w) => match chrono::Duration::from_std(*w) {
|
2022-05-05 05:01:00 +00:00
|
|
|
Ok(d) => match v.duration_round(d) {
|
2022-01-13 17:37:46 +00:00
|
|
|
Ok(v) => Ok(v.into()),
|
|
|
|
_ => Ok(Value::None),
|
|
|
|
},
|
|
|
|
_ => Ok(Value::None),
|
|
|
|
},
|
|
|
|
_ => Ok(Value::None),
|
|
|
|
},
|
|
|
|
_ => Ok(Value::None),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-14 12:35:08 +00:00
|
|
|
pub fn secs(_: &Context, mut args: Vec<Value>) -> Result<Value, Error> {
|
2022-01-13 17:37:46 +00:00
|
|
|
match args.len() {
|
|
|
|
0 => Ok(Utc::now().second().into()),
|
|
|
|
_ => match args.remove(0) {
|
2022-05-05 05:01:00 +00:00
|
|
|
Value::Datetime(v) => Ok(v.second().into()),
|
2022-01-13 17:37:46 +00:00
|
|
|
_ => Ok(Value::None),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-14 12:35:08 +00:00
|
|
|
pub fn unix(_: &Context, mut args: Vec<Value>) -> Result<Value, Error> {
|
2022-01-13 17:37:46 +00:00
|
|
|
match args.len() {
|
|
|
|
0 => Ok(Utc::now().timestamp().into()),
|
|
|
|
_ => match args.remove(0) {
|
2022-05-05 05:01:00 +00:00
|
|
|
Value::Datetime(v) => Ok(v.timestamp().into()),
|
2022-01-13 17:37:46 +00:00
|
|
|
_ => Ok(Value::None),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-14 12:35:08 +00:00
|
|
|
pub fn wday(_: &Context, mut args: Vec<Value>) -> Result<Value, Error> {
|
2022-01-13 17:37:46 +00:00
|
|
|
match args.len() {
|
|
|
|
0 => Ok(Utc::now().weekday().number_from_monday().into()),
|
|
|
|
_ => match args.remove(0) {
|
2022-05-05 05:01:00 +00:00
|
|
|
Value::Datetime(v) => Ok(v.weekday().number_from_monday().into()),
|
2022-01-13 17:37:46 +00:00
|
|
|
_ => Ok(Value::None),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-14 12:35:08 +00:00
|
|
|
pub fn week(_: &Context, mut args: Vec<Value>) -> Result<Value, Error> {
|
2022-01-13 17:37:46 +00:00
|
|
|
match args.len() {
|
|
|
|
0 => Ok(Utc::now().iso_week().week().into()),
|
|
|
|
_ => match args.remove(0) {
|
2022-05-05 05:01:00 +00:00
|
|
|
Value::Datetime(v) => Ok(v.iso_week().week().into()),
|
2022-01-13 17:37:46 +00:00
|
|
|
_ => Ok(Value::None),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-14 12:35:08 +00:00
|
|
|
pub fn yday(_: &Context, mut args: Vec<Value>) -> Result<Value, Error> {
|
2022-01-13 17:37:46 +00:00
|
|
|
match args.len() {
|
|
|
|
0 => Ok(Utc::now().ordinal().into()),
|
|
|
|
_ => match args.remove(0) {
|
2022-05-05 05:01:00 +00:00
|
|
|
Value::Datetime(v) => Ok(v.ordinal().into()),
|
2022-01-13 17:37:46 +00:00
|
|
|
_ => Ok(Value::None),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-14 12:35:08 +00:00
|
|
|
pub fn year(_: &Context, mut args: Vec<Value>) -> Result<Value, Error> {
|
2022-01-13 17:37:46 +00:00
|
|
|
match args.len() {
|
|
|
|
0 => Ok(Utc::now().year().into()),
|
|
|
|
_ => match args.remove(0) {
|
2022-05-05 05:01:00 +00:00
|
|
|
Value::Datetime(v) => Ok(v.year().into()),
|
2022-01-13 17:37:46 +00:00
|
|
|
_ => Ok(Value::None),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|