Files
turso/simulator/runner/clock.rs
pedrocarlo 46a7d20c12 clippy
2025-07-17 12:24:43 -03:00

35 lines
781 B
Rust

use std::cell::RefCell;
use chrono::{DateTime, Utc};
use rand::Rng;
use rand_chacha::ChaCha8Rng;
#[derive(Debug)]
pub struct SimulatorClock {
curr_time: RefCell<DateTime<Utc>>,
rng: RefCell<ChaCha8Rng>,
}
impl SimulatorClock {
const MIN_TICK: u64 = 1;
const MAX_TICK: u64 = 30;
pub fn new(rng: ChaCha8Rng) -> Self {
Self {
curr_time: RefCell::new(Utc::now()),
rng: RefCell::new(rng),
}
}
pub fn now(&self) -> DateTime<Utc> {
let mut time = self.curr_time.borrow_mut();
let nanos = self
.rng
.borrow_mut()
.gen_range(Self::MIN_TICK..Self::MAX_TICK);
let nanos = std::time::Duration::from_micros(nanos);
*time += nanos;
*time
}
}