Add TimeDelta::{MIN, MAX} const values

This commit is contained in:
Dirkjan Ochtman 2024-07-08 09:56:36 +02:00
parent e6f5d515cc
commit 2cec1d3a6b
3 changed files with 12 additions and 4 deletions

View File

@ -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]

View File

@ -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]

View File

@ -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 {