These Rust examples use Unix seconds for the portable baseline and show milliseconds only where the runtime exposes them naturally. The question to answer before copying a snippet is simple: what unit does the next API expect?

Get the current Unix time

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

let seconds = SystemTime::now()
    .duration_since(UNIX_EPOCH)?
    .as_secs();

Convert a timestamp to UTC

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

let instant = UNIX_EPOCH + Duration::from_secs(1_700_000_000);

Production note

SystemTime can report an error for dates before the Unix epoch. Handle that result instead of assuming every timestamp is positive.

Keep the unit in the field name when a value crosses a boundary: createdAtSeconds and createdAtMs are longer than createdAt, but much less mysterious during an incident.

Frequent questions:

Q: Can SystemTime handle dates before 1970?
A: SystemTime can represent times around the Unix epoch, but duration_since(UNIX_EPOCH) returns an error for earlier instants. Handle that case if external timestamps may be negative.
Q: Do I need chrono for every timestamp?
A: No. The standard library is enough for current Unix seconds and milliseconds. Use chrono or time when you need parsing, formatting, or calendar logic.
Q: How do I verify Rust timestamp units?
A: Convert one sample value to RFC3339 with chrono or time, then confirm it matches the API or database contract before accepting the field as seconds or milliseconds.