pub struct Timestamp(/* private fields */);Expand description
A point on the monotonic timeline, in nanoseconds since an unspecified
epoch (CLOCK_MONOTONIC, i.e. system boot on Linux).
Only intervals are meaningful; the absolute value carries no wall-clock meaning and must never be formatted as a date. The counter is monotonic and does not jump when the wall clock is stepped by NTP or by the user.
A u64 nanosecond counter covers roughly 584 years of uptime, so overflow
is not a practical concern — but arithmetic here saturates rather than
wrapping, because a clock that silently runs backwards would turn a pacing
bug into a torn frame.
Implementations§
Source§impl Timestamp
impl Timestamp
Sourcepub const ZERO: Self
pub const ZERO: Self
The zero point of the monotonic timeline. Useful as a “never” sentinel
and as the starting value for a ManualClock.
Sourcepub const fn from_nanos(nanos: u64) -> Self
pub const fn from_nanos(nanos: u64) -> Self
Build a timestamp from a raw CLOCK_MONOTONIC nanosecond count — for
example one the kernel reported with a page-flip event.
Sourcepub const fn from_timespec(secs: i64, nanos: i64) -> Self
pub const fn from_timespec(secs: i64, nanos: i64) -> Self
Build a timestamp from a CLOCK_MONOTONIC timespec pair, the shape
DRM event structs and libinput report.
Negative components clamp to zero: CLOCK_MONOTONIC never yields them,
and clamping keeps a malformed event from producing a timestamp that
sorts before Timestamp::ZERO.
Sourcepub const fn from_timeval(secs: i64, micros: i64) -> Self
pub const fn from_timeval(secs: i64, micros: i64) -> Self
Build a timestamp from a CLOCK_MONOTONIC timeval pair (microsecond
resolution), the shape drmEventVBlank reports.
Sourcepub const fn saturating_duration_since(self, earlier: Self) -> Duration
pub const fn saturating_duration_since(self, earlier: Self) -> Duration
Interval from earlier to self, saturating at zero if earlier is
in fact later.
Saturating is the right failure mode for a display pipeline: a negative interval would otherwise wrap to ~584 years and stall pacing until reboot.
use core::time::Duration;
use drmkit_time::Timestamp;
let t0 = Timestamp::from_nanos(1_000);
let t1 = Timestamp::from_nanos(17_667_000);
assert_eq!(t1.saturating_duration_since(t0), Duration::from_nanos(17_666_000));
assert_eq!(t0.saturating_duration_since(t1), Duration::ZERO);Sourcepub const fn checked_duration_since(self, earlier: Self) -> Option<Duration>
pub const fn checked_duration_since(self, earlier: Self) -> Option<Duration>
Interval from earlier to self, or None if earlier is later.
Use this where going backwards indicates a bug worth surfacing rather than absorbing.
Trait Implementations§
Source§impl AddAssign<Duration> for Timestamp
impl AddAssign<Duration> for Timestamp
Source§fn add_assign(&mut self, rhs: Duration)
fn add_assign(&mut self, rhs: Duration)
+= operation. Read more