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

Add documentation to OffsetDateTime::unix_timestamp about constructing a timestamp-nanosecond pair from an OffsetDateTime #723

Open
sorairolake opened this issue Jan 10, 2025 · 0 comments

Comments

@sorairolake
Copy link

That is, the inverse operation of the following example:

/// If you have a timestamp-nanosecond pair, you can use something along the lines of the
/// following:
///
/// ```rust
/// # use time::{Duration, OffsetDateTime, ext::NumericalDuration};
/// let (timestamp, nanos) = (1, 500_000_000);
/// assert_eq!(
/// OffsetDateTime::from_unix_timestamp(timestamp)? + Duration::nanoseconds(nanos),
/// OffsetDateTime::UNIX_EPOCH + 1.5.seconds()
/// );
/// # Ok::<_, time::Error>(())
/// ```
pub const fn from_unix_timestamp(timestamp: i64) -> Result<Self, error::ComponentRange> {

I think this operation can be written as follows:

use time::{ext::NumericalDuration, OffsetDateTime};

let dt = OffsetDateTime::UNIX_EPOCH + 1.5.seconds();
assert_eq!((dt.unix_timestamp(), dt.nanosecond()), (1, 500_000_000));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant