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);
}
Timestamps (Unix Epoch) in Rust get the current time as a single number representing seconds or milliseconds since January 1, 1970. They work like a digital stopwatch that started at a fixed point in history, allowing you to measure exactly how much time has passed. You use this to log events, set timeouts, or sync data across different systems.