Skip to content

Commit

Permalink
Make TimeDelta::minutes infallible
Browse files Browse the repository at this point in the history
  • Loading branch information
pitdicker committed Mar 14, 2024
1 parent bdd8aca commit a06283c
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 36 deletions.
8 changes: 4 additions & 4 deletions src/datetime/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1426,20 +1426,20 @@ fn test_datetime_sub_assign() {
let datetime = naivedatetime.and_utc();
let mut datetime_sub = datetime;

datetime_sub -= TimeDelta::minutes(90).unwrap();
assert_eq!(datetime_sub, datetime - TimeDelta::minutes(90).unwrap());
datetime_sub -= TimeDelta::minutes(90);
assert_eq!(datetime_sub, datetime - TimeDelta::minutes(90));

let timezone = FixedOffset::east(60 * 60).unwrap();
let datetime = datetime.with_timezone(&timezone);
let datetime_sub = datetime_sub.with_timezone(&timezone);

assert_eq!(datetime_sub, datetime - TimeDelta::minutes(90).unwrap());
assert_eq!(datetime_sub, datetime - TimeDelta::minutes(90));

let timezone = FixedOffset::west(2 * 60 * 60).unwrap();
let datetime = datetime.with_timezone(&timezone);
let datetime_sub = datetime_sub.with_timezone(&timezone);

assert_eq!(datetime_sub, datetime - TimeDelta::minutes(90).unwrap());
assert_eq!(datetime_sub, datetime - TimeDelta::minutes(90));
}

