I believe tokio::time::sleep only counts time while the computer is switched on ( at least running under windows ).
I would like a sleep function that count times while the computer is switched off (sleeping) ( "real time" ).
I can think of a way to implement this using a task that sleeps for a fairly short time, and uses "real time" to schedule sleeping tasks, but was wondering if there is an existing function in tokio or some other crate that handles this?
I made my own async function, which does what I need ( it doesn't have to be especially accurate, and if it returns a bit early on rare occasions it doesn't matter ):
async fn sleep_real(secs: u64) {
let start = std::time::SystemTime::now();
let mut n = 0;
while n < secs {
tokio::time::sleep(core::time::Duration::from_secs(10)).await;
match start.elapsed() {
Ok(e) => {
if e >= core::time::Duration::from_secs(secs) {
return;
}
}
Err(_) => {
return;
}
}
n += 10;
}
}