std-method

SystemTime::duration_since: handle reversed timestamps

Unlike Instant arithmetic, this comparison is fallible because wall-clock readings can be reversed. Handle both branches, for example when an event timestamp is unexpectedly before a baseline. Expected stdout (not executed): 5 true Common mistake: Unwrapping arbitrary system-clock differences as though they were always nonnegative.

Formal Syntax & Structure

rust syntaxrust
later.duration_since(earlier: SystemTime) -> Result<Duration, SystemTimeError>

Working Example

Example Snippetrust
fn main() {    use std::time::{Duration, UNIX_EPOCH};    let earlier = UNIX_EPOCH + Duration::from_secs(10);    let later = UNIX_EPOCH + Duration::from_secs(15);    println!("{}", later.duration_since(earlier).unwrap().as_secs());    println!("{}", earlier.duration_since(later).is_err());}
Rust Reference