I'm writing a status bar, and i want to have a clock widget, displaying the current time with minute precision.
In order to integrate well with the rest of the widgets, i wanted to manage the clock update using a stream that yields "please update the time" each minute.
However, if i just use a timer that yields events every minute (like tokio::time::interval), if that timer happens to start at 12:00:59, it will almost always be one minute behind the true clock (displaying 12:00 from 12:00:59 to 12:01:58, etc...)
I looked for a way to round the time up to a minute, and found time::UtcDateTime, which does allow truncation. However, it seems that it is impossible to construct an Instant from a UtcDateTime, so i can't use that either.
What is the way to go for implementing a clock that ticks at the beginning of each minute ?
You can use interval_at. You have to calculate the duration between now and the start of the next minute (e.g. using the time crate you mentioned). Then you can add that duration to Instant::now() to get the start time for your interval.
While I'm pretty sure it will (mostly) not matter for the intended application, there are no guarantees that waiting 60 seconds means the system clock (wall clock) has changed by exactly one minute. Consider adjustments to the system clock for various reasons,for example leap seconds, or simply an incorrect time setting.
A practical solution is to first decide how accurate your display needs to be. If you want to be off by at most one second, you need to check the system time every second and, if the minute has changed since your last recording, emit an event or update your status bar.