Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate panicking constructors of TimeDelta #1450

Merged
merged 13 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Tests: replace TimeDelta::minutes with try_minutes
  • Loading branch information
pitdicker committed Mar 6, 2024
commit 786123e204839d778ec47397478eeb64b3daa401
8 changes: 4 additions & 4 deletions src/datetime/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1511,20 +1511,20 @@ fn test_datetime_sub_assign() {
let datetime = naivedatetime.and_utc();
let mut datetime_sub = datetime;

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

let timezone = FixedOffset::east_opt(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));
assert_eq!(datetime_sub, datetime - TimeDelta::try_minutes(90).unwrap());

let timezone = FixedOffset::west_opt(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));
assert_eq!(datetime_sub, datetime - TimeDelta::try_minutes(90).unwrap());
}

#[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 @@ -73,7 +73,7 @@ fn test_datetime_addassignment() {
let ymdhms =
|y, m, d, h, n, s| NaiveDate::from_ymd_opt(y, m, d).unwrap().and_hms_opt(h, n, s).unwrap();
let mut date = ymdhms(2016, 10, 1, 10, 10, 10);
date += TimeDelta::minutes(10_000_000);
date += TimeDelta::try_minutes(10_000_000).unwrap();
assert_eq!(date, ymdhms(2035, 10, 6, 20, 50, 10));
date += TimeDelta::try_days(10).unwrap();
assert_eq!(date, ymdhms(2035, 10, 16, 20, 50, 10));
Expand All @@ -84,7 +84,7 @@ fn test_datetime_subassignment() {
let ymdhms =
|y, m, d, h, n, s| NaiveDate::from_ymd_opt(y, m, d).unwrap().and_hms_opt(h, n, s).unwrap();
let mut date = ymdhms(2016, 10, 1, 10, 10, 10);
date -= TimeDelta::minutes(10_000_000);
date -= TimeDelta::try_minutes(10_000_000).unwrap();
assert_eq!(date, ymdhms(1997, 9, 26, 23, 30, 10));
date -= TimeDelta::try_days(10).unwrap();
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 @@ -478,7 +478,7 @@ mod tests {
)
.unwrap();
assert_eq!(
dt.duration_round(TimeDelta::minutes(5)).unwrap().to_string(),
dt.duration_round(TimeDelta::try_minutes(5).unwrap()).unwrap().to_string(),
"2012-12-12 18:25:00 UTC"
);
// round down
Expand All @@ -491,16 +491,16 @@ mod tests {
)
.unwrap();
assert_eq!(
dt.duration_round(TimeDelta::minutes(5)).unwrap().to_string(),
dt.duration_round(TimeDelta::try_minutes(5).unwrap()).unwrap().to_string(),
"2012-12-12 18:20:00 UTC"
);

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

assert_eq!(
dt.duration_round(TimeDelta::minutes(10)).unwrap().to_string(),
dt.duration_round(TimeDelta::try_minutes(10).unwrap()).unwrap().to_string(),
"2012-12-12 18:20:00"
);
assert_eq!(
dt.duration_round(TimeDelta::minutes(30)).unwrap().to_string(),
dt.duration_round(TimeDelta::try_minutes(30).unwrap()).unwrap().to_string(),
"2012-12-12 18:30:00"
);
assert_eq!(
Expand All @@ -610,7 +610,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().to_string(),
dt.duration_round(TimeDelta::try_minutes(10).unwrap()).unwrap().to_string(),
"1969-12-12 12:10:00 UTC"
);
}
Expand Down Expand Up @@ -641,7 +641,7 @@ mod tests {
)
.unwrap();
assert_eq!(
dt.duration_trunc(TimeDelta::minutes(5)).unwrap().to_string(),
dt.duration_trunc(TimeDelta::try_minutes(5).unwrap()).unwrap().to_string(),
"2012-12-12 18:20:00 UTC"
);
// would round down
Expand All @@ -654,15 +654,15 @@ mod tests {
)
.unwrap();
assert_eq!(
dt.duration_trunc(TimeDelta::minutes(5)).unwrap().to_string(),
dt.duration_trunc(TimeDelta::try_minutes(5).unwrap()).unwrap().to_string(),
"2012-12-12 18:20:00 UTC"
);
assert_eq!(
dt.duration_trunc(TimeDelta::minutes(10)).unwrap().to_string(),
dt.duration_trunc(TimeDelta::try_minutes(10).unwrap()).unwrap().to_string(),
"2012-12-12 18:20:00 UTC"
);
assert_eq!(
dt.duration_trunc(TimeDelta::minutes(30)).unwrap().to_string(),
dt.duration_trunc(TimeDelta::try_minutes(30).unwrap()).unwrap().to_string(),
"2012-12-12 18:00:00 UTC"
);
assert_eq!(
Expand Down Expand Up @@ -727,7 +727,7 @@ mod tests {
.unwrap()
.naive_utc();
assert_eq!(
dt.duration_trunc(TimeDelta::minutes(5)).unwrap().to_string(),
dt.duration_trunc(TimeDelta::try_minutes(5).unwrap()).unwrap().to_string(),
"2012-12-12 18:20:00"
);
// would round down
Expand All @@ -741,15 +741,15 @@ mod tests {
.unwrap()
.naive_utc();
assert_eq!(
dt.duration_trunc(TimeDelta::minutes(5)).unwrap().to_string(),
dt.duration_trunc(TimeDelta::try_minutes(5).unwrap()).unwrap().to_string(),
"2012-12-12 18:20:00"
);
assert_eq!(
dt.duration_trunc(TimeDelta::minutes(10)).unwrap().to_string(),
dt.duration_trunc(TimeDelta::try_minutes(10).unwrap()).unwrap().to_string(),
"2012-12-12 18:20:00"
);
assert_eq!(
dt.duration_trunc(TimeDelta::minutes(30)).unwrap().to_string(),
dt.duration_trunc(TimeDelta::try_minutes(30).unwrap()).unwrap().to_string(),
"2012-12-12 18:00:00"
);
assert_eq!(
Expand All @@ -766,7 +766,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().to_string(),
dt.duration_trunc(TimeDelta::try_minutes(10).unwrap()).unwrap().to_string(),
"1969-12-12 12:10:00 UTC"
);
}
Expand All @@ -788,7 +788,7 @@ mod tests {

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

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

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

let dt = NaiveDate::from_ymd_opt(1970, 1, 1).unwrap().and_hms_opt(0, 0, 15).unwrap();
assert_eq!(dt.duration_round(span).unwrap().to_string(), "1970-01-01 00:00:00");
Expand Down
4 changes: 2 additions & 2 deletions src/time_delta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,7 @@ mod tests {
assert_eq!(-(days(3) + TimeDelta::seconds(70)), days(-4) + TimeDelta::seconds(86_400 - 70));

let mut d = TimeDelta::default();
d += TimeDelta::minutes(1);
d += TimeDelta::try_minutes(1).unwrap();
d -= TimeDelta::seconds(30);
assert_eq!(d, TimeDelta::seconds(30));
}
Expand Down Expand Up @@ -1201,7 +1201,7 @@ mod tests {
const ONE_WEEK: TimeDelta = expect!(TimeDelta::try_weeks(1), "");
const ONE_DAY: TimeDelta = expect!(TimeDelta::try_days(1), "");
const ONE_HOUR: TimeDelta = expect!(TimeDelta::try_hours(1), "");
const ONE_MINUTE: TimeDelta = TimeDelta::minutes(1);
const ONE_MINUTE: TimeDelta = expect!(TimeDelta::try_minutes(1), "");
const ONE_SECOND: TimeDelta = TimeDelta::seconds(1);
const ONE_MILLI: TimeDelta = 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),
diff < chrono::TimeDelta::try_minutes(5).unwrap(),
"expected {} - {} == {} < 5m (env var: {})",
utc,
actual,
Expand Down