Rename remaining uses of Duration to TimeDelta

This commit is contained in:
Paul Dicker 2024-02-02 19:25:53 +01:00 committed by Paul Dicker
parent 3a9d8a3a3c
commit 6da68c8d04
7 changed files with 20 additions and 21 deletions

View File

@ -6,7 +6,7 @@ use chrono::format::StrftimeItems;
use chrono::prelude::*;
#[cfg(feature = "unstable-locales")]
use chrono::Locale;
use chrono::{DateTime, Duration, FixedOffset, Local, Utc, __BenchYearFlags};
use chrono::{DateTime, FixedOffset, Local, TimeDelta, Utc, __BenchYearFlags};
fn bench_datetime_parse_from_rfc2822(c: &mut Criterion) {
c.bench_function("bench_datetime_parse_from_rfc2822", |b| {
@ -198,7 +198,7 @@ fn bench_format_manual(c: &mut Criterion) {
fn bench_naivedate_add_signed(c: &mut Criterion) {
let date = NaiveDate::from_ymd_opt(2023, 7, 29).unwrap();
let extra = Duration::days(25);
let extra = TimeDelta::days(25);
c.bench_function("bench_naivedate_add_signed", |b| {
b.iter(|| black_box(date).checked_add_signed(extra).unwrap())
});

View File

@ -5,10 +5,9 @@
//! They can be constructed incrementally while being checked for consistency.
use super::{ParseResult, IMPOSSIBLE, NOT_ENOUGH, OUT_OF_RANGE};
use crate::duration::Duration as OldDuration;
use crate::naive::{NaiveDate, NaiveDateTime, NaiveTime};
use crate::offset::{FixedOffset, LocalResult, Offset, TimeZone};
use crate::{DateTime, Datelike, Timelike, Weekday};
use crate::{DateTime, Datelike, TimeDelta, Timelike, Weekday};
/// Parsed parts of date and time. There are two classes of methods:
///
@ -430,7 +429,7 @@ impl Parsed {
+ (week_from_sun as i32 - 1) * 7
+ weekday.num_days_from_sunday() as i32;
let date = newyear
.checked_add_signed(OldDuration::days(i64::from(ndays)))
.checked_add_signed(TimeDelta::days(i64::from(ndays)))
.ok_or(OUT_OF_RANGE)?;
if date.year() != year {
return Err(OUT_OF_RANGE);
@ -464,7 +463,7 @@ impl Parsed {
+ (week_from_mon as i32 - 1) * 7
+ weekday.num_days_from_monday() as i32;
let date = newyear
.checked_add_signed(OldDuration::days(i64::from(ndays)))
.checked_add_signed(TimeDelta::days(i64::from(ndays)))
.ok_or(OUT_OF_RANGE)?;
if date.year() != year {
return Err(OUT_OF_RANGE);
@ -587,7 +586,7 @@ impl Parsed {
59 => {}
// `datetime` is known to be off by one second.
0 => {
datetime -= OldDuration::seconds(1);
datetime -= TimeDelta::seconds(1);
}
// otherwise it is impossible.
_ => return Err(IMPOSSIBLE),

View File

@ -174,7 +174,7 @@
//!
//! ```rust
//! use chrono::prelude::*;
//! use chrono::Duration;
//! use chrono::TimeDelta;
//!
//! // assume this returned `2014-11-28T21:45:59.324310806+09:00`:
//! let dt = FixedOffset::east_opt(9*3600).unwrap().from_local_datetime(&NaiveDate::from_ymd_opt(2014, 11, 28).unwrap().and_hms_nano_opt(21, 45, 59, 324310806).unwrap()).unwrap();
@ -201,11 +201,11 @@
//! // arithmetic operations
//! let dt1 = Utc.with_ymd_and_hms(2014, 11, 14, 8, 9, 10).unwrap();
//! let dt2 = Utc.with_ymd_and_hms(2014, 11, 14, 10, 9, 8).unwrap();
//! assert_eq!(dt1.signed_duration_since(dt2), Duration::seconds(-2 * 3600 + 2));
//! assert_eq!(dt2.signed_duration_since(dt1), Duration::seconds(2 * 3600 - 2));
//! assert_eq!(Utc.with_ymd_and_hms(1970, 1, 1, 0, 0, 0).unwrap() + Duration::seconds(1_000_000_000),
//! assert_eq!(dt1.signed_duration_since(dt2), TimeDelta::seconds(-2 * 3600 + 2));
//! assert_eq!(dt2.signed_duration_since(dt1), TimeDelta::seconds(2 * 3600 - 2));
//! assert_eq!(Utc.with_ymd_and_hms(1970, 1, 1, 0, 0, 0).unwrap() + TimeDelta::seconds(1_000_000_000),
//! Utc.with_ymd_and_hms(2001, 9, 9, 1, 46, 40).unwrap());
//! assert_eq!(Utc.with_ymd_and_hms(1970, 1, 1, 0, 0, 0).unwrap() - Duration::seconds(1_000_000_000),
//! assert_eq!(Utc.with_ymd_and_hms(1970, 1, 1, 0, 0, 0).unwrap() - TimeDelta::seconds(1_000_000_000),
//! Utc.with_ymd_and_hms(1938, 4, 24, 22, 13, 20).unwrap());
//! ```
//!

View File

@ -187,7 +187,7 @@ impl TimeZone for Local {
mod tests {
use super::Local;
use crate::offset::TimeZone;
use crate::{Datelike, Duration, Utc};
use crate::{Datelike, TimeDelta, Utc};
#[test]
fn verify_correct_offsets() {
@ -204,8 +204,8 @@ mod tests {
#[test]
fn verify_correct_offsets_distant_past() {
// let distant_past = Local::now() - Duration::days(365 * 100);
let distant_past = Local::now() - Duration::days(250 * 31);
// let distant_past = Local::now() - TimeDelta::days(365 * 100);
let distant_past = Local::now() - TimeDelta::days(250 * 31);
let from_local = Local.from_local_datetime(&distant_past.naive_local()).unwrap();
let from_utc = Local.from_utc_datetime(&distant_past.naive_utc());
@ -218,7 +218,7 @@ mod tests {
#[test]
fn verify_correct_offsets_distant_future() {
let distant_future = Local::now() + Duration::days(250 * 31);
let distant_future = Local::now() + TimeDelta::days(250 * 31);
let from_local = Local.from_local_datetime(&distant_future.naive_local()).unwrap();
let from_utc = Local.from_utc_datetime(&distant_future.naive_utc());

View File

@ -330,7 +330,7 @@ pub trait Timelike: Sized {
#[cfg(test)]
mod tests {
use super::Datelike;
use crate::{Duration, NaiveDate};
use crate::{NaiveDate, TimeDelta};
/// Tests `Datelike::num_days_from_ce` against an alternative implementation.
///
@ -377,7 +377,7 @@ mod tests {
"on {:?}",
jan1_year
);
let mid_year = jan1_year + Duration::days(133);
let mid_year = jan1_year + TimeDelta::days(133);
assert_eq!(
mid_year.num_days_from_ce(),
num_days_from_ce(&mid_year),

View File

@ -94,7 +94,7 @@ fn try_verify_against_date_command() {
let end = NaiveDate::from_ymd_opt(*year + 1, 1, 1).unwrap().and_time(NaiveTime::MIN);
while date <= end {
verify_against_date_command_local(DATE_PATH, date);
date += chrono::Duration::hours(1);
date += chrono::TimeDelta::hours(1);
}
}));
}
@ -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::Duration::days(55);
date += chrono::TimeDelta::days(55);
}
}

View File

@ -25,7 +25,7 @@ fn now() {
let actual = NaiveDateTime::parse_from_str(&now, "%s").unwrap().and_utc();
let diff = utc - actual;
assert!(
diff < chrono::Duration::minutes(5),
diff < chrono::TimeDelta::minutes(5),
"expected {} - {} == {} < 5m (env var: {})",
utc,
actual,