#cycle #sleep #wait

eb_cycles

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

5 releases

new 0.2.1 Dec 20, 2024
0.2.0 Nov 22, 2024
0.1.2 Nov 19, 2024
0.1.1 Nov 19, 2024
0.1.0 Nov 18, 2024

#3 in #cycles

Download history 331/week @ 2024-11-16 74/week @ 2024-11-23 4/week @ 2024-11-30 2/week @ 2024-12-07

411 downloads per month

AGPL-3.0+

16KB
111 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.
// 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)
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