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
Add DateTime::<Utc>::from_timestamp_opt
#1278
Conversation
Codecov Report
@@ Coverage Diff @@
## main #1278 +/- ##
==========================================
- Coverage 86.32% 86.30% -0.03%
==========================================
Files 35 35
Lines 13896 13900 +4
==========================================
Hits 11996 11996
- Misses 1900 1904 +4
📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more |
The discussion in #832 proposed to add Note that doctests were added, but Codecov seems to not count them. |
a3ae3b8
to
0e52353
Compare
This commit adds the new constructor `from_timestamp_opt` to build a `DateTime<Utc>` from a UNIX timestamp. Figuring out how to convert a timestamp into a `DateTime<Utc>` was a common issue: - chronotope#88 - chronotope#200 - chronotope#832 This commit should make `DateTime<Utc>` creation more discoverable and intuitive. This commit respects the current convention of using the `_opt` suffix for fallible functions. As panicking variants on invalid input are deprecated, no panicking variant is provided. See [this issue](chronotope#815) for discussion about error handling and panics. Closes chronotope#832
0e52353
to
990a4e3
Compare
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.
Thank you for working on this!
Can you please rebase on 0.4.x? Then it can be included in a 0.4 release, instead of a far-off 0.5.
That should also take care of code coverage with doctests.
/// invalid nanosecond, otherwise returns `Some(DateTime {...})`. | ||
/// | ||
/// If you need to create a `DateTime` with some other [`TimeZone`], use [`TimeZone::timestamp_opt`] | ||
/// or [`DateTime::with_timezone`]. |
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 documentation 👍
#[must_use] | ||
pub fn from_timestamp_opt(secs: i64, nsecs: u32) -> Option<Self> { | ||
NaiveDateTime::from_timestamp_opt(secs, nsecs) | ||
.map(|ndt| DateTime::from_naive_utc_and_offset(ndt, Utc)) |
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 can be simplified to NaiveDateTime::from_timestamp_opt(secs, nsecs).map(|ndt| ndt.and_utc())
@@ -301,7 +301,7 @@ | |||
//! | |||
//! ### Conversion from and to EPOCH timestamps | |||
//! | |||
//! Use [`Utc.timestamp(seconds, nanoseconds)`](./offset/trait.TimeZone.html#method.timestamp) | |||
//! Use [`DateTime::from_timestamp_opt(seconds, nanoseconds)`](./struct.DateTime.html#method.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.
It is pre-existing, but can you replace the html link with:
[`DateTime::from_timestamp_opt(seconds, nanoseconds)`](DateTime::from_timestamp_opt)
For new methods I think we just drop the |
Thank you for the quick review, I'll apply the changes and open a new PR for the branch |
Superseded by the PR for the branch |
This commit adds the new constructor
from_timestamp_opt
to build aDateTime<Utc>
from a UNIX timestamp.Figuring out how to convert a timestamp into a
DateTime<Utc>
was a common issue:This commit should make
DateTime<Utc>
creation more discoverable and intuitive.This commit respects the current convention of using the
_opt
suffix for fallible functions. As panicking variants on invalid input are deprecated, no panicking variant is provided. See this issue for discussion about error handling and panics.Closes #832