Skip to main content

Timestamp

Struct Timestamp 

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

A timestamp representing a point in time within a media stream.

Timestamps are represented as a presentation timestamp (PTS) value combined with a time base that defines the unit of measurement.

§Time Base

The time base is a rational number that represents the duration of one timestamp unit. For example:

  • 1/90000: Each PTS unit is 1/90000 of a second (MPEG-TS)
  • 1/1000: Each PTS unit is 1 millisecond
  • 1/48000: Each PTS unit is one audio sample at 48kHz

§Examples

use ff_format::{Rational, Timestamp};
use std::time::Duration;

// Create a timestamp at 1 second using 90kHz time base
let time_base = Rational::new(1, 90000);
let ts = Timestamp::new(90000, time_base);

assert!((ts.as_secs_f64() - 1.0).abs() < 0.0001);
assert_eq!(ts.as_millis(), 1000);

// Convert from Duration
let ts2 = Timestamp::from_duration(Duration::from_secs(1), time_base);
assert_eq!(ts2.pts(), 90000);

Implementations§

Source§

impl Timestamp

Source

pub const fn new(pts: i64, time_base: Rational) -> Timestamp

Creates a new timestamp with the given PTS value and time base.

§Arguments
  • pts - The presentation timestamp value
  • time_base - The time base (duration of one PTS unit)
§Examples
use ff_format::{Rational, Timestamp};

let time_base = Rational::new(1, 1000);  // milliseconds
let ts = Timestamp::new(500, time_base);  // 500ms
assert_eq!(ts.as_millis(), 500);
Source

pub const fn zero(time_base: Rational) -> Timestamp

Creates a timestamp representing zero (0 PTS).

§Examples
use ff_format::{Rational, Timestamp};

let time_base = Rational::new(1, 90000);
let zero = Timestamp::zero(time_base);
assert_eq!(zero.pts(), 0);
assert_eq!(zero.as_secs_f64(), 0.0);
Source

pub fn from_duration(duration: Duration, time_base: Rational) -> Timestamp

Creates a timestamp from a Duration value.

§Arguments
  • duration - The duration to convert
  • time_base - The target time base for the resulting timestamp
§Examples
use ff_format::{Rational, Timestamp};
use std::time::Duration;

let time_base = Rational::new(1, 90000);
let ts = Timestamp::from_duration(Duration::from_millis(1000), time_base);
assert_eq!(ts.pts(), 90000);
Source

pub fn from_secs_f64(secs: f64, time_base: Rational) -> Timestamp

Creates a timestamp from a seconds value.

§Examples
use ff_format::{Rational, Timestamp};

let time_base = Rational::new(1, 1000);
let ts = Timestamp::from_secs_f64(1.5, time_base);
assert_eq!(ts.pts(), 1500);
Source

pub fn from_millis(millis: i64, time_base: Rational) -> Timestamp

Creates a timestamp from milliseconds.

§Examples
use ff_format::{Rational, Timestamp};

let time_base = Rational::new(1, 90000);
let ts = Timestamp::from_millis(1000, time_base);
assert_eq!(ts.pts(), 90000);
Source

pub const fn pts(&self) -> i64

Returns the presentation timestamp value.

§Examples
use ff_format::{Rational, Timestamp};

let ts = Timestamp::new(12345, Rational::new(1, 90000));
assert_eq!(ts.pts(), 12345);
Source

pub const fn time_base(&self) -> Rational

Returns the time base.

§Examples
use ff_format::{Rational, Timestamp};

let time_base = Rational::new(1, 90000);
let ts = Timestamp::new(100, time_base);
assert_eq!(ts.time_base(), time_base);
Source

pub fn as_duration(&self) -> Duration

Converts the timestamp to a Duration.

Note: Negative timestamps will be clamped to zero Duration.

§Examples
use ff_format::{Rational, Timestamp};
use std::time::Duration;

let ts = Timestamp::new(90000, Rational::new(1, 90000));
let duration = ts.as_duration();
assert_eq!(duration, Duration::from_secs(1));
Source

pub fn as_secs_f64(&self) -> f64

Converts the timestamp to seconds as a floating-point value.

§Examples
use ff_format::{Rational, Timestamp};

