Skip to content

Commit

Permalink
Rename chrono::Duration to OldDuration in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
djc committed Aug 29, 2023
1 parent c6bf89e commit e602b4c
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 83 deletions.
48 changes: 24 additions & 24 deletions src/datetime/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::naive::{NaiveDate, NaiveTime};
use crate::offset::{FixedOffset, TimeZone, Utc};
#[cfg(feature = "clock")]
use crate::offset::{Local, Offset};
use crate::oldtime::Duration;
use crate::oldtime::Duration as OldDuration;
use crate::{Datelike, Days, LocalResult, Months, NaiveDateTime};

#[derive(Clone)]
Expand Down Expand Up @@ -54,7 +54,7 @@ impl TimeZone for DstTester {
DstTester::TO_WINTER_MONTH_DAY.1,
)
.unwrap()
.and_time(DstTester::transition_start_local() - Duration::hours(1));
.and_time(DstTester::transition_start_local() - OldDuration::hours(1));

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

if *local < local_to_winter_transition_end || *local >= local_to_summer_transition_end {
LocalResult::Single(DstTester::summer_offset())
Expand Down Expand Up @@ -267,7 +267,7 @@ fn ymdhms_milli(
fixedoffset
.with_ymd_and_hms(year, month, day, hour, min, sec)
.unwrap()
.checked_add_signed(Duration::milliseconds(milli))
.checked_add_signed(OldDuration::milliseconds(milli))
.unwrap()
}

Expand All @@ -287,7 +287,7 @@ fn ymdhms_micro(
fixedoffset
.with_ymd_and_hms(year, month, day, hour, min, sec)
.unwrap()
.checked_add_signed(Duration::microseconds(micro))
.checked_add_signed(OldDuration::microseconds(micro))
.unwrap()
}

Expand All @@ -307,7 +307,7 @@ fn ymdhms_nano(
fixedoffset
.with_ymd_and_hms(year, month, day, hour, min, sec)
.unwrap()
.checked_add_signed(Duration::nanoseconds(nano))
.checked_add_signed(OldDuration::nanoseconds(nano))
.unwrap()
}

Expand All @@ -328,7 +328,7 @@ fn ymdhms_milli_utc(
) -> DateTime<Utc> {
Utc.with_ymd_and_hms(year, month, day, hour, min, sec)
.unwrap()
.checked_add_signed(Duration::milliseconds(milli))
.checked_add_signed(OldDuration::milliseconds(milli))
.unwrap()
}

Expand Down Expand Up @@ -392,12 +392,12 @@ fn test_datetime_offset() {
let dt = Utc.with_ymd_and_hms(2014, 5, 6, 7, 8, 9).unwrap();
assert_eq!(dt, edt.with_ymd_and_hms(2014, 5, 6, 3, 8, 9).unwrap());
assert_eq!(
dt + Duration::seconds(3600 + 60 + 1),
dt + OldDuration::seconds(3600 + 60 + 1),
Utc.with_ymd_and_hms(2014, 5, 6, 8, 9, 10).unwrap()
);
assert_eq!(
dt.signed_duration_since(edt.with_ymd_and_hms(2014, 5, 6, 10, 11, 12).unwrap()),
Duration::seconds(-7 * 3600 - 3 * 60 - 3)
OldDuration::seconds(-7 * 3600 - 3 * 60 - 3)
);

assert_eq!(*Utc.with_ymd_and_hms(2014, 5, 6, 7, 8, 9).unwrap().offset(), Utc);
Expand Down Expand Up @@ -1275,16 +1275,16 @@ fn test_years_elapsed() {

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

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() + Duration::weeks(12);
let future = Utc::now().date_naive() + OldDuration::weeks(12);
assert_eq!(Utc::now().date_naive().years_since(future), None);
}

Expand All @@ -1294,20 +1294,20 @@ fn test_datetime_add_assign() {
let datetime = naivedatetime.and_utc();
let mut datetime_add = datetime;

datetime_add += Duration::seconds(60);
assert_eq!(datetime_add, datetime + Duration::seconds(60));
datetime_add += OldDuration::seconds(60);
assert_eq!(datetime_add, datetime + OldDuration::seconds(60));

let timezone = FixedOffset::east_opt(60 * 60).unwrap();
let datetime = datetime.with_timezone(&timezone);
let datetime_add = datetime_add.with_timezone(&timezone);

assert_eq!(datetime_add, datetime + Duration::seconds(60));
assert_eq!(datetime_add, datetime + OldDuration::seconds(60));

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

assert_eq!(datetime_add, datetime + Duration::seconds(60));
assert_eq!(datetime_add, datetime + OldDuration::seconds(60));
}

#[test]
Expand All @@ -1320,8 +1320,8 @@ fn test_datetime_add_assign_local() {

// ensure we cross a DST transition
for i in 1..=365 {
datetime_add += Duration::days(1);
assert_eq!(datetime_add, datetime + Duration::days(i))
datetime_add += OldDuration::days(1);
assert_eq!(datetime_add, datetime + OldDuration::days(i))
}
}

Expand All @@ -1331,20 +1331,20 @@ fn test_datetime_sub_assign() {
let datetime = naivedatetime.and_utc();
let mut datetime_sub = datetime;

datetime_sub -= Duration::minutes(90);
assert_eq!(datetime_sub, datetime - Duration::minutes(90));
datetime_sub -= OldDuration::minutes(90);
assert_eq!(datetime_sub, datetime - OldDuration::minutes(90));

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 - Duration::minutes(90));
assert_eq!(datetime_sub, datetime - OldDuration::minutes(90));

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 - Duration::minutes(90));
assert_eq!(datetime_sub, datetime - OldDuration::minutes(90));
}

