Skip to main content

Timestamp

Struct Timestamp 

Source
pub struct Timestamp { /* private fields */ }
Expand description

A timestamp in a track’s timescale (units per second).

All timestamps within a track are relative, so zero for one track is not zero for another. The underlying value is constrained to fit within a QUIC VarInt (2^62 - 1) so it can be encoded and decoded easily; the scale is carried alongside so frames from different sources can be compared and converted without lossy detours through a single fixed scale.

The scale is a Timescale (always non-zero), so unit conversions (as_secs, as_millis, etc.) are infallible. Use Option<Timestamp> at call sites that need a “missing” sentinel instead of relying on a magic value.

§An instant, not a number

A Timestamp is a point in time (like std::time::Instant), not a scalar, so it has no arithmetic operators: adding two instants is meaningless, and a scale mismatch can’t be a silent panic. Use Self::checked_add / Self::checked_sub, which require both operands to share a scale and return TimeOverflow otherwise. To combine timestamps from different scales, Self::convert one to the other’s scale first.

§Equality vs ordering

These two intentionally disagree, so pick the one you mean:

  • Eq / Hash are structural (field-wise): from_secs(1) != from_millis(1000), because they encode as different (value, scale) pairs on the wire. Two timestamps are equal only when both their value and scale match.
  • Ord is temporal: it cross-multiplies scales, so from_millis(1000) orders after from_millis(999) and from_secs(1) slots in between. When a cross-scale comparison is otherwise a tie, it breaks by (scale, value) to stay consistent with Eq.

So from_secs(1).cmp(&from_millis(1000)) is not Equal, and neither is == true. If you want “same instant regardless of encoding”, compare after a Self::convert to a common scale.

Implementations§

Source§

impl Timestamp

Source

pub const ZERO: Self

The zero timestamp: value 0 at Timescale::SECOND.

The scale is not incidental. Equality and ordering are scale-aware (see the type docs), so this is not interchangeable with 0 at another scale; use Self::is_zero to test a zero value regardless of scale. In particular, don’t seed a .max() accumulator with this: a later value at a finer scale would lose the tie-break. Reach for Option<Timestamp> instead.

Source

pub const fn new(value: u64, scale: Timescale) -> Result<Self, TimeOverflow>

Construct a timestamp directly from a raw value at the given scale. Returns TimeOverflow if value exceeds 2^62 - 1.

Source

pub const fn new_const(value: u64, scale: Timescale) -> Self

Const-context twin of Self::new that panics on overflow.

For building const timestamps where ?/unwrap on the Result isn’t available. The panic fires only on a compile-time-known out-of-range literal, so it’s a build-time assertion, not a runtime failure path. Use Self::new everywhere else.

Source

pub fn from_scale( value: u64, units_per_second: u64, ) -> Result<Self, TimeOverflow>

Construct a timestamp from a raw value and a units_per_second scale. Returns TimeOverflow if the scale is zero or the value is out of range.

Source

pub const fn from_secs(seconds: u64) -> Result<Self, TimeOverflow>

Convert a number of seconds to a timestamp at Timescale::SECOND.

Source

pub const fn from_millis(millis: u64) -> Result<Self, TimeOverflow>

Convert a number of milliseconds to a timestamp at Timescale::MILLI.

Source

pub const fn from_micros(micros: u64) -> Result<Self, TimeOverflow>

Convert a number of microseconds to a timestamp at Timescale::MICRO.

Source

pub const fn from_nanos(nanos: u64) -> Result<Self, TimeOverflow>

Convert a number of nanoseconds to a timestamp at Timescale::NANO.

Source

pub const fn value(self) -> u64

The raw value in the timestamp’s own scale.

Source

pub const fn scale(self) -> Timescale

The scale (units per second) attached to this timestamp.

Source

pub const fn is_zero(self) -> bool

Whether the raw value is zero. Does not consider scale.

Source

pub const fn convert(self, new_scale: Timescale) -> Result<Self, TimeOverflow>

Re-express this timestamp at a new scale. Returns TimeOverflow if the new value would exceed 2^62 - 1.

Source

pub const fn as_scale(self, target: Timescale) -> u128

The value re-expressed at target as a u128.

Source

pub const fn as_secs(self) -> u64

The value re-expressed in seconds.

Source

pub const fn as_millis(self) -> u128

The value re-expressed in milliseconds.

Source

pub const fn as_micros(self) -> u128

The value re-expressed in microseconds.

Source

pub const fn as_nanos(self) -> u128

The value re-expressed in nanoseconds.

Source

pub const fn checked_add(self, rhs: Self) -> Result<Self, TimeOverflow>

Add two timestamps. Returns TimeOverflow if the sum exceeds 2^62 - 1 or if the scales differ.

Source

pub const fn checked_sub(self, rhs: Self) -> Result<Self, TimeOverflow>

Subtract rhs from self. Returns TimeOverflow if rhs > self or if the scales differ.

Source

pub fn now() -> Self

Current point on the local monotonic clock, expressed in the default timescale (Timescale::MILLI).

This is the one-way bridge from a local clock to a track timestamp: there is deliberately no inverse (a Timestamp is relative and jittered, never a clock). Used to stamp frames that arrive without one, e.g. on protocols whose wire can’t carry a timestamp. Uses web_async::time::Instant::now so it works on wasm and honors tokio::time::pause in tests.

Trait Implementations§

Source§

impl Clone for Timestamp

Source§

fn clone(&self) -> Timestamp

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for Timestamp

Source§

impl Debug for Timestamp

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for Timestamp

Source§

impl From<Instant> for Timestamp

Source§

fn from(instant: Instant) -> Self

Convert an std::time::Instant into a millisecond-scale timestamp (the default timescale), anchored at 2020-01-01 plus a per-process jitter (see TIME_ANCHOR).

One-way only: there is no inverse, since the anchor is jittered to keep a Timestamp from being read back as a clock.

Source§

impl From<Timestamp> for Duration

Source§

fn from(time: Timestamp) -> Self

Converts to this type from the input type.
Source§

impl Hash for Timestamp

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Ord for Timestamp

Source§

fn cmp(&self, other: &Self) -> Ordering

Temporal comparison, normalizing across scales (see the type-level docs for how this relates to structural Eq).

  • Equal scales compare raw values directly.
  • Otherwise cross-multiplies in 128-bit so e.g. 1s > 2ms orders correctly.
  • A would-be cross-scale tie (e.g. from_secs(1) vs from_millis(1000)) breaks by (scale, value), keeping Ord consistent with the field-wise Eq/Hash.
1.21.0 (const: unstable) · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for Timestamp

Source§

fn eq(&self, other: &Timestamp) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl PartialOrd for Timestamp

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 (const: unstable) · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 (const: unstable) · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 (const: unstable) · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl StructuralPartialEq for Timestamp

Source§

impl TryFrom<Duration> for Timestamp

Source§

fn try_from(duration: Duration) -> Result<Self, Self::Error>

Convert a std::time::Duration into a nanosecond-scale timestamp.

Source§

type Error = TimeOverflow

The type returned in the event of a conversion error.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> MaybeSend for T
where T: Send,

Source§

impl<T> MaybeSend for T
where T: Send,

Source§

impl<T> MaybeSync for T
where T: Sync,

Source§

impl<T> MaybeSync for T
where T: Sync,

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more