-
Notifications
You must be signed in to change notification settings - Fork 554
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
Conversation
Despite the consistency argument I'm inclined to ditch the |
Done (we can always revert to the original). |
Construct NaiveDateTime from millis since epoch
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.
Thanks for working on this! LGTM modulo some documentation nits.
src/naive/datetime/mod.rs
Outdated
/// 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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
No, there are no invalid nanos in this case. Changed.
src/naive/datetime/mod.rs
Outdated
@@ -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, |
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.
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.
Seems like an improvement, some remaining nits.
src/naive/datetime/mod.rs
Outdated
/// | ||
/// Returns `None` on the out-of-range number of milliseconds and/or invalid nanosecond. | ||
/// Returns `None` on an out-of-range number of milliseconds. |
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.
Should ditch the prefix space here.
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.
done
src/naive/datetime/mod.rs
Outdated
/// 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"). | ||
/// Creates a new [NaiveDateTime] from milliseconds since UNIX Epoch. |
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.
Both here and in the next line, I think prefixing "UNIX" with "the" ("the UNIX epoch") would read better.
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.
Sure, done,
src/naive/datetime/mod.rs
Outdated
#[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 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
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.
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 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()
.
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.
Good call RE. avoiding TimeDelta
especially as from_millis
may well get deprecated anyway
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.
Done. The nanos calculation is split, as when the timestamp in millis is negative, an additional abs()
op is done.
Thanks for the PR @Pscheidl - this will be a useful helper function to have, and the docs look good - just noted a possible issue in the implementation when the input timestamp is negative |
All comments addressed. |
src/naive/datetime/mod.rs
Outdated
|
||
if millis < 0 { | ||
let nsecs = (millis % 1000).abs() as u32 * NANOS_IN_MILLISECOND; | ||
NaiveDateTime::from_timestamp_opt(secs - 1, NANOS_IN_SECOND - nsecs) |
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.
Thanks for the impl that handles negative millis. If possible could you subtract with checked_sub
here to avoid potential overflow
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.
Done
src/naive/datetime/mod.rs
Outdated
let secs = millis / 1000; | ||
|
||
if millis < 0 { | ||
let nsecs = (millis % 1000).abs() as u32 * NANOS_IN_MILLISECOND; |
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.
Do you mind switching as
for u32::try_from(expr).ok()?
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.
Done
src/naive/datetime/mod.rs
Outdated
let nsecs = (millis % 1000).abs() as u32 * NANOS_IN_MILLISECOND; | ||
NaiveDateTime::from_timestamp_opt(secs - 1, NANOS_IN_SECOND - nsecs) | ||
} else { | ||
let nsecs = (millis % 1000) as u32 * NANOS_IN_MILLISECOND; |
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.
Do you mind switching as for u32::try_from(expr).ok()?
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.
Done
Thanks @Pscheidl, the new negative millis handling looks good- I've just requested a few more small changes |
Awesome @esheppa , thank you. All comments addressed. |
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.
Thanks for those changes @Pscheidl
Learned a lot about the crate, thank you for the guidance. I can't hit the |
Oops, should have squashed this instead of rebasing. :( |
Backport to 0.4.x is in #823, which also includes some deduplication of the logic. |
Construct NaiveDateTime from millis since epoch.
Addresses #430.