Skip to main content

luaur_common/functions/
get_clock_microseconds.rs

1use crate::functions::get_clock_period::get_clock_period;
2use crate::functions::get_clock_timestamp::get_clock_timestamp;
3use std::sync::OnceLock;
4
5pub fn get_clock_microseconds() -> u32 {
6    struct ClockState {
7        period: f64,
8        start: f64,
9    }
10
11    static STATE: OnceLock<ClockState> = OnceLock::new();
12
13    let state = STATE.get_or_init(|| ClockState {
14        period: get_clock_period() * 1e6,
15        start: get_clock_timestamp(),
16    });
17
18    ((get_clock_timestamp() - state.start) * state.period) as u32
19}