Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NaiveDateTime::from_timestamp_millis(_opt) #818

Merged
merged 5 commits into from Sep 19, 2022
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/naive/datetime/mod.rs
Expand Up @@ -126,6 +126,27 @@ impl NaiveDateTime {
let datetime = NaiveDateTime::from_timestamp_opt(secs, nsecs);
datetime.expect("invalid or out-of-range datetime")
}
/// Creates a new [NaiveDateTime] from milliseconds since the UNIX Epoch.
Pscheidl marked this conversation as resolved.
Show resolved Hide resolved
///
/// The UNIX epoch starts on midnight, January 1, 1970, UTC.
///
/// Returns `None` on an out-of-range number of milliseconds.
///
/// # 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<NaiveDateTime> {
let secs = millis / 1000;
let nsecs = (millis % 1000) as u32 * 1_000_000;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this looks problematic as the input millis could be negative, and will still be negative after taking the modulo (playground)

One option would be to have two cases, depending on whether millis is positive or negative, similar to the logic used here

The other option is just:
NaiveDate::from_ymd(1970,1, 1).and_hms(0, 0, 0) + TimeDelta::from_millis(millis)

Also it would be preferable to use TryFrom instead of as

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, let's deal with that. I'm super busy ATM, please give me by the end of the week :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should go through TimeDelta here, which adds a bunch of overhead and feels like a layering violation. Would be better to do a minimal calculation and still defer to NaiveDateTime::from_timestamp_opt().

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call RE. avoiding TimeDelta especially as from_millis may well get deprecated anyway

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. The nanos calculation is split, as when the timestamp in millis is negative, an additional abs() op is done.

NaiveDateTime::from_timestamp_opt(secs, nsecs)
}

/// Makes a new `NaiveDateTime` corresponding to a UTC date and time,
/// from the number of non-leap seconds
Expand Down