Skip to content

Commit

Permalink
Tests: use Days type when it is more appropriate than TimeDelta
Browse files Browse the repository at this point in the history
  • Loading branch information
pitdicker committed Mar 6, 2024
1 parent 4251bd1 commit 51a1aa2
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 16 deletions.
16 changes: 6 additions & 10 deletions src/datetime/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ fn test_nanosecond_range() {
// Far beyond range
let maximum = "2262-04-11T23:47:16.854775804UTC";
let parsed: DateTime<Utc> = maximum.parse().unwrap();
let beyond_max = parsed + TimeDelta::days(365);
let beyond_max = parsed + Days::new(365);
assert!(beyond_max.timestamp_nanos_opt().is_none());
}

Expand Down Expand Up @@ -1455,20 +1455,16 @@ fn test_datetime_before_windows_api_limits() {
#[test]
#[cfg(feature = "clock")]
fn test_years_elapsed() {
const WEEKS_PER_YEAR: f32 = 52.1775;

// This is always at least one year because 1 year = 52.1775 weeks.
let one_year_ago =
Utc::now().date_naive() - TimeDelta::weeks((WEEKS_PER_YEAR * 1.5).ceil() as i64);
// A bit more than 2 years.
let two_year_ago =
Utc::now().date_naive() - TimeDelta::weeks((WEEKS_PER_YEAR * 2.5).ceil() as i64);
// A bit more than 1 year
let one_year_ago = Utc::now().date_naive() - Days::new(400);
// A bit more than 2 years
let two_year_ago = Utc::now().date_naive() - Days::new(750);

assert_eq!(Utc::now().date_naive().years_since(one_year_ago), Some(1));
assert_eq!(Utc::now().date_naive().years_since(two_year_ago), Some(2));

// If the given DateTime is later than now, the function will always return 0.
let future = Utc::now().date_naive() + TimeDelta::weeks(12);
let future = Utc::now().date_naive() + Days(100);
assert_eq!(Utc::now().date_naive().years_since(future), None);
}

Expand Down
6 changes: 3 additions & 3 deletions src/offset/local/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ mod tests {
#[cfg(windows)]
use crate::offset::local::{lookup_with_dst_transitions, Transition};
use crate::offset::TimeZone;
use crate::{Datelike, TimeDelta, Utc};
use crate::{Datelike, Days, Utc};
#[cfg(windows)]
use crate::{FixedOffset, LocalResult, NaiveDate, NaiveDateTime};

Expand All @@ -292,7 +292,7 @@ mod tests {

#[test]
fn verify_correct_offsets_distant_past() {
let distant_past = Local::now() - TimeDelta::days(365 * 500);
let distant_past = Local::now() - Days::new(365 * 500);
let from_local = Local.from_local_datetime(&distant_past.naive_local()).unwrap();
let from_utc = Local.from_utc_datetime(&distant_past.naive_utc());

Expand All @@ -305,7 +305,7 @@ mod tests {

#[test]
fn verify_correct_offsets_distant_future() {
let distant_future = Local::now() + TimeDelta::days(365 * 35000);
let distant_future = Local::now() + Days::new(365 * 35000);
let from_local = Local.from_local_datetime(&distant_future.naive_local()).unwrap();
let from_utc = Local.from_utc_datetime(&distant_future.naive_utc());

Expand Down
4 changes: 2 additions & 2 deletions src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ pub trait Timelike: Sized {
#[cfg(test)]
mod tests {
use super::Datelike;
use crate::{NaiveDate, TimeDelta};
use crate::{Days, NaiveDate};

/// Tests `Datelike::num_days_from_ce` against an alternative implementation.
///
Expand Down Expand Up @@ -377,7 +377,7 @@ mod tests {
"on {:?}",
jan1_year
);
let mid_year = jan1_year + TimeDelta::days(133);
let mid_year = jan1_year + Days::new(133);
assert_eq!(
mid_year.num_days_from_ce(),
num_days_from_ce(&mid_year),
Expand Down
2 changes: 1 addition & 1 deletion tests/dateutils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,6 @@ fn try_verify_against_date_command_format() {
let mut date = NaiveDate::from_ymd_opt(1970, 1, 1).unwrap().and_hms_opt(12, 11, 13).unwrap();
while date.year() < 2008 {
verify_against_date_command_format_local(DATE_PATH, date);
date += chrono::TimeDelta::days(55);
date = date + Days::new(55);
}
}

0 comments on commit 51a1aa2

Please sign in to comment.