diff --git a/src/datetime/mod.rs b/src/datetime/mod.rs index 11b2627f96..10ad4694ed 100644 --- a/src/datetime/mod.rs +++ b/src/datetime/mod.rs @@ -126,12 +126,18 @@ impl DateTime { DateTime { datetime, offset } } + /// Makes a new `DateTime` from its components: a `NaiveDateTime` in UTC and an `Offset`. + #[inline] + #[must_use] + #[deprecated( + since = "0.4.27", + note = "Use TimeZone::from_utc_datetime() or DateTime::from_naive_utc_and_offset instead" + )] pub fn from_utc(datetime: NaiveDateTime, offset: Tz::Offset) -> DateTime { DateTime { datetime, offset } } - /// Makes a new `DateTime` with given **local** datetime and offset that - /// presents local timezone. + /// Makes a new `DateTime` from a `NaiveDateTime` in *local* time and an `Offset`. /// /// # Panics /// @@ -139,30 +145,12 @@ impl DateTime { /// /// This can happen if `datetime` is near the end of the representable range of `NaiveDateTime`, /// and the offset from UTC pushes it beyond that. - /// - /// # Example - /// - /// ``` - /// use chrono::DateTime; - /// use chrono::naive::NaiveDate; - /// use chrono::offset::{Utc, FixedOffset}; - /// - /// let naivedatetime_utc = NaiveDate::from_ymd_opt(2000, 1, 12).unwrap().and_hms_opt(2, 0, 0).unwrap(); - /// let datetime_utc = DateTime::::from_utc(naivedatetime_utc, Utc); - /// - /// let timezone_east = FixedOffset::east_opt(8 * 60 * 60).unwrap(); - /// let naivedatetime_east = NaiveDate::from_ymd_opt(2000, 1, 12).unwrap().and_hms_opt(10, 0, 0).unwrap(); - /// let datetime_east = DateTime::::from_local(naivedatetime_east, timezone_east); - /// - /// let timezone_west = FixedOffset::west_opt(7 * 60 * 60).unwrap(); - /// let naivedatetime_west = NaiveDate::from_ymd_opt(2000, 1, 11).unwrap().and_hms_opt(19, 0, 0).unwrap(); - /// let datetime_west = DateTime::::from_local(naivedatetime_west, timezone_west); - - /// assert_eq!(datetime_east, datetime_utc.with_timezone(&timezone_east)); - /// assert_eq!(datetime_west, datetime_utc.with_timezone(&timezone_west)); - /// ``` #[inline] #[must_use] + #[deprecated( + since = "0.4.27", + note = "Use TimeZone::from_local_datetime() or NaiveDateTime::and_local_timezone instead" + )] pub fn from_local(datetime: NaiveDateTime, offset: Tz::Offset) -> DateTime { let datetime_utc = datetime - offset.fix(); diff --git a/src/datetime/tests.rs b/src/datetime/tests.rs index 2d273f605d..9eb582a294 100644 --- a/src/datetime/tests.rs +++ b/src/datetime/tests.rs @@ -469,7 +469,7 @@ fn test_rfc3339_opts() { assert_eq!(dt.to_rfc3339_opts(Nanos, false), "2018-01-11T10:05:13.084660000+08:00"); assert_eq!(dt.to_rfc3339_opts(AutoSi, false), "2018-01-11T10:05:13.084660+08:00"); - let ut = DateTime::::from_utc(dt.naive_utc(), Utc); + let ut = dt.naive_utc().and_utc(); assert_eq!(ut.to_rfc3339_opts(Secs, false), "2018-01-11T02:05:13+00:00"); assert_eq!(ut.to_rfc3339_opts(Secs, true), "2018-01-11T02:05:13Z"); assert_eq!(ut.to_rfc3339_opts(Millis, false), "2018-01-11T02:05:13.084+00:00"); @@ -783,6 +783,7 @@ fn test_datetime_format_alignment() { } #[test] +#[allow(deprecated)] fn test_datetime_from_local() { // 2000-01-12T02:00:00Z let naivedatetime_utc = @@ -828,7 +829,7 @@ fn test_years_elapsed() { #[test] fn test_datetime_add_assign() { let naivedatetime = NaiveDate::from_ymd_opt(2000, 1, 1).unwrap().and_hms_opt(0, 0, 0).unwrap(); - let datetime = DateTime::::from_utc(naivedatetime, Utc); + let datetime = naivedatetime.and_utc(); let mut datetime_add = datetime; datetime_add += Duration::seconds(60); @@ -865,7 +866,7 @@ fn test_datetime_add_assign_local() { #[test] fn test_datetime_sub_assign() { let naivedatetime = NaiveDate::from_ymd_opt(2000, 1, 1).unwrap().and_hms_opt(12, 0, 0).unwrap(); - let datetime = DateTime::::from_utc(naivedatetime, Utc); + let datetime = naivedatetime.and_utc(); let mut datetime_sub = datetime; datetime_sub -= Duration::minutes(90); diff --git a/src/offset/utc.rs b/src/offset/utc.rs index dbcb8eecbb..f531bcae67 100644 --- a/src/offset/utc.rs +++ b/src/offset/utc.rs @@ -33,9 +33,9 @@ use crate::{Date, DateTime}; /// # Example /// /// ``` -/// use chrono::{DateTime, TimeZone, NaiveDateTime, Utc}; +/// use chrono::{TimeZone, NaiveDateTime, Utc}; /// -/// let dt = DateTime::::from_utc(NaiveDateTime::from_timestamp_opt(61, 0).unwrap(), Utc); +/// let dt = Utc.from_utc_datetime(&NaiveDateTime::from_timestamp_opt(61, 0).unwrap()); /// /// assert_eq!(Utc.timestamp_opt(61, 0).unwrap(), dt); /// assert_eq!(Utc.with_ymd_and_hms(1970, 1, 1, 0, 1, 1).unwrap(), dt); @@ -71,7 +71,7 @@ impl Utc { SystemTime::now().duration_since(UNIX_EPOCH).expect("system time before Unix epoch"); let naive = NaiveDateTime::from_timestamp_opt(now.as_secs() as i64, now.subsec_nanos()).unwrap(); - DateTime::from_utc(naive, Utc) + Utc.from_utc_datetime(&naive) } /// Returns a `DateTime` which corresponds to the current date and time.