Check for zero duration before trunc and round datetime ()

This commit is contained in:
hchockarprasad 2023-05-29 03:24:08 +05:30 committed by GitHub
parent f6c909e1a2
commit 88e6dc274a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -15,7 +15,10 @@ pub fn ceil((val, duration): (Datetime, Duration)) -> Result<Value, Error> {
floor.checked_add_signed(d)
}
};
// Check for zero duration.
if d.is_zero() {
return Ok(Value::Datetime(val));
}
let result = val
.duration_trunc(d)
.ok()
@ -45,12 +48,18 @@ pub fn day((val,): (Option<Datetime>,)) -> Result<Value, Error> {
pub fn floor((val, duration): (Datetime, Duration)) -> Result<Value, Error> {
match chrono::Duration::from_std(*duration) {
Ok(d) => match val.duration_trunc(d){
Ok(v) => Ok(v.into()),
_ => Err(Error::InvalidArguments {
name: String::from("time::floor"),
message: String::from("The second argument must be a duration, and must be able to be represented as nanoseconds."),
}),
Ok(d) => {
// Check for zero duration
if d.is_zero() {
return Ok(Value::Datetime(val));
}
match val.duration_trunc(d){
Ok(v) => Ok(v.into()),
_ => Err(Error::InvalidArguments {
name: String::from("time::floor"),
message: String::from("The second argument must be a duration, and must be able to be represented as nanoseconds."),
}),
}
},
_ => Err(Error::InvalidArguments {
name: String::from("time::floor"),
@ -136,12 +145,18 @@ pub fn now(_: ()) -> Result<Value, Error> {
pub fn round((val, duration): (Datetime, Duration)) -> Result<Value, Error> {
match chrono::Duration::from_std(*duration) {
Ok(d) => match val.duration_round(d) {
Ok(v) => Ok(v.into()),
_ => Err(Error::InvalidArguments {
name: String::from("time::round"),
message: String::from("The second argument must be a duration, and must be able to be represented as nanoseconds."),
}),
Ok(d) => {
// Check for zero duration
if d.is_zero() {
return Ok(Value::Datetime(val));
}
match val.duration_round(d) {
Ok(v) => Ok(v.into()),
_ => Err(Error::InvalidArguments {
name: String::from("time::round"),
message: String::from("The second argument must be a duration, and must be able to be represented as nanoseconds."),
}),
}
},
_ => Err(Error::InvalidArguments {
name: String::from("time::round"),