use core::fmt;
use rusty_time_core::ClockCommand;
#[derive(Clone, Debug)]
pub struct ClockError {
pub op: &'static str,
pub detail: String,
}
impl fmt::Display for ClockError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "clock {}: {}", self.op, self.detail)
}
}
impl std::error::Error for ClockError {}
pub trait ClockRead {
fn wall_ns(&self) -> Result<i128, ClockError>;
fn mono_s(&self) -> Result<f64, ClockError>;
fn wall_parts(&self) -> Result<(i64, u32), ClockError> {
let ns = self.wall_ns()?;
let secs = ns.div_euclid(1_000_000_000) as i64;
let nanos = ns.rem_euclid(1_000_000_000) as u32;
Ok((secs, nanos))
}
}
pub trait ClockDrive {
fn apply(&mut self, cmd: &ClockCommand) -> Result<(), ClockError>;
}
#[derive(Clone, Debug, PartialEq)]
pub struct ClockCapabilities {
pub os: &'static str,
pub arch: &'static str,
pub can_read: bool,
pub can_discipline: bool,
pub discipline_requirement: &'static str,
pub slew_resolution_ppm: Option<f64>,
pub max_slew_ppm: f64,
pub batch_receive: bool,
pub mono_resolution_ns: Option<f64>,
}
impl ClockCapabilities {
pub fn measure_mono_resolution<C: ClockRead>(clock: &C) -> Option<f64> {
let mut smallest = f64::INFINITY;
let mut last = clock.mono_s().ok()?;
for _ in 0..20_000 {
let now = clock.mono_s().ok()?;
let delta = now - last;
if delta > 0.0 && delta < smallest {
smallest = delta;
}
last = now;
}
smallest.is_finite().then_some(smallest * 1e9)
}
}
#[cfg(target_os = "linux")]
mod linux;
#[cfg(target_os = "linux")]
pub use linux::SystemClock;
#[cfg(target_os = "macos")]
mod macos;
#[cfg(target_os = "macos")]
pub use macos::SystemClock;
#[cfg(windows)]
mod windows;
#[cfg(windows)]
pub use windows::SystemClock;
mod virtual_driver;
pub use virtual_driver::VirtualDriver;
pub mod slew;
#[cfg(any(unix, target_os = "linux"))]
pub mod refclock;
#[cfg(any(unix, windows))]
pub mod net;
pub mod stop;
pub fn capabilities() -> ClockCapabilities {
#[cfg(any(target_os = "linux", target_os = "macos", windows))]
{
let mut caps = platform_capabilities();
let clock = SystemClock;
caps.can_read = clock.wall_ns().is_ok() && clock.mono_s().is_ok();
caps.mono_resolution_ns = ClockCapabilities::measure_mono_resolution(&clock);
caps
}
#[cfg(not(any(target_os = "linux", target_os = "macos", windows)))]
{
ClockCapabilities {
os: std::env::consts::OS,
arch: std::env::consts::ARCH,
can_read: false,
can_discipline: false,
discipline_requirement: "no platform driver for this target",
slew_resolution_ppm: None,
max_slew_ppm: 0.0,
batch_receive: false,
mono_resolution_ns: None,
}
}
}
#[cfg(target_os = "linux")]
use linux::platform_capabilities;
#[cfg(target_os = "macos")]
use macos::platform_capabilities;
#[cfg(windows)]
use windows::platform_capabilities;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn capabilities_are_self_consistent() {
let caps = capabilities();
assert_eq!(caps.os, std::env::consts::OS);
assert!(caps.can_read, "clock must be readable without privilege");
assert!(
!caps.discipline_requirement.is_empty(),
"an operator who cannot discipline must be told what it needs"
);
if let Some(res) = caps.mono_resolution_ns {
assert!(
res > 0.0 && res < 1e9,
"implausible monotonic resolution {res} ns"
);
}
assert!(caps.max_slew_ppm > 0.0);
}
#[test]
fn batch_receive_matches_the_platform() {
assert_eq!(capabilities().batch_receive, cfg!(target_os = "linux"));
}
}