Now to tokio::time::Instant and chrono::DateTime

Is here a way to convert a now() to a tokio::time::Instant and a chrono::DateTime struct?

No. The Instant types (both in std and Tokio) are generally not connected to real world time. For example, if you change the clock on your computer an hour backwards, that would affect the chrono time, but not the tokio::time::Instant object. Similarly, the Instant type will not advance when the computer is suspended on some operating systems.

However, what you can do is to ask chrono what the duration until your event is, and then add that duration to Instant::now(). This works because the Duration type is compatible between the two clocks. (But if the duration is very large, you might want to sleep for a shorter duration multiple times in case the clock is changed or the computer is suspended.)

4 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.