Files
oak-gpui/crates/gpui_scheduler/src/clock.rs
T
Mike-Solar af5482d774 style: cargo fmt with the workspace tab policy (hard_tabs)
Whitespace only; the fork has no own rustfmt.toml so the Oak root
policy applies.
2026-08-11 23:06:13 +08:00

56 lines
1.0 KiB
Rust

use chrono::{DateTime, Utc};
use parking_lot::Mutex;
use std::time::Duration;
pub use web_time::Instant;
pub trait Clock {
fn utc_now(&self) -> DateTime<Utc>;
fn now(&self) -> Instant;
}
pub struct TestClock(Mutex<TestClockState>);
struct TestClockState {
now: Instant,
utc_now: DateTime<Utc>,
}
impl TestClock {
pub fn new() -> Self {
const START_TIME: &str = "2025-07-01T23:59:58-00:00";
let utc_now = DateTime::parse_from_rfc3339(START_TIME).unwrap().to_utc();
Self(Mutex::new(TestClockState {
now: Instant::now(),
utc_now,
}))
}
pub fn set_utc_now(&self, now: DateTime<Utc>) {
let mut state = self.0.lock();
state.utc_now = now;
}
pub fn advance(&self, duration: Duration) {
let mut state = self.0.lock();
state.now += duration;
state.utc_now += duration;
}
}
impl Default for TestClock {
fn default() -> Self {
Self::new()
}
}
impl Clock for TestClock {
fn utc_now(&self) -> DateTime<Utc> {
self.0.lock().utc_now
}
fn now(&self) -> Instant {
self.0.lock().now
}
}