From 221e6407720bd06cae99665e6f09d5d63cd95c68 Mon Sep 17 00:00:00 2001 From: Paul Dicker Date: Tue, 12 Mar 2024 13:32:53 +0100 Subject: [PATCH] Add `DateTime::with_time()` --- src/datetime/mod.rs | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/datetime/mod.rs b/src/datetime/mod.rs index 6715dfc843..5e5f96da5c 100644 --- a/src/datetime/mod.rs +++ b/src/datetime/mod.rs @@ -25,7 +25,7 @@ use crate::format::{write_rfc2822, write_rfc3339, DelayedFormat, SecondsFormat}; use crate::naive::{Days, IsoWeek, NaiveDate, NaiveDateTime, NaiveTime}; #[cfg(feature = "clock")] use crate::offset::Local; -use crate::offset::{FixedOffset, Offset, TimeZone, Utc}; +use crate::offset::{FixedOffset, LocalResult, Offset, TimeZone, Utc}; #[allow(deprecated)] use crate::Date; use crate::{expect, try_opt}; @@ -684,6 +684,31 @@ impl DateTime { result } + /// Set the time to a new fixed time on the existing date. + /// + /// # Errors + /// + /// Returns `LocalResult::None` if the datetime is at the edge of the representable range for a + /// `DateTime`, and `with_time` would push the value in UTC out of range. + /// + /// # Example + /// + #[cfg_attr(not(feature = "clock"), doc = "```ignore")] + #[cfg_attr(feature = "clock", doc = "```rust")] + /// use chrono::{Local, NaiveTime}; + /// + /// let noon = NaiveTime::from_hms_opt(12, 0, 0).unwrap(); + /// let today_noon = Local::now().with_time(noon); + /// let today_midnight = Local::now().with_time(NaiveTime::MIN); + /// + /// assert_eq!(today_noon.single().unwrap().time(), noon); + /// assert_eq!(today_midnight.single().unwrap().time(), NaiveTime::MIN); + /// ``` + #[must_use] + pub fn with_time(&self, time: NaiveTime) -> LocalResult { + self.timezone().from_local_datetime(&self.overflowing_naive_local().date().and_time(time)) + } + /// The minimum possible `DateTime`. pub const MIN_UTC: DateTime = DateTime { datetime: NaiveDateTime::MIN, offset: Utc }; /// The maximum possible `DateTime`.