#[test]
Expand All @@ -1357,8 +1357,8 @@ fn test_datetime_sub_assign_local() {

// ensure we cross a DST transition
for i in 1..=365 {
datetime_sub -= Duration::days(1);
assert_eq!(datetime_sub, datetime - Duration::days(i))
datetime_sub -= OldDuration::days(1);
assert_eq!(datetime_sub, datetime - OldDuration::days(i))
}
}

Expand Down
57 changes: 32 additions & 25 deletions src/naive/datetime/tests.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::NaiveDateTime;
use crate::oldtime::Duration;
use crate::oldtime::Duration as OldDuration;
use crate::NaiveDate;
use crate::{Datelike, FixedOffset, Utc};

Expand Down Expand Up @@ -97,7 +97,7 @@ fn test_datetime_from_timestamp() {
fn test_datetime_add() {
fn check(
(y, m, d, h, n, s): (i32, u32, u32, u32, u32, u32),
rhs: Duration,
rhs: OldDuration,
result: Option<(i32, u32, u32, u32, u32, u32)>,
) {
let lhs = NaiveDate::from_ymd_opt(y, m, d).unwrap().and_hms_opt(h, n, s).unwrap();
Expand All @@ -108,12 +108,16 @@ fn test_datetime_add() {
assert_eq!(lhs.checked_sub_signed(-rhs), sum);
}

check((2014, 5, 6, 7, 8, 9), Duration::seconds(3600 + 60 + 1), Some((2014, 5, 6, 8, 9, 10)));
check((2014, 5, 6, 7, 8, 9), Duration::seconds(-(3600 + 60 + 1)), Some((2014, 5, 6, 6, 7, 8)));
check((2014, 5, 6, 7, 8, 9), Duration::seconds(86399), Some((2014, 5, 7, 7, 8, 8)));
check((2014, 5, 6, 7, 8, 9), Duration::seconds(86_400 * 10), Some((2014, 5, 16, 7, 8, 9)));
check((2014, 5, 6, 7, 8, 9), Duration::seconds(-86_400 * 10), Some((2014, 4, 26, 7, 8, 9)));
check((2014, 5, 6, 7, 8, 9), Duration::seconds(86_400 * 10), Some((2014, 5, 16, 7, 8, 9)));
check((2014, 5, 6, 7, 8, 9), OldDuration::seconds(3600 + 60 + 1), Some((2014, 5, 6, 8, 9, 10)));
check(
(2014, 5, 6, 7, 8, 9),
OldDuration::seconds(-(3600 + 60 + 1)),
Some((2014, 5, 6, 6, 7, 8)),
);
check((2014, 5, 6, 7, 8, 9), OldDuration::seconds(86399), Some((2014, 5, 7, 7, 8, 8)));
check((2014, 5, 6, 7, 8, 9), OldDuration::seconds(86_400 * 10), Some((2014, 5, 16, 7, 8, 9)));
check((2014, 5, 6, 7, 8, 9), OldDuration::seconds(-86_400 * 10), Some((2014, 4, 26, 7, 8, 9)));
check((2014, 5, 6, 7, 8, 9), OldDuration::seconds(86_400 * 10), Some((2014, 5, 16, 7, 8, 9)));

// overflow check
// assumes that we have correct values for MAX/MIN_DAYS_FROM_YEAR_0 from `naive::date`.
Expand All @@ -123,40 +127,43 @@ fn test_datetime_add() {
check((0, 1, 1, 0, 0, 0), max_days_from_year_0, Some((NaiveDate::MAX.year(), 12, 31, 0, 0, 0)));
check(
(0, 1, 1, 0, 0, 0),
max_days_from_year_0 + Duration::seconds(86399),
max_days_from_year_0 + OldDuration::seconds(86399),
Some((NaiveDate::MAX.year(), 12, 31, 23, 59, 59)),
);
check((0, 1, 1, 0, 0, 0), max_days_from_year_0 + Duration::seconds(86_400), None);
check((0, 1, 1, 0, 0, 0), Duration::max_value(), None);
check((0, 1, 1, 0, 0, 0), max_days_from_year_0 + OldDuration::seconds(86_400), None);
check((0, 1, 1, 0, 0, 0), OldDuration::max_value(), None);

let min_days_from_year_0 =
NaiveDate::MIN.signed_duration_since(NaiveDate::from_ymd_opt(0, 1, 1).unwrap());
check((0, 1, 1, 0, 0, 0), min_days_from_year_0, Some((NaiveDate::MIN.year(), 1, 1, 0, 0, 0)));
check((0, 1, 1, 0, 0, 0), min_days_from_year_0 - Duration::seconds(1), None);
check((0, 1, 1, 0, 0, 0), Duration::min_value(), None);
check((0, 1, 1, 0, 0, 0), min_days_from_year_0 - OldDuration::seconds(1), None);
check((0, 1, 1, 0, 0, 0), OldDuration::min_value(), None);
}

#[test]
fn test_datetime_sub() {
let ymdhms =
|y, m, d, h, n, s| NaiveDate::from_ymd_opt(y, m, d).unwrap().and_hms_opt(h, n, s).unwrap();
let since = NaiveDateTime::signed_duration_since;
assert_eq!(since(ymdhms(2014, 5, 6, 7, 8, 9), ymdhms(2014, 5, 6, 7, 8, 9)), Duration::zero());
assert_eq!(
since(ymdhms(2014, 5, 6, 7, 8, 9), ymdhms(2014, 5, 6, 7, 8, 9)),
OldDuration::zero()
);
assert_eq!(
since(ymdhms(2014, 5, 6, 7, 8, 10), ymdhms(2014, 5, 6, 7, 8, 9)),
Duration::seconds(1)
OldDuration::seconds(1)
);
assert_eq!(
since(ymdhms(2014, 5, 6, 7, 8, 9), ymdhms(2014, 5, 6, 7, 8, 10)),
Duration::seconds(-1)
OldDuration::seconds(-1)
);
assert_eq!(
since(ymdhms(2014, 5, 7, 7, 8, 9), ymdhms(2014, 5, 6, 7, 8, 10)),
Duration::seconds(86399)
OldDuration::seconds(86399)
);
assert_eq!(
since(ymdhms(2001, 9, 9, 1, 46, 39), ymdhms(1970, 1, 1, 0, 0, 0)),
Duration::seconds(999_999_999)
OldDuration::seconds(999_999_999)
);
}

Expand All @@ -165,9 +172,9 @@ 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 += Duration::minutes(10_000_000);
date += OldDuration::minutes(10_000_000);
assert_eq!(date, ymdhms(2035, 10, 6, 20, 50, 10));
date += Duration::days(10);
date += OldDuration::days(10);
assert_eq!(date, ymdhms(2035, 10, 16, 20, 50, 10));
}

Expand All @@ -176,9 +183,9 @@ 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 -= Duration::minutes(10_000_000);
date -= OldDuration::minutes(10_000_000);
assert_eq!(date, ymdhms(1997, 9, 26, 23, 30, 10));
date -= Duration::days(10);
date -= OldDuration::days(10);
assert_eq!(date, ymdhms(1997, 9, 16, 23, 30, 10));
}

Expand Down Expand Up @@ -315,7 +322,7 @@ fn test_datetime_add_sub_invariant() {
// issue #37
let base = NaiveDate::from_ymd_opt(2000, 1, 1).unwrap().and_hms_opt(0, 0, 0).unwrap();
let t = -946684799990000;
let time = base + Duration::microseconds(t);
let time = base + OldDuration::microseconds(t);
assert_eq!(t, time.signed_duration_since(base).num_microseconds().unwrap());
}

Expand Down Expand Up @@ -344,7 +351,7 @@ fn test_nanosecond_range() {
fn test_nanosecond_just_beyond_range() {
let maximum = "2262-04-11T23:47:16.854775804";
let parsed: NaiveDateTime = maximum.parse().unwrap();
let beyond_max = parsed + Duration::milliseconds(300);
let beyond_max = parsed + OldDuration::milliseconds(300);
let _ = beyond_max.timestamp_nanos();
}

Expand All @@ -353,7 +360,7 @@ fn test_nanosecond_just_beyond_range() {
fn test_nanosecond_far_beyond_range() {
let maximum = "2262-04-11T23:47:16.854775804";
let parsed: NaiveDateTime = maximum.parse().unwrap();
let beyond_max = parsed + Duration::days(365);
let beyond_max = parsed + OldDuration::days(365);
let _ = beyond_max.timestamp_nanos();
}

Expand Down
Loading

0 comments on commit e602b4c

Please sign in to comment.