#[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 @@ -72,7 +72,7 @@ fn test_datetime_sub() {
fn test_datetime_addassignment() {
let ymdhms = |y, m, d, h, n, s| NaiveDate::from_ymd(y, m, d).unwrap().and_hms(h, n, s).unwrap();
let mut date = ymdhms(2016, 10, 1, 10, 10, 10);
date += TimeDelta::minutes(10_000_000).unwrap();
date += TimeDelta::minutes(10_000_000);
assert_eq!(date, ymdhms(2035, 10, 6, 20, 50, 10));
date += TimeDelta::days(10);
assert_eq!(date, ymdhms(2035, 10, 16, 20, 50, 10));
Expand All @@ -82,7 +82,7 @@ fn test_datetime_addassignment() {
fn test_datetime_subassignment() {
let ymdhms = |y, m, d, h, n, s| NaiveDate::from_ymd(y, m, d).unwrap().and_hms(h, n, s).unwrap();
let mut date = ymdhms(2016, 10, 1, 10, 10, 10);
date -= TimeDelta::minutes(10_000_000).unwrap();
date -= TimeDelta::minutes(10_000_000);
assert_eq!(date, ymdhms(1997, 9, 26, 23, 30, 10));
date -= TimeDelta::days(10);
assert_eq!(date, ymdhms(1997, 9, 16, 23, 30, 10));
Expand Down
40 changes: 20 additions & 20 deletions src/round.rs
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ mod tests {
)
.unwrap();
assert_eq!(
dt.duration_round(TimeDelta::minutes(5).unwrap()).unwrap().to_string(),
dt.duration_round(TimeDelta::minutes(5)).unwrap().to_string(),
"2012-12-12 18:25:00 UTC"
);
// round down
Expand All @@ -481,16 +481,16 @@ mod tests {
)
.unwrap();
assert_eq!(
dt.duration_round(TimeDelta::minutes(5).unwrap()).unwrap().to_string(),
dt.duration_round(TimeDelta::minutes(5)).unwrap().to_string(),
"2012-12-12 18:20:00 UTC"
);

assert_eq!(
dt.duration_round(TimeDelta::minutes(10).unwrap()).unwrap().to_string(),
dt.duration_round(TimeDelta::minutes(10)).unwrap().to_string(),
"2012-12-12 18:20:00 UTC"
);
assert_eq!(
dt.duration_round(TimeDelta::minutes(30).unwrap()).unwrap().to_string(),
dt.duration_round(TimeDelta::minutes(30)).unwrap().to_string(),
"2012-12-12 18:30:00 UTC"
);
assert_eq!(
Expand Down Expand Up @@ -555,7 +555,7 @@ mod tests {
.unwrap()
.naive_utc();
assert_eq!(
dt.duration_round(TimeDelta::minutes(5).unwrap()).unwrap().to_string(),
dt.duration_round(TimeDelta::minutes(5)).unwrap().to_string(),
"2012-12-12 18:25:00"
);
// round down
Expand All @@ -566,16 +566,16 @@ mod tests {
.unwrap()
.naive_utc();
assert_eq!(
dt.duration_round(TimeDelta::minutes(5).unwrap()).unwrap().to_string(),
dt.duration_round(TimeDelta::minutes(5)).unwrap().to_string(),
"2012-12-12 18:20:00"
);

assert_eq!(
dt.duration_round(TimeDelta::minutes(10).unwrap()).unwrap().to_string(),
dt.duration_round(TimeDelta::minutes(10)).unwrap().to_string(),
"2012-12-12 18:20:00"
);
assert_eq!(
dt.duration_round(TimeDelta::minutes(30).unwrap()).unwrap().to_string(),
dt.duration_round(TimeDelta::minutes(30)).unwrap().to_string(),
"2012-12-12 18:30:00"
);
assert_eq!(
Expand All @@ -592,7 +592,7 @@ mod tests {
fn test_duration_round_pre_epoch() {
let dt = Utc.with_ymd_and_hms(1969, 12, 12, 12, 12, 12).unwrap();
assert_eq!(
dt.duration_round(TimeDelta::minutes(10).unwrap()).unwrap().to_string(),
dt.duration_round(TimeDelta::minutes(10)).unwrap().to_string(),
"1969-12-12 12:10:00 UTC"
);
}
Expand Down Expand Up @@ -620,7 +620,7 @@ mod tests {
)
.unwrap();
assert_eq!(
dt.duration_trunc(TimeDelta::minutes(5).unwrap()).unwrap().to_string(),
dt.duration_trunc(TimeDelta::minutes(5)).unwrap().to_string(),
"2012-12-12 18:20:00 UTC"
);
// would round down
Expand All @@ -630,15 +630,15 @@ mod tests {
)
.unwrap();
assert_eq!(
dt.duration_trunc(TimeDelta::minutes(5).unwrap()).unwrap().to_string(),
dt.duration_trunc(TimeDelta::minutes(5)).unwrap().to_string(),
"2012-12-12 18:20:00 UTC"
);
assert_eq!(
dt.duration_trunc(TimeDelta::minutes(10).unwrap()).unwrap().to_string(),
dt.duration_trunc(TimeDelta::minutes(10)).unwrap().to_string(),
"2012-12-12 18:20:00 UTC"
);
assert_eq!(
dt.duration_trunc(TimeDelta::minutes(30).unwrap()).unwrap().to_string(),
dt.duration_trunc(TimeDelta::minutes(30)).unwrap().to_string(),
"2012-12-12 18:00:00 UTC"
);
assert_eq!(
Expand Down Expand Up @@ -698,7 +698,7 @@ mod tests {
.unwrap()
.naive_utc();
assert_eq!(
dt.duration_trunc(TimeDelta::minutes(5).unwrap()).unwrap().to_string(),
dt.duration_trunc(TimeDelta::minutes(5)).unwrap().to_string(),
"2012-12-12 18:20:00"
);
// would round down
Expand All @@ -709,15 +709,15 @@ mod tests {
.unwrap()
.naive_utc();
assert_eq!(
dt.duration_trunc(TimeDelta::minutes(5).unwrap()).unwrap().to_string(),
dt.duration_trunc(TimeDelta::minutes(5)).unwrap().to_string(),
"2012-12-12 18:20:00"
);
assert_eq!(
dt.duration_trunc(TimeDelta::minutes(10).unwrap()).unwrap().to_string(),
dt.duration_trunc(TimeDelta::minutes(10)).unwrap().to_string(),
"2012-12-12 18:20:00"
);
assert_eq!(
dt.duration_trunc(TimeDelta::minutes(30).unwrap()).unwrap().to_string(),
dt.duration_trunc(TimeDelta::minutes(30)).unwrap().to_string(),
"2012-12-12 18:00:00"
);
assert_eq!(
Expand All @@ -734,7 +734,7 @@ mod tests {
fn test_duration_trunc_pre_epoch() {
let dt = Utc.with_ymd_and_hms(1969, 12, 12, 12, 12, 12).unwrap();
assert_eq!(
dt.duration_trunc(TimeDelta::minutes(10).unwrap()).unwrap().to_string(),
dt.duration_trunc(TimeDelta::minutes(10)).unwrap().to_string(),
"1969-12-12 12:10:00 UTC"
);
}
Expand All @@ -756,7 +756,7 @@ mod tests {

#[test]
fn test_duration_trunc_close_to_epoch() {
let span = TimeDelta::minutes(15).unwrap();
let span = TimeDelta::minutes(15);

let dt = NaiveDate::from_ymd(1970, 1, 1).unwrap().and_hms(0, 0, 15).unwrap();
assert_eq!(dt.duration_trunc(span).unwrap().to_string(), "1970-01-01 00:00:00");
Expand All @@ -767,7 +767,7 @@ mod tests {

#[test]
fn test_duration_round_close_to_epoch() {
let span = TimeDelta::minutes(15).unwrap();
let span = TimeDelta::minutes(15);

let dt = NaiveDate::from_ymd(1970, 1, 1).unwrap().and_hms(0, 0, 15).unwrap();
assert_eq!(dt.duration_round(span).unwrap().to_string(), "1970-01-01 00:00:00");
Expand Down
14 changes: 5 additions & 9 deletions src/time_delta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,10 @@ impl TimeDelta {

/// Makes a new `TimeDelta` with the given number of minutes.
///
/// Equivalent to `TimeDelta::seconds(minutes * 60)` with overflow checks.
///
/// # Errors
///
/// Returns `None` when the `TimeDelta` would be out of bounds.
/// Equivalent to `TimeDelta::new(minutes as i64 * 60, 0).unwrap()`.
#[inline]
pub const fn minutes(minutes: i64) -> Option<TimeDelta> {
TimeDelta::seconds(try_opt!(minutes.checked_mul(SECS_PER_MINUTE)))
pub const fn minutes(minutes: i32) -> TimeDelta {
expect!(TimeDelta::new(minutes as i64 * SECS_PER_MINUTE, 0), "always in range")
}

/// Makes a new `TimeDelta` with the given number of seconds.
Expand Down Expand Up @@ -557,7 +553,7 @@ mod tests {
assert_eq!(-(days(3) + seconds(70)), days(-4) + seconds(86_400 - 70));

let mut d = TimeDelta::default();
d += TimeDelta::minutes(1).unwrap();
d += TimeDelta::minutes(1);
d -= seconds(30);
assert_eq!(d, seconds(30));
}
Expand Down Expand Up @@ -1079,7 +1075,7 @@ mod tests {
const ONE_WEEK: TimeDelta = TimeDelta::weeks(1);
const ONE_DAY: TimeDelta = TimeDelta::days(1);
const ONE_HOUR: TimeDelta = TimeDelta::hours(1);
const ONE_MINUTE: TimeDelta = expect!(TimeDelta::minutes(1), "");
const ONE_MINUTE: TimeDelta = TimeDelta::minutes(1);
const ONE_SECOND: TimeDelta = expect!(TimeDelta::seconds(1), "");
const ONE_MILLI: TimeDelta = expect!(TimeDelta::milliseconds(1), "");
const ONE_MICRO: TimeDelta = TimeDelta::microseconds(1);
Expand Down
2 changes: 1 addition & 1 deletion tests/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ fn now() {
let actual = NaiveDateTime::parse_from_str(&now, "%s").unwrap().and_utc();
let diff = utc - actual;
assert!(
diff < chrono::TimeDelta::minutes(5).unwrap(),
diff < chrono::TimeDelta::minutes(5),
"expected {} - {} == {} < 5m (env var: {})",
utc,
actual,
Expand Down

0 comments on commit a06283c

Please sign in to comment.