How to Get the Current Unix Time in Rust
A Unix timestamp is the number of seconds since 1970-01-01 00:00:00 UTC. Rust's standard library gets it with three calls, SystemTime::now(), duration_since(UNIX_EPOCH) and as_secs(), without any crate.
Syntax
use std::time::{SystemTime, UNIX_EPOCH};
let <seconds>: u64 = SystemTime::now()
.duration_since(UNIX_EPOCH)
.expect("<message>")
.as_secs();
| Part | What it does |
|---|---|
SystemTime::now() | The current time from the system clock. |
.duration_since(UNIX_EPOCH) | The time between the Unix epoch and now, as a Result holding a Duration. |
.expect("<message>") | Takes the Duration out, and stops the program with <message> in the rare case that the clock reads earlier than 1970. |
.as_secs() | The whole seconds in the Duration, as a u64. |
From the standard library documentation: UNIX_EPOCH "is defined to be “1970-01-01 00:00:00 UTC” on all systems with respect to the system clock". duration_since "Returns the amount of time elapsed from an earlier point in time." as_secs "Returns the number of whole seconds contained by this Duration."
The Example
The example prints the current Unix time in seconds.
Prerequisites
- Rust, installed with rustup, which comes with Cargo. This guide uses Rust 1.100.0.
Step 1: Create a Project
cargo new demo
cd demo
Creating binary (application) `demo` package
note: see more `Cargo.toml` keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
cargo new demo creates a folder named demo with a Cargo.toml file, which describes the project, and src/main.rs, which holds the code. cd demo moves into it.
Step 2: Write the Code
Replace the contents of src/main.rs with:

use std::time::{SystemTime, UNIX_EPOCH};
fn main() {
let seconds = SystemTime::now()
.duration_since(UNIX_EPOCH)
.expect("the clock is set before 1970")
.as_secs();
println!("{seconds}");
}
Each call works on the result of the one before it, so the chain reads top to bottom: now, minus the epoch, checked, in seconds.
Step 3: Run It
cargo run -q
1795680697

cargo run compiles the program and runs it, and -q hides Cargo's own status lines. Your number will be larger: it grows by one every second. This one is 26 November 2026, 08:11:37 UTC.
Milliseconds
For milliseconds, call as_millis() instead of as_secs(). It returns a u128, not a u64. Printing both from the same Duration gave:
1795680759 1795680759654
The second number has three more digits: the same moment, in thousandths of a second.
When duration_since Fails
duration_since returns an Err when the time you pass is later than the time you call it on. Ten seconds before the epoch, measured from the epoch, gives:
use std::time::{Duration, UNIX_EPOCH};
fn main() {
let before = UNIX_EPOCH - Duration::from_secs(10);
println!("{:?}", before.duration_since(UNIX_EPOCH));
}
Err(SystemTimeError(10s))
With the current time, that only happens if the system clock is set before 1970, which is why expect is enough for it. The documentation explains why the method can fail at all: "measurements taken earlier are not guaranteed to always be before later measurements (due to anomalies such as the system clock being adjusted either forwards or backwards)."
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Forgetting the use line | error[E0433]: cannot find type `SystemTime` in this scope and error[E0425]: cannot find value `UNIX_EPOCH` in this scope | Add use std::time::{SystemTime, UNIX_EPOCH}; |
| Sending milliseconds where seconds are expected | A 13-digit number, a time far in the future | Use as_secs() for Unix seconds |
Measuring how long code takes with SystemTime | The clock can be adjusted while you measure | Use std::time::Instant for durations |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| The number has 13 digits | It's in milliseconds | Use as_secs() |
expected `u64`, found `u128` when storing as_millis() | It returns u128 | Store it in a u128 |
FAQ
Is this time in UTC?
A Unix timestamp has no time zone. It counts seconds from a moment defined in UTC, so the number is the same everywhere at the same instant.
What type is the result?
as_secs() returns u64, which holds seconds far beyond the year 2038 limit of 32-bit timestamps.
Conclusion
SystemTime::now().duration_since(UNIX_EPOCH) gives the time since the epoch, expect handles the clock-before-1970 case, and as_secs() turns it into Unix seconds. Use as_millis() for milliseconds.
Additional Resources
- SystemTime, in the standard library documentation.
- UNIX_EPOCH and Duration, in the standard library documentation.
