Skip to content

Commit 88e2b1d

Browse files
committed
Added trait for clocks.
1 parent f6d9453 commit 88e2b1d

2 files changed

Lines changed: 75 additions & 0 deletions

File tree

statime-base/src/clock.rs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
use crate::{Duration, TAI, Timestamp};
2+
3+
/// Interface for a controllable clock
4+
/// This needs to be a trait as a single system can have multiple clocks
5+
/// which need different implementation for steering and/or now.
6+
pub trait Clock: Clone + Send + 'static {
7+
/// Error that can occur in operations on the clock.
8+
type Error: core::error::Error + Send + Sync;
9+
10+
/// Get current time
11+
///
12+
/// # Errors
13+
/// Should return an error if the clock is unable to provide a timestamp.
14+
fn now(&self) -> Result<Timestamp<TAI>, Self::Error>;
15+
16+
/// Change the frequency of the clock, returning the time
17+
/// at which the change was applied.
18+
///
19+
/// # Errors
20+
/// Should return an error if the clock is unable to be steered by the requested amount.
21+
fn set_frequency(&self, freq: f64) -> Result<Timestamp<TAI>, Self::Error>;
22+
23+
/// Get the frequency of the clock
24+
///
25+
/// # Errors
26+
/// Should return an error if the clock is unable to provide its current steering frequency.
27+
fn get_frequency(&self) -> Result<f64, Self::Error>;
28+
29+
/// Maximum frequency offset the clock is capable of.
30+
///
31+
/// # Errors
32+
/// Should return an error if the maximum frequency offset could not be determined.
33+
fn max_frequency(&self) -> Result<f64, Self::Error>;
34+
35+
/// Change the current time of the clock by offset. Returns
36+
/// the time at which the change was applied.
37+
///
38+
/// # Errors
39+
/// Should return an error if the clock cannot be stepped by the amount requested.
40+
fn step_clock(&self, offset: Duration) -> Result<Timestamp<TAI>, Self::Error>;
41+
42+
/// Provide the system with our current best estimates for
43+
/// the statistical error of the clock (`est_error`), and
44+
/// the maximum deviation due to frequency error and
45+
/// distance to the root clock.
46+
///
47+
/// # Errors
48+
/// Should return an error if the error estimate update cannot be applied to the clock.
49+
fn error_estimate_update(
50+
&self,
51+
est_error: Duration,
52+
max_error: Duration,
53+
) -> Result<(), Self::Error>;
54+
55+
/// Change the indicators for upcoming leap seconds and
56+
/// the clocks synchronization status. Application should happen at the end of the UTC month.
57+
///
58+
/// # Errors
59+
/// Should return an error if the status update cannot be applied to the clock.
60+
fn status_update(&self, leap_status: LeapStatus) -> Result<(), Self::Error>;
61+
}
62+
63+
/// Information on what the next leap second is going to be.
64+
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)]
65+
pub enum LeapStatus {
66+
/// There is no leap second at the end of the month.
67+
#[default]
68+
None,
69+
/// A second needs to be removed from the last minute of the month.
70+
Leap59,
71+
/// A second needs to be inserted into the last minute of the month.
72+
Leap61,
73+
}

statime-base/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
//! Base types and traits for time management.
22
#![no_std]
33

4+
mod clock;
45
mod identifiers;
56
mod time_types;
67

8+
pub use clock::*;
79
pub use identifiers::*;
810
pub use time_types::*;
911

0 commit comments

Comments
 (0)