From b98111b57937d91d1d56a05e7a8380f4c7322556 Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Mon, 8 Jul 2024 09:54:21 +0200 Subject: [PATCH 1/2] Apply clippy suggestions from Rust 1.79 --- src/format/parse.rs | 1 - src/offset/local/tz_info/rule.rs | 6 +++--- src/offset/local/tz_info/timezone.rs | 14 +++++++------- src/offset/mod.rs | 4 ++-- src/time_delta.rs | 2 +- tests/dateutils.rs | 5 ++++- 6 files changed, 17 insertions(+), 15 deletions(-) diff --git a/src/format/parse.rs b/src/format/parse.rs index ee26e66334..9c08bdb67f 100644 --- a/src/format/parse.rs +++ b/src/format/parse.rs @@ -6,7 +6,6 @@ use core::borrow::Borrow; use core::str; -use core::usize; use super::scan; use super::{Fixed, InternalFixed, InternalInternal, Item, Numeric, Pad, Parsed}; diff --git a/src/offset/local/tz_info/rule.rs b/src/offset/local/tz_info/rule.rs index daaed416f1..b74eeed156 100644 --- a/src/offset/local/tz_info/rule.rs +++ b/src/offset/local/tz_info/rule.rs @@ -156,7 +156,7 @@ impl AlternateTime { }; // Check if the current year is valid for the following computations - if !(i32::min_value() + 2 <= current_year && current_year <= i32::max_value() - 2) { + if !(i32::MIN + 2..=i32::MAX - 2).contains(¤t_year) { return Err(Error::OutOfRange("out of range date time")); } @@ -233,7 +233,7 @@ impl AlternateTime { current_year: i32, ) -> Result, Error> { // Check if the current year is valid for the following computations - if !(i32::min_value() + 2 <= current_year && current_year <= i32::max_value() - 2) { + if !(i32::MIN + 2..=i32::MAX - 2).contains(¤t_year) { return Err(Error::OutOfRange("out of range date time")); } @@ -687,7 +687,7 @@ impl UtcDateTime { let minute = (remaining_seconds / SECONDS_PER_MINUTE) % MINUTES_PER_HOUR; let second = remaining_seconds % SECONDS_PER_MINUTE; - let year = match year >= i32::min_value() as i64 && year <= i32::max_value() as i64 { + let year = match year >= i32::MIN as i64 && year <= i32::MAX as i64 { true => year as i32, false => return Err(Error::OutOfRange("i64 is out of range for i32")), }; diff --git a/src/offset/local/tz_info/timezone.rs b/src/offset/local/tz_info/timezone.rs index dbb2def931..1b089902ed 100644 --- a/src/offset/local/tz_info/timezone.rs +++ b/src/offset/local/tz_info/timezone.rs @@ -415,7 +415,7 @@ impl<'a> TimeZoneRef<'a> { /// Convert Unix leap time to Unix time, from the list of leap seconds in a time zone fn unix_leap_time_to_unix_time(&self, unix_leap_time: i64) -> Result { - if unix_leap_time == i64::min_value() { + if unix_leap_time == i64::MIN { return Err(Error::OutOfRange("out of range operation")); } @@ -572,7 +572,7 @@ pub(crate) struct LocalTimeType { impl LocalTimeType { /// Construct a local time type pub(super) fn new(ut_offset: i32, is_dst: bool, name: Option<&[u8]>) -> Result { - if ut_offset == i32::min_value() { + if ut_offset == i32::MIN { return Err(Error::LocalTimeType("invalid UTC offset")); } @@ -586,7 +586,7 @@ impl LocalTimeType { /// Construct a local time type with the specified UTC offset in seconds pub(super) const fn with_offset(ut_offset: i32) -> Result { - if ut_offset == i32::min_value() { + if ut_offset == i32::MIN { return Err(Error::LocalTimeType("invalid UTC offset")); } @@ -818,7 +818,7 @@ mod tests { let time_zone_3 = TimeZone::new(vec![Transition::new(0, 0)], utc_local_time_types.clone(), vec![], None)?; let time_zone_4 = TimeZone::new( - vec![Transition::new(i32::min_value().into(), 0), Transition::new(0, 1)], + vec![Transition::new(i32::MIN.into(), 0), Transition::new(0, 1)], vec![utc, cet], Vec::new(), Some(fixed_extra_rule), @@ -926,7 +926,7 @@ mod tests { #[test] fn test_leap_seconds_overflow() -> Result<(), Error> { let time_zone_err = TimeZone::new( - vec![Transition::new(i64::min_value(), 0)], + vec![Transition::new(i64::MIN, 0)], vec![LocalTimeType::UTC], vec![LeapSecond::new(0, 1)], Some(TransitionRule::from(LocalTimeType::UTC)), @@ -934,13 +934,13 @@ mod tests { assert!(time_zone_err.is_err()); let time_zone = TimeZone::new( - vec![Transition::new(i64::max_value(), 0)], + vec![Transition::new(i64::MAX, 0)], vec![LocalTimeType::UTC], vec![LeapSecond::new(0, 1)], None, )?; assert!(matches!( - time_zone.find_local_time_type(i64::max_value()), + time_zone.find_local_time_type(i64::MAX), Err(Error::FindLocalTimeType(_)) )); diff --git a/src/offset/mod.rs b/src/offset/mod.rs index c8f9c59c20..c04b9ef908 100644 --- a/src/offset/mod.rs +++ b/src/offset/mod.rs @@ -674,9 +674,9 @@ mod tests { #[test] fn test_nanos_never_panics() { - Utc.timestamp_nanos(i64::max_value()); + Utc.timestamp_nanos(i64::MAX); Utc.timestamp_nanos(i64::default()); - Utc.timestamp_nanos(i64::min_value()); + Utc.timestamp_nanos(i64::MIN); } #[test] diff --git a/src/time_delta.rs b/src/time_delta.rs index 6cf28eecde..608d43561f 100644 --- a/src/time_delta.rs +++ b/src/time_delta.rs @@ -10,9 +10,9 @@ //! Temporal quantification +use core::fmt; use core::ops::{Add, AddAssign, Div, Mul, Neg, Sub, SubAssign}; use core::time::Duration; -use core::{fmt, i64}; #[cfg(feature = "std")] use std::error::Error; diff --git a/tests/dateutils.rs b/tests/dateutils.rs index 5aa00da6bf..2dfca90023 100644 --- a/tests/dateutils.rs +++ b/tests/dateutils.rs @@ -1,8 +1,11 @@ #![cfg(all(unix, feature = "clock", feature = "std"))] -use chrono::{Datelike, Days, Local, NaiveDate, NaiveDateTime, NaiveTime, TimeZone, Timelike}; use std::{path, process, thread}; +#[cfg(target_os = "linux")] +use chrono::Days; +use chrono::{Datelike, Local, NaiveDate, NaiveDateTime, NaiveTime, TimeZone, Timelike}; + fn verify_against_date_command_local(path: &'static str, dt: NaiveDateTime) { let output = process::Command::new(path) .arg("-d") From 7bce97a2ac5850e3bde58713378c4b2c58fe5718 Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Mon, 8 Jul 2024 09:56:36 +0200 Subject: [PATCH 2/2] Add TimeDelta::{MIN, MAX} const values --- src/naive/date/tests.rs | 4 ++-- src/naive/datetime/tests.rs | 4 ++-- src/time_delta.rs | 8 ++++++++ 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/naive/date/tests.rs b/src/naive/date/tests.rs index ce5ffa4a68..6ae6867fc1 100644 --- a/src/naive/date/tests.rs +++ b/src/naive/date/tests.rs @@ -471,14 +471,14 @@ fn test_date_checked_add_signed() { ymd(MAX_YEAR, 12, 31), ); check(ymd(0, 1, 1), TimeDelta::try_days(MAX_DAYS_FROM_YEAR_0 as i64 + 1).unwrap(), None); - check(ymd(0, 1, 1), TimeDelta::max_value(), None); + check(ymd(0, 1, 1), TimeDelta::MAX, None); check( ymd(0, 1, 1), TimeDelta::try_days(MIN_DAYS_FROM_YEAR_0 as i64).unwrap(), ymd(MIN_YEAR, 1, 1), ); check(ymd(0, 1, 1), TimeDelta::try_days(MIN_DAYS_FROM_YEAR_0 as i64 - 1).unwrap(), None); - check(ymd(0, 1, 1), TimeDelta::min_value(), None); + check(ymd(0, 1, 1), TimeDelta::MIN, None); } #[test] diff --git a/src/naive/datetime/tests.rs b/src/naive/datetime/tests.rs index b5a3d5cf4f..161245813d 100644 --- a/src/naive/datetime/tests.rs +++ b/src/naive/datetime/tests.rs @@ -36,13 +36,13 @@ fn test_datetime_add() { Some((NaiveDate::MAX.year(), 12, 31, 23, 59, 59)), ); check((0, 1, 1, 0, 0, 0), max_days_from_year_0 + seconds(86_400), None); - check((0, 1, 1, 0, 0, 0), TimeDelta::max_value(), None); + check((0, 1, 1, 0, 0, 0), TimeDelta::MAX, None); let min_days_from_year_0 = NaiveDate::MIN.signed_duration_since(NaiveDate::from_ymd_opt(0, 1, 1).unwrap()); check((0, 1, 1, 0, 0, 0), min_days_from_year_0, Some((NaiveDate::MIN.year(), 1, 1, 0, 0, 0))); check((0, 1, 1, 0, 0, 0), min_days_from_year_0 - seconds(1), None); - check((0, 1, 1, 0, 0, 0), TimeDelta::min_value(), None); + check((0, 1, 1, 0, 0, 0), TimeDelta::MIN, None); } #[test] diff --git a/src/time_delta.rs b/src/time_delta.rs index 608d43561f..88aa31c6e3 100644 --- a/src/time_delta.rs +++ b/src/time_delta.rs @@ -417,12 +417,14 @@ impl TimeDelta { } /// The minimum possible `TimeDelta`: `-i64::MAX` milliseconds. + #[deprecated(since = "0.4.39", note = "Use `TimeDelta::MIN` instead")] #[inline] pub const fn min_value() -> TimeDelta { MIN } /// The maximum possible `TimeDelta`: `i64::MAX` milliseconds. + #[deprecated(since = "0.4.39", note = "Use `TimeDelta::MAX` instead")] #[inline] pub const fn max_value() -> TimeDelta { MAX @@ -474,6 +476,12 @@ impl TimeDelta { }; TimeDelta { secs: -self.secs - secs_diff, nanos } } + + /// The minimum possible `TimeDelta`: `-i64::MAX` milliseconds. + pub const MIN: Self = MIN; + + /// The maximum possible `TimeDelta`: `i64::MAX` milliseconds. + pub const MAX: Self = MAX; } impl Neg for TimeDelta {