Skip to content

Commit

Permalink
Add TimeZone::with_ymd_and_hms() helper method
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
djc committed Oct 20, 2022
1 parent e7a2bfc commit 812b7c5
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/offset/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<DateTime<Self>> {
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.
///
Expand Down

0 comments on commit 812b7c5

Please sign in to comment.