2022-01-13 17:37:46 +00:00
|
|
|
use crate::err::Error;
|
|
|
|
use crate::sql::datetime::Datetime;
|
|
|
|
use crate::sql::value::Value;
|
2023-01-08 20:56:59 +00:00
|
|
|
use chrono::offset::TimeZone;
|
2022-01-13 17:37:46 +00:00
|
|
|
use chrono::Datelike;
|
|
|
|
use chrono::DurationRound;
|
2023-01-08 20:56:59 +00:00
|
|
|
use chrono::Local;
|
2022-01-13 17:37:46 +00:00
|
|
|
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> {
|
2022-09-22 23:54:53 +00:00
|
|
|
Ok(match (datetime, duration) {
|
|
|
|
(Value::Datetime(v), Value::Duration(w)) => match chrono::Duration::from_std(*w) {
|
|
|
|
Ok(d) => match v.duration_trunc(d) {
|
|
|
|
Ok(v) => v.into(),
|
|
|
|
_ => Value::None,
|
2022-01-13 17:37:46 +00:00
|
|
|
},
|
2022-09-22 23:54:53 +00:00
|
|
|
_ => Value::None,
|
2022-01-13 17:37:46 +00:00
|
|
|
},
|
2022-09-22 23:54:53 +00:00
|
|
|
_ => Value::None,
|
|
|
|
})
|
2022-01-13 17:37:46 +00:00
|
|
|
}
|
|
|
|
|
2022-10-04 21:09:24 +00:00
|
|
|
pub fn format((datetime, format): (Value, String)) -> Result<Value, Error> {
|
|
|
|
Ok(match datetime {
|
|
|
|
Value::Datetime(v) => v.format(&format).to_string().into(),
|
|
|
|
_ => 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() {
|
2023-01-14 19:28:33 +00:00
|
|
|
"year" => Ok(Utc
|
|
|
|
.with_ymd_and_hms(v.year(), 1, 1, 0,0,0)
|
|
|
|
.earliest()
|
|
|
|
.unwrap()
|
|
|
|
.into()),
|
|
|
|
"month" => Ok(Utc
|
|
|
|
.with_ymd_and_hms(v.year(), v.month(), 1, 0,0,0)
|
|
|
|
.earliest()
|
|
|
|
.unwrap()
|
|
|
|
.into()),
|
2022-01-18 22:23:46 +00:00
|
|
|
"day" => Ok(Utc
|
2023-01-14 19:28:33 +00:00
|
|
|
.with_ymd_and_hms(v.year(), v.month(), v.day(), 0,0,0)
|
|
|
|
.earliest()
|
|
|
|
.unwrap()
|
2022-01-18 22:23:46 +00:00
|
|
|
.into()),
|
|
|
|
"hour" => Ok(Utc
|
2023-01-14 19:28:33 +00:00
|
|
|
.with_ymd_and_hms(v.year(), v.month(), v.day(), v.hour(),0,0)
|
|
|
|
.earliest()
|
|
|
|
.unwrap()
|
2022-01-18 22:23:46 +00:00
|
|
|
.into()),
|
|
|
|
"minute" => Ok(Utc
|
2023-01-14 19:28:33 +00:00
|
|
|
.with_ymd_and_hms(v.year(), v.month(), v.day(), v.hour(),v.minute(),0)
|
|
|
|
.earliest()
|
|
|
|
.unwrap()
|
2022-01-18 22:23:46 +00:00
|
|
|
.into()),
|
|
|
|
"second" => Ok(Utc
|
2023-01-14 19:28:33 +00:00
|
|
|
.with_ymd_and_hms(v.year(), v.month(), v.day(), v.hour(), v.minute(), v.second())
|
|
|
|
.earliest()
|
|
|
|
.unwrap()
|
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-11-03 10:50:51 +00:00
|
|
|
pub fn minute((datetime,): (Option<Value>,)) -> Result<Value, Error> {
|
2022-09-21 00:57:33 +00:00
|
|
|
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> {
|
2022-09-22 23:54:53 +00:00
|
|
|
Ok(match (datetime, duration) {
|
|
|
|
(Value::Datetime(v), Value::Duration(w)) => match chrono::Duration::from_std(*w) {
|
|
|
|
Ok(d) => match v.duration_round(d) {
|
|
|
|
Ok(v) => v.into(),
|
|
|
|
_ => Value::None,
|
2022-01-13 17:37:46 +00:00
|
|
|
},
|
2022-09-22 23:54:53 +00:00
|
|
|
_ => Value::None,
|
2022-01-13 17:37:46 +00:00
|
|
|
},
|
2022-09-22 23:54:53 +00:00
|
|
|
_ => Value::None,
|
|
|
|
})
|
2022-01-13 17:37:46 +00:00
|
|
|
}
|
|
|
|
|
2022-11-03 10:50:51 +00:00
|
|
|
pub fn second((datetime,): (Option<Value>,)) -> Result<Value, Error> {
|
2022-09-21 00:57:33 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-01-08 20:56:59 +00:00
|
|
|
pub fn timezone(_: ()) -> Result<Value, Error> {
|
|
|
|
Ok(Local::now().offset().to_string().into())
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|