#cycle #sleep #wait

eb_cycles

Put your thread to sleep during set intervals, or.. 'cycles'..

3 releases

new 0.1.2 Nov 19, 2024
0.1.1 Nov 19, 2024
0.1.0 Nov 18, 2024

#3 in #cycles

Download history 55/week @ 2024-11-12

63 downloads per month

AGPL-3.0+

16KB
108 lines

eb_cycles

Rust library providing an easy way to put your threads to sleep during set intervals, or.. "cycles"..


lib.rs:

eb_cycles provides an easy way to put your thread to sleep during set intervals, or.. "cycles".

Examples

use eb_cycles::{chrono_tz, 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.
let tz = let chrono_tz::Europe::Oslo;
// You can for example just use Utc.
let utc_tz = let chrono_tz::UTC;
// Or you can parse the timezone if you want.
let south_pole_tz: chrono_tz::Tz = "Antarctica/South_Pole".parse().unwrap();

// Instantiate the sleep cycle struct with selected timezone.
let slp_cle = SleepCycle::new(tz);

// 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)
slp_cle.hour();

// Sleep until next quarter.
// * if time is 11:35 it will sleep until time is 11:45)
slp_cle.quarter();

// Sleep until next minute.
// * if time is 09:37:19 it will sleep until time is 09:38:00)
slp_cle.minute();

// This is how you would do stuff every full hour.
loop {
    slp_cle.hour(); // sleep until time is XX:00
    println!("time to do some stuff!");
    do_some_stuff_at_every_hour(); // Do recurring task.
    // use slp_cle.quarter() or slp_cle.minute() for every quarter or minute respectively.
}

// By the way, you can easily get the current timestamp for your chosen timezone.
println!("Time is: {}", slp_cle.datetime_now());

Dependencies

~1.5–2.4MB
~35K SLoC