Unix Timestamp in Rust
The standard library gives you SystemTime; for formatting and time zones the ecosystem uses chrono or time.
Need to convert one value rather than write code? Use the interactive converter — it handles every unit and time zone without leaving your browser.
Current Unix timestamp (std only)
use std::time::{SystemTime, UNIX_EPOCH};
let d = SystemTime::now().duration_since(UNIX_EPOCH).expect("clock before 1970");
println!("{}", d.as_secs()); // 1710050400
println!("{}", d.as_millis()); // 1710050400000 Timestamp to date (chrono)
use chrono::{DateTime, Utc, TimeZone};
use chrono_tz::Asia::Kolkata;
let dt: DateTime<Utc> = Utc.timestamp_opt(1710050400, 0).unwrap();
println!("{}", dt.to_rfc3339());
println!("{}", dt.with_timezone(&Kolkata).format("%Y-%m-%d %H:%M:%S %Z")); Date to timestamp
use chrono::{TimeZone, Utc, NaiveDate};
let dt = Utc.with_ymd_and_hms(2024, 3, 10, 6, 0, 0).unwrap();
println!("{}", dt.timestamp()); // 1710050400
let parsed = DateTime::parse_from_rfc3339("2024-03-10T06:00:00Z").unwrap();
println!("{}", parsed.timestamp()); Fallible by design
// timestamp_opt returns LocalResult, because a local time can be
// ambiguous or nonexistent across a DST transition.
match Kolkata.with_ymd_and_hms(2024, 3, 10, 11, 30, 0) {
chrono::LocalResult::Single(t) => println!("{}", t.timestamp()),
chrono::LocalResult::Ambiguous(a, b) => println!("ambiguous: {a} or {b}"),
chrono::LocalResult::None => println!("that local time does not exist"),
} Pitfalls specific to Rust
duration_since(UNIX_EPOCH)returns aResultbecause the system clock can legitimately be set before 1970.- The deprecated
Utc.timestamp()panicked on out-of-range input;timestamp_opt()returns an option instead. Prefer the latter.
Rules that apply in every language
- Store UTC, display local. Keep the instant in UTC everywhere in your system and convert only at the point a human reads it.
- Name the unit in the identifier.
expires_at_msrather thanexpires_atcosts nothing and prevents the single most common timestamp bug. - Never trust a client clock. Stamp anything security-relevant on the server. See the note on clock skew.
- Use 64-bit time. Anything still storing seconds in a signed 32-bit field breaks in January 2038 — see the Year 2038 problem.
The same task in other languages
- Python float seconds
- JavaScript integer milliseconds
- PHP integer seconds
- Java integer milliseconds
- Go integer seconds and nanoseconds
- TypeScript integer milliseconds
- Ruby float seconds
- C# ticks (100 ns)
- SQL varies by engine
- Bash integer seconds
- C++ chrono duration
- Swift float seconds
- Kotlin integer milliseconds
- C time_t seconds
- Perl integer seconds
- Dart integer milliseconds
- PowerShell .NET DateTimeOffset
- Scala integer milliseconds
- R float seconds
- Excel days since 1899-12-30
Frequently asked questions
How do I get the current Unix timestamp in Rust?
Use the snippet in the "Current Unix timestamp" section above. Rust works in seconds + nanoseconds natively, so converting to another unit is a multiplication or an integer division away.
How do I convert a Unix timestamp to a date in Rust?
The "Timestamp to date" snippet above shows the idiomatic approach, including how to render the result in a specific time zone rather than whatever zone the machine happens to be set to.
Does Rust handle time zones and daylight saving correctly?
Yes, provided you pass an explicit zone rather than relying on the system default. The gotchas listed on this page cover the specific ways Rust makes that easy to get wrong.