Skip to content

Commit

Permalink
Add From<NaiveDate> for NaiveDateTime
Browse files Browse the repository at this point in the history
  • Loading branch information
dcechano authored and djc committed Nov 20, 2023
1 parent 039189c commit 6f3ab9d
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/naive/datetime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1048,6 +1048,22 @@ impl NaiveDateTime {
expect!(NaiveDate::from_ymd_opt(1970, 1, 1), "").and_time(NaiveTime::MIN);
}

impl From<NaiveDate> for NaiveDateTime {
/// Converts a `NaiveDate` to a `NaiveDateTime` of the same date but at midnight.
///
/// # Example
///
/// ```
/// use chrono::{NaiveDate, NaiveDateTime};
///
/// let nd = NaiveDate::from_ymd_opt(2016, 5, 28).unwrap();
/// let ndt = NaiveDate::from_ymd_opt(2016, 5, 28).unwrap().and_hms_opt(0, 0, 0).unwrap();
/// assert_eq!(ndt, NaiveDateTime::from(nd));
fn from(date: NaiveDate) -> Self {
date.and_hms_opt(0, 0, 0).unwrap()
}
}

impl Datelike for NaiveDateTime {
/// Returns the year number in the [calendar date](./struct.NaiveDate.html#calendar-date).
///
Expand Down

0 comments on commit 6f3ab9d

Please sign in to comment.