How to Use Timestamps (Unix Epoch) in Rust

Get the current Unix epoch timestamp in seconds or milliseconds using std::time::SystemTime and UNIX_EPOCH.

Use std::time::SystemTime to get the current Unix epoch timestamp in seconds or milliseconds since 1970-01-01 UTC.

use std::time::{SystemTime, UNIX_EPOCH};

fn main() {
    let duration = SystemTime::now().duration_since(UNIX_EPOCH).unwrap();
    let timestamp_secs = duration.as_secs();
    let timestamp_millis = duration.as_millis();
    println!("Seconds: {}, Millis: {}", timestamp_secs, timestamp_millis);
}