Skip to content

Commit

Permalink
Make TimeDelta::hours infallible
Browse files Browse the repository at this point in the history
  • Loading branch information
pitdicker committed Mar 14, 2024
1 parent 7de31f5 commit bdd8aca
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 37 deletions.
14 changes: 4 additions & 10 deletions src/datetime/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl TimeZone for DstTester {
DstTester::TO_WINTER_MONTH_DAY.1,
)
.unwrap()
.and_time(DstTester::transition_start_local() - TimeDelta::hours(1).unwrap());
.and_time(DstTester::transition_start_local() - TimeDelta::hours(1));

let local_to_summer_transition_start = NaiveDate::from_ymd(
local.year(),
Expand All @@ -67,7 +67,7 @@ impl TimeZone for DstTester {
DstTester::TO_SUMMER_MONTH_DAY.1,
)
.unwrap()
.and_time(DstTester::transition_start_local() + TimeDelta::hours(1).unwrap());
.and_time(DstTester::transition_start_local() + TimeDelta::hours(1));

if *local < local_to_winter_transition_end || *local >= local_to_summer_transition_end {
LocalResult::Single(DstTester::summer_offset())
Expand Down Expand Up @@ -1522,10 +1522,7 @@ fn test_min_max_setters() {
assert_eq!(beyond_min.with_ordinal0(beyond_min.ordinal0()), Some(beyond_min));
assert_eq!(beyond_min.with_ordinal0(200), None);
assert_eq!(beyond_min.with_hour(beyond_min.hour()), Some(beyond_min));
assert_eq!(
beyond_min.with_hour(23),
beyond_min.checked_add_signed(TimeDelta::hours(1).unwrap())
);
assert_eq!(beyond_min.with_hour(23), beyond_min.checked_add_signed(TimeDelta::hours(1)));
assert_eq!(beyond_min.with_hour(5), None);
assert_eq!(beyond_min.with_minute(0), Some(beyond_min));
assert_eq!(beyond_min.with_second(0), Some(beyond_min));
Expand All @@ -1546,10 +1543,7 @@ fn test_min_max_setters() {
assert_eq!(beyond_max.with_ordinal0(beyond_max.ordinal0()), Some(beyond_max));
assert_eq!(beyond_max.with_ordinal0(200), None);
assert_eq!(beyond_max.with_hour(beyond_max.hour()), Some(beyond_max));
assert_eq!(
beyond_max.with_hour(0),
beyond_max.checked_sub_signed(TimeDelta::hours(1).unwrap())
);
assert_eq!(beyond_max.with_hour(0), beyond_max.checked_sub_signed(TimeDelta::hours(1)));
assert_eq!(beyond_max.with_hour(5), None);
assert_eq!(beyond_max.with_minute(beyond_max.minute()), Some(beyond_max));
assert_eq!(beyond_max.with_second(beyond_max.second()), Some(beyond_max));
Expand Down
12 changes: 6 additions & 6 deletions src/naive/time/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -527,15 +527,15 @@ impl NaiveTime {
/// let from_hms = |h, m, s| NaiveTime::from_hms(h, m, s).unwrap();
///
/// assert_eq!(
/// from_hms(3, 4, 5).overflowing_add_signed(TimeDelta::hours(11).unwrap()),
/// from_hms(3, 4, 5).overflowing_add_signed(TimeDelta::hours(11)),
/// (from_hms(14, 4, 5), 0)
/// );
/// assert_eq!(
/// from_hms(3, 4, 5).overflowing_add_signed(TimeDelta::hours(23).unwrap()),
/// from_hms(3, 4, 5).overflowing_add_signed(TimeDelta::hours(23)),
/// (from_hms(2, 4, 5), 86_400)
/// );
/// assert_eq!(
/// from_hms(3, 4, 5).overflowing_add_signed(TimeDelta::hours(-7).unwrap()),
/// from_hms(3, 4, 5).overflowing_add_signed(TimeDelta::hours(-7)),
/// (from_hms(20, 4, 5), -86_400)
/// );
/// ```
Expand Down Expand Up @@ -589,15 +589,15 @@ impl NaiveTime {
/// let from_hms = |h, m, s| NaiveTime::from_hms(h, m, s).unwrap();
///
/// assert_eq!(
/// from_hms(3, 4, 5).overflowing_sub_signed(TimeDelta::hours(2).unwrap()),
/// from_hms(3, 4, 5).overflowing_sub_signed(TimeDelta::hours(2)),
/// (from_hms(1, 4, 5), 0)
/// );
/// assert_eq!(
/// from_hms(3, 4, 5).overflowing_sub_signed(TimeDelta::hours(17).unwrap()),
/// from_hms(3, 4, 5).overflowing_sub_signed(TimeDelta::hours(17)),
/// (from_hms(10, 4, 5), 86_400)
/// );
/// assert_eq!(
/// from_hms(3, 4, 5).overflowing_sub_signed(TimeDelta::hours(-22).unwrap()),
/// from_hms(3, 4, 5).overflowing_sub_signed(TimeDelta::hours(-22)),
/// (from_hms(1, 4, 5), -86_400)
/// );
/// ```
Expand Down
14 changes: 7 additions & 7 deletions src/naive/time/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,15 +126,15 @@ fn test_time_overflowing_add() {
let hmsm = |h, m, s, ms| NaiveTime::from_hms_milli(h, m, s, ms).unwrap();

assert_eq!(
hmsm(3, 4, 5, 678).overflowing_add_signed(TimeDelta::hours(11).unwrap()),
hmsm(3, 4, 5, 678).overflowing_add_signed(TimeDelta::hours(11)),
(hmsm(14, 4, 5, 678), 0)
);
assert_eq!(
hmsm(3, 4, 5, 678).overflowing_add_signed(TimeDelta::hours(23).unwrap()),
hmsm(3, 4, 5, 678).overflowing_add_signed(TimeDelta::hours(23)),
(hmsm(2, 4, 5, 678), 86_400)
);
assert_eq!(
hmsm(3, 4, 5, 678).overflowing_add_signed(TimeDelta::hours(-7).unwrap()),
hmsm(3, 4, 5, 678).overflowing_add_signed(TimeDelta::hours(-7)),
(hmsm(20, 4, 5, 678), -86_400)
);

Expand All @@ -153,19 +153,19 @@ fn test_time_overflowing_add() {
fn test_time_addassignment() {
let hms = |h, m, s| NaiveTime::from_hms(h, m, s).unwrap();
let mut time = hms(12, 12, 12);
time += TimeDelta::hours(10).unwrap();
time += TimeDelta::hours(10);
assert_eq!(time, hms(22, 12, 12));
time += TimeDelta::hours(10).unwrap();
time += TimeDelta::hours(10);
assert_eq!(time, hms(8, 12, 12));
}

#[test]
fn test_time_subassignment() {
let hms = |h, m, s| NaiveTime::from_hms(h, m, s).unwrap();
let mut time = hms(12, 12, 12);
time -= TimeDelta::hours(10).unwrap();
time -= TimeDelta::hours(10);
assert_eq!(time, hms(2, 12, 12));
time -= TimeDelta::hours(10).unwrap();
time -= TimeDelta::hours(10);
assert_eq!(time, hms(16, 12, 12));
}

Expand Down
2 changes: 1 addition & 1 deletion src/offset/local/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ mod tests {
if let Some(our_result) = Local.from_local_datetime(&date).earliest() {
assert_eq!(from_local_time(&date), our_result);
}
date += TimeDelta::hours(1).unwrap();
date += TimeDelta::hours(1);
}
}
}
8 changes: 4 additions & 4 deletions src/round.rs
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ mod tests {
"2012-12-12 18:30:00 UTC"
);
assert_eq!(
dt.duration_round(TimeDelta::hours(1).unwrap()).unwrap().to_string(),
dt.duration_round(TimeDelta::hours(1)).unwrap().to_string(),
"2012-12-12 18:00:00 UTC"
);
assert_eq!(
Expand Down Expand Up @@ -579,7 +579,7 @@ mod tests {
"2012-12-12 18:30:00"
);
assert_eq!(
dt.duration_round(TimeDelta::hours(1).unwrap()).unwrap().to_string(),
dt.duration_round(TimeDelta::hours(1)).unwrap().to_string(),
"2012-12-12 18:00:00"
);
assert_eq!(
Expand Down Expand Up @@ -642,7 +642,7 @@ mod tests {
"2012-12-12 18:00:00 UTC"
);
assert_eq!(
dt.duration_trunc(TimeDelta::hours(1).unwrap()).unwrap().to_string(),
dt.duration_trunc(TimeDelta::hours(1)).unwrap().to_string(),
"2012-12-12 18:00:00 UTC"
);
assert_eq!(
Expand Down Expand Up @@ -721,7 +721,7 @@ mod tests {
"2012-12-12 18:00:00"
);
assert_eq!(
dt.duration_trunc(TimeDelta::hours(1).unwrap()).unwrap().to_string(),
dt.duration_trunc(TimeDelta::hours(1)).unwrap().to_string(),
"2012-12-12 18:00:00"
);
assert_eq!(
Expand Down
12 changes: 4 additions & 8 deletions src/time_delta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,10 @@ impl TimeDelta {

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

/// Makes a new `TimeDelta` with the given number of minutes.
Expand Down Expand Up @@ -1082,7 +1078,7 @@ mod tests {
fn test_duration_const() {
const ONE_WEEK: TimeDelta = TimeDelta::weeks(1);
const ONE_DAY: TimeDelta = TimeDelta::days(1);
const ONE_HOUR: TimeDelta = expect!(TimeDelta::hours(1), "");
const ONE_HOUR: TimeDelta = TimeDelta::hours(1);
const ONE_MINUTE: TimeDelta = expect!(TimeDelta::minutes(1), "");
const ONE_SECOND: TimeDelta = expect!(TimeDelta::seconds(1), "");
const ONE_MILLI: TimeDelta = expect!(TimeDelta::milliseconds(1), "");
Expand Down
2 changes: 1 addition & 1 deletion tests/dateutils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ fn try_verify_against_date_command() {
let end = NaiveDate::from_ymd(*year + 1, 1, 1).unwrap().and_time(NaiveTime::MIN);
while date <= end {
verify_against_date_command_local(DATE_PATH, date);
date += chrono::TimeDelta::hours(1).unwrap();
date += chrono::TimeDelta::hours(1);
}
}));
}
Expand Down

0 comments on commit bdd8aca

Please sign in to comment.