diff --git a/src/datetime/mod.rs b/src/datetime/mod.rs index e77043e298..38593e9042 100644 --- a/src/datetime/mod.rs +++ b/src/datetime/mod.rs @@ -617,6 +617,14 @@ impl DateTime { pub fn from_timestamp(secs: i64, nsecs: u32) -> Option { NaiveDateTime::from_timestamp_opt(secs, nsecs).as_ref().map(NaiveDateTime::and_utc) } + + // FIXME: remove when our MSRV is 1.61+ + // This method is used by `NaiveDateTime::and_utc` because `DateTime::from_naive_utc_and_offset` + // can't be made const yet. + // Trait bounds in const function / implementation blocks were not supported until 1.61. + pub(crate) const fn from_naive_utc(datetime: NaiveDateTime) -> Self { + DateTime { datetime, offset: Utc } + } } impl Default for DateTime { diff --git a/src/naive/datetime/mod.rs b/src/naive/datetime/mod.rs index 33eaacab4c..0feac9f2e7 100644 --- a/src/naive/datetime/mod.rs +++ b/src/naive/datetime/mod.rs @@ -963,8 +963,9 @@ impl NaiveDateTime { /// assert_eq!(dt.timezone(), Utc); /// ``` #[must_use] - pub fn and_utc(&self) -> DateTime { - Utc.from_utc_datetime(self) + pub const fn and_utc(&self) -> DateTime { + // FIXME: use `DateTime::from_naive_utc_and_offset` when our MSRV is 1.61+. + DateTime::from_naive_utc(*self) } /// The minimum possible `NaiveDateTime`.