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
Changes from 1 commit
341ed5a
80885d8
63b26bb
b175f9c
de12842
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In this case, there's no "invalid nanosecond", right? Also I think "the" -> "an" would read better? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, there are no invalid nanos in this case. Changed. |
||
/// | ||
/// # 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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this looks problematic as the input One option would be to have two cases, depending on whether The other option is just: Also it would be preferable to use There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we should go through There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good call RE. avoiding There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
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") | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You probably copy-pasted this from one of the other methods, but we're trying to make the first line of docstrings self-contained sentence fragments. After that we should have an empty line, then a more detailed description. Would you mind tweaking the docs for this new method at least?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the whole doc can be simplified. Gave it a try. Please have a look.