diff --git a/Cargo.toml b/Cargo.toml index a2b2554376..c2e058ad92 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,14 +23,13 @@ libc = [] winapi = ["windows-targets"] std = [] clock = ["std", "winapi", "iana-time-zone", "android-tzdata"] -oldtime = ["time"] +oldtime = [] wasmbind = ["wasm-bindgen", "js-sys"] unstable-locales = ["pure-rust-locales", "alloc"] __internal_bench = [] __doctest = [] [dependencies] -time = { version = "0.1.43", optional = true } num-traits = { version = "0.2", default-features = false } rustc-serialize = { version = "0.3.20", optional = true } serde = { version = "1.0.99", default-features = false, optional = true } diff --git a/README.md b/README.md index dde079804e..c3222ddaa3 100644 --- a/README.md +++ b/README.md @@ -61,7 +61,6 @@ Optional features: * `serde`: Enable serialization/deserialization via serde. * `rkyv`: Enable serialization/deserialization via rkyv. * `rustc-serialize`: Enable serialization/deserialization via rustc-serialize (deprecated). -* `old_time`: compatability with the `Duration` type of the `time` 0.1 crate (deprecated). * `arbitrary`: construct arbitrary instances of a type with the Arbitrary crate. * `unstable-locales`: Enable localization. This adds various methods with a `_localized` suffix. The implementation and API may change or even be removed in a patch release. Feedback welcome. diff --git a/src/date.rs b/src/date.rs index 15f3d436d1..5e84692370 100644 --- a/src/date.rs +++ b/src/date.rs @@ -13,13 +13,13 @@ use core::{fmt, hash}; #[cfg(feature = "rkyv")] use rkyv::{Archive, Deserialize, Serialize}; +use crate::duration::Duration as OldDuration; #[cfg(feature = "unstable-locales")] use crate::format::Locale; #[cfg(any(feature = "alloc", feature = "std"))] use crate::format::{DelayedFormat, Item, StrftimeItems}; use crate::naive::{IsoWeek, NaiveDate, NaiveTime}; use crate::offset::{TimeZone, Utc}; -use crate::oldtime::Duration as OldDuration; use crate::DateTime; use crate::{Datelike, Weekday}; @@ -576,7 +576,7 @@ where mod tests { use super::Date; - use crate::oldtime::Duration; + use crate::duration::Duration; use crate::{FixedOffset, NaiveDate, Utc}; #[cfg(feature = "clock")] diff --git a/src/datetime/mod.rs b/src/datetime/mod.rs index ec6ed4fe0e..650c63d0c0 100644 --- a/src/datetime/mod.rs +++ b/src/datetime/mod.rs @@ -14,6 +14,7 @@ use core::{fmt, hash, str}; #[cfg(feature = "std")] use std::time::{SystemTime, UNIX_EPOCH}; +use crate::duration::Duration as OldDuration; #[cfg(feature = "unstable-locales")] use crate::format::Locale; use crate::format::{ @@ -26,7 +27,6 @@ use crate::naive::{Days, IsoWeek, NaiveDate, NaiveDateTime, NaiveTime}; #[cfg(feature = "clock")] use crate::offset::Local; use crate::offset::{FixedOffset, Offset, TimeZone, Utc}; -use crate::oldtime::Duration as OldDuration; #[allow(deprecated)] use crate::Date; use crate::{Datelike, Months, Timelike, Weekday}; diff --git a/src/datetime/tests.rs b/src/datetime/tests.rs index 53d46682e8..cf24cb4963 100644 --- a/src/datetime/tests.rs +++ b/src/datetime/tests.rs @@ -1,9 +1,9 @@ use super::DateTime; +use crate::duration::Duration as OldDuration; use crate::naive::{NaiveDate, NaiveTime}; use crate::offset::{FixedOffset, TimeZone, Utc}; #[cfg(feature = "clock")] use crate::offset::{Local, Offset}; -use crate::oldtime::Duration as OldDuration; use crate::{Datelike, Days, LocalResult, Months, NaiveDateTime, Timelike}; #[derive(Clone)] diff --git a/src/oldtime.rs b/src/duration.rs similarity index 92% rename from src/oldtime.rs rename to src/duration.rs index 64ade7be6a..296c071093 100644 --- a/src/oldtime.rs +++ b/src/duration.rs @@ -22,11 +22,11 @@ use rkyv::{Archive, Deserialize, Serialize}; /// The number of nanoseconds in a microsecond. const NANOS_PER_MICRO: i32 = 1000; /// The number of nanoseconds in a millisecond. -const NANOS_PER_MILLI: i32 = 1000_000; +const NANOS_PER_MILLI: i32 = 1_000_000; /// The number of nanoseconds in seconds. const NANOS_PER_SEC: i32 = 1_000_000_000; /// The number of microseconds per second. -const MICROS_PER_SEC: i64 = 1000_000; +const MICROS_PER_SEC: i64 = 1_000_000; /// The number of milliseconds per second. const MILLIS_PER_SEC: i64 = 1000; /// The number of seconds in a minute. @@ -34,9 +34,9 @@ const SECS_PER_MINUTE: i64 = 60; /// The number of seconds in an hour. const SECS_PER_HOUR: i64 = 3600; /// The number of (non-leap) seconds in days. -const SECS_PER_DAY: i64 = 86400; +const SECS_PER_DAY: i64 = 86_400; /// The number of (non-leap) seconds in a week. -const SECS_PER_WEEK: i64 = 604800; +const SECS_PER_WEEK: i64 = 604_800; macro_rules! try_opt { ($e:expr) => { @@ -128,7 +128,7 @@ impl Duration { pub const fn milliseconds(milliseconds: i64) -> Duration { let (secs, millis) = div_mod_floor_64(milliseconds, MILLIS_PER_SEC); let nanos = millis as i32 * NANOS_PER_MILLI; - Duration { secs: secs, nanos: nanos } + Duration { secs, nanos } } /// Makes a new `Duration` with given number of microseconds. @@ -136,14 +136,14 @@ impl Duration { pub const fn microseconds(microseconds: i64) -> Duration { let (secs, micros) = div_mod_floor_64(microseconds, MICROS_PER_SEC); let nanos = micros as i32 * NANOS_PER_MICRO; - Duration { secs: secs, nanos: nanos } + Duration { secs, nanos } } /// Makes a new `Duration` with given number of nanoseconds. #[inline] pub const fn nanoseconds(nanos: i64) -> Duration { let (secs, nanos) = div_mod_floor_64(nanos, NANOS_PER_SEC as i64); - Duration { secs: secs, nanos: nanos as i32 } + Duration { secs, nanos: nanos as i32 } } /// Returns the total number of whole weeks in the duration. @@ -224,7 +224,7 @@ impl Duration { nanos -= NANOS_PER_SEC; secs = try_opt!(secs.checked_add(1)); } - let d = Duration { secs: secs, nanos: nanos }; + let d = Duration { secs, nanos }; // Even if d is within the bounds of i64 seconds, // it might still overflow i64 milliseconds. if d < MIN || d > MAX { @@ -243,7 +243,7 @@ impl Duration { nanos += NANOS_PER_SEC; secs = try_opt!(secs.checked_sub(1)); } - let d = Duration { secs: secs, nanos: nanos }; + let d = Duration { secs, nanos }; // Even if d is within the bounds of i64 seconds, // it might still overflow i64 milliseconds. if d < MIN || d > MAX { @@ -338,7 +338,7 @@ impl Add for Duration { nanos -= NANOS_PER_SEC; secs += 1; } - Duration { secs: secs, nanos: nanos } + Duration { secs, nanos } } } @@ -352,7 +352,7 @@ impl Sub for Duration { nanos += NANOS_PER_SEC; secs -= 1; } - Duration { secs: secs, nanos: nanos } + Duration { secs, nanos } } } @@ -364,7 +364,7 @@ impl Mul for Duration { let total_nanos = self.nanos as i64 * rhs as i64; let (extra_secs, nanos) = div_mod_floor_64(total_nanos, NANOS_PER_SEC as i64); let secs = self.secs * rhs as i64 + extra_secs; - Duration { secs: secs, nanos: nanos as i32 } + Duration { secs, nanos: nanos as i32 } } } @@ -384,7 +384,7 @@ impl Div for Duration { nanos += NANOS_PER_SEC; secs -= 1; } - Duration { secs: secs, nanos: nanos } + Duration { secs, nanos } } } @@ -491,19 +491,19 @@ mod tests { assert!(Duration::seconds(1) != Duration::zero()); assert_eq!(Duration::seconds(1) + Duration::seconds(2), Duration::seconds(3)); assert_eq!( - Duration::seconds(86399) + Duration::seconds(4), + Duration::seconds(86_399) + Duration::seconds(4), Duration::days(1) + Duration::seconds(3) ); - assert_eq!(Duration::days(10) - Duration::seconds(1000), Duration::seconds(863000)); - assert_eq!(Duration::days(10) - Duration::seconds(1000000), Duration::seconds(-136000)); + assert_eq!(Duration::days(10) - Duration::seconds(1000), Duration::seconds(863_000)); + assert_eq!(Duration::days(10) - Duration::seconds(1_000_000), Duration::seconds(-136_000)); assert_eq!( - Duration::days(2) + Duration::seconds(86399) + Duration::nanoseconds(1234567890), - Duration::days(3) + Duration::nanoseconds(234567890) + Duration::days(2) + Duration::seconds(86_399) + Duration::nanoseconds(1_234_567_890), + Duration::days(3) + Duration::nanoseconds(234_567_890) ); assert_eq!(-Duration::days(3), Duration::days(-3)); assert_eq!( -(Duration::days(3) + Duration::seconds(70)), - Duration::days(-4) + Duration::seconds(86400 - 70) + Duration::days(-4) + Duration::seconds(86_400 - 70) ); } @@ -512,10 +512,10 @@ mod tests { assert_eq!(Duration::zero().num_days(), 0); assert_eq!(Duration::days(1).num_days(), 1); assert_eq!(Duration::days(-1).num_days(), -1); - assert_eq!(Duration::seconds(86399).num_days(), 0); - assert_eq!(Duration::seconds(86401).num_days(), 1); - assert_eq!(Duration::seconds(-86399).num_days(), 0); - assert_eq!(Duration::seconds(-86401).num_days(), -1); + assert_eq!(Duration::seconds(86_399).num_days(), 0); + assert_eq!(Duration::seconds(86_401).num_days(), 1); + assert_eq!(Duration::seconds(-86_399).num_days(), 0); + assert_eq!(Duration::seconds(-86_401).num_days(), -1); assert_eq!(Duration::days(i32::MAX as i64).num_days(), i32::MAX as i64); assert_eq!(Duration::days(i32::MIN as i64).num_days(), i32::MIN as i64); } @@ -561,7 +561,7 @@ mod tests { assert_eq!(MIN.num_microseconds(), None); // overflow checks - const MICROS_PER_DAY: i64 = 86400_000_000; + const MICROS_PER_DAY: i64 = 86_400_000_000; assert_eq!( Duration::days(i64::MAX / MICROS_PER_DAY).num_microseconds(), Some(i64::MAX / MICROS_PER_DAY * MICROS_PER_DAY) @@ -585,7 +585,7 @@ mod tests { assert_eq!(MIN.num_nanoseconds(), None); // overflow checks - const NANOS_PER_DAY: i64 = 86400_000_000_000; + const NANOS_PER_DAY: i64 = 86_400_000_000_000; assert_eq!( Duration::days(i64::MAX / NANOS_PER_DAY).num_nanoseconds(), Some(i64::MAX / NANOS_PER_DAY * NANOS_PER_DAY) @@ -629,6 +629,7 @@ mod tests { } #[test] + #[allow(clippy::erasing_op)] fn test_duration_mul() { assert_eq!(Duration::zero() * i32::MAX, Duration::zero()); assert_eq!(Duration::zero() * i32::MIN, Duration::zero()); @@ -693,7 +694,7 @@ mod tests { assert_eq!(Duration::microseconds(42).to_string(), "PT0.000042S"); assert_eq!(Duration::nanoseconds(42).to_string(), "PT0.000000042S"); assert_eq!((Duration::days(7) + Duration::milliseconds(6543)).to_string(), "P7DT6.543S"); - assert_eq!(Duration::seconds(-86401).to_string(), "-P1DT1S"); + assert_eq!(Duration::seconds(-86_401).to_string(), "-P1DT1S"); assert_eq!(Duration::nanoseconds(-1).to_string(), "-PT0.000000001S"); // the format specifier should have no effect on `Duration` @@ -706,11 +707,14 @@ mod tests { #[test] fn test_to_std() { assert_eq!(Duration::seconds(1).to_std(), Ok(StdDuration::new(1, 0))); - assert_eq!(Duration::seconds(86401).to_std(), Ok(StdDuration::new(86401, 0))); - assert_eq!(Duration::milliseconds(123).to_std(), Ok(StdDuration::new(0, 123000000))); - assert_eq!(Duration::milliseconds(123765).to_std(), Ok(StdDuration::new(123, 765000000))); + assert_eq!(Duration::seconds(86_401).to_std(), Ok(StdDuration::new(86_401, 0))); + assert_eq!(Duration::milliseconds(123).to_std(), Ok(StdDuration::new(0, 123_000_000))); + assert_eq!( + Duration::milliseconds(123_765).to_std(), + Ok(StdDuration::new(123, 765_000_000)) + ); assert_eq!(Duration::nanoseconds(777).to_std(), Ok(StdDuration::new(0, 777))); - assert_eq!(MAX.to_std(), Ok(StdDuration::new(9223372036854775, 807000000))); + assert_eq!(MAX.to_std(), Ok(StdDuration::new(9_223_372_036_854_775, 807_000_000))); assert_eq!(Duration::seconds(-1).to_std(), Err(OutOfRangeError(()))); assert_eq!(Duration::milliseconds(-1).to_std(), Err(OutOfRangeError(()))); } @@ -718,23 +722,26 @@ mod tests { #[test] fn test_from_std() { assert_eq!(Ok(Duration::seconds(1)), Duration::from_std(StdDuration::new(1, 0))); - assert_eq!(Ok(Duration::seconds(86401)), Duration::from_std(StdDuration::new(86401, 0))); + assert_eq!(Ok(Duration::seconds(86_401)), Duration::from_std(StdDuration::new(86_401, 0))); assert_eq!( Ok(Duration::milliseconds(123)), - Duration::from_std(StdDuration::new(0, 123000000)) + Duration::from_std(StdDuration::new(0, 123_000_000)) ); assert_eq!( - Ok(Duration::milliseconds(123765)), - Duration::from_std(StdDuration::new(123, 765000000)) + Ok(Duration::milliseconds(123_765)), + Duration::from_std(StdDuration::new(123, 765_000_000)) ); assert_eq!(Ok(Duration::nanoseconds(777)), Duration::from_std(StdDuration::new(0, 777))); - assert_eq!(Ok(MAX), Duration::from_std(StdDuration::new(9223372036854775, 807000000))); assert_eq!( - Duration::from_std(StdDuration::new(9223372036854776, 0)), + Ok(MAX), + Duration::from_std(StdDuration::new(9_223_372_036_854_775, 807_000_000)) + ); + assert_eq!( + Duration::from_std(StdDuration::new(9_223_372_036_854_776, 0)), Err(OutOfRangeError(())) ); assert_eq!( - Duration::from_std(StdDuration::new(9223372036854775, 807000001)), + Duration::from_std(StdDuration::new(9_223_372_036_854_775, 807_000_001)), Err(OutOfRangeError(())) ); } diff --git a/src/format/parsed.rs b/src/format/parsed.rs index 431833ec7d..ad0d0013a3 100644 --- a/src/format/parsed.rs +++ b/src/format/parsed.rs @@ -5,9 +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::oldtime::Duration as OldDuration; use crate::{DateTime, Datelike, Timelike, Weekday}; /// Parsed parts of date and time. There are two classes of methods: diff --git a/src/lib.rs b/src/lib.rs index 3bcb789400..66368d9407 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -40,6 +40,8 @@ //! - `unstable-locales`: Enable localization. This adds various methods with a //! `_localized` suffix. The implementation and API may change or even be //! removed in a patch release. Feedback welcome. +//! - `oldtime`: this feature no langer has a function, but once offered compatibility with the +//! `time` 0.1 crate. //! //! [`serde`]: https://github.com/serde-rs/serde //! [wasm-bindgen]: https://github.com/rustwasm/wasm-bindgen @@ -60,14 +62,6 @@ //! nanoseconds and does not represent "nominal" components such as days or //! months. //! -//! When the `oldtime` feature is enabled, [`Duration`] is an alias for the -//! [`time::Duration`](https://docs.rs/time/0.1.40/time/struct.Duration.html) -//! type from v0.1 of the time crate. time v0.1 is deprecated, so new code -//! should disable the `oldtime` feature and use the `chrono::Duration` type -//! instead. The `oldtime` feature is enabled by default for backwards -//! compatibility, but future versions of Chrono are likely to remove the -//! feature entirely. -//! //! Chrono does not yet natively support //! the standard [`Duration`](https://doc.rust-lang.org/std/time/struct.Duration.html) type, //! but it will be supported in the future. @@ -385,15 +379,10 @@ #[cfg(feature = "alloc")] extern crate alloc; -#[cfg(feature = "oldtime")] -#[cfg_attr(docsrs, doc(cfg(feature = "oldtime")))] -extern crate time as oldtime; -#[cfg(not(feature = "oldtime"))] -mod oldtime; -// this reexport is to aid the transition and should not be in the prelude! -pub use oldtime::Duration; +mod duration; +pub use duration::Duration; #[cfg(feature = "std")] -pub use oldtime::OutOfRangeError; +pub use duration::OutOfRangeError; use core::fmt; diff --git a/src/naive/date.rs b/src/naive/date.rs index 53d698e3b1..860593498d 100644 --- a/src/naive/date.rs +++ b/src/naive/date.rs @@ -16,6 +16,7 @@ use rkyv::{Archive, Deserialize, Serialize}; #[cfg(feature = "unstable-locales")] use pure_rust_locales::Locale; +use crate::duration::Duration as OldDuration; #[cfg(any(feature = "alloc", feature = "std"))] use crate::format::DelayedFormat; use crate::format::{ @@ -24,7 +25,6 @@ use crate::format::{ }; use crate::month::Months; use crate::naive::{IsoWeek, NaiveDateTime, NaiveTime}; -use crate::oldtime::Duration as OldDuration; use crate::{expect, try_opt}; use crate::{Datelike, Weekday}; @@ -2390,7 +2390,7 @@ mod serde { #[cfg(test)] mod tests { use super::{Days, Months, NaiveDate, MAX_YEAR, MIN_YEAR}; - use crate::oldtime::Duration; + use crate::duration::Duration; use crate::{Datelike, Weekday}; // as it is hard to verify year flags in `NaiveDate::MIN` and `NaiveDate::MAX`, diff --git a/src/naive/datetime/mod.rs b/src/naive/datetime/mod.rs index 4943e764d4..09268aeffd 100644 --- a/src/naive/datetime/mod.rs +++ b/src/naive/datetime/mod.rs @@ -13,13 +13,13 @@ use core::{fmt, str}; #[cfg(feature = "rkyv")] use rkyv::{Archive, Deserialize, Serialize}; +use crate::duration::Duration as OldDuration; #[cfg(any(feature = "alloc", feature = "std"))] use crate::format::DelayedFormat; use crate::format::{parse, parse_and_remainder, ParseError, ParseResult, Parsed, StrftimeItems}; use crate::format::{Fixed, Item, Numeric, Pad}; use crate::naive::{Days, IsoWeek, NaiveDate, NaiveTime}; use crate::offset::Utc; -use crate::oldtime::Duration as OldDuration; use crate::{DateTime, Datelike, LocalResult, Months, TimeZone, Timelike, Weekday}; #[cfg(feature = "rustc-serialize")] diff --git a/src/naive/datetime/tests.rs b/src/naive/datetime/tests.rs index 66f3ff9957..5ded98f385 100644 --- a/src/naive/datetime/tests.rs +++ b/src/naive/datetime/tests.rs @@ -1,5 +1,5 @@ use super::NaiveDateTime; -use crate::oldtime::Duration as OldDuration; +use crate::duration::Duration as OldDuration; use crate::NaiveDate; use crate::{Datelike, FixedOffset, Utc}; diff --git a/src/naive/time/mod.rs b/src/naive/time/mod.rs index 4348e4ca79..43e614bab0 100644 --- a/src/naive/time/mod.rs +++ b/src/naive/time/mod.rs @@ -12,13 +12,13 @@ use core::{fmt, str}; #[cfg(feature = "rkyv")] use rkyv::{Archive, Deserialize, Serialize}; +use crate::duration::Duration as OldDuration; #[cfg(any(feature = "alloc", feature = "std"))] use crate::format::DelayedFormat; use crate::format::{ parse, parse_and_remainder, write_hundreds, Fixed, Item, Numeric, Pad, ParseError, ParseResult, Parsed, StrftimeItems, }; -use crate::oldtime::Duration as OldDuration; use crate::Timelike; use crate::{expect, try_opt}; diff --git a/src/naive/time/tests.rs b/src/naive/time/tests.rs index 0a45381dea..b77ca87339 100644 --- a/src/naive/time/tests.rs +++ b/src/naive/time/tests.rs @@ -1,5 +1,5 @@ use super::NaiveTime; -use crate::oldtime::Duration as OldDuration; +use crate::duration::Duration as OldDuration; use crate::Timelike; #[test] diff --git a/src/offset/fixed.rs b/src/offset/fixed.rs index acfae81085..53e734cc16 100644 --- a/src/offset/fixed.rs +++ b/src/offset/fixed.rs @@ -11,9 +11,9 @@ use core::str::FromStr; use rkyv::{Archive, Deserialize, Serialize}; use super::{LocalResult, Offset, TimeZone}; +use crate::duration::Duration as OldDuration; use crate::format::{scan, OUT_OF_RANGE}; use crate::naive::{NaiveDate, NaiveDateTime, NaiveTime}; -use crate::oldtime::Duration as OldDuration; use crate::{DateTime, ParseError, Timelike}; /// The time zone with fixed offset, from UTC-23:59:59 to UTC+23:59:59. diff --git a/src/round.rs b/src/round.rs index 69ecef9e8b..9271353b77 100644 --- a/src/round.rs +++ b/src/round.rs @@ -2,7 +2,7 @@ // See README.md and LICENSE.txt for details. use crate::datetime::DateTime; -use crate::oldtime::Duration; +use crate::duration::Duration; use crate::NaiveDateTime; use crate::TimeZone; use crate::Timelike;