expression
Signed Unix milliseconds: pre-epoch timestamps
Convert a wall-clock reading to signed integer milliseconds using the error magnitude for pre-epoch values. This example truncates toward zero, including for negative values; storage protocols may instead require flooring. i128 can represent the signed millisecond magnitude of any Duration. Expected stdout (not executed): -1500 2500 Common mistake: Casting an unsigned timestamp to a signed integer without considering pre-epoch dates or rounding policy.
Formal Syntax & Structure
rust syntaxrust
match t.duration_since(UNIX_EPOCH) { Ok(d) => ..., Err(e) => ... }Working Example
Example Snippetrust
fn main() { use std::time::{Duration, SystemTime, UNIX_EPOCH}; fn millis(t: SystemTime) -> i128 { match t.duration_since(UNIX_EPOCH) { Ok(d) => d.as_millis() as i128, Err(e) => -(e.duration().as_millis() as i128), } } let before = UNIX_EPOCH.checked_sub(Duration::from_millis(1500)).unwrap(); let after = UNIX_EPOCH.checked_add(Duration::from_millis(2500)).unwrap(); println!("{} {}", millis(before), millis(after));}