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-09-21 00:57:33 +00:00
|
|
|
pub fn day((datetime,): (Option<Value>,)) -> Result<Value, Error> {
|
|
|
|
let date = match datetime {
|
|
|
|
Some(Value::Datetime(v)) => v,
|
|
|
|
None => Datetime::default(),
|
|
|
|
Some(_) => return Ok(Value::None),
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(date.day().into())
|
2022-01-13 17:37:46 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 00:57:33 +00:00
|
|
|
pub fn floor((datetime, duration): (Value, Value)) -> Result<Value, Error> {
|
|
|
|
match datetime {
|
|
|
|
Value::Datetime(v) => match duration {
|
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-09-21 00:57:33 +00:00
|
|
|
pub fn group((datetime, strand): (Value, Value)) -> Result<Value, Error> {
|
|
|
|
match datetime {
|
|
|
|
Value::Datetime(v) => match strand {
|
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-09-21 00:57:33 +00:00
|
|
|
pub fn hour((datetime,): (Option<Value>,)) -> Result<Value, Error> {
|
|
|
|
let date = match datetime {
|
|
|
|
Some(Value::Datetime(v)) => v,
|
|
|
|
None => Datetime::default(),
|
|
|
|
Some(_) => return Ok(Value::None),
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(date.hour().into())
|
2022-01-13 17:37:46 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 00:57:33 +00:00
|
|
|
pub fn mins((datetime,): (Option<Value>,)) -> Result<Value, Error> {
|
|
|
|
let date = match datetime {
|
|
|
|
Some(Value::Datetime(v)) => v,
|
|
|
|
None => Datetime::default(),
|
|
|
|
Some(_) => return Ok(Value::None),
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(date.minute().into())
|
2022-01-13 17:37:46 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 00:57:33 +00:00
|
|
|
pub fn month((datetime,): (Option<Value>,)) -> Result<Value, Error> {
|
|
|
|
let date = match datetime {
|
|
|
|
Some(Value::Datetime(v)) => v,
|
|
|
|
None => Datetime::default(),
|
|
|
|
Some(_) => return Ok(Value::None),
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(date.month().into())
|
2022-01-13 17:37:46 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 00:57:33 +00:00
|
|
|
pub fn nano((datetime,): (Option<Value>,)) -> Result<Value, Error> {
|
|
|
|
let date = match datetime {
|
|
|
|
Some(Value::Datetime(v)) => v,
|
|
|
|
None => Datetime::default(),
|
|
|
|
Some(_) => return Ok(Value::None),
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(date.timestamp_nanos().into())
|
2022-01-13 17:37:46 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 00:57:33 +00:00
|
|
|
pub fn now(_: ()) -> Result<Value, Error> {
|
2022-01-13 17:37:46 +00:00
|
|
|
Ok(Datetime::default().into())
|
|
|
|
}
|
|
|
|
|
2022-09-21 00:57:33 +00:00
|
|
|
pub fn round((datetime, duration): (Value, Value)) -> Result<Value, Error> {
|
|
|
|
match datetime {
|
|
|
|
Value::Datetime(v) => match duration {
|
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-09-21 00:57:33 +00:00
|
|
|
pub fn secs((datetime,): (Option<Value>,)) -> Result<Value, Error> {
|
|
|
|
let date = match datetime {
|
|
|
|
Some(Value::Datetime(v)) => v,
|
|
|
|
None => Datetime::default(),
|
|
|
|
Some(_) => return Ok(Value::None),
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(date.second().into())
|
2022-01-13 17:37:46 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 00:57:33 +00:00
|
|
|
pub fn unix((datetime,): (Option<Value>,)) -> Result<Value, Error> {
|
|
|
|
let date = match datetime {
|
|
|
|
Some(Value::Datetime(v)) => v,
|
|
|
|
None => Datetime::default(),
|
|
|
|
Some(_) => return Ok(Value::None),
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(date.timestamp().into())
|
2022-01-13 17:37:46 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 00:57:33 +00:00
|
|
|
pub fn wday((datetime,): (Option<Value>,)) -> Result<Value, Error> {
|
|
|
|
let date = match datetime {
|
|
|
|
Some(Value::Datetime(v)) => v,
|
|
|
|
None => Datetime::default(),
|
|
|
|
Some(_) => return Ok(Value::None),
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(date.weekday().number_from_monday().into())
|
2022-01-13 17:37:46 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 00:57:33 +00:00
|
|
|
pub fn week((datetime,): (Option<Value>,)) -> Result<Value, Error> {
|
|
|
|
let date = match datetime {
|
|
|
|
Some(Value::Datetime(v)) => v,
|
|
|
|
None => Datetime::default(),
|
|
|
|
Some(_) => return Ok(Value::None),
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(date.iso_week().week().into())
|
2022-01-13 17:37:46 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 00:57:33 +00:00
|
|
|
pub fn yday((datetime,): (Option<Value>,)) -> Result<Value, Error> {
|
|
|
|
let date = match datetime {
|
|
|
|
Some(Value::Datetime(v)) => v,
|
|
|
|
None => Datetime::default(),
|
|
|
|
Some(_) => return Ok(Value::None),
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(date.ordinal().into())
|
2022-01-13 17:37:46 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 00:57:33 +00:00
|
|
|
pub fn year((datetime,): (Option<Value>,)) -> Result<Value, Error> {
|
|
|
|
let date = match datetime {
|
|
|
|
Some(Value::Datetime(v)) => v,
|
|
|
|
None => Datetime::default(),
|
|
|
|
Some(_) => return Ok(Value::None),
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(date.year().into())
|
2022-01-13 17:37:46 +00:00
|
|
|
}
|