Skip to main content

gpui_query/client/
time.rs

1//! Neutral time helper shared across the client layer.
2//!
3//! Previously co-located with the type-erased bucket traits in `erased.rs`, this
4//! helper moved to its own module so the `persist` feature gate (which now owns
5//! `erased.rs`'s persistence symbols) does not pull `current_time_ms` behind a
6//! `cfg`: the GC subsystem and several non-persistence call sites depend on it.
7
8/// Returns the current time as milliseconds since the UNIX epoch.
9///
10/// Used internally by `gc()` and other time-sensitive operations.
11/// Exposed so callers can cache the value and pass it to `gc_with_time()`
12/// to avoid repeated syscalls.
13///
14/// # Clock-before-epoch fallback
15///
16/// `duration_since(UNIX_EPOCH)` errors if the system clock reports a time
17/// *before* the Unix epoch (1970-01-01 UTC) — e.g. a misconfigured RTC or a
18/// clock skewed backwards on cold boot. The `.unwrap_or_default()` silently
19/// clamps that case to a `Duration::ZERO`, i.e. this function returns `0`.
20/// That `0` is treated as "ancient" by GC, so the only observable effect is
21/// that entries become immediately eligible for garbage collection for the
22/// duration of the clock anomaly; no panic, no error propagation. This is a
23/// deliberate silent clamp rather than a propagating error because every
24/// caller treats `now_ms` as infallible and time-sensitive operations
25/// degrading to "collect now" is the safest default under a broken clock.
26pub fn current_time_ms() -> u64 {
27    std::time::SystemTime::now()
28        .duration_since(std::time::UNIX_EPOCH)
29        .unwrap_or_default()
30        .as_millis() as u64
31}