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

Convert between Instant and OffsetDateTime #199

Closed
binomial0 opened this issue Dec 29, 2019 · 1 comment
Closed

Convert between Instant and OffsetDateTime #199

binomial0 opened this issue Dec 29, 2019 · 1 comment
Labels
C-feature-request Category: a new feature (not already implemented)

Comments

@binomial0
Copy link

It seems to me that Instant and OffsetDateTime represent a fairly similar concept (some absolute point in time). It would be useful to convert between them, for example to obtain an Instant for "tomorrow at 11am" and use tokio or something else to wait until then. However, I can't find an obvious way to perform this conversion.

It might be possible to do something like Instant::now() + (datetime - OffsetDateTime::now()), but this seems quite unintuitive and I'm also not sure if the internal data formats are suitable for this conversion.

Is Instant (or, for that matter, std::time::Instant) even suitable for representing instants that are days or even weeks into the past/future?
If so, would an explicit method for converting from OffsetDateTime to Instant be in scope of this crate?
If not, I think we should document why this conversion is a bad idea.

@jhpratt
Copy link
Member

jhpratt commented Dec 29, 2019

The Instant in the time crate is nearly identical to that in the standard library (and is actually just a thin wrapper). Instants can only be used with themselves and Durations, either from this crate or stdlib.

Instant is primarily used for timing things, and can not be used to represent a known moment in time. That's what OffsetDateTime is for.

Completely aside from the two structs serving entirely different purposes, this is quite literally impossible. Instant ultimately performs syscalls via the stdlib implementation, and these are almost always a counter since boot time. Needless to say, there is no concept of an epoch there, unless you can somehow guarantee the boot time.

@jhpratt jhpratt closed this as completed Dec 29, 2019
@jhpratt jhpratt added C-feature-request Category: a new feature (not already implemented) wontfix labels Dec 29, 2019
@jhpratt jhpratt closed this as not planned Won't fix, can't repro, duplicate, stale Jun 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-feature-request Category: a new feature (not already implemented)
Projects
None yet
Development

No branches or pull requests

2 participants