Skip to content

Commit

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

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

if *local < local_to_winter_transition_end || *local >= local_to_summer_transition_end {
LocalResult::Single(DstTester::summer_offset())
Expand Down Expand Up @@ -1607,7 +1607,10 @@ 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)));
assert_eq!(
beyond_min.with_hour(23),
beyond_min.checked_add_signed(TimeDelta::try_hours(1).unwrap())
);
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 @@ -1628,7 +1631,10 @@ 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)));
assert_eq!(
beyond_max.with_hour(0),
beyond_max.checked_sub_signed(TimeDelta::try_hours(1).unwrap())
);
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 @@ -594,15 +594,15 @@ impl NaiveTime {
/// let from_hms = |h, m, s| NaiveTime::from_hms_opt(h, m, s).unwrap();
///
/// assert_eq!(
/// from_hms(3, 4, 5).overflowing_add_signed(TimeDelta::hours(11)),
/// from_hms(3, 4, 5).overflowing_add_signed(TimeDelta::try_hours(11).unwrap()),
/// (from_hms(14, 4, 5), 0)
/// );
/// assert_eq!(
/// from_hms(3, 4, 5).overflowing_add_signed(TimeDelta::hours(23)),
/// from_hms(3, 4, 5).overflowing_add_signed(TimeDelta::try_hours(23).unwrap()),
/// (from_hms(2, 4, 5), 86_400)
/// );
/// assert_eq!(
/// from_hms(3, 4, 5).overflowing_add_signed(TimeDelta::hours(-7)),
/// from_hms(3, 4, 5).overflowing_add_signed(TimeDelta::try_hours(-7).unwrap()),
/// (from_hms(20, 4, 5), -86_400)
/// );
/// ```
Expand Down Expand Up @@ -656,15 +656,15 @@ impl NaiveTime {
/// let from_hms = |h, m, s| NaiveTime::from_hms_opt(h, m, s).unwrap();
///
/// assert_eq!(
/// from_hms(3, 4, 5).overflowing_sub_signed(TimeDelta::hours(2)),
/// from_hms(3, 4, 5).overflowing_sub_signed(TimeDelta::try_hours(2).unwrap()),
/// (from_hms(1, 4, 5), 0)
/// );
/// assert_eq!(
/// from_hms(3, 4, 5).overflowing_sub_signed(TimeDelta::hours(17)),
/// from_hms(3, 4, 5).overflowing_sub_signed(TimeDelta::try_hours(17).unwrap()),
/// (from_hms(10, 4, 5), 86_400)
/// );
/// assert_eq!(
/// from_hms(3, 4, 5).overflowing_sub_signed(TimeDelta::hours(-22)),
/// from_hms(3, 4, 5).overflowing_sub_signed(TimeDelta::try_hours(-22).unwrap()),
/// (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 @@ -117,15 +117,15 @@ fn test_time_overflowing_add() {
let hmsm = |h, m, s, ms| NaiveTime::from_hms_milli_opt(h, m, s, ms).unwrap();

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

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

#[test]
fn test_time_subassignment() {
let hms = |h, m, s| NaiveTime::from_hms_opt(h, m, s).unwrap();
let mut time = hms(12, 12, 12);
time -= TimeDelta::hours(10);
time -= TimeDelta::try_hours(10).unwrap();
assert_eq!(time, hms(2, 12, 12));
time -= TimeDelta::hours(10);
time -= TimeDelta::try_hours(10).unwrap();
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 @@ -256,7 +256,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);
date += TimeDelta::try_hours(1).unwrap();
}
}
}
8 changes: 4 additions & 4 deletions src/round.rs
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ mod tests {
"2012-12-12 18:30:00 UTC"
);
assert_eq!(
dt.duration_round(TimeDelta::hours(1)).unwrap().to_string(),
dt.duration_round(TimeDelta::try_hours(1).unwrap()).unwrap().to_string(),
"2012-12-12 18:00:00 UTC"
);
assert_eq!(
Expand Down Expand Up @@ -597,7 +597,7 @@ mod tests {
"2012-12-12 18:30:00"
);
assert_eq!(
dt.duration_round(TimeDelta::hours(1)).unwrap().to_string(),
dt.duration_round(TimeDelta::try_hours(1).unwrap()).unwrap().to_string(),
"2012-12-12 18:00:00"
);
assert_eq!(
Expand Down Expand Up @@ -666,7 +666,7 @@ mod tests {
"2012-12-12 18:00:00 UTC"
);
assert_eq!(
dt.duration_trunc(TimeDelta::hours(1)).unwrap().to_string(),
dt.duration_trunc(TimeDelta::try_hours(1).unwrap()).unwrap().to_string(),
"2012-12-12 18:00:00 UTC"
);
assert_eq!(
Expand Down Expand Up @@ -753,7 +753,7 @@ mod tests {
"2012-12-12 18:00:00"
);
assert_eq!(
dt.duration_trunc(TimeDelta::hours(1)).unwrap().to_string(),
dt.duration_trunc(TimeDelta::try_hours(1).unwrap()).unwrap().to_string(),
"2012-12-12 18:00:00"
);
assert_eq!(
Expand Down
2 changes: 1 addition & 1 deletion src/time_delta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1200,7 +1200,7 @@ mod tests {
fn test_duration_const() {
const ONE_WEEK: TimeDelta = expect!(TimeDelta::try_weeks(1), "");
const ONE_DAY: TimeDelta = expect!(TimeDelta::try_days(1), "");
const ONE_HOUR: TimeDelta = TimeDelta::hours(1);
const ONE_HOUR: TimeDelta = expect!(TimeDelta::try_hours(1), "");
const ONE_MINUTE: TimeDelta = TimeDelta::minutes(1);
const ONE_SECOND: TimeDelta = TimeDelta::seconds(1);
const ONE_MILLI: TimeDelta = TimeDelta::milliseconds(1);
Expand Down

0 comments on commit 9fc931a

Please sign in to comment.