From 7ba090d484cb93f73e5a7803dd62e4c4295f3b50 Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Thu, 20 Oct 2022 12:31:01 +0200 Subject: [PATCH] Add TimeZone::with_ymd_and_hms() helper method We don't add `with_yo_and_hms()` and `with_isoywd_and_hms()` here, instead recommending users can use `from_local_datetime()` with a separately constructed `NaiveDateTime`. --- src/offset/mod.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/offset/mod.rs b/src/offset/mod.rs index 40d5a76f..78256a66 100644 --- a/src/offset/mod.rs +++ b/src/offset/mod.rs @@ -206,6 +206,27 @@ pub trait TimeZone: Sized + Clone { /// The original `TimeZone` value can be recovered via `TimeZone::from_offset`. type Offset: Offset; + /// Make a new `DateTime` from year, month, day, time components and current time zone. + /// + /// This assumes the proleptic Gregorian calendar, with the year 0 being 1 BCE. + /// + /// Returns `LocalResult::None` on invalid input data. + fn with_ymd_and_hms( + &self, + year: i32, + month: u32, + day: u32, + hour: u32, + min: u32, + sec: u32, + ) -> LocalResult> { + match NaiveDate::from_ymd_opt(year, month, day).and_then(|d| d.and_hms_opt(hour, min, sec)) + { + Some(dt) => self.from_local_datetime(&dt), + None => LocalResult::None, + } + } + /// Makes a new `Date` from year, month, day and the current time zone. /// This assumes the proleptic Gregorian calendar, with the year 0 being 1 BCE. ///