For Developers

Unix Timestamp in Zig

How to get the current timestamp, convert it to a date, and convert a date back to a timestamp in Zig, using the standard library's std.time module.

const std = @import("std");
const ts_ns = std.time.nanoTimestamp();
const ts_sec = std.time.timestamp(); // seconds, i64
// Zig's standard library has no built-in calendar/date-formatting API yet —
// converting a timestamp to year/month/day fields means either writing the
// civil-from-days algorithm yourself or pulling in a third-party package.
const epoch_seconds = std.time.epoch.EpochSeconds{ .secs = @intCast(ts_sec) };
const day = epoch_seconds.getEpochDay();
const year_day = day.calculateYearDay();
const month_day = year_day.calculateMonthDay();
// Going the other direction (date -> timestamp) has the same gap: Zig gives
// you the epoch-day building blocks, but no single stdlib call that takes
// year/month/day and returns a Unix timestamp directly.

The Zig-specific pitfall

Zig's standard library deliberately keeps its time API minimal and still-evolving (Zig itself hadn't reached a 1.0 release as of this page's last update) — it gives you epoch seconds and nanoseconds, plus low-level day/month/year decomposition helpers, but no single high-level "format this timestamp as a date string" function the way Python or JavaScript do. Most real projects either write a small helper themselves on top of std.time.epoch or pull in a community package for full calendar and timezone handling. Expect this corner of the standard library to keep changing across Zig versions more than in most established languages.

Have a timestamp right now?

Paste it into the Timestamp ⇄ Date Converter to check it instantly, in any timezone, without writing any code. Working across several languages on the same project? The full Epoch Time in Programming Languages guide has all of them side by side for quick comparison.

Related

Related tools & reading