Expand description
eb_cycles
provides an easy way to put your thread to sleep during set intervals, or.. “cycles”.
§Examples
use eb_cycles::{SleepTarget, SleepCycle};
// First we need to choose a chrono_tz timezone to work with (re-exported from this library)
// See https://docs.rs/chrono-tz/latest/chrono_tz/ for docs about timezones.
// Instantiate the sleep cycle struct with selected timezone.
let slp_cle = SleepCycle::from_tz(chrono_tz::Europe::Oslo);
// Or instantiate by sending a string literal with the timzone identifier.
let slp_cle = SleepCycle::from_tz_str("Europe/London");
// Sleep until next full hour.
// ..if time is 15:55:10 it will sleep for 4 minutes and 50 seconds e.g. until time is 16:00)
let secs = slp_cle.sleep(SleepTarget::EveryHour);
println!("I just slept for {secs} seconds");
// Sleep until next quarter.
// ..if time is 11:35 it will sleep until time is 11:45)
let secs = slp_cle.sleep(SleepTarget::EveryQuarter);
println!("I just slept for {secs} seconds");
// Sleep until next minute.
// ..if time is 09:37:19 it will sleep until time is 09:38:00)
let secs = slp_cle.sleep(SleepTarget::EveryMinute);
println!("I just slept for {secs} seconds");
// This is ann example of how you could do stuff every full hour.
loop {
slp_cle.sleep(SleepTarget::EveryHour); // sleep until time is XX:00
println!("time to do some stuff!");
// do()...
// stuff()...
}
// By the way, you can easily get the current timestamp for your chosen timezone.
println!("Time is: {}", slp_cle.datetime_now());
Re-exports§
pub use chrono_tz;