From b4b87b3c0bbc683b5e19bd0602778a86e6bc633e Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Thu, 20 Oct 2022 12:40:08 +0200 Subject: [PATCH] Deprecate usage of the Date type --- src/date.rs | 10 +--------- src/datetime/mod.rs | 13 ++---------- src/offset/local/mod.rs | 3 +++ src/offset/mod.rs | 44 +++++++++++++++-------------------------- src/offset/utc.rs | 2 ++ 5 files changed, 24 insertions(+), 48 deletions(-) diff --git a/src/date.rs b/src/date.rs index 7a2635c0f4..7a0f996bd6 100644 --- a/src/date.rs +++ b/src/date.rs @@ -53,6 +53,7 @@ use crate::{Datelike, Weekday}; /// - The date is timezone-agnostic up to one day (i.e. practically always), /// so the local date and UTC date should be equal for most cases /// even though the raw calculation between `NaiveDate` and `Duration` may not. +#[deprecated(since = "0.4.23", note = "Use `NaiveDate` or `DateTime` instead")] #[derive(Clone)] #[cfg_attr(feature = "rkyv", derive(Archive, Deserialize, Serialize))] pub struct Date { @@ -324,15 +325,6 @@ where /// Formats the date with the specified format string. /// See the [`crate::format::strftime`] module /// on the supported escape sequences. - /// - /// # Example - /// ```rust - /// use chrono::prelude::*; - /// - /// let date_time: Date = Utc.ymd_opt(2017, 04, 02).unwrap(); - /// let formatted = format!("{}", date_time.format("%d/%m/%Y")); - /// assert_eq!(formatted, "02/04/2017"); - /// ``` #[cfg(any(feature = "alloc", feature = "std", test))] #[cfg_attr(docsrs, doc(cfg(any(feature = "alloc", feature = "std"))))] #[inline] diff --git a/src/datetime/mod.rs b/src/datetime/mod.rs index c5a4c721bb..e2e3b9461d 100644 --- a/src/datetime/mod.rs +++ b/src/datetime/mod.rs @@ -151,18 +151,9 @@ impl DateTime { /// Unless you are immediately planning on turning this into a `DateTime` /// with the same Timezone you should use the /// [`date_naive`](DateTime::date_naive) method. - /// - /// ``` - /// use chrono::prelude::*; - /// - /// let date: Date = Utc.ymd_opt(2020, 1, 1).unwrap(); - /// let dt: DateTime = date.and_hms_opt(0, 0, 0).unwrap(); - /// - /// assert_eq!(dt.date(), date); - /// - /// assert_eq!(dt.date().and_hms_opt(1, 1, 1).unwrap(), date.and_hms_opt(1, 1, 1).unwrap()); - /// ``` #[inline] + #[deprecated(since = "0.4.23", note = "Use `date_naive()` instead")] + #[allow(deprecated)] pub fn date(&self) -> Date { Date::from_utc(self.naive_local().date(), self.offset.clone()) } diff --git a/src/offset/local/mod.rs b/src/offset/local/mod.rs index 6abfebadc6..5a57feb5d3 100644 --- a/src/offset/local/mod.rs +++ b/src/offset/local/mod.rs @@ -57,6 +57,8 @@ pub struct Local; impl Local { /// Returns a `Date` which corresponds to the current date. + #[deprecated(since = "0.4.23", note = "use `Local::now().date_naive()` instead")] + #[allow(deprecated)] pub fn today() -> Date { Local::now().date() } @@ -97,6 +99,7 @@ impl TimeZone for Local { } // they are easier to define in terms of the finished date and time unlike other offsets + #[allow(deprecated)] fn offset_from_local_date(&self, local: &NaiveDate) -> LocalResult { self.from_local_date(local).map(|date| *date.offset()) } diff --git a/src/offset/mod.rs b/src/offset/mod.rs index 78256a663c..dd153a85ac 100644 --- a/src/offset/mod.rs +++ b/src/offset/mod.rs @@ -234,7 +234,8 @@ pub trait TimeZone: Sized + Clone { /// but it will propagate to the `DateTime` values constructed via this date. /// /// Panics on the out-of-range date, invalid month and/or day. - #[deprecated(since = "0.4.23", note = "use `ymd_opt()` instead")] + #[deprecated(since = "0.4.23", note = "use `with_ymd_and_hms()` instead")] + #[allow(deprecated)] fn ymd(&self, year: i32, month: u32, day: u32) -> Date { self.ymd_opt(year, month, day).unwrap() } @@ -246,15 +247,8 @@ pub trait TimeZone: Sized + Clone { /// but it will propagate to the `DateTime` values constructed via this date. /// /// Returns `None` on the out-of-range date, invalid month and/or day. - /// - /// # Example - /// - /// ``` - /// use chrono::{Utc, LocalResult, TimeZone}; - /// - /// assert_eq!(Utc.ymd_opt(2015, 5, 15).unwrap().to_string(), "2015-05-15UTC"); - /// assert_eq!(Utc.ymd_opt(2000, 0, 0), LocalResult::None); - /// ``` + #[deprecated(since = "0.4.23", note = "use `with_ymd_and_hms()` instead")] + #[allow(deprecated)] fn ymd_opt(&self, year: i32, month: u32, day: u32) -> LocalResult> { match NaiveDate::from_ymd_opt(year, month, day) { Some(d) => self.from_local_date(&d), @@ -269,7 +263,8 @@ pub trait TimeZone: Sized + Clone { /// but it will propagate to the `DateTime` values constructed via this date. /// /// Panics on the out-of-range date and/or invalid DOY. - #[deprecated(since = "0.4.23", note = "use `ymd_opt()` instead")] + #[deprecated(since = "0.4.23", note = "use `NaiveDateTime::and_timezone()` instead")] + #[allow(deprecated)] fn yo(&self, year: i32, ordinal: u32) -> Date { self.yo_opt(year, ordinal).unwrap() } @@ -281,14 +276,8 @@ pub trait TimeZone: Sized + Clone { /// but it will propagate to the `DateTime` values constructed via this date. /// /// Returns `None` on the out-of-range date and/or invalid DOY. - /// - /// # Example - /// - /// ``` - /// use chrono::{Utc, TimeZone}; - /// - /// assert_eq!(Utc.yo_opt(2015, 135).unwrap().to_string(), "2015-05-15UTC"); - /// ``` + #[deprecated(since = "0.4.23", note = "use `NaiveDateTime::and_timezone()` instead")] + #[allow(deprecated)] fn yo_opt(&self, year: i32, ordinal: u32) -> LocalResult> { match NaiveDate::from_yo_opt(year, ordinal) { Some(d) => self.from_local_date(&d), @@ -305,7 +294,8 @@ pub trait TimeZone: Sized + Clone { /// but it will propagate to the `DateTime` values constructed via this date. /// /// Panics on the out-of-range date and/or invalid week number. - #[deprecated(since = "0.4.23", note = "use `isoywd_opt()` instead")] + #[deprecated(since = "0.4.23", note = "use `NaiveDateTime::and_timezone()` instead")] + #[allow(deprecated)] fn isoywd(&self, year: i32, week: u32, weekday: Weekday) -> Date { self.isoywd_opt(year, week, weekday).unwrap() } @@ -319,14 +309,8 @@ pub trait TimeZone: Sized + Clone { /// but it will propagate to the `DateTime` values constructed via this date. /// /// Returns `None` on the out-of-range date and/or invalid week number. - /// - /// # Example - /// - /// ``` - /// use chrono::{Utc, Weekday, TimeZone}; - /// - /// assert_eq!(Utc.isoywd_opt(2015, 20, Weekday::Fri).unwrap().to_string(), "2015-05-15UTC"); - /// ``` + #[deprecated(since = "0.4.23", note = "use `NaiveDateTime::and_timezone()` instead")] + #[allow(deprecated)] fn isoywd_opt(&self, year: i32, week: u32, weekday: Weekday) -> LocalResult> { match NaiveDate::from_isoywd_opt(year, week, weekday) { Some(d) => self.from_local_date(&d), @@ -452,6 +436,8 @@ pub trait TimeZone: Sized + Clone { /// Converts the local `NaiveDate` to the timezone-aware `Date` if possible. #[allow(clippy::wrong_self_convention)] + #[deprecated(since = "0.4.23", note = "use `from_local_datetime()` instead")] + #[allow(deprecated)] fn from_local_date(&self, local: &NaiveDate) -> LocalResult> { self.offset_from_local_date(local).map(|offset| { // since FixedOffset is within +/- 1 day, the date is never affected @@ -475,6 +461,8 @@ pub trait TimeZone: Sized + Clone { /// Converts the UTC `NaiveDate` to the local time. /// The UTC is continuous and thus this cannot fail (but can give the duplicate local time). #[allow(clippy::wrong_self_convention)] + #[deprecated(since = "0.4.23", note = "use `from_utc_datetime()` instead")] + #[allow(deprecated)] fn from_utc_date(&self, utc: &NaiveDate) -> Date { Date::from_utc(*utc, self.offset_from_utc_date(utc)) } diff --git a/src/offset/utc.rs b/src/offset/utc.rs index 8cf03daa5c..071ff808ad 100644 --- a/src/offset/utc.rs +++ b/src/offset/utc.rs @@ -48,6 +48,8 @@ pub struct Utc; #[cfg_attr(docsrs, doc(cfg(feature = "clock")))] impl Utc { /// Returns a `Date` which corresponds to the current date. + #[deprecated(since = "0.4.23", note = "use `Utc::now()` or `NaiveDate::today()` instead")] + #[allow(deprecated)] pub fn today() -> Date { Utc::now().date() }