From a9b250764fe8502116389738fc50012d6e70a3f4 Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Wed, 28 Sep 2022 14:19:06 +0200 Subject: [PATCH] Deprecate methods that have an _opt() alternative --- benches/chrono.rs | 10 +-- src/date.rs | 6 ++ src/naive/date.rs | 129 ++++---------------------------------- src/naive/datetime/mod.rs | 13 +--- src/naive/time/mod.rs | 65 ++----------------- src/offset/fixed.rs | 34 +++++----- src/offset/mod.rs | 69 +++++++++----------- tests/dateutils.rs | 13 ++-- 8 files changed, 83 insertions(+), 256 deletions(-) diff --git a/benches/chrono.rs b/benches/chrono.rs index c44a2a15..c0704e33 100644 --- a/benches/chrono.rs +++ b/benches/chrono.rs @@ -35,14 +35,14 @@ fn bench_datetime_from_str(c: &mut Criterion) { } fn bench_datetime_to_rfc2822(c: &mut Criterion) { - let pst = FixedOffset::east(8 * 60 * 60); - let dt = pst.ymd(2018, 1, 11).and_hms_nano(10, 5, 13, 84_660_000); + let pst = FixedOffset::east_opt(8 * 60 * 60).unwrap(); + let dt = pst.ymd_opt(2018, 1, 11).unwrap().and_hms_nano_opt(10, 5, 13, 84_660_000).unwrap(); c.bench_function("bench_datetime_to_rfc2822", |b| b.iter(|| black_box(dt).to_rfc2822())); } fn bench_datetime_to_rfc3339(c: &mut Criterion) { - let pst = FixedOffset::east(8 * 60 * 60); - let dt = pst.ymd(2018, 1, 11).and_hms_nano(10, 5, 13, 84_660_000); + let pst = FixedOffset::east_opt(8 * 60 * 60).unwrap(); + let dt = pst.ymd_opt(2018, 1, 11).and_hms_nano_opt(10, 5, 13, 84_660_000).unwrap(); c.bench_function("bench_datetime_to_rfc3339", |b| b.iter(|| black_box(dt).to_rfc3339())); } @@ -90,7 +90,7 @@ fn num_days_from_ce_alt(date: &Date) -> i32 { fn bench_num_days_from_ce(c: &mut Criterion) { let mut group = c.benchmark_group("num_days_from_ce"); for year in &[1, 500, 2000, 2019] { - let d = NaiveDate::from_ymd(*year, 1, 1); + let d = NaiveDate::from_ymd_opt(*year, 1, 1).unwrap(); group.bench_with_input(BenchmarkId::new("new", year), &d, |b, y| { b.iter(|| num_days_from_ce_alt(y)) }); diff --git a/src/date.rs b/src/date.rs index 5d30d887..2a18110c 100644 --- a/src/date.rs +++ b/src/date.rs @@ -93,6 +93,7 @@ impl Date { /// The offset in the current date is preserved. /// /// Panics on invalid hour, minute and/or second. + #[deprecated(since = "0.4.23", note = "Use and_hms_opt() instead")] #[inline] pub fn and_hms(&self, hour: u32, min: u32, sec: u32) -> DateTime { self.and_hms_opt(hour, min, sec).expect("invalid time") @@ -112,6 +113,7 @@ impl Date { /// The offset in the current date is preserved. /// /// Panics on invalid hour, minute, second and/or millisecond. + #[deprecated(since = "0.4.23", note = "Use and_hms_milli_opt() instead")] #[inline] pub fn and_hms_milli(&self, hour: u32, min: u32, sec: u32, milli: u32) -> DateTime { self.and_hms_milli_opt(hour, min, sec, milli).expect("invalid time") @@ -138,6 +140,7 @@ impl Date { /// The offset in the current date is preserved. /// /// Panics on invalid hour, minute, second and/or microsecond. + #[deprecated(since = "0.4.23", note = "Use and_hms_micro_opt() instead")] #[inline] pub fn and_hms_micro(&self, hour: u32, min: u32, sec: u32, micro: u32) -> DateTime { self.and_hms_micro_opt(hour, min, sec, micro).expect("invalid time") @@ -164,6 +167,7 @@ impl Date { /// The offset in the current date is preserved. /// /// Panics on invalid hour, minute, second and/or nanosecond. + #[deprecated(since = "0.4.23", note = "Use and_hms_nano_opt() instead")] #[inline] pub fn and_hms_nano(&self, hour: u32, min: u32, sec: u32, nano: u32) -> DateTime { self.and_hms_nano_opt(hour, min, sec, nano).expect("invalid time") @@ -188,6 +192,7 @@ impl Date { /// Makes a new `Date` for the next date. /// /// Panics when `self` is the last representable date. + #[deprecated(since = "0.4.23", note = "Use succ_opt() instead")] #[inline] pub fn succ(&self) -> Date { self.succ_opt().expect("out of bound") @@ -204,6 +209,7 @@ impl Date { /// Makes a new `Date` for the prior date. /// /// Panics when `self` is the first representable date. + #[deprecated(since = "0.4.23", note = "Use pred_opt() instead")] #[inline] pub fn pred(&self) -> Date { self.pred_opt().expect("out of bound") diff --git a/src/naive/date.rs b/src/naive/date.rs index 27dcafbb..09a4c23d 100644 --- a/src/naive/date.rs +++ b/src/naive/date.rs @@ -241,22 +241,7 @@ impl NaiveDate { /// (year, month and day). /// /// Panics on the out-of-range date, invalid month and/or day. - /// - /// # Example - /// - /// ``` - /// use chrono::{NaiveDate, Datelike, Weekday}; - /// - /// let d = NaiveDate::from_ymd(2015, 3, 14); - /// assert_eq!(d.year(), 2015); - /// assert_eq!(d.month(), 3); - /// assert_eq!(d.day(), 14); - /// assert_eq!(d.ordinal(), 73); // day of year - /// assert_eq!(d.iso_week().year(), 2015); - /// assert_eq!(d.iso_week().week(), 11); - /// assert_eq!(d.weekday(), Weekday::Sat); - /// assert_eq!(d.num_days_from_ce(), 735671); // days since January 1, 1 CE - /// ``` + #[deprecated(since = "0.4.23", note = "use `from_ymd_opt()` instead")] pub fn from_ymd(year: i32, month: u32, day: u32) -> NaiveDate { NaiveDate::from_ymd_opt(year, month, day).expect("invalid or out-of-range date") } @@ -289,22 +274,7 @@ impl NaiveDate { /// (year and day of the year). /// /// Panics on the out-of-range date and/or invalid day of year. - /// - /// # Example - /// - /// ``` - /// use chrono::{NaiveDate, Datelike, Weekday}; - /// - /// let d = NaiveDate::from_yo(2015, 73); - /// assert_eq!(d.ordinal(), 73); - /// assert_eq!(d.year(), 2015); - /// assert_eq!(d.month(), 3); - /// assert_eq!(d.day(), 14); - /// assert_eq!(d.iso_week().year(), 2015); - /// assert_eq!(d.iso_week().week(), 11); - /// assert_eq!(d.weekday(), Weekday::Sat); - /// assert_eq!(d.num_days_from_ce(), 735671); // days since January 1, 1 CE - /// ``` + #[deprecated(since = "0.4.23", note = "use `from_yo_opt()` instead")] pub fn from_yo(year: i32, ordinal: u32) -> NaiveDate { NaiveDate::from_yo_opt(year, ordinal).expect("invalid or out-of-range date") } @@ -339,22 +309,7 @@ impl NaiveDate { /// The resulting `NaiveDate` may have a different year from the input year. /// /// Panics on the out-of-range date and/or invalid week number. - /// - /// # Example - /// - /// ``` - /// use chrono::{NaiveDate, Datelike, Weekday}; - /// - /// let d = NaiveDate::from_isoywd(2015, 11, Weekday::Sat); - /// assert_eq!(d.iso_week().year(), 2015); - /// assert_eq!(d.iso_week().week(), 11); - /// assert_eq!(d.weekday(), Weekday::Sat); - /// assert_eq!(d.year(), 2015); - /// assert_eq!(d.month(), 3); - /// assert_eq!(d.day(), 14); - /// assert_eq!(d.ordinal(), 73); // day of year - /// assert_eq!(d.num_days_from_ce(), 735671); // days since January 1, 1 CE - /// ``` + #[deprecated(since = "0.4.23", note = "use `from_isoywd_opt()` instead")] pub fn from_isoywd(year: i32, week: u32, weekday: Weekday) -> NaiveDate { NaiveDate::from_isoywd_opt(year, week, weekday).expect("invalid or out-of-range date") } @@ -438,45 +393,7 @@ impl NaiveDate { /// January 1, 1 being day 1. /// /// Panics if the date is out of range. - /// - /// # Example - /// - /// ``` - /// use chrono::{NaiveDate, Datelike, Weekday}; - /// - /// let d = NaiveDate::from_num_days_from_ce(735671); - /// assert_eq!(d.num_days_from_ce(), 735671); // days since January 1, 1 CE - /// assert_eq!(d.year(), 2015); - /// assert_eq!(d.month(), 3); - /// assert_eq!(d.day(), 14); - /// assert_eq!(d.ordinal(), 73); // day of year - /// assert_eq!(d.iso_week().year(), 2015); - /// assert_eq!(d.iso_week().week(), 11); - /// assert_eq!(d.weekday(), Weekday::Sat); - /// ``` - /// - /// While not directly supported by Chrono, - /// it is easy to convert from the Julian day number - /// (January 1, 4713 BCE in the *Julian* calendar being Day 0) - /// to Gregorian with this method. - /// (Note that this panics when `jd` is out of range.) - /// - /// ``` - /// use chrono::NaiveDate; - /// - /// fn jd_to_date(jd: i32) -> NaiveDate { - /// // keep in mind that the Julian day number is 0-based - /// // while this method requires an 1-based number. - /// NaiveDate::from_num_days_from_ce(jd - 1721425) - /// } - /// - /// // January 1, 4713 BCE in Julian = November 24, 4714 BCE in Gregorian - /// assert_eq!(jd_to_date(0), NaiveDate::from_ymd(-4713, 11, 24)); - /// - /// assert_eq!(jd_to_date(1721426), NaiveDate::from_ymd(1, 1, 1)); - /// assert_eq!(jd_to_date(2450000), NaiveDate::from_ymd(1995, 10, 9)); - /// assert_eq!(jd_to_date(2451545), NaiveDate::from_ymd(2000, 1, 1)); - /// ``` + #[deprecated(since = "0.4.23", note = "use `from_num_days_from_ce_opt()` instead")] #[inline] pub fn from_num_days_from_ce(days: i32) -> NaiveDate { NaiveDate::from_num_days_from_ce_opt(days).expect("out-of-range date") @@ -493,7 +410,7 @@ impl NaiveDate { /// use chrono::NaiveDate; /// /// let from_ndays_opt = NaiveDate::from_num_days_from_ce_opt; - /// let from_ymd = NaiveDate::from_ymd; + /// let from_ymd = |y, m, d| NaiveDate::from_ymd_opt(y, m, d).unwrap(); /// /// assert_eq!(from_ndays_opt(730_000), Some(from_ymd(1999, 9, 3))); /// assert_eq!(from_ndays_opt(1), Some(from_ymd(1, 1, 1))); @@ -520,21 +437,7 @@ impl NaiveDate { /// of `weekday` in `month` (eg. the 6th Friday of March 2017) then this function will panic. /// /// `n` is 1-indexed. Passing `n=0` will cause a panic. - /// - /// # Example - /// - /// ``` - /// use chrono::{NaiveDate, Weekday}; - /// - /// let from_weekday_of_month = NaiveDate::from_weekday_of_month; - /// let from_ymd = NaiveDate::from_ymd; - /// - /// assert_eq!(from_weekday_of_month(2018, 8, Weekday::Wed, 1), from_ymd(2018, 8, 1)); - /// assert_eq!(from_weekday_of_month(2018, 8, Weekday::Fri, 1), from_ymd(2018, 8, 3)); - /// assert_eq!(from_weekday_of_month(2018, 8, Weekday::Tue, 2), from_ymd(2018, 8, 14)); - /// assert_eq!(from_weekday_of_month(2018, 8, Weekday::Fri, 4), from_ymd(2018, 8, 24)); - /// assert_eq!(from_weekday_of_month(2018, 8, Weekday::Fri, 5), from_ymd(2018, 8, 31)); - /// ``` + #[deprecated(since = "0.4.23", note = "use `from_weekday_of_month_opt()` instead")] pub fn from_weekday_of_month(year: i32, month: u32, weekday: Weekday, n: u8) -> NaiveDate { NaiveDate::from_weekday_of_month_opt(year, month, weekday, n).expect("out-of-range date") } @@ -799,6 +702,7 @@ impl NaiveDate { /// assert_eq!(dt.weekday(), Weekday::Wed); /// assert_eq!(dt.second(), 56); /// ``` + #[deprecated(since = "0.4.23", note = "use `and_hms_opt()` instead")] #[inline] pub fn and_hms(&self, hour: u32, min: u32, sec: u32) -> NaiveDateTime { self.and_hms_opt(hour, min, sec).expect("invalid time") @@ -847,6 +751,7 @@ impl NaiveDate { /// assert_eq!(dt.second(), 56); /// assert_eq!(dt.nanosecond(), 789_000_000); /// ``` + #[deprecated(since = "0.4.23", note = "use `and_hms_milli_opt()` instead")] #[inline] pub fn and_hms_milli(&self, hour: u32, min: u32, sec: u32, milli: u32) -> NaiveDateTime { self.and_hms_milli_opt(hour, min, sec, milli).expect("invalid time") @@ -903,6 +808,7 @@ impl NaiveDate { /// assert_eq!(dt.second(), 56); /// assert_eq!(dt.nanosecond(), 789_012_000); /// ``` + #[deprecated(since = "0.4.23", note = "use `and_hms_micro_opt()` instead")] #[inline] pub fn and_hms_micro(&self, hour: u32, min: u32, sec: u32, micro: u32) -> NaiveDateTime { self.and_hms_micro_opt(hour, min, sec, micro).expect("invalid time") @@ -945,20 +851,7 @@ impl NaiveDate { /// in order to represent the [leap second](./struct.NaiveTime.html#leap-second-handling). /// /// Panics on invalid hour, minute, second and/or nanosecond. - /// - /// # Example - /// - /// ``` - /// use chrono::{NaiveDate, NaiveDateTime, Datelike, Timelike, Weekday}; - /// - /// let d = NaiveDate::from_ymd(2015, 6, 3); - /// - /// let dt: NaiveDateTime = d.and_hms_nano(12, 34, 56, 789_012_345); - /// assert_eq!(dt.year(), 2015); - /// assert_eq!(dt.weekday(), Weekday::Wed); - /// assert_eq!(dt.second(), 56); - /// assert_eq!(dt.nanosecond(), 789_012_345); - /// ``` + #[deprecated(since = "0.4.23", note = "use `and_hms_nano_opt()` instead")] #[inline] pub fn and_hms_nano(&self, hour: u32, min: u32, sec: u32, nano: u32) -> NaiveDateTime { self.and_hms_nano_opt(hour, min, sec, nano).expect("invalid time") @@ -1041,6 +934,7 @@ impl NaiveDate { /// assert_eq!(NaiveDate::from_ymd(2015, 6, 30).succ(), NaiveDate::from_ymd(2015, 7, 1)); /// assert_eq!(NaiveDate::from_ymd(2015, 12, 31).succ(), NaiveDate::from_ymd(2016, 1, 1)); /// ``` + #[deprecated(since = "0.4.23", note = "use `succ_opt()` instead")] #[inline] pub fn succ(&self) -> NaiveDate { self.succ_opt().expect("out of bound") @@ -1077,6 +971,7 @@ impl NaiveDate { /// assert_eq!(NaiveDate::from_ymd(2015, 6, 1).pred(), NaiveDate::from_ymd(2015, 5, 31)); /// assert_eq!(NaiveDate::from_ymd(2015, 1, 1).pred(), NaiveDate::from_ymd(2014, 12, 31)); /// ``` + #[deprecated(since = "0.4.23", note = "use `pred_opt()` instead")] #[inline] pub fn pred(&self) -> NaiveDate { self.pred_opt().expect("out of bound") diff --git a/src/naive/datetime/mod.rs b/src/naive/datetime/mod.rs index 3e92895a..428341ee 100644 --- a/src/naive/datetime/mod.rs +++ b/src/naive/datetime/mod.rs @@ -119,18 +119,7 @@ impl NaiveDateTime { /// timestamp" cannot represent a leap second unambiguously.) /// /// Panics on the out-of-range number of seconds and/or invalid nanosecond. - /// - /// # Example - /// - /// ``` - /// use chrono::{NaiveDateTime, NaiveDate}; - /// - /// let dt = NaiveDateTime::from_timestamp(0, 42_000_000); - /// assert_eq!(dt, NaiveDate::from_ymd(1970, 1, 1).and_hms_milli(0, 0, 0, 42)); - /// - /// let dt = NaiveDateTime::from_timestamp(1_000_000_000, 0); - /// assert_eq!(dt, NaiveDate::from_ymd(2001, 9, 9).and_hms(1, 46, 40)); - /// ``` + #[deprecated(since = "0.4.23", note = "use `from_timestamp_opt()` instead")] #[inline] pub fn from_timestamp(secs: i64, nsecs: u32) -> NaiveDateTime { let datetime = NaiveDateTime::from_timestamp_opt(secs, nsecs); diff --git a/src/naive/time/mod.rs b/src/naive/time/mod.rs index a0345f8f..3132a004 100644 --- a/src/naive/time/mod.rs +++ b/src/naive/time/mod.rs @@ -201,18 +201,7 @@ impl NaiveTime { /// use `NaiveTime::from_hms_*` methods with a subsecond parameter instead. /// /// Panics on invalid hour, minute and/or second. - /// - /// # Example - /// - /// ``` - /// use chrono::{NaiveTime, Timelike}; - /// - /// let t = NaiveTime::from_hms(23, 56, 4); - /// assert_eq!(t.hour(), 23); - /// assert_eq!(t.minute(), 56); - /// assert_eq!(t.second(), 4); - /// assert_eq!(t.nanosecond(), 0); - /// ``` + #[deprecated(since = "0.4.23", note = "use `from_hms_opt()` instead")] #[inline] pub fn from_hms(hour: u32, min: u32, sec: u32) -> NaiveTime { NaiveTime::from_hms_opt(hour, min, sec).expect("invalid time") @@ -249,18 +238,7 @@ impl NaiveTime { /// in order to represent the [leap second](#leap-second-handling). /// /// Panics on invalid hour, minute, second and/or millisecond. - /// - /// # Example - /// - /// ``` - /// use chrono::{NaiveTime, Timelike}; - /// - /// let t = NaiveTime::from_hms_milli(23, 56, 4, 12); - /// assert_eq!(t.hour(), 23); - /// assert_eq!(t.minute(), 56); - /// assert_eq!(t.second(), 4); - /// assert_eq!(t.nanosecond(), 12_000_000); - /// ``` + #[deprecated(since = "0.4.23", note = "use `from_hms_milli_opt()` instead")] #[inline] pub fn from_hms_milli(hour: u32, min: u32, sec: u32, milli: u32) -> NaiveTime { NaiveTime::from_hms_milli_opt(hour, min, sec, milli).expect("invalid time") @@ -301,18 +279,7 @@ impl NaiveTime { /// in order to represent the [leap second](#leap-second-handling). /// /// Panics on invalid hour, minute, second and/or microsecond. - /// - /// # Example - /// - /// ``` - /// use chrono::{NaiveTime, Timelike}; - /// - /// let t = NaiveTime::from_hms_micro(23, 56, 4, 12_345); - /// assert_eq!(t.hour(), 23); - /// assert_eq!(t.minute(), 56); - /// assert_eq!(t.second(), 4); - /// assert_eq!(t.nanosecond(), 12_345_000); - /// ``` + #[deprecated(since = "0.4.23", note = "use `from_hms_micro_opt()` instead")] #[inline] pub fn from_hms_micro(hour: u32, min: u32, sec: u32, micro: u32) -> NaiveTime { NaiveTime::from_hms_micro_opt(hour, min, sec, micro).expect("invalid time") @@ -351,18 +318,7 @@ impl NaiveTime { /// in order to represent the [leap second](#leap-second-handling). /// /// Panics on invalid hour, minute, second and/or nanosecond. - /// - /// # Example - /// - /// ``` - /// use chrono::{NaiveTime, Timelike}; - /// - /// let t = NaiveTime::from_hms_nano(23, 56, 4, 12_345_678); - /// assert_eq!(t.hour(), 23); - /// assert_eq!(t.minute(), 56); - /// assert_eq!(t.second(), 4); - /// assert_eq!(t.nanosecond(), 12_345_678); - /// ``` + #[deprecated(since = "0.4.23", note = "use `from_hms_nano_opt()` instead")] #[inline] pub fn from_hms_nano(hour: u32, min: u32, sec: u32, nano: u32) -> NaiveTime { NaiveTime::from_hms_nano_opt(hour, min, sec, nano).expect("invalid time") @@ -405,18 +361,7 @@ impl NaiveTime { /// in order to represent the [leap second](#leap-second-handling). /// /// Panics on invalid number of seconds and/or nanosecond. - /// - /// # Example - /// - /// ``` - /// use chrono::{NaiveTime, Timelike}; - /// - /// let t = NaiveTime::from_num_seconds_from_midnight(86164, 12_345_678); - /// assert_eq!(t.hour(), 23); - /// assert_eq!(t.minute(), 56); - /// assert_eq!(t.second(), 4); - /// assert_eq!(t.nanosecond(), 12_345_678); - /// ``` + #[deprecated(since = "0.4.23", note = "use `from_num_seconds_from_midnight_opt()` instead")] #[inline] pub fn from_num_seconds_from_midnight(secs: u32, nano: u32) -> NaiveTime { NaiveTime::from_num_seconds_from_midnight_opt(secs, nano).expect("invalid time") diff --git a/src/offset/fixed.rs b/src/offset/fixed.rs index df1a990b..0dc047e6 100644 --- a/src/offset/fixed.rs +++ b/src/offset/fixed.rs @@ -33,6 +33,15 @@ impl FixedOffset { /// The negative `secs` means the Western Hemisphere. /// /// Panics on the out-of-bound `secs`. + #[deprecated(since = "0.4.23", note = "use `east_opt()` instead")] + pub fn east(secs: i32) -> FixedOffset { + FixedOffset::east_opt(secs).expect("FixedOffset::east out of bounds") + } + + /// Makes a new `FixedOffset` for the Eastern Hemisphere with given timezone difference. + /// The negative `secs` means the Western Hemisphere. + /// + /// Returns `None` on the out-of-bound `secs`. /// /// # Example /// @@ -43,14 +52,6 @@ impl FixedOffset { /// .and_hms(0, 0, 0); /// assert_eq!(&datetime.to_rfc3339(), "2016-11-08T00:00:00+05:00") /// ``` - pub fn east(secs: i32) -> FixedOffset { - FixedOffset::east_opt(secs).expect("FixedOffset::east out of bounds") - } - - /// Makes a new `FixedOffset` for the Eastern Hemisphere with given timezone difference. - /// The negative `secs` means the Western Hemisphere. - /// - /// Returns `None` on the out-of-bound `secs`. pub fn east_opt(secs: i32) -> Option { if -86_400 < secs && secs < 86_400 { Some(FixedOffset { local_minus_utc: secs }) @@ -63,6 +64,15 @@ impl FixedOffset { /// The negative `secs` means the Eastern Hemisphere. /// /// Panics on the out-of-bound `secs`. + #[deprecated(since = "0.4.23", note = "use `west_opt()` instead")] + pub fn west(secs: i32) -> FixedOffset { + FixedOffset::west_opt(secs).expect("FixedOffset::west out of bounds") + } + + /// Makes a new `FixedOffset` for the Western Hemisphere with given timezone difference. + /// The negative `secs` means the Eastern Hemisphere. + /// + /// Returns `None` on the out-of-bound `secs`. /// /// # Example /// @@ -73,14 +83,6 @@ impl FixedOffset { /// .and_hms(0, 0, 0); /// assert_eq!(&datetime.to_rfc3339(), "2016-11-08T00:00:00-05:00") /// ``` - pub fn west(secs: i32) -> FixedOffset { - FixedOffset::west_opt(secs).expect("FixedOffset::west out of bounds") - } - - /// Makes a new `FixedOffset` for the Western Hemisphere with given timezone difference. - /// The negative `secs` means the Eastern Hemisphere. - /// - /// Returns `None` on the out-of-bound `secs`. pub fn west_opt(secs: i32) -> Option { if -86_400 < secs && secs < 86_400 { Some(FixedOffset { local_minus_utc: -secs }) diff --git a/src/offset/mod.rs b/src/offset/mod.rs index eb89680a..9bf7480d 100644 --- a/src/offset/mod.rs +++ b/src/offset/mod.rs @@ -213,14 +213,7 @@ 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. - /// - /// # Example - /// - /// ``` - /// use chrono::{Utc, TimeZone}; - /// - /// assert_eq!(Utc.ymd(2015, 5, 15).to_string(), "2015-05-15UTC"); - /// ``` + #[deprecated(since = "0.4.23", note = "use `ymd_opt()` instead")] fn ymd(&self, year: i32, month: u32, day: u32) -> Date { self.ymd_opt(year, month, day).unwrap() } @@ -255,14 +248,7 @@ 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. - /// - /// # Example - /// - /// ``` - /// use chrono::{Utc, TimeZone}; - /// - /// assert_eq!(Utc.yo(2015, 135).to_string(), "2015-05-15UTC"); - /// ``` + #[deprecated(since = "0.4.23", note = "use `ymd_opt()` instead")] fn yo(&self, year: i32, ordinal: u32) -> Date { self.yo_opt(year, ordinal).unwrap() } @@ -274,6 +260,14 @@ 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"); + /// ``` fn yo_opt(&self, year: i32, ordinal: u32) -> LocalResult> { match NaiveDate::from_yo_opt(year, ordinal) { Some(d) => self.from_local_date(&d), @@ -290,14 +284,7 @@ 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. - /// - /// # Example - /// - /// ``` - /// use chrono::{Utc, Weekday, TimeZone}; - /// - /// assert_eq!(Utc.isoywd(2015, 20, Weekday::Fri).to_string(), "2015-05-15UTC"); - /// ``` + #[deprecated(since = "0.4.23", note = "use `isoywd_opt()` instead")] fn isoywd(&self, year: i32, week: u32, weekday: Weekday) -> Date { self.isoywd_opt(year, week, weekday).unwrap() } @@ -311,6 +298,14 @@ 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"); + /// ``` 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), @@ -324,14 +319,7 @@ pub trait TimeZone: Sized + Clone { /// /// Panics on the out-of-range number of seconds and/or invalid nanosecond, /// for a non-panicking version see [`timestamp_opt`](#method.timestamp_opt). - /// - /// # Example - /// - /// ``` - /// use chrono::{Utc, TimeZone}; - /// - /// assert_eq!(Utc.timestamp(1431648000, 0).to_string(), "2015-05-15 00:00:00 UTC"); - /// ``` + #[deprecated(since = "0.4.23", note = "use `timestamp_opt()` instead")] fn timestamp(&self, secs: i64, nsecs: u32) -> DateTime { self.timestamp_opt(secs, nsecs).unwrap() } @@ -342,6 +330,14 @@ pub trait TimeZone: Sized + Clone { /// /// Returns `LocalResult::None` on out-of-range number of seconds and/or /// invalid nanosecond, otherwise always returns `LocalResult::Single`. + /// + /// # Example + /// + /// ``` + /// use chrono::{Utc, TimeZone}; + /// + /// assert_eq!(Utc.timestamp_opt(1431648000, 0).unwrap().to_string(), "2015-05-15 00:00:00 UTC"); + /// ``` fn timestamp_opt(&self, secs: i64, nsecs: u32) -> LocalResult> { match NaiveDateTime::from_timestamp_opt(secs, nsecs) { Some(dt) => LocalResult::Single(self.from_utc_datetime(&dt)), @@ -354,14 +350,7 @@ pub trait TimeZone: Sized + Clone { /// /// Panics on out-of-range number of milliseconds for a non-panicking /// version see [`timestamp_millis_opt`](#method.timestamp_millis_opt). - /// - /// # Example - /// - /// ``` - /// use chrono::{Utc, TimeZone}; - /// - /// assert_eq!(Utc.timestamp_millis(1431648000).timestamp(), 1431648); - /// ``` + #[deprecated(since = "0.4.23", note = "use `timestamp_millis_opt()` instead")] fn timestamp_millis(&self, millis: i64) -> DateTime { self.timestamp_millis_opt(millis).unwrap() } diff --git a/tests/dateutils.rs b/tests/dateutils.rs index d2d3aaf5..8d0516dc 100644 --- a/tests/dateutils.rs +++ b/tests/dateutils.rs @@ -33,11 +33,12 @@ fn verify_against_date_command_local(path: &'static str, dt: NaiveDateTime) { // This is used while a decision is made wheter the `date` output needs to // be exactly matched, or whether LocalResult::Ambigious should be handled // differently - match Local.from_local_datetime(&NaiveDate::from_ymd(dt.year(), dt.month(), dt.day()).and_hms( - dt.hour(), - 5, - 1, - )) { + match Local.from_local_datetime( + &NaiveDate::from_ymd_opt(dt.year(), dt.month(), dt.day()) + .unwrap() + .and_hms_opt(dt.hour(), 5, 1) + .unwrap(), + ) { chrono::LocalResult::Ambiguous(a, b) => assert!( format!("{}\n", a) == date_command_str || format!("{}\n", b) == date_command_str ), @@ -63,7 +64,7 @@ fn try_verify_against_date_command() { return; } - let mut date = NaiveDate::from_ymd(1975, 1, 1).and_hms(0, 0, 0); + let mut date = NaiveDate::from_ymd_opt(1975, 1, 1).unwrap().and_hms_opt(0, 0, 0).unwrap(); while date.year() < 2078 { if (1975..=1977).contains(&date.year())