Skip to content

Commit

Permalink
Add TimeDelta::{MIN, MAX} const values
Browse files Browse the repository at this point in the history
  • Loading branch information
djc committed Jul 8, 2024
1 parent e1cb2b6 commit f69baa4
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/naive/date/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
4 changes: 2 additions & 2 deletions src/naive/datetime/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
8 changes: 8 additions & 0 deletions src/time_delta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit f69baa4

Please sign in to comment.