diff --git a/src/naive/datetime/mod.rs b/src/naive/datetime/mod.rs index 90f0a08377..cbf7b57522 100644 --- a/src/naive/datetime/mod.rs +++ b/src/naive/datetime/mod.rs @@ -127,6 +127,28 @@ impl NaiveDateTime { datetime.expect("invalid or out-of-range datetime") } + /// Makes a new `NaiveDateTime` corresponding to a UTC date and time, + /// from the number of milliseconds + /// since the midnight UTC on January 1, 1970 (aka "UNIX timestamp"). + /// + /// Returns `None` on the out-of-range number of milliseconds and/or invalid nanosecond. + /// + /// # Example + /// + /// ``` + /// use chrono::NaiveDateTime; + /// let timestamp_millis: i64 = 1662921288; //Sunday, September 11, 2022 6:34:48 PM + /// let naive_datetime = NaiveDateTime::from_timestamp_millis(timestamp_millis); + /// assert!(naive_datetime.is_some()); + /// assert_eq!(timestamp_millis, naive_datetime.unwrap().timestamp_millis()); + /// ``` + #[inline] + pub fn from_timestamp_millis(millis: i64) -> Option { + let secs = millis / 1000; + let nsecs = (millis % 1000) as u32 * 1_000_000; + NaiveDateTime::from_timestamp_opt(secs, nsecs) + } + /// Makes a new `NaiveDateTime` corresponding to a UTC date and time, /// from the number of non-leap seconds /// since the midnight UTC on January 1, 1970 (aka "UNIX timestamp")