pub struct Timestamp { /* private fields */ }Expand description
A timestamp representing a point in time within media.
Timestamps have millisecond precision and support a range from
00:00:00,000 to 99:59:59,999.
§Display Format
When formatted with Display, timestamps use the standard SRT format:
HH:MM:SS,mmm (e.g., 01:23:45,678).
§Example
use skrt::Timestamp;
let ts = Timestamp::from_millis(5025000).unwrap(); // 1h 23m 45s
assert_eq!("01:23:45,000", ts.to_string());
assert_eq!(5025000, ts.to_millis());
// Shift the timestamp forward by 500ms
let shifted = ts.shift_millis(500).unwrap();
assert_eq!("01:23:45,500", shifted.to_string());Implementations§
Source§impl Timestamp
impl Timestamp
Sourcepub fn from_millis(millis: u64) -> Result<Self>
pub fn from_millis(millis: u64) -> Result<Self>
Creates a new timestamp from a total number of milliseconds.
§Errors
Returns SrtError::TimestampOutOfRange if millis exceeds
359,999,999 (equivalent to 99:59:59,999).
§Example
use skrt::Timestamp;
let ts = Timestamp::from_millis(3661001).unwrap();
assert_eq!("01:01:01,001", ts.to_string());Examples found in repository?
examples/timeshift.rs (line 7)
3fn main() {
4 let mut srt = Srt::new();
5
6 srt.add_subtitle(
7 Timestamp::from_millis(0).unwrap(),
8 Timestamp::from_millis(1000).unwrap(),
9 "First".into(),
10 );
11
12 srt.add_subtitle(
13 Timestamp::from_millis(1000).unwrap(),
14 Timestamp::from_millis(2000).unwrap(),
15 "Second".into(),
16 );
17
18 for sub in &mut srt {
19 sub.set_start(sub.start().shift_millis(300).unwrap());
20 sub.set_end(sub.end().shift_millis(300).unwrap());
21 }
22
23 println!("{}", srt.serialize());
24}Sourcepub fn to_millis(&self) -> u64
pub fn to_millis(&self) -> u64
Converts this timestamp to a total number of milliseconds.
§Example
use skrt::Timestamp;
let ts = Timestamp::from_millis(12345).unwrap();
assert_eq!(12345, ts.to_millis());Sourcepub fn shift_millis(&self, millis: i64) -> Result<Timestamp>
pub fn shift_millis(&self, millis: i64) -> Result<Timestamp>
Returns a new timestamp shifted by the given number of milliseconds.
Positive values shift the timestamp forward (later in time), and negative values shift it backward (earlier in time).
§Errors
- Returns
SrtError::NegativeTimestampif the shift would result in a negative timestamp. - Returns
SrtError::TimestampOutOfRangeif the shift would exceed the maximum timestamp value.
§Example
use skrt::Timestamp;
let ts = Timestamp::from_millis(5000).unwrap();
// Shift forward
let later = ts.shift_millis(1000).unwrap();
assert_eq!(6000, later.to_millis());
// Shift backward
let earlier = ts.shift_millis(-2000).unwrap();
assert_eq!(3000, earlier.to_millis());Examples found in repository?
examples/timeshift.rs (line 19)
3fn main() {
4 let mut srt = Srt::new();
5
6 srt.add_subtitle(
7 Timestamp::from_millis(0).unwrap(),
8 Timestamp::from_millis(1000).unwrap(),
9 "First".into(),
10 );
11
12 srt.add_subtitle(
13 Timestamp::from_millis(1000).unwrap(),
14 Timestamp::from_millis(2000).unwrap(),
15 "Second".into(),
16 );
17
18 for sub in &mut srt {
19 sub.set_start(sub.start().shift_millis(300).unwrap());
20 sub.set_end(sub.end().shift_millis(300).unwrap());
21 }
22
23 println!("{}", srt.serialize());
24}Trait Implementations§
Source§impl Ord for Timestamp
impl Ord for Timestamp
Source§impl PartialOrd for Timestamp
impl PartialOrd for Timestamp
impl Copy for Timestamp
impl Eq for Timestamp
impl StructuralPartialEq for Timestamp
Auto Trait Implementations§
impl Freeze for Timestamp
impl RefUnwindSafe for Timestamp
impl Send for Timestamp
impl Sync for Timestamp
impl Unpin for Timestamp
impl UnwindSafe for Timestamp
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more