From f775c8ea62ca18bd0704d8cafd2b8e183f492b70 Mon Sep 17 00:00:00 2001 From: Paul Dicker Date: Wed, 10 May 2023 16:07:26 +0200 Subject: [PATCH 1/8] Remove mention of deprecated `Date` in main documentation --- src/lib.rs | 28 ---------------------------- 1 file changed, 28 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 336e18751d..d56bd1fdc7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -330,34 +330,6 @@ //! assert_eq!(dt.timestamp(), 1_500_000_000); //! ``` //! -//! ### Individual date -//! -//! Chrono also provides an individual date type ([**`Date`**](./struct.Date.html)). -//! It also has time zones attached, and have to be constructed via time zones. -//! Most operations available to `DateTime` are also available to `Date` whenever appropriate. -//! -#![cfg_attr(not(feature = "std"), doc = "```ignore")] -#![cfg_attr(feature = "std", doc = "```rust")] -//! use chrono::prelude::*; -//! use chrono::offset::LocalResult; -//! -//! # // these *may* fail, but only very rarely. just rerun the test if you were that unfortunate ;) -//! assert_eq!(Utc::today(), Utc::now().date()); -//! assert_eq!(Local::today(), Local::now().date()); -//! -//! assert_eq!(Utc.ymd_opt(2014, 11, 28).unwrap().weekday(), Weekday::Fri); -//! assert_eq!(Utc.ymd_opt(2014, 11, 31), LocalResult::None); -//! assert_eq!(NaiveDate::from_ymd_opt(2014, 11, 28).unwrap().and_hms_milli_opt(7, 8, 9, 10).unwrap().and_local_timezone(Utc).unwrap().format("%H%M%S").to_string(), -//! "070809"); -//! ``` -//! -//! There is no timezone-aware `Time` due to the lack of usefulness and also the complexity. -//! -//! `DateTime` has [`date`](./struct.DateTime.html#method.date) method -//! which returns a `Date` which represents its date component. -//! There is also a [`time`](./struct.DateTime.html#method.time) method, -//! which simply returns a naive local time described below. -//! //! ### Naive date and time //! //! Chrono provides naive counterparts to `Date`, (non-existent) `Time` and `DateTime` From 447982142603a7ee6bbcb3d0a1b4e671e9981136 Mon Sep 17 00:00:00 2001 From: Paul Dicker Date: Wed, 17 May 2023 14:40:05 +0200 Subject: [PATCH 2/8] Remove unused imports in doctests --- src/datetime/mod.rs | 12 ++++++------ src/datetime/serde.rs | 24 ++++++++++++------------ src/format/mod.rs | 3 +-- src/lib.rs | 1 + src/naive/date.rs | 4 ++-- src/naive/datetime/mod.rs | 18 +++++++----------- src/naive/datetime/serde.rs | 8 ++++---- src/naive/time/mod.rs | 4 ++-- src/round.rs | 14 +++++++------- 9 files changed, 42 insertions(+), 46 deletions(-) diff --git a/src/datetime/mod.rs b/src/datetime/mod.rs index 185cb6f3ee..1bdda19009 100644 --- a/src/datetime/mod.rs +++ b/src/datetime/mod.rs @@ -208,7 +208,7 @@ impl DateTime { /// # Example /// /// ``` - /// use chrono::{Utc, TimeZone, NaiveDate}; + /// use chrono::{Utc, NaiveDate}; /// /// let dt = NaiveDate::from_ymd_opt(1970, 1, 1).unwrap().and_hms_milli_opt(0, 0, 1, 444).unwrap().and_local_timezone(Utc).unwrap(); /// assert_eq!(dt.timestamp_millis(), 1_444); @@ -232,7 +232,7 @@ impl DateTime { /// # Example /// /// ``` - /// use chrono::{Utc, TimeZone, NaiveDate}; + /// use chrono::{Utc, NaiveDate}; /// /// let dt = NaiveDate::from_ymd_opt(1970, 1, 1).unwrap().and_hms_micro_opt(0, 0, 1, 444).unwrap().and_local_timezone(Utc).unwrap(); /// assert_eq!(dt.timestamp_micros(), 1_000_444); @@ -256,7 +256,7 @@ impl DateTime { /// # Example /// /// ``` - /// use chrono::{Utc, TimeZone, NaiveDate}; + /// use chrono::{Utc, NaiveDate}; /// /// let dt = NaiveDate::from_ymd_opt(1970, 1, 1).unwrap().and_hms_nano_opt(0, 0, 1, 444).unwrap().and_local_timezone(Utc).unwrap(); /// assert_eq!(dt.timestamp_nanos(), 1_000_000_444); @@ -564,7 +564,7 @@ impl DateTime { /// See [RFC 2822 Appendix A.5] /// /// ``` - /// # use chrono::{DateTime, FixedOffset, TimeZone, NaiveDate}; + /// # use chrono::{DateTime, FixedOffset, TimeZone}; /// assert_eq!( /// DateTime::parse_from_rfc2822("Wed, 18 Feb 2015 23:16:09 GMT").unwrap(), /// FixedOffset::east_opt(0).unwrap().with_ymd_and_hms(2015, 2, 18, 23, 16, 9).unwrap() @@ -640,7 +640,7 @@ impl DateTime { /// # Example /// /// ```rust - /// # use chrono::{DateTime, FixedOffset, TimeZone, NaiveDate}; + /// # use chrono::{DateTime, FixedOffset, TimeZone}; /// let (datetime, remainder) = DateTime::parse_and_remainder( /// "2015-02-18 23:16:09 +0200 trailing text", "%Y-%m-%d %H:%M:%S %z").unwrap(); /// assert_eq!( @@ -695,7 +695,7 @@ where /// # Examples /// /// ```rust - /// # use chrono::{DateTime, FixedOffset, SecondsFormat, TimeZone, Utc, NaiveDate}; + /// # use chrono::{FixedOffset, SecondsFormat, TimeZone, Utc, NaiveDate}; /// let dt = NaiveDate::from_ymd_opt(2018, 1, 26).unwrap().and_hms_micro_opt(18, 30, 9, 453_829).unwrap().and_local_timezone(Utc).unwrap(); /// assert_eq!(dt.to_rfc3339_opts(SecondsFormat::Millis, false), /// "2018-01-26T18:30:09.453+00:00"); diff --git a/src/datetime/serde.rs b/src/datetime/serde.rs index d30f805198..0c49abf92a 100644 --- a/src/datetime/serde.rs +++ b/src/datetime/serde.rs @@ -122,7 +122,7 @@ impl<'de> de::Deserialize<'de> for DateTime { /// # Example: /// /// ```rust -/// # use chrono::{TimeZone, DateTime, Utc, NaiveDate}; +/// # use chrono::{DateTime, Utc, NaiveDate}; /// # use serde_derive::{Deserialize, Serialize}; /// use chrono::serde::ts_nanoseconds; /// #[derive(Deserialize, Serialize)] @@ -158,7 +158,7 @@ pub mod ts_nanoseconds { /// # Example: /// /// ```rust - /// # use chrono::{TimeZone, DateTime, Utc, NaiveDate}; + /// # use chrono::{DateTime, Utc, NaiveDate}; /// # use serde_derive::Serialize; /// use chrono::serde::ts_nanoseconds::serialize as to_nano_ts; /// #[derive(Serialize)] @@ -247,7 +247,7 @@ pub mod ts_nanoseconds { /// # Example: /// /// ```rust -/// # use chrono::{TimeZone, DateTime, Utc, NaiveDate}; +/// # use chrono::{DateTime, Utc, NaiveDate}; /// # use serde_derive::{Deserialize, Serialize}; /// use chrono::serde::ts_nanoseconds_option; /// #[derive(Deserialize, Serialize)] @@ -282,7 +282,7 @@ pub mod ts_nanoseconds_option { /// # Example: /// /// ```rust - /// # use chrono::{TimeZone, DateTime, Utc, NaiveDate}; + /// # use chrono::{DateTime, Utc, NaiveDate}; /// # use serde_derive::Serialize; /// use chrono::serde::ts_nanoseconds_option::serialize as to_nano_tsopt; /// #[derive(Serialize)] @@ -378,7 +378,7 @@ pub mod ts_nanoseconds_option { /// # Example: /// /// ```rust -/// # use chrono::{TimeZone, DateTime, Utc, NaiveDate}; +/// # use chrono::{DateTime, Utc, NaiveDate}; /// # use serde_derive::{Deserialize, Serialize}; /// use chrono::serde::ts_microseconds; /// #[derive(Deserialize, Serialize)] @@ -413,7 +413,7 @@ pub mod ts_microseconds { /// # Example: /// /// ```rust - /// # use chrono::{TimeZone, DateTime, Utc, NaiveDate}; + /// # use chrono::{DateTime, Utc, NaiveDate}; /// # use serde_derive::Serialize; /// use chrono::serde::ts_microseconds::serialize as to_micro_ts; /// #[derive(Serialize)] @@ -502,7 +502,7 @@ pub mod ts_microseconds { /// # Example: /// /// ```rust -/// # use chrono::{TimeZone, DateTime, Utc, NaiveDate}; +/// # use chrono::{DateTime, Utc, NaiveDate}; /// # use serde_derive::{Deserialize, Serialize}; /// use chrono::serde::ts_microseconds_option; /// #[derive(Deserialize, Serialize)] @@ -536,7 +536,7 @@ pub mod ts_microseconds_option { /// # Example: /// /// ```rust - /// # use chrono::{TimeZone, DateTime, Utc, NaiveDate}; + /// # use chrono::{DateTime, Utc, NaiveDate}; /// # use serde_derive::Serialize; /// use chrono::serde::ts_microseconds_option::serialize as to_micro_tsopt; /// #[derive(Serialize)] @@ -632,7 +632,7 @@ pub mod ts_microseconds_option { /// # Example /// /// ```rust -/// # use chrono::{TimeZone, DateTime, Utc, NaiveDate}; +/// # use chrono::{DateTime, Utc, NaiveDate}; /// # use serde_derive::{Deserialize, Serialize}; /// use chrono::serde::ts_milliseconds; /// #[derive(Deserialize, Serialize)] @@ -667,7 +667,7 @@ pub mod ts_milliseconds { /// # Example: /// /// ```rust - /// # use chrono::{TimeZone, DateTime, Utc, NaiveDate}; + /// # use chrono::{DateTime, Utc, NaiveDate}; /// # use serde_derive::Serialize; /// use chrono::serde::ts_milliseconds::serialize as to_milli_ts; /// #[derive(Serialize)] @@ -753,7 +753,7 @@ pub mod ts_milliseconds { /// # Example /// /// ```rust -/// # use chrono::{TimeZone, DateTime, Utc, NaiveDate}; +/// # use chrono::{DateTime, Utc, NaiveDate}; /// # use serde_derive::{Deserialize, Serialize}; /// use chrono::serde::ts_milliseconds_option; /// #[derive(Deserialize, Serialize)] @@ -787,7 +787,7 @@ pub mod ts_milliseconds_option { /// # Example: /// /// ```rust - /// # use chrono::{TimeZone, DateTime, Utc, NaiveDate}; + /// # use chrono::{DateTime, Utc, NaiveDate}; /// # use serde_derive::Serialize; /// use chrono::serde::ts_milliseconds_option::serialize as to_milli_tsopt; /// #[derive(Serialize)] diff --git a/src/format/mod.rs b/src/format/mod.rs index baabdd6740..75c0194fd5 100644 --- a/src/format/mod.rs +++ b/src/format/mod.rs @@ -18,8 +18,7 @@ //! # Example #![cfg_attr(not(feature = "std"), doc = "```ignore")] #![cfg_attr(feature = "std", doc = "```rust")] -//! # use std::error::Error; -//! use chrono::prelude::*; +//! use chrono::{TimeZone, Utc}; //! //! let date_time = Utc.with_ymd_and_hms(2020, 11, 10, 0, 1, 32).unwrap(); //! diff --git a/src/lib.rs b/src/lib.rs index d56bd1fdc7..a1547b13ae 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -219,6 +219,7 @@ //! The `unstable-locales` feature requires and implies at least the `alloc` feature. //! //! ```rust +//! # #[allow(unused_imports)] //! use chrono::prelude::*; //! //! # #[cfg(feature = "unstable-locales")] diff --git a/src/naive/date.rs b/src/naive/date.rs index 54d5e2258b..314354731a 100644 --- a/src/naive/date.rs +++ b/src/naive/date.rs @@ -1754,7 +1754,7 @@ impl Add for NaiveDate { /// # Example /// /// ``` - /// use chrono::{Duration, NaiveDate, Months}; + /// use chrono::{NaiveDate, Months}; /// /// let from_ymd = NaiveDate::from_ymd; /// @@ -1782,7 +1782,7 @@ impl Sub for NaiveDate { /// # Example /// /// ``` - /// use chrono::{Duration, NaiveDate, Months}; + /// use chrono::{NaiveDate, Months}; /// /// let from_ymd = NaiveDate::from_ymd; /// diff --git a/src/naive/datetime/mod.rs b/src/naive/datetime/mod.rs index 4a4ac8221e..880d96f011 100644 --- a/src/naive/datetime/mod.rs +++ b/src/naive/datetime/mod.rs @@ -196,7 +196,7 @@ impl NaiveDateTime { /// # Example /// /// ``` - /// use chrono::{NaiveDateTime, NaiveDate}; + /// use chrono::NaiveDateTime; /// use std::i64; /// /// let from_timestamp_opt = NaiveDateTime::from_timestamp_opt; @@ -288,7 +288,7 @@ impl NaiveDateTime { /// Years before 1 BCE or after 9999 CE, require an initial sign /// ///``` - /// # use chrono::{NaiveDate, NaiveDateTime}; + /// # use chrono::NaiveDateTime; /// # let parse_from_str = NaiveDateTime::parse_from_str; /// let fmt = "%Y-%m-%d %H:%M:%S"; /// assert!(parse_from_str("10000-09-09 01:46:39", fmt).is_err()); @@ -638,8 +638,7 @@ impl NaiveDateTime { /// # Example /// /// ``` - /// use std::str::FromStr; - /// use chrono::{Months, NaiveDate, NaiveDateTime}; + /// use chrono::{Months, NaiveDate}; /// /// assert_eq!( /// NaiveDate::from_ymd_opt(2014, 1, 1).unwrap().and_hms_opt(1, 0, 0).unwrap() @@ -741,8 +740,7 @@ impl NaiveDateTime { /// # Example /// /// ``` - /// use std::str::FromStr; - /// use chrono::{Months, NaiveDate, NaiveDateTime}; + /// use chrono::{Months, NaiveDate}; /// /// assert_eq!( /// NaiveDate::from_ymd_opt(2014, 1, 1).unwrap().and_hms_opt(1, 0, 0).unwrap() @@ -929,7 +927,7 @@ impl NaiveDateTime { /// # Example /// /// ``` - /// use chrono::{NaiveDate, NaiveTime, Utc}; + /// use chrono::{NaiveDate, Utc}; /// let dt = NaiveDate::from_ymd_opt(2023, 1, 30).unwrap().and_hms_opt(19, 32, 33).unwrap().and_utc(); /// assert_eq!(dt.timezone(), Utc); /// ``` @@ -1496,8 +1494,7 @@ impl Add for NaiveDateTime { /// # Example /// /// ``` - /// use chrono::{Duration, NaiveDateTime, Months, NaiveDate}; - /// use std::str::FromStr; + /// use chrono::{Months, NaiveDate}; /// /// assert_eq!( /// NaiveDate::from_ymd_opt(2014, 1, 1).unwrap().and_hms_opt(1, 0, 0).unwrap() + Months::new(1), @@ -1603,8 +1600,7 @@ impl SubAssign for NaiveDateTime { /// # Example /// /// ``` -/// use chrono::{Duration, NaiveDateTime, Months, NaiveDate}; -/// use std::str::FromStr; +/// use chrono::{Months, NaiveDate}; /// /// assert_eq!( /// NaiveDate::from_ymd_opt(2014, 01, 01).unwrap().and_hms_opt(01, 00, 00).unwrap() - Months::new(11), diff --git a/src/naive/datetime/serde.rs b/src/naive/datetime/serde.rs index 79509f0277..5ab92d9559 100644 --- a/src/naive/datetime/serde.rs +++ b/src/naive/datetime/serde.rs @@ -250,7 +250,7 @@ pub mod ts_nanoseconds_option { /// # Example: /// /// ```rust - /// # use chrono::naive::{NaiveDate, NaiveDateTime}; + /// # use chrono::naive::NaiveDateTime; /// # use serde_derive::Deserialize; /// use chrono::naive::serde::ts_nanoseconds_option::deserialize as from_nano_tsopt; /// #[derive(Deserialize)] @@ -503,7 +503,7 @@ pub mod ts_microseconds_option { /// # Example: /// /// ```rust - /// # use chrono::naive::{NaiveDate, NaiveDateTime}; + /// # use chrono::naive::NaiveDateTime; /// # use serde_derive::Deserialize; /// use chrono::naive::serde::ts_microseconds_option::deserialize as from_micro_tsopt; /// #[derive(Deserialize)] @@ -753,7 +753,7 @@ pub mod ts_milliseconds_option { /// # Example: /// /// ```rust - /// # use chrono::naive::{NaiveDate, NaiveDateTime}; + /// # use chrono::naive::NaiveDateTime; /// # use serde_derive::Deserialize; /// use chrono::naive::serde::ts_milliseconds_option::deserialize as from_milli_tsopt; /// #[derive(Deserialize)] @@ -1000,7 +1000,7 @@ pub mod ts_seconds_option { /// # Example: /// /// ```rust - /// # use chrono::naive::{NaiveDate, NaiveDateTime}; + /// # use chrono::naive::NaiveDateTime; /// # use serde_derive::Deserialize; /// use chrono::naive::serde::ts_seconds_option::deserialize as from_tsopt; /// #[derive(Deserialize)] diff --git a/src/naive/time/mod.rs b/src/naive/time/mod.rs index 880617e511..440ffead26 100644 --- a/src/naive/time/mod.rs +++ b/src/naive/time/mod.rs @@ -75,7 +75,7 @@ mod tests; /// All methods accepting fractional seconds will accept such values. /// /// ``` -/// use chrono::{NaiveDate, NaiveTime, Utc, TimeZone}; +/// use chrono::{NaiveDate, NaiveTime, Utc}; /// /// let t = NaiveTime::from_hms_milli_opt(8, 59, 59, 1_000).unwrap(); /// @@ -162,7 +162,7 @@ mod tests; /// will be represented as the second part being 60, as required by ISO 8601. /// /// ``` -/// use chrono::{Utc, TimeZone, NaiveDate}; +/// use chrono::{Utc, NaiveDate}; /// /// let dt = NaiveDate::from_ymd_opt(2015, 6, 30).unwrap().and_hms_milli_opt(23, 59, 59, 1_000).unwrap().and_local_timezone(Utc).unwrap(); /// assert_eq!(format!("{:?}", dt), "2015-06-30T23:59:60Z"); diff --git a/src/round.rs b/src/round.rs index 6f19efe841..101055a0e5 100644 --- a/src/round.rs +++ b/src/round.rs @@ -24,7 +24,7 @@ pub trait SubsecRound { /// /// # Example /// ``` rust - /// # use chrono::{DateTime, SubsecRound, Timelike, TimeZone, Utc, NaiveDate}; + /// # use chrono::{SubsecRound, Timelike, Utc, NaiveDate}; /// let dt = NaiveDate::from_ymd_opt(2018, 1, 11).unwrap().and_hms_milli_opt(12, 0, 0, 154).unwrap().and_local_timezone(Utc).unwrap(); /// assert_eq!(dt.round_subsecs(2).nanosecond(), 150_000_000); /// assert_eq!(dt.round_subsecs(1).nanosecond(), 200_000_000); @@ -36,7 +36,7 @@ pub trait SubsecRound { /// /// # Example /// ``` rust - /// # use chrono::{DateTime, SubsecRound, Timelike, TimeZone, Utc, NaiveDate}; + /// # use chrono::{SubsecRound, Timelike, Utc, NaiveDate}; /// let dt = NaiveDate::from_ymd_opt(2018, 1, 11).unwrap().and_hms_milli_opt(12, 0, 0, 154).unwrap().and_local_timezone(Utc).unwrap(); /// assert_eq!(dt.trunc_subsecs(2).nanosecond(), 150_000_000); /// assert_eq!(dt.trunc_subsecs(1).nanosecond(), 100_000_000); @@ -111,7 +111,7 @@ pub trait DurationRound: Sized { /// /// # Example /// ``` rust - /// # use chrono::{DateTime, DurationRound, Duration, TimeZone, Utc, NaiveDate}; + /// # use chrono::{DurationRound, Duration, Utc, NaiveDate}; /// let dt = NaiveDate::from_ymd_opt(2018, 1, 11).unwrap().and_hms_milli_opt(12, 0, 0, 154).unwrap().and_local_timezone(Utc).unwrap(); /// assert_eq!( /// dt.duration_round(Duration::milliseconds(10)).unwrap().to_string(), @@ -128,7 +128,7 @@ pub trait DurationRound: Sized { /// /// # Example /// ``` rust - /// # use chrono::{DateTime, DurationRound, Duration, TimeZone, Utc, NaiveDate}; + /// # use chrono::{DurationRound, Duration, Utc, NaiveDate}; /// let dt = NaiveDate::from_ymd_opt(2018, 1, 11).unwrap().and_hms_milli_opt(12, 0, 0, 154).unwrap().and_local_timezone(Utc).unwrap(); /// assert_eq!( /// dt.duration_trunc(Duration::milliseconds(10)).unwrap().to_string(), @@ -243,7 +243,7 @@ pub enum RoundingError { /// Error when the Duration exceeds the Duration from or until the Unix epoch. /// /// ``` rust - /// # use chrono::{DateTime, DurationRound, Duration, RoundingError, TimeZone, Utc}; + /// # use chrono::{DurationRound, Duration, RoundingError, TimeZone, Utc}; /// let dt = Utc.with_ymd_and_hms(1970, 12, 12, 0, 0, 0).unwrap(); /// /// assert_eq!( @@ -256,7 +256,7 @@ pub enum RoundingError { /// Error when `Duration.num_nanoseconds` exceeds the limit. /// /// ``` rust - /// # use chrono::{DateTime, DurationRound, Duration, RoundingError, TimeZone, Utc, NaiveDate}; + /// # use chrono::{DurationRound, Duration, RoundingError, Utc, NaiveDate}; /// let dt = NaiveDate::from_ymd_opt(2260, 12, 31).unwrap().and_hms_nano_opt(23, 59, 59, 1_75_500_000).unwrap().and_local_timezone(Utc).unwrap(); /// /// assert_eq!( @@ -269,7 +269,7 @@ pub enum RoundingError { /// Error when `DateTime.timestamp_nanos` exceeds the limit. /// /// ``` rust - /// # use chrono::{DateTime, DurationRound, Duration, RoundingError, TimeZone, Utc}; + /// # use chrono::{DurationRound, Duration, RoundingError, TimeZone, Utc}; /// let dt = Utc.with_ymd_and_hms(2300, 12, 12, 0, 0, 0).unwrap(); /// /// assert_eq!(dt.duration_round(Duration::days(1)), Err(RoundingError::TimestampExceedsLimit),); From 554242520fa1917e106561088b2a134c0202e369 Mon Sep 17 00:00:00 2001 From: Paul Dicker Date: Tue, 23 May 2023 15:00:33 +0200 Subject: [PATCH 3/8] Avoid deprecated methods in doctests --- src/datetime/serde.rs | 2 +- src/lib.rs | 2 +- src/naive/date.rs | 22 +++++++++++----------- src/naive/datetime/mod.rs | 30 +++++++++++++++--------------- src/naive/isoweek.rs | 8 ++++---- src/naive/time/mod.rs | 24 ++++++++++++------------ src/offset/fixed.rs | 12 ++++++++---- src/offset/local/mod.rs | 2 +- src/offset/utc.rs | 4 ++-- 9 files changed, 55 insertions(+), 51 deletions(-) diff --git a/src/datetime/serde.rs b/src/datetime/serde.rs index 0c49abf92a..12fc3dc89c 100644 --- a/src/datetime/serde.rs +++ b/src/datetime/serde.rs @@ -838,7 +838,7 @@ pub mod ts_milliseconds_option { /// } /// /// let my_s: E = serde_json::from_str(r#"{ "time": 1526522699918 }"#)?; - /// assert_eq!(my_s, E::V(S { time: Some(Utc.timestamp(1526522699, 918000000)) })); + /// assert_eq!(my_s, E::V(S { time: Some(Utc.timestamp_opt(1526522699, 918000000).unwrap()) })); /// let s: E = serde_json::from_str(r#"{ "time": null }"#)?; /// assert_eq!(s, E::V(S { time: None })); /// let t: E = serde_json::from_str(r#"{}"#)?; diff --git a/src/lib.rs b/src/lib.rs index a1547b13ae..76835f2112 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -323,7 +323,7 @@ //! use chrono::{DateTime, TimeZone, Utc}; //! //! // Construct a datetime from epoch: -//! let dt = Utc.timestamp(1_500_000_000, 0); +//! let dt = Utc.timestamp_opt(1_500_000_000, 0).unwrap(); //! assert_eq!(dt.to_rfc2822(), "Fri, 14 Jul 2017 02:40:00 +0000"); //! //! // Get epoch value from a datetime: diff --git a/src/naive/date.rs b/src/naive/date.rs index 314354731a..1fab3c5c71 100644 --- a/src/naive/date.rs +++ b/src/naive/date.rs @@ -376,7 +376,7 @@ impl NaiveDate { /// ``` /// use chrono::{NaiveDate, Weekday}; /// - /// let from_ymd = NaiveDate::from_ymd; + /// let from_ymd = |y, m, d| NaiveDate::from_ymd_opt(y, m, d).unwrap(); /// let from_isoywd_opt = NaiveDate::from_isoywd_opt; /// /// assert_eq!(from_isoywd_opt(2015, 0, Weekday::Sun), None); @@ -392,7 +392,7 @@ impl NaiveDate { /// /// ``` /// # use chrono::{NaiveDate, Weekday}; - /// # let from_ymd = NaiveDate::from_ymd; + /// # let from_ymd = |y, m, d| NaiveDate::from_ymd_opt(y, m, d).unwrap(); /// # let from_isoywd_opt = NaiveDate::from_isoywd_opt; /// // Mo Tu We Th Fr Sa Su /// // 2014-W52 22 23 24 25 26 27 28 has 4+ days of new year, @@ -875,7 +875,7 @@ impl NaiveDate { /// /// let d = NaiveDate::from_ymd_opt(2015, 6, 3).unwrap(); /// - /// let dt: NaiveDateTime = d.and_hms_micro(12, 34, 56, 789_012); + /// let dt: NaiveDateTime = d.and_hms_micro_opt(12, 34, 56, 789_012).unwrap(); /// assert_eq!(dt.year(), 2015); /// assert_eq!(dt.weekday(), Weekday::Wed); /// assert_eq!(dt.second(), 56); @@ -1133,7 +1133,7 @@ impl NaiveDate { /// ``` /// use chrono::{Duration, NaiveDate}; /// - /// let from_ymd = NaiveDate::from_ymd; + /// let from_ymd = |y, m, d| NaiveDate::from_ymd_opt(y, m, d).unwrap(); /// let since = NaiveDate::signed_duration_since; /// /// assert_eq!(since(from_ymd(2014, 1, 1), from_ymd(2014, 1, 1)), Duration::zero()); @@ -1434,7 +1434,7 @@ impl Datelike for NaiveDate { /// let d = NaiveDate::from_ymd_opt(y, m, 1).unwrap(); /// /// // ...is preceded by the last day of the original month - /// d.pred().day() + /// d.pred_opt().unwrap().day() /// } /// /// assert_eq!(ndays_in_month(2015, 8), 31); @@ -1490,7 +1490,7 @@ impl Datelike for NaiveDate { /// let d = NaiveDate::from_ymd_opt(year + 1, 1, 1).unwrap(); /// /// // ...is preceded by the last day of the original year - /// d.pred().ordinal() + /// d.pred_opt().unwrap().ordinal() /// } /// /// assert_eq!(ndays_in_year(2015), 365); @@ -1713,7 +1713,7 @@ impl Datelike for NaiveDate { /// ``` /// use chrono::{Duration, NaiveDate}; /// -/// let from_ymd = NaiveDate::from_ymd; +/// let from_ymd = |y, m, d| NaiveDate::from_ymd_opt(y, m, d).unwrap(); /// /// assert_eq!(from_ymd(2014, 1, 1) + Duration::zero(), from_ymd(2014, 1, 1)); /// assert_eq!(from_ymd(2014, 1, 1) + Duration::seconds(86399), from_ymd(2014, 1, 1)); @@ -1756,7 +1756,7 @@ impl Add for NaiveDate { /// ``` /// use chrono::{NaiveDate, Months}; /// - /// let from_ymd = NaiveDate::from_ymd; + /// let from_ymd = |y, m, d| NaiveDate::from_ymd_opt(y, m, d).unwrap(); /// /// assert_eq!(from_ymd(2014, 1, 1) + Months::new(1), from_ymd(2014, 2, 1)); /// assert_eq!(from_ymd(2014, 1, 1) + Months::new(11), from_ymd(2014, 12, 1)); @@ -1784,7 +1784,7 @@ impl Sub for NaiveDate { /// ``` /// use chrono::{NaiveDate, Months}; /// - /// let from_ymd = NaiveDate::from_ymd; + /// let from_ymd = |y, m, d| NaiveDate::from_ymd_opt(y, m, d).unwrap(); /// /// assert_eq!(from_ymd(2014, 1, 1) - Months::new(11), from_ymd(2013, 2, 1)); /// assert_eq!(from_ymd(2014, 1, 1) - Months::new(12), from_ymd(2013, 1, 1)); @@ -1822,7 +1822,7 @@ impl Sub for NaiveDate { /// ``` /// use chrono::{Duration, NaiveDate}; /// -/// let from_ymd = NaiveDate::from_ymd; +/// let from_ymd = |y, m, d| NaiveDate::from_ymd_opt(y, m, d).unwrap(); /// /// assert_eq!(from_ymd(2014, 1, 1) - Duration::zero(), from_ymd(2014, 1, 1)); /// assert_eq!(from_ymd(2014, 1, 1) - Duration::seconds(86399), from_ymd(2014, 1, 1)); @@ -1865,7 +1865,7 @@ impl SubAssign for NaiveDate { /// ``` /// use chrono::{Duration, NaiveDate}; /// -/// let from_ymd = NaiveDate::from_ymd; +/// let from_ymd = |y, m, d| NaiveDate::from_ymd_opt(y, m, d).unwrap(); /// /// assert_eq!(from_ymd(2014, 1, 1) - from_ymd(2014, 1, 1), Duration::zero()); /// assert_eq!(from_ymd(2014, 1, 1) - from_ymd(2013, 12, 31), Duration::days(1)); diff --git a/src/naive/datetime/mod.rs b/src/naive/datetime/mod.rs index 880d96f011..7a38d4ffd6 100644 --- a/src/naive/datetime/mod.rs +++ b/src/naive/datetime/mod.rs @@ -474,8 +474,8 @@ impl NaiveDateTime { /// let nanos = dt.timestamp_nanos(); /// assert_eq!(nanos, 1_000_000_000_000_000_555); /// assert_eq!( - /// dt, - /// NaiveDateTime::from_timestamp(nanos / A_BILLION, (nanos % A_BILLION) as u32) + /// Some(dt), + /// NaiveDateTime::from_timestamp_opt(nanos / A_BILLION, (nanos % A_BILLION) as u32) /// ); /// ``` #[inline] @@ -565,7 +565,7 @@ impl NaiveDateTime { /// ``` /// use chrono::{Duration, NaiveDate}; /// - /// let from_ymd = NaiveDate::from_ymd; + /// let from_ymd = |y, m, d| NaiveDate::from_ymd_opt(y, m, d).unwrap(); /// /// let d = from_ymd(2016, 7, 8); /// let hms = |h, m, s| d.and_hms_opt(h, m, s).unwrap(); @@ -598,7 +598,7 @@ impl NaiveDateTime { /// /// ``` /// # use chrono::{Duration, NaiveDate}; - /// # let from_ymd = NaiveDate::from_ymd; + /// # let from_ymd = |y, m, d| NaiveDate::from_ymd_opt(y, m, d).unwrap(); /// # let hmsm = |h, m, s, milli| from_ymd(2016, 7, 8).and_hms_milli_opt(h, m, s, milli).unwrap(); /// let leap = hmsm(3, 5, 59, 1_300); /// assert_eq!(leap.checked_add_signed(Duration::zero()), @@ -671,7 +671,7 @@ impl NaiveDateTime { /// ``` /// use chrono::{Duration, NaiveDate}; /// - /// let from_ymd = NaiveDate::from_ymd; + /// let from_ymd = |y, m, d| NaiveDate::from_ymd_opt(y, m, d).unwrap(); /// /// let d = from_ymd(2016, 7, 8); /// let hms = |h, m, s| d.and_hms_opt(h, m, s).unwrap(); @@ -704,7 +704,7 @@ impl NaiveDateTime { /// /// ``` /// # use chrono::{Duration, NaiveDate}; - /// # let from_ymd = NaiveDate::from_ymd; + /// # let from_ymd = |y, m, d| NaiveDate::from_ymd_opt(y, m, d).unwrap(); /// # let hmsm = |h, m, s, milli| from_ymd(2016, 7, 8).and_hms_milli_opt(h, m, s, milli).unwrap(); /// let leap = hmsm(3, 5, 59, 1_300); /// assert_eq!(leap.checked_sub_signed(Duration::zero()), @@ -789,7 +789,7 @@ impl NaiveDateTime { /// ``` /// use chrono::{Duration, NaiveDate}; /// - /// let from_ymd = NaiveDate::from_ymd; + /// let from_ymd = |y, m, d| NaiveDate::from_ymd_opt(y, m, d).unwrap(); /// /// let d = from_ymd(2016, 7, 8); /// assert_eq!(d.and_hms_opt(3, 5, 7).unwrap().signed_duration_since(d.and_hms_opt(2, 4, 6).unwrap()), @@ -806,7 +806,7 @@ impl NaiveDateTime { /// /// ``` /// # use chrono::{Duration, NaiveDate}; - /// # let from_ymd = NaiveDate::from_ymd; + /// # let from_ymd = |y, m, d| NaiveDate::from_ymd_opt(y, m, d).unwrap(); /// let leap = from_ymd(2015, 6, 30).and_hms_milli_opt(23, 59, 59, 1_500).unwrap(); /// assert_eq!(leap.signed_duration_since(from_ymd(2015, 6, 30).and_hms_opt(23, 0, 0).unwrap()), /// Duration::seconds(3600) + Duration::milliseconds(500)); @@ -1430,7 +1430,7 @@ impl Timelike for NaiveDateTime { /// ``` /// use chrono::{Duration, NaiveDate}; /// -/// let from_ymd = NaiveDate::from_ymd; +/// let from_ymd = |y, m, d| NaiveDate::from_ymd_opt(y, m, d).unwrap(); /// /// let d = from_ymd(2016, 7, 8); /// let hms = |h, m, s| d.and_hms_opt(h, m, s).unwrap(); @@ -1452,7 +1452,7 @@ impl Timelike for NaiveDateTime { /// /// ``` /// # use chrono::{Duration, NaiveDate}; -/// # let from_ymd = NaiveDate::from_ymd; +/// # let from_ymd = |y, m, d| NaiveDate::from_ymd_opt(y, m, d).unwrap(); /// # let hmsm = |h, m, s, milli| from_ymd(2016, 7, 8).and_hms_milli_opt(h, m, s, milli).unwrap(); /// let leap = hmsm(3, 5, 59, 1_300); /// assert_eq!(leap + Duration::zero(), hmsm(3, 5, 59, 1_300)); @@ -1541,7 +1541,7 @@ impl Add for NaiveDateTime { /// ``` /// use chrono::{Duration, NaiveDate}; /// -/// let from_ymd = NaiveDate::from_ymd; +/// let from_ymd = |y, m, d| NaiveDate::from_ymd_opt(y, m, d).unwrap(); /// /// let d = from_ymd(2016, 7, 8); /// let hms = |h, m, s| d.and_hms_opt(h, m, s).unwrap(); @@ -1563,7 +1563,7 @@ impl Add for NaiveDateTime { /// /// ``` /// # use chrono::{Duration, NaiveDate}; -/// # let from_ymd = NaiveDate::from_ymd; +/// # let from_ymd = |y, m, d| NaiveDate::from_ymd_opt(y, m, d).unwrap(); /// # let hmsm = |h, m, s, milli| from_ymd(2016, 7, 8).and_hms_milli_opt(h, m, s, milli).unwrap(); /// let leap = hmsm(3, 5, 59, 1_300); /// assert_eq!(leap - Duration::zero(), hmsm(3, 5, 59, 1_300)); @@ -1639,7 +1639,7 @@ impl Sub for NaiveDateTime { /// ``` /// use chrono::{Duration, NaiveDate}; /// -/// let from_ymd = NaiveDate::from_ymd; +/// let from_ymd = |y, m, d| NaiveDate::from_ymd_opt(y, m, d).unwrap(); /// /// let d = from_ymd(2016, 7, 8); /// assert_eq!(d.and_hms_opt(3, 5, 7).unwrap() - d.and_hms_opt(2, 4, 6).unwrap(), Duration::seconds(3600 + 60 + 1)); @@ -1655,7 +1655,7 @@ impl Sub for NaiveDateTime { /// /// ``` /// # use chrono::{Duration, NaiveDate}; -/// # let from_ymd = NaiveDate::from_ymd; +/// # let from_ymd = |y, m, d| NaiveDate::from_ymd_opt(y, m, d).unwrap(); /// let leap = from_ymd(2015, 6, 30).and_hms_milli_opt(23, 59, 59, 1_500).unwrap(); /// assert_eq!(leap - from_ymd(2015, 6, 30).and_hms_opt(23, 0, 0).unwrap(), /// Duration::seconds(3600) + Duration::milliseconds(500)); @@ -1811,7 +1811,7 @@ impl str::FromStr for NaiveDateTime { /// use chrono::NaiveDateTime; /// /// let default_date = NaiveDateTime::default(); -/// assert_eq!(default_date, NaiveDateTime::from_timestamp(0, 0)); +/// assert_eq!(Some(default_date), NaiveDateTime::from_timestamp_opt(0, 0)); /// ``` impl Default for NaiveDateTime { fn default() -> Self { diff --git a/src/naive/isoweek.rs b/src/naive/isoweek.rs index 45c2a81ede..df5556a120 100644 --- a/src/naive/isoweek.rs +++ b/src/naive/isoweek.rs @@ -58,7 +58,7 @@ impl IsoWeek { /// ``` /// use chrono::{NaiveDate, Datelike, Weekday}; /// - /// let d = NaiveDate::from_isoywd(2015, 1, Weekday::Mon); + /// let d = NaiveDate::from_isoywd_opt(2015, 1, Weekday::Mon).unwrap(); /// assert_eq!(d.iso_week().year(), 2015); /// ``` /// @@ -67,7 +67,7 @@ impl IsoWeek { /// /// ``` /// # use chrono::{NaiveDate, Datelike, Weekday}; - /// # let d = NaiveDate::from_isoywd(2015, 1, Weekday::Mon); + /// # let d = NaiveDate::from_isoywd_opt(2015, 1, Weekday::Mon).unwrap(); /// assert_eq!(d.year(), 2014); /// assert_eq!(d, NaiveDate::from_ymd_opt(2014, 12, 29).unwrap()); /// ``` @@ -85,7 +85,7 @@ impl IsoWeek { /// ``` /// use chrono::{NaiveDate, Datelike, Weekday}; /// - /// let d = NaiveDate::from_isoywd(2015, 15, Weekday::Mon); + /// let d = NaiveDate::from_isoywd_opt(2015, 15, Weekday::Mon).unwrap(); /// assert_eq!(d.iso_week().week(), 15); /// ``` #[inline] @@ -102,7 +102,7 @@ impl IsoWeek { /// ``` /// use chrono::{NaiveDate, Datelike, Weekday}; /// - /// let d = NaiveDate::from_isoywd(2015, 15, Weekday::Mon); + /// let d = NaiveDate::from_isoywd_opt(2015, 15, Weekday::Mon).unwrap(); /// assert_eq!(d.iso_week().week0(), 14); /// ``` #[inline] diff --git a/src/naive/time/mod.rs b/src/naive/time/mod.rs index 440ffead26..24453bf6a2 100644 --- a/src/naive/time/mod.rs +++ b/src/naive/time/mod.rs @@ -516,7 +516,7 @@ impl NaiveTime { /// ``` /// use chrono::{Duration, NaiveTime}; /// - /// let from_hms = NaiveTime::from_hms; + /// let from_hms = |h, m, s| { NaiveTime::from_hms_opt(h, m, s).unwrap() }; /// /// assert_eq!(from_hms(3, 4, 5).overflowing_add_signed(Duration::hours(11)), /// (from_hms(14, 4, 5), 0)); @@ -599,7 +599,7 @@ impl NaiveTime { /// ``` /// use chrono::{Duration, NaiveTime}; /// - /// let from_hms = NaiveTime::from_hms; + /// let from_hms = |h, m, s| { NaiveTime::from_hms_opt(h, m, s).unwrap() }; /// /// assert_eq!(from_hms(3, 4, 5).overflowing_sub_signed(Duration::hours(2)), /// (from_hms(1, 4, 5), 0)); @@ -630,7 +630,7 @@ impl NaiveTime { /// ``` /// use chrono::{Duration, NaiveTime}; /// - /// let from_hmsm = NaiveTime::from_hms_milli; + /// let from_hmsm = |h, m, s, milli| { NaiveTime::from_hms_milli_opt(h, m, s, milli).unwrap() }; /// let since = NaiveTime::signed_duration_since; /// /// assert_eq!(since(from_hmsm(3, 5, 7, 900), from_hmsm(3, 5, 7, 900)), @@ -656,7 +656,7 @@ impl NaiveTime { /// /// ``` /// # use chrono::{Duration, NaiveTime}; - /// # let from_hmsm = NaiveTime::from_hms_milli; + /// # let from_hmsm = |h, m, s, milli| { NaiveTime::from_hms_milli_opt(h, m, s, milli).unwrap() }; /// # let since = NaiveTime::signed_duration_since; /// assert_eq!(since(from_hmsm(3, 0, 59, 1_000), from_hmsm(3, 0, 59, 0)), /// Duration::seconds(1)); @@ -1020,7 +1020,7 @@ impl Timelike for NaiveTime { /// ``` /// use chrono::{Duration, NaiveTime}; /// -/// let from_hmsm = NaiveTime::from_hms_milli; +/// let from_hmsm = |h, m, s, milli| { NaiveTime::from_hms_milli_opt(h, m, s, milli).unwrap() }; /// /// assert_eq!(from_hmsm(3, 5, 7, 0) + Duration::zero(), from_hmsm(3, 5, 7, 0)); /// assert_eq!(from_hmsm(3, 5, 7, 0) + Duration::seconds(1), from_hmsm(3, 5, 8, 0)); @@ -1036,7 +1036,7 @@ impl Timelike for NaiveTime { /// /// ``` /// # use chrono::{Duration, NaiveTime}; -/// # let from_hmsm = NaiveTime::from_hms_milli; +/// # let from_hmsm = |h, m, s, milli| { NaiveTime::from_hms_milli_opt(h, m, s, milli).unwrap() }; /// assert_eq!(from_hmsm(3, 5, 7, 0) + Duration::seconds(22*60*60), from_hmsm(1, 5, 7, 0)); /// assert_eq!(from_hmsm(3, 5, 7, 0) + Duration::seconds(-8*60*60), from_hmsm(19, 5, 7, 0)); /// assert_eq!(from_hmsm(3, 5, 7, 0) + Duration::days(800), from_hmsm(3, 5, 7, 0)); @@ -1046,7 +1046,7 @@ impl Timelike for NaiveTime { /// /// ``` /// # use chrono::{Duration, NaiveTime}; -/// # let from_hmsm = NaiveTime::from_hms_milli; +/// # let from_hmsm = |h, m, s, milli| { NaiveTime::from_hms_milli_opt(h, m, s, milli).unwrap() }; /// let leap = from_hmsm(3, 5, 59, 1_300); /// assert_eq!(leap + Duration::zero(), from_hmsm(3, 5, 59, 1_300)); /// assert_eq!(leap + Duration::milliseconds(-500), from_hmsm(3, 5, 59, 800)); @@ -1087,7 +1087,7 @@ impl AddAssign for NaiveTime { /// ``` /// use chrono::{Duration, NaiveTime}; /// -/// let from_hmsm = NaiveTime::from_hms_milli; +/// let from_hmsm = |h, m, s, milli| { NaiveTime::from_hms_milli_opt(h, m, s, milli).unwrap() }; /// /// assert_eq!(from_hmsm(3, 5, 7, 0) - Duration::zero(), from_hmsm(3, 5, 7, 0)); /// assert_eq!(from_hmsm(3, 5, 7, 0) - Duration::seconds(1), from_hmsm(3, 5, 6, 0)); @@ -1101,7 +1101,7 @@ impl AddAssign for NaiveTime { /// /// ``` /// # use chrono::{Duration, NaiveTime}; -/// # let from_hmsm = NaiveTime::from_hms_milli; +/// # let from_hmsm = |h, m, s, milli| { NaiveTime::from_hms_milli_opt(h, m, s, milli).unwrap() }; /// assert_eq!(from_hmsm(3, 5, 7, 0) - Duration::seconds(8*60*60), from_hmsm(19, 5, 7, 0)); /// assert_eq!(from_hmsm(3, 5, 7, 0) - Duration::days(800), from_hmsm(3, 5, 7, 0)); /// ``` @@ -1110,7 +1110,7 @@ impl AddAssign for NaiveTime { /// /// ``` /// # use chrono::{Duration, NaiveTime}; -/// # let from_hmsm = NaiveTime::from_hms_milli; +/// # let from_hmsm = |h, m, s, milli| { NaiveTime::from_hms_milli_opt(h, m, s, milli).unwrap() }; /// let leap = from_hmsm(3, 5, 59, 1_300); /// assert_eq!(leap - Duration::zero(), from_hmsm(3, 5, 59, 1_300)); /// assert_eq!(leap - Duration::milliseconds(200), from_hmsm(3, 5, 59, 1_100)); @@ -1154,7 +1154,7 @@ impl SubAssign for NaiveTime { /// ``` /// use chrono::{Duration, NaiveTime}; /// -/// let from_hmsm = NaiveTime::from_hms_milli; +/// let from_hmsm = |h, m, s, milli| { NaiveTime::from_hms_milli_opt(h, m, s, milli).unwrap() }; /// /// assert_eq!(from_hmsm(3, 5, 7, 900) - from_hmsm(3, 5, 7, 900), Duration::zero()); /// assert_eq!(from_hmsm(3, 5, 7, 900) - from_hmsm(3, 5, 7, 875), Duration::milliseconds(25)); @@ -1172,7 +1172,7 @@ impl SubAssign for NaiveTime { /// /// ``` /// # use chrono::{Duration, NaiveTime}; -/// # let from_hmsm = NaiveTime::from_hms_milli; +/// # let from_hmsm = |h, m, s, milli| { NaiveTime::from_hms_milli_opt(h, m, s, milli).unwrap() }; /// assert_eq!(from_hmsm(3, 0, 59, 1_000) - from_hmsm(3, 0, 59, 0), Duration::seconds(1)); /// assert_eq!(from_hmsm(3, 0, 59, 1_500) - from_hmsm(3, 0, 59, 0), /// Duration::milliseconds(1500)); diff --git a/src/offset/fixed.rs b/src/offset/fixed.rs index 475c3e99dc..246d6666ea 100644 --- a/src/offset/fixed.rs +++ b/src/offset/fixed.rs @@ -49,8 +49,10 @@ impl FixedOffset { #[cfg_attr(feature = "std", doc = "```")] /// use chrono::{FixedOffset, TimeZone}; /// let hour = 3600; - /// let datetime = FixedOffset::east_opt(5 * hour).unwrap().ymd_opt(2016, 11, 08).unwrap() - /// .and_hms_opt(0, 0, 0).unwrap(); + /// let datetime = FixedOffset::east_opt(5 * hour) + /// .unwrap() + /// .with_ymd_and_hms(2016, 11, 08, 0, 0, 0) + /// .unwrap(); /// assert_eq!(&datetime.to_rfc3339(), "2016-11-08T00:00:00+05:00") /// ``` #[must_use] @@ -83,8 +85,10 @@ impl FixedOffset { #[cfg_attr(feature = "std", doc = "```")] /// use chrono::{FixedOffset, TimeZone}; /// let hour = 3600; - /// let datetime = FixedOffset::west_opt(5 * hour).unwrap().ymd_opt(2016, 11, 08).unwrap() - /// .and_hms_opt(0, 0, 0).unwrap(); + /// let datetime = FixedOffset::west_opt(5 * hour) + /// .unwrap() + /// .with_ymd_and_hms(2016, 11, 08, 0, 0, 0) + /// .unwrap(); /// assert_eq!(&datetime.to_rfc3339(), "2016-11-08T00:00:00-05:00") /// ``` #[must_use] diff --git a/src/offset/local/mod.rs b/src/offset/local/mod.rs index 47b7570c10..030b6f952b 100644 --- a/src/offset/local/mod.rs +++ b/src/offset/local/mod.rs @@ -49,7 +49,7 @@ mod tz_info; /// use chrono::{Local, DateTime, TimeZone}; /// /// let dt: DateTime = Local::now(); -/// let dt: DateTime = Local.timestamp(0, 0); +/// let dt: DateTime = Local.timestamp_opt(0, 0).unwrap(); /// ``` #[derive(Copy, Clone, Debug)] #[cfg_attr(feature = "rkyv", derive(Archive, Deserialize, Serialize))] diff --git a/src/offset/utc.rs b/src/offset/utc.rs index aeaeb672dc..dbcb8eecbb 100644 --- a/src/offset/utc.rs +++ b/src/offset/utc.rs @@ -35,9 +35,9 @@ use crate::{Date, DateTime}; /// ``` /// use chrono::{DateTime, TimeZone, NaiveDateTime, Utc}; /// -/// let dt = DateTime::::from_utc(NaiveDateTime::from_timestamp(61, 0), Utc); +/// let dt = DateTime::::from_utc(NaiveDateTime::from_timestamp_opt(61, 0).unwrap(), Utc); /// -/// assert_eq!(Utc.timestamp(61, 0), dt); +/// assert_eq!(Utc.timestamp_opt(61, 0).unwrap(), dt); /// assert_eq!(Utc.with_ymd_and_hms(1970, 1, 1, 0, 1, 1).unwrap(), dt); /// ``` #[derive(Copy, Clone, PartialEq, Eq, Hash)] From cddc999f67e470e4ac5160af0507651da802565b Mon Sep 17 00:00:00 2001 From: Paul Dicker Date: Tue, 23 May 2023 15:06:38 +0200 Subject: [PATCH 4/8] Update main examples and use `?` --- src/lib.rs | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 76835f2112..25df724952 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -128,21 +128,25 @@ //! use chrono::prelude::*; //! use chrono::offset::LocalResult; //! +//! # fn doctest() -> Option<()> { +//! //! let dt = Utc.with_ymd_and_hms(2014, 7, 8, 9, 10, 11).unwrap(); // `2014-07-08T09:10:11Z` +//! assert_eq!(dt, NaiveDate::from_ymd_opt(2014, 7, 8)?.and_hms_opt(9, 10, 11)?.and_local_timezone(Utc).unwrap()); +//! //! // July 8 is 188th day of the year 2014 (`o` for "ordinal") -//! assert_eq!(dt, Utc.yo(2014, 189).and_hms_opt(9, 10, 11).unwrap()); +//! assert_eq!(dt, NaiveDate::from_yo_opt(2014, 189)?.and_hms_opt(9, 10, 11)?.and_utc()); //! // July 8 is Tuesday in ISO week 28 of the year 2014. -//! assert_eq!(dt, Utc.isoywd(2014, 28, Weekday::Tue).and_hms_opt(9, 10, 11).unwrap()); +//! assert_eq!(dt, NaiveDate::from_isoywd_opt(2014, 28, Weekday::Tue)?.and_hms_opt(9, 10, 11)?.and_utc()); //! -//! let dt = NaiveDate::from_ymd_opt(2014, 7, 8).unwrap().and_hms_milli_opt(9, 10, 11, 12).unwrap().and_local_timezone(Utc).unwrap(); // `2014-07-08T09:10:11.012Z` -//! assert_eq!(dt, NaiveDate::from_ymd_opt(2014, 7, 8).unwrap().and_hms_micro_opt(9, 10, 11, 12_000).unwrap().and_local_timezone(Utc).unwrap()); -//! assert_eq!(dt, NaiveDate::from_ymd_opt(2014, 7, 8).unwrap().and_hms_nano_opt(9, 10, 11, 12_000_000).unwrap().and_local_timezone(Utc).unwrap()); +//! let dt = NaiveDate::from_ymd_opt(2014, 7, 8)?.and_hms_milli_opt(9, 10, 11, 12)?.and_local_timezone(Utc).unwrap(); // `2014-07-08T09:10:11.012Z` +//! assert_eq!(dt, NaiveDate::from_ymd_opt(2014, 7, 8)?.and_hms_micro_opt(9, 10, 11, 12_000)?.and_local_timezone(Utc).unwrap()); +//! assert_eq!(dt, NaiveDate::from_ymd_opt(2014, 7, 8)?.and_hms_nano_opt(9, 10, 11, 12_000_000)?.and_local_timezone(Utc).unwrap()); //! //! // dynamic verification -//! assert_eq!(Utc.ymd_opt(2014, 7, 8).and_hms_opt(21, 15, 33), -//! LocalResult::Single(Utc.with_ymd_and_hms(2014, 7, 8, 21, 15, 33).unwrap())); -//! assert_eq!(Utc.ymd_opt(2014, 7, 8).and_hms_opt(80, 15, 33), LocalResult::None); -//! assert_eq!(Utc.ymd_opt(2014, 7, 38).and_hms_opt(21, 15, 33), LocalResult::None); +//! assert_eq!(Utc.with_ymd_and_hms(2014, 7, 8, 21, 15, 33), +//! LocalResult::Single(NaiveDate::from_ymd_opt(2014, 7, 8)?.and_hms_opt(21, 15, 33)?.and_utc())); +//! assert_eq!(Utc.with_ymd_and_hms(2014, 7, 8, 80, 15, 33), LocalResult::None); +//! assert_eq!(Utc.with_ymd_and_hms(2014, 7, 38, 21, 15, 33), LocalResult::None); //! //! // other time zone objects can be used to construct a local datetime. //! // obviously, `local_dt` is normally different from `dt`, but `fixed_dt` should be identical. @@ -150,6 +154,9 @@ //! let fixed_dt = FixedOffset::east_opt(9 * 3600).unwrap().from_local_datetime(&NaiveDate::from_ymd_opt(2014, 7, 8).unwrap().and_hms_milli_opt(18, 10, 11, 12).unwrap()).unwrap(); //! assert_eq!(dt, fixed_dt); //! # let _ = local_dt; +//! # Some(()) +//! # } +//! # doctest().unwrap(); //! ``` //! //! Various properties are available to the date and time, and can be altered individually. From daf19a319a04d9c0d3160a2adfbd86a2e113b230 Mon Sep 17 00:00:00 2001 From: Paul Dicker Date: Tue, 23 May 2023 15:08:33 +0200 Subject: [PATCH 5/8] Ensure results are used --- src/format/parse.rs | 5 +++-- src/offset/local/mod.rs | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/format/parse.rs b/src/format/parse.rs index 4c79f4a5bc..a48c026cff 100644 --- a/src/format/parse.rs +++ b/src/format/parse.rs @@ -530,8 +530,9 @@ where /// /// ``` /// # use chrono::{DateTime, offset::FixedOffset}; -/// "2000-01-02T03:04:05Z".parse::>(); -/// "2000-01-02 03:04:05Z".parse::>(); +/// "2000-01-02T03:04:05Z".parse::>()?; +/// "2000-01-02 03:04:05Z".parse::>()?; +/// # Ok::<(), chrono::ParseError>(()) /// ``` impl str::FromStr for DateTime { type Err = ParseError; diff --git a/src/offset/local/mod.rs b/src/offset/local/mod.rs index 030b6f952b..1adddb7d60 100644 --- a/src/offset/local/mod.rs +++ b/src/offset/local/mod.rs @@ -48,8 +48,9 @@ mod tz_info; /// ``` /// use chrono::{Local, DateTime, TimeZone}; /// -/// let dt: DateTime = Local::now(); -/// let dt: DateTime = Local.timestamp_opt(0, 0).unwrap(); +/// let dt1: DateTime = Local::now(); +/// let dt2: DateTime = Local.timestamp_opt(0, 0).unwrap(); +/// assert!(dt1 >= dt2); /// ``` #[derive(Copy, Clone, Debug)] #[cfg_attr(feature = "rkyv", derive(Archive, Deserialize, Serialize))] From f90894a2474effb6e5f4b4985347906943fdbed1 Mon Sep 17 00:00:00 2001 From: Paul Dicker Date: Tue, 23 May 2023 15:28:11 +0200 Subject: [PATCH 6/8] Fix documentation that is no longer true --- src/datetime/mod.rs | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/datetime/mod.rs b/src/datetime/mod.rs index 1bdda19009..2e9203a307 100644 --- a/src/datetime/mod.rs +++ b/src/datetime/mod.rs @@ -1087,14 +1087,16 @@ where /// Accepts a relaxed form of RFC3339. /// A space or a 'T' are acepted as the separator between the date and time -/// parts. Additional spaces are allowed between each component. +/// parts. /// /// All of these examples are equivalent: /// ``` /// # use chrono::{DateTime, Utc}; -/// "2012-12-12T12:12:12Z".parse::>(); -/// "2012-12-12 12:12:12Z".parse::>(); -/// "2012- 12-12T12: 12:12Z".parse::>(); +/// "2012-12-12T12:12:12Z".parse::>()?; +/// "2012-12-12 12:12:12Z".parse::>()?; +/// "2012-12-12 12:12:12+0000".parse::>()?; +/// "2012-12-12 12:12:12+00:00".parse::>()?; +/// # Ok::<(), chrono::ParseError>(()) /// ``` impl str::FromStr for DateTime { type Err = ParseError; @@ -1106,14 +1108,16 @@ impl str::FromStr for DateTime { /// Accepts a relaxed form of RFC3339. /// A space or a 'T' are acepted as the separator between the date and time -/// parts. Additional spaces are allowed between each component. +/// parts. /// /// All of these examples are equivalent: /// ``` /// # use chrono::{DateTime, Local}; -/// "2012-12-12T12:12:12Z".parse::>(); -/// "2012-12-12 12:12:12Z".parse::>(); -/// "2012- 12-12T12: 12:12Z".parse::>(); +/// "2012-12-12T12:12:12Z".parse::>()?; +/// "2012-12-12 12:12:12Z".parse::>()?; +/// "2012-12-12 12:12:12+0000".parse::>()?; +/// "2012-12-12 12:12:12+00:00".parse::>()?; +/// # Ok::<(), chrono::ParseError>(()) /// ``` #[cfg(feature = "clock")] #[cfg_attr(docsrs, doc(cfg(feature = "clock")))] From ea52986f239e5e842f45637b48d54d454deae76f Mon Sep 17 00:00:00 2001 From: Paul Dicker Date: Tue, 23 May 2023 16:46:43 +0200 Subject: [PATCH 7/8] Use deserialization result in doctests --- src/datetime/serde.rs | 35 +++++++++++++++++++++-------------- src/naive/datetime/serde.rs | 28 ++++++++++++++++++---------- 2 files changed, 39 insertions(+), 24 deletions(-) diff --git a/src/datetime/serde.rs b/src/datetime/serde.rs index 12fc3dc89c..72ada0ccaa 100644 --- a/src/datetime/serde.rs +++ b/src/datetime/serde.rs @@ -189,16 +189,17 @@ pub mod ts_nanoseconds { /// # Example: /// /// ```rust - /// # use chrono::{DateTime, Utc}; + /// # use chrono::{DateTime, TimeZone, Utc}; /// # use serde_derive::Deserialize; /// use chrono::serde::ts_nanoseconds::deserialize as from_nano_ts; - /// #[derive(Deserialize)] + /// #[derive(Debug, PartialEq, Deserialize)] /// struct S { /// #[serde(deserialize_with = "from_nano_ts")] /// time: DateTime /// } /// /// let my_s: S = serde_json::from_str(r#"{ "time": 1526522699918355733 }"#)?; + /// assert_eq!(my_s, S { time: Utc.timestamp_opt(1526522699, 918355733).unwrap() }); /// # Ok::<(), serde_json::Error>(()) /// ``` #[must_use] @@ -316,16 +317,17 @@ pub mod ts_nanoseconds_option { /// # Example: /// /// ```rust - /// # use chrono::{DateTime, Utc}; + /// # use chrono::{DateTime, TimeZone, Utc}; /// # use serde_derive::Deserialize; /// use chrono::serde::ts_nanoseconds_option::deserialize as from_nano_tsopt; - /// #[derive(Deserialize)] + /// #[derive(Debug, PartialEq, Deserialize)] /// struct S { /// #[serde(deserialize_with = "from_nano_tsopt")] /// time: Option> /// } /// /// let my_s: S = serde_json::from_str(r#"{ "time": 1526522699918355733 }"#)?; + /// assert_eq!(my_s, S { time: Utc.timestamp_opt(1526522699, 918355733).single() }); /// # Ok::<(), serde_json::Error>(()) /// ``` #[must_use] @@ -444,16 +446,17 @@ pub mod ts_microseconds { /// # Example: /// /// ```rust - /// # use chrono::{DateTime, Utc}; + /// # use chrono::{DateTime, TimeZone, Utc}; /// # use serde_derive::Deserialize; /// use chrono::serde::ts_microseconds::deserialize as from_micro_ts; - /// #[derive(Deserialize)] + /// #[derive(Debug, PartialEq, Deserialize)] /// struct S { /// #[serde(deserialize_with = "from_micro_ts")] /// time: DateTime /// } /// /// let my_s: S = serde_json::from_str(r#"{ "time": 1526522699918355 }"#)?; + /// assert_eq!(my_s, S { time: Utc.timestamp_opt(1526522699, 918355000).unwrap() }); /// # Ok::<(), serde_json::Error>(()) /// ``` #[must_use] @@ -570,16 +573,17 @@ pub mod ts_microseconds_option { /// # Example: /// /// ```rust - /// # use chrono::{DateTime, Utc}; + /// # use chrono::{DateTime, TimeZone, Utc}; /// # use serde_derive::Deserialize; /// use chrono::serde::ts_microseconds_option::deserialize as from_micro_tsopt; - /// #[derive(Deserialize)] + /// #[derive(Debug, PartialEq, Deserialize)] /// struct S { /// #[serde(deserialize_with = "from_micro_tsopt")] /// time: Option> /// } /// /// let my_s: S = serde_json::from_str(r#"{ "time": 1526522699918355 }"#)?; + /// assert_eq!(my_s, S { time: Utc.timestamp_opt(1526522699, 918355000).single() }); /// # Ok::<(), serde_json::Error>(()) /// ``` #[must_use] @@ -698,16 +702,17 @@ pub mod ts_milliseconds { /// # Example: /// /// ```rust - /// # use chrono::{DateTime, Utc}; + /// # use chrono::{DateTime, TimeZone, Utc}; /// # use serde_derive::Deserialize; /// use chrono::serde::ts_milliseconds::deserialize as from_milli_ts; - /// #[derive(Deserialize)] + /// #[derive(Debug, PartialEq, Deserialize)] /// struct S { /// #[serde(deserialize_with = "from_milli_ts")] /// time: DateTime /// } /// /// let my_s: S = serde_json::from_str(r#"{ "time": 1526522699918 }"#)?; + /// assert_eq!(my_s, S { time: Utc.timestamp_opt(1526522699, 918000000).unwrap() }); /// # Ok::<(), serde_json::Error>(()) /// ``` #[must_use] @@ -962,16 +967,17 @@ pub mod ts_seconds { /// # Example: /// /// ```rust - /// # use chrono::{DateTime, Utc}; + /// # use chrono::{DateTime, TimeZone, Utc}; /// # use serde_derive::Deserialize; /// use chrono::serde::ts_seconds::deserialize as from_ts; - /// #[derive(Deserialize)] + /// #[derive(Debug, PartialEq, Deserialize)] /// struct S { /// #[serde(deserialize_with = "from_ts")] /// time: DateTime /// } /// /// let my_s: S = serde_json::from_str(r#"{ "time": 1431684000 }"#)?; + /// assert_eq!(my_s, S { time: Utc.timestamp_opt(1431684000, 0).unwrap() }); /// # Ok::<(), serde_json::Error>(()) /// ``` #[must_use] @@ -1082,16 +1088,17 @@ pub mod ts_seconds_option { /// # Example: /// /// ```rust - /// # use chrono::{DateTime, Utc}; + /// # use chrono::{DateTime, TimeZone, Utc}; /// # use serde_derive::Deserialize; /// use chrono::serde::ts_seconds_option::deserialize as from_tsopt; - /// #[derive(Deserialize)] + /// #[derive(Debug, PartialEq, Deserialize)] /// struct S { /// #[serde(deserialize_with = "from_tsopt")] /// time: Option> /// } /// /// let my_s: S = serde_json::from_str(r#"{ "time": 1431684000 }"#)?; + /// assert_eq!(my_s, S { time: Utc.timestamp_opt(1431684000, 0).single() }); /// # Ok::<(), serde_json::Error>(()) /// ``` #[must_use] diff --git a/src/naive/datetime/serde.rs b/src/naive/datetime/serde.rs index 5ab92d9559..8107f384d9 100644 --- a/src/naive/datetime/serde.rs +++ b/src/naive/datetime/serde.rs @@ -128,13 +128,14 @@ pub mod ts_nanoseconds { /// # use chrono::NaiveDateTime; /// # use serde_derive::Deserialize; /// use chrono::naive::serde::ts_nanoseconds::deserialize as from_nano_ts; - /// #[derive(Deserialize)] + /// #[derive(Debug, PartialEq, Deserialize)] /// struct S { /// #[serde(deserialize_with = "from_nano_ts")] /// time: NaiveDateTime /// } /// /// let my_s: S = serde_json::from_str(r#"{ "time": 1526522699918355733 }"#)?; + /// assert_eq!(my_s, S { time: NaiveDateTime::from_timestamp_opt(1526522699, 918355733).unwrap() }); /// # Ok::<(), serde_json::Error>(()) /// ``` #[must_use] @@ -253,13 +254,14 @@ pub mod ts_nanoseconds_option { /// # use chrono::naive::NaiveDateTime; /// # use serde_derive::Deserialize; /// use chrono::naive::serde::ts_nanoseconds_option::deserialize as from_nano_tsopt; - /// #[derive(Deserialize)] + /// #[derive(Debug, PartialEq, Deserialize)] /// struct S { /// #[serde(deserialize_with = "from_nano_tsopt")] /// time: Option /// } /// /// let my_s: S = serde_json::from_str(r#"{ "time": 1526522699918355733 }"#)?; + /// assert_eq!(my_s, S { time: NaiveDateTime::from_timestamp_opt(1526522699, 918355733) }); /// # Ok::<(), serde_json::Error>(()) /// ``` #[must_use] @@ -378,13 +380,14 @@ pub mod ts_microseconds { /// # use chrono::NaiveDateTime; /// # use serde_derive::Deserialize; /// use chrono::naive::serde::ts_microseconds::deserialize as from_micro_ts; - /// #[derive(Deserialize)] + /// #[derive(Debug, PartialEq, Deserialize)] /// struct S { /// #[serde(deserialize_with = "from_micro_ts")] /// time: NaiveDateTime /// } /// /// let my_s: S = serde_json::from_str(r#"{ "time": 1526522699918355 }"#)?; + /// assert_eq!(my_s, S { time: NaiveDateTime::from_timestamp_opt(1526522699, 918355000).unwrap() }); /// # Ok::<(), serde_json::Error>(()) /// ``` #[must_use] @@ -506,13 +509,14 @@ pub mod ts_microseconds_option { /// # use chrono::naive::NaiveDateTime; /// # use serde_derive::Deserialize; /// use chrono::naive::serde::ts_microseconds_option::deserialize as from_micro_tsopt; - /// #[derive(Deserialize)] + /// #[derive(Debug, PartialEq, Deserialize)] /// struct S { /// #[serde(deserialize_with = "from_micro_tsopt")] /// time: Option /// } /// /// let my_s: S = serde_json::from_str(r#"{ "time": 1526522699918355 }"#)?; + /// assert_eq!(my_s, S { time: NaiveDateTime::from_timestamp_opt(1526522699, 918355000) }); /// # Ok::<(), serde_json::Error>(()) /// ``` #[must_use] @@ -631,13 +635,14 @@ pub mod ts_milliseconds { /// # use chrono::NaiveDateTime; /// # use serde_derive::Deserialize; /// use chrono::naive::serde::ts_milliseconds::deserialize as from_milli_ts; - /// #[derive(Deserialize)] + /// #[derive(Debug, PartialEq, Deserialize)] /// struct S { /// #[serde(deserialize_with = "from_milli_ts")] /// time: NaiveDateTime /// } /// /// let my_s: S = serde_json::from_str(r#"{ "time": 1526522699918 }"#)?; + /// assert_eq!(my_s, S { time: NaiveDateTime::from_timestamp_opt(1526522699, 918000000).unwrap() }); /// # Ok::<(), serde_json::Error>(()) /// ``` #[must_use] @@ -746,7 +751,7 @@ pub mod ts_milliseconds_option { } } - /// Deserialize a `NaiveDateTime` from a nanosecond timestamp or none + /// Deserialize a `NaiveDateTime` from a millisecond timestamp or none /// /// Intended for use with `serde`s `deserialize_with` attribute. /// @@ -756,13 +761,14 @@ pub mod ts_milliseconds_option { /// # use chrono::naive::NaiveDateTime; /// # use serde_derive::Deserialize; /// use chrono::naive::serde::ts_milliseconds_option::deserialize as from_milli_tsopt; - /// #[derive(Deserialize)] + /// #[derive(Debug, PartialEq, Deserialize)] /// struct S { /// #[serde(deserialize_with = "from_milli_tsopt")] /// time: Option /// } /// - /// let my_s: S = serde_json::from_str(r#"{ "time": 1526522699918355 }"#)?; + /// let my_s: S = serde_json::from_str(r#"{ "time": 1526522699918 }"#)?; + /// assert_eq!(my_s, S { time: NaiveDateTime::from_timestamp_opt(1526522699, 918000000) }); /// # Ok::<(), serde_json::Error>(()) /// ``` #[must_use] @@ -881,13 +887,14 @@ pub mod ts_seconds { /// # use chrono::NaiveDateTime; /// # use serde_derive::Deserialize; /// use chrono::naive::serde::ts_seconds::deserialize as from_ts; - /// #[derive(Deserialize)] + /// #[derive(Debug, PartialEq, Deserialize)] /// struct S { /// #[serde(deserialize_with = "from_ts")] /// time: NaiveDateTime /// } /// /// let my_s: S = serde_json::from_str(r#"{ "time": 1431684000 }"#)?; + /// assert_eq!(my_s, S { time: NaiveDateTime::from_timestamp_opt(1431684000, 0).unwrap() }); /// # Ok::<(), serde_json::Error>(()) /// ``` #[must_use] @@ -1003,13 +1010,14 @@ pub mod ts_seconds_option { /// # use chrono::naive::NaiveDateTime; /// # use serde_derive::Deserialize; /// use chrono::naive::serde::ts_seconds_option::deserialize as from_tsopt; - /// #[derive(Deserialize)] + /// #[derive(Debug, PartialEq, Deserialize)] /// struct S { /// #[serde(deserialize_with = "from_tsopt")] /// time: Option /// } /// /// let my_s: S = serde_json::from_str(r#"{ "time": 1431684000 }"#)?; + /// assert_eq!(my_s, S { time: NaiveDateTime::from_timestamp_opt(1431684000, 0) }); /// # Ok::<(), serde_json::Error>(()) /// ``` #[must_use] From 02c5069accb862cb40687e87bd764d752787e427 Mon Sep 17 00:00:00 2001 From: Paul Dicker Date: Tue, 23 May 2023 14:57:55 +0200 Subject: [PATCH 8/8] Deny warnings in doctests --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 25df724952..3737d1a9b2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -382,7 +382,7 @@ //! Advanced time zone handling is not yet supported. //! For now you can try the [Chrono-tz](https://github.com/chronotope/chrono-tz/) crate instead. -#![doc(html_root_url = "https://docs.rs/chrono/latest/")] +#![doc(html_root_url = "https://docs.rs/chrono/latest/", test(attr(deny(warnings))))] #![cfg_attr(feature = "bench", feature(test))] // lib stability features as per RFC #507 #![deny(missing_docs)] #![deny(missing_debug_implementations)]