diff --git a/extensions/time/src/lib.rs b/extensions/time/src/lib.rs index 2f17af309..5c4d5a383 100644 --- a/extensions/time/src/lib.rs +++ b/extensions/time/src/lib.rs @@ -134,7 +134,7 @@ type Result = core::result::Result; #[scalar(name = "time_now", alias = "now")] fn time_now(args: &[Value]) -> Value { - if args.len() != 0 { + if !args.is_empty() { return Value::error(ResultCode::InvalidArgs); } let t = Time::new(); @@ -241,7 +241,7 @@ fn time_get(args: &[Value]) -> Value { let field = ok_tri!(args[1].to_text(), "2nd parameter: should be a field name"); - let field = tri!(TimeField::from_str(&field)); + let field = tri!(TimeField::from_str(field)); t.time_get(field) } @@ -627,7 +627,7 @@ fn time_equal(args: &[Value]) -> Value { /// 1 nanosecond #[scalar(name = "dur_ns")] fn dur_ns(args: &[Value]) -> Value { - if args.len() != 0 { + if !args.is_empty() { return Value::error(ResultCode::InvalidArgs); } @@ -637,7 +637,7 @@ fn dur_ns(args: &[Value]) -> Value { /// 1 microsecond #[scalar(name = "dur_us")] fn dur_us(args: &[Value]) -> Value { - if args.len() != 0 { + if !args.is_empty() { return Value::error(ResultCode::InvalidArgs); } @@ -647,7 +647,7 @@ fn dur_us(args: &[Value]) -> Value { /// 1 millisecond #[scalar(name = "dur_ms")] fn dur_ms(args: &[Value]) -> Value { - if args.len() != 0 { + if !args.is_empty() { return Value::error(ResultCode::InvalidArgs); } @@ -657,7 +657,7 @@ fn dur_ms(args: &[Value]) -> Value { /// 1 second #[scalar(name = "dur_s")] fn dur_s(args: &[Value]) -> Value { - if args.len() != 0 { + if !args.is_empty() { return Value::error(ResultCode::InvalidArgs); } @@ -667,7 +667,7 @@ fn dur_s(args: &[Value]) -> Value { /// 1 minute #[scalar(name = "dur_m")] fn dur_m(args: &[Value]) -> Value { - if args.len() != 0 { + if !args.is_empty() { return Value::error(ResultCode::InvalidArgs); } @@ -677,7 +677,7 @@ fn dur_m(args: &[Value]) -> Value { /// 1 hour #[scalar(name = "dur_h")] fn dur_h(args: &[Value]) -> Value { - if args.len() != 0 { + if !args.is_empty() { return Value::error(ResultCode::InvalidArgs); } @@ -832,7 +832,7 @@ fn time_trunc(args: &[Value]) -> Value { ValueType::Text => { let field = ok_tri!(args[1].to_text()); - let field = tri!(TimeRoundField::from_str(&field)); + let field = tri!(TimeRoundField::from_str(field)); tri!(t.trunc_field(field)).into_blob() } @@ -986,17 +986,17 @@ fn time_parse(args: &[Value]) -> Value { let dt_str = ok_tri!(args[0].to_text()); - if let Ok(dt) = chrono::DateTime::parse_from_rfc3339(&dt_str) { + if let Ok(dt) = chrono::DateTime::parse_from_rfc3339(dt_str) { return Time::from_datetime(dt.to_utc()).into_blob(); } - if let Ok(mut dt) = chrono::NaiveDateTime::parse_from_str(&dt_str, "%Y-%m-%d %H:%M:%S") { + if let Ok(mut dt) = chrono::NaiveDateTime::parse_from_str(dt_str, "%Y-%m-%d %H:%M:%S") { // Unwrap is safe here dt = dt.with_nanosecond(0).unwrap(); return Time::from_datetime(dt.and_utc()).into_blob(); } - if let Ok(date) = chrono::NaiveDate::parse_from_str(&dt_str, "%Y-%m-%d") { + if let Ok(date) = chrono::NaiveDate::parse_from_str(dt_str, "%Y-%m-%d") { // Unwrap is safe here let dt = date @@ -1008,7 +1008,7 @@ fn time_parse(args: &[Value]) -> Value { } let time = tri!( - chrono::NaiveTime::parse_from_str(&dt_str, "%H:%M:%S"), + chrono::NaiveTime::parse_from_str(dt_str, "%H:%M:%S"), "error parsing datetime string" ); let dt = NaiveDateTime::new(NaiveDate::from_ymd_opt(1, 1, 1).unwrap(), time) diff --git a/extensions/time/src/time.rs b/extensions/time/src/time.rs index 3b9d56b75..238d599d5 100644 --- a/extensions/time/src/time.rs +++ b/extensions/time/src/time.rs @@ -128,9 +128,9 @@ impl Time { let timezone_date = self.inner.with_timezone(offset); if timezone_date.nanosecond() == 0 { - return Ok(timezone_date.format("%FT%T%:z").to_string()); + Ok(timezone_date.format("%FT%T%:z").to_string()) } else { - return Ok(timezone_date.format("%FT%T%.9f%:z").to_string()); + Ok(timezone_date.format("%FT%T%.9f%:z").to_string()) } } @@ -145,7 +145,7 @@ impl Time { let timezone_date = self.inner.with_timezone(offset); - return Ok(timezone_date.format(fmt).to_string()); + Ok(timezone_date.format(fmt).to_string()) } pub fn fmt_date(&self, offset_sec: i32) -> Result { @@ -159,7 +159,7 @@ impl Time { let timezone_date = self.inner.with_timezone(offset); - return Ok(timezone_date.format(fmt).to_string()); + Ok(timezone_date.format(fmt).to_string()) } pub fn fmt_time(&self, offset_sec: i32) -> Result { @@ -173,7 +173,7 @@ impl Time { let timezone_date = self.inner.with_timezone(offset); - return Ok(timezone_date.format(fmt).to_string()); + Ok(timezone_date.format(fmt).to_string()) } /// Adjust the datetime to the offset @@ -182,6 +182,7 @@ impl Time { } // + #[allow(clippy::too_many_arguments)] pub fn time_date( year: i32, month: i32, @@ -197,25 +198,33 @@ impl Time { .and_hms_opt(0, 0, 0) .unwrap(); - if year > 0 { - dt = dt - .checked_add_months(chrono::Months::new((year - 1).abs() as u32 * 12)) - .ok_or(TimeError::CreationError)?; - } else if year < 0 { - dt = dt - .checked_sub_months(chrono::Months::new((year - 1).abs() as u32 * 12)) - .ok_or(TimeError::CreationError)?; - } + match year.cmp(&0) { + std::cmp::Ordering::Greater => { + dt = dt + .checked_add_months(chrono::Months::new((year - 1).unsigned_abs() * 12)) + .ok_or(TimeError::CreationError)? + } + std::cmp::Ordering::Less => { + dt = dt + .checked_sub_months(chrono::Months::new((year - 1).unsigned_abs() * 12)) + .ok_or(TimeError::CreationError)? + } + std::cmp::Ordering::Equal => (), + }; - if month > 0 { - dt = dt - .checked_add_months(chrono::Months::new((month - 1).abs() as u32)) - .ok_or(TimeError::CreationError)?; - } else if month < 0 { - dt = dt - .checked_sub_months(chrono::Months::new((month - 1).abs() as u32)) - .ok_or(TimeError::CreationError)?; - } + match month.cmp(&0) { + std::cmp::Ordering::Greater => { + dt = dt + .checked_add_months(chrono::Months::new((month - 1).unsigned_abs())) + .ok_or(TimeError::CreationError)? + } + std::cmp::Ordering::Less => { + dt = dt + .checked_sub_months(chrono::Months::new((month - 1).unsigned_abs())) + .ok_or(TimeError::CreationError)? + } + std::cmp::Ordering::Equal => (), + }; dt += chrono::Duration::try_days(day - 1).ok_or(TimeError::CreationError)?; @@ -237,25 +246,33 @@ impl Time { pub fn time_add_date(self, years: i32, months: i32, days: i64) -> Result { let mut dt: NaiveDateTime = self.into(); - if years > 0 { - dt = dt - .checked_add_months(chrono::Months::new(years.abs() as u32 * 12)) - .ok_or(TimeError::CreationError)?; - } else if years < 0 { - dt = dt - .checked_sub_months(chrono::Months::new(years.abs() as u32 * 12)) - .ok_or(TimeError::CreationError)?; - } + match years.cmp(&0) { + std::cmp::Ordering::Greater => { + dt = dt + .checked_add_months(chrono::Months::new(years.unsigned_abs() * 12)) + .ok_or(TimeError::CreationError)?; + } + std::cmp::Ordering::Less => { + dt = dt + .checked_sub_months(chrono::Months::new(years.unsigned_abs() * 12)) + .ok_or(TimeError::CreationError)?; + } + std::cmp::Ordering::Equal => (), + }; - if months > 0 { - dt = dt - .checked_add_months(chrono::Months::new(months.abs() as u32)) - .ok_or(TimeError::CreationError)?; - } else if months < 0 { - dt = dt - .checked_sub_months(chrono::Months::new(months.abs() as u32)) - .ok_or(TimeError::CreationError)?; - } + match months.cmp(&0) { + std::cmp::Ordering::Greater => { + dt = dt + .checked_add_months(chrono::Months::new(months.unsigned_abs())) + .ok_or(TimeError::CreationError)? + } + std::cmp::Ordering::Less => { + dt = dt + .checked_sub_months(chrono::Months::new(months.unsigned_abs())) + .ok_or(TimeError::CreationError)? + } + std::cmp::Ordering::Equal => (), + }; dt += chrono::Duration::try_days(days).ok_or(TimeError::CreationError)?; @@ -423,8 +440,7 @@ impl Time { Hour => Value::from_integer(self.inner.hour() as i64), Minute => Value::from_integer(self.inner.minute() as i64), Second => Value::from_float( - self.inner.second() as f64 - + (self.inner.nanosecond() as f64) / (1_000_000_000 as f64), + self.inner.second() as f64 + (self.inner.nanosecond() as f64) / (1_000_000_000_f64), ), MilliSecond | Milli => { Value::from_integer((self.inner.nanosecond() / 1_000_000 % 1_000) as i64) @@ -441,7 +457,7 @@ impl Time { YearDay => Value::from_integer(self.inner.ordinal() as i64), WeekDay => Value::from_integer(self.inner.weekday().num_days_from_sunday() as i64), Epoch => Value::from_float( - self.inner.timestamp() as f64 + self.inner.nanosecond() as f64 / 1_000_000_000 as f64, + self.inner.timestamp() as f64 + self.inner.nanosecond() as f64 / 1_000_000_000_f64, ), } } @@ -499,7 +515,7 @@ impl TryFrom> for Time { Ok(Self { inner: DateTime::from_timestamp(seconds - (3600 * 24 * DAYS_BEFORE_EPOCH), nanoseconds) - .ok_or_else(|| TimeError::InvalidFormat)? + .ok_or(TimeError::InvalidFormat)? .to_utc(), }) }