let ts = Timestamp::new(45000, Rational::new(1, 90000));
assert!((ts.as_secs_f64() - 0.5).abs() < 0.0001);
Source

pub fn as_millis(&self) -> i64

Converts the timestamp to milliseconds.

§Examples
use ff_format::{Rational, Timestamp};

let ts = Timestamp::new(90000, Rational::new(1, 90000));
assert_eq!(ts.as_millis(), 1000);
Source

pub fn as_micros(&self) -> i64

Converts the timestamp to microseconds.

§Examples
use ff_format::{Rational, Timestamp};

let ts = Timestamp::new(90, Rational::new(1, 90000));
assert_eq!(ts.as_micros(), 1000);  // 90/90000 = 0.001 sec = 1000 microseconds
Source

pub fn as_frame_number(&self, fps: f64) -> u64

Converts the timestamp to a frame number at the given frame rate.

§Arguments
  • fps - The frame rate (frames per second)
§Examples
use ff_format::{Rational, Timestamp};

let ts = Timestamp::new(90000, Rational::new(1, 90000));  // 1 second
assert_eq!(ts.as_frame_number(30.0), 30);  // 30 fps
assert_eq!(ts.as_frame_number(60.0), 60);  // 60 fps
Source

pub fn as_frame_number_rational(&self, fps: Rational) -> u64

Converts the timestamp to a frame number using a rational frame rate.

§Arguments
  • fps - The frame rate as a rational number
§Examples
use ff_format::{Rational, Timestamp};

let ts = Timestamp::new(90000, Rational::new(1, 90000));  // 1 second
let fps = Rational::new(30000, 1001);  // 29.97 fps
let frame = ts.as_frame_number_rational(fps);
assert!(frame == 29 || frame == 30);  // Should be approximately 30
Source

pub fn rescale(&self, new_time_base: Rational) -> Timestamp

Rescales this timestamp to a different time base.

§Arguments
  • new_time_base - The target time base
§Examples
use ff_format::{Rational, Timestamp};

let ts = Timestamp::new(1000, Rational::new(1, 1000));  // 1 second
let rescaled = ts.rescale(Rational::new(1, 90000));
assert_eq!(rescaled.pts(), 90000);
Source

pub const fn is_zero(&self) -> bool

Returns true if this timestamp is zero.

§Examples
use ff_format::{Rational, Timestamp};

let zero = Timestamp::zero(Rational::new(1, 90000));
assert!(zero.is_zero());

let non_zero = Timestamp::new(100, Rational::new(1, 90000));
assert!(!non_zero.is_zero());
Source

pub const fn is_negative(&self) -> bool

Returns true if this timestamp is negative.

§Examples
use ff_format::{Rational, Timestamp};

let negative = Timestamp::new(-100, Rational::new(1, 90000));
assert!(negative.is_negative());

Trait Implementations§

Source§

impl Add<Duration> for Timestamp

Source§

type Output = Timestamp

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Duration) -> <Timestamp as Add<Duration>>::Output

Performs the + operation. Read more
Source§

impl Add for Timestamp

Source§

type Output = Timestamp

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Timestamp) -> <Timestamp as Add>::Output

Performs the + operation. Read more
Source§

impl Clone for Timestamp

Source§

fn clone(&self) -> Timestamp

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Timestamp

Source§

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

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

impl Default for Timestamp

Source§

fn default() -> Timestamp

Returns a default timestamp (0 with 1/90000 time base).

Source§

impl Display for Timestamp

Source§

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

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

impl Ord for Timestamp

Source§

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

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

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

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

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

Compares and returns the minimum of two values. Read more
1.50.0 · 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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for Timestamp

Source§

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

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · 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 · 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 · 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 · 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 Sub<Duration> for Timestamp

Source§

type Output = Timestamp

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Duration) -> <Timestamp as Sub<Duration>>::Output

Performs the - operation. Read more
Source§

impl Sub for Timestamp

Source§

type Output = Timestamp

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Timestamp) -> <Timestamp as Sub>::Output

Performs the - operation. Read more
Source§

impl Copy for Timestamp

Source§

impl Eq for Timestamp

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, 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> 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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.