//! time — clock helpers wrapping the Io timestamp source. //! //! every reader thread / fiber needs "what time is it" in some unit; these //! were re-implemented in five modules before being centralized here. const std = @import("std"); const Io = std.Io; /// seconds since the unix epoch. pub fn timestamp(io: Io) i64 { return @intCast(@divFloor(Io.Timestamp.now(io, .real).nanoseconds, std.time.ns_per_s)); } /// milliseconds since the unix epoch. pub fn milliTimestamp(io: Io) i64 { return Io.Timestamp.now(io, .real).toMilliseconds(); } /// microseconds since the unix epoch. pub fn microTimestamp(io: Io) i64 { return Io.Timestamp.now(io, .real).toMicroseconds(); } /// nanoseconds since the unix epoch. pub fn nanoTimestamp(io: Io) i96 { return Io.Timestamp.now(io, .real).toNanoseconds(); } /// format the current wall-clock time as RFC-3339 (e.g. "2026-06-07T12:34:56Z"). /// writes into a caller-provided 24-byte buffer and returns the slice. pub fn formatTimestamp(buf: *[24]u8) []const u8 { var tp: std.c.timespec = undefined; _ = std.c.clock_gettime(.REALTIME, &tp); const ts: u64 = @intCast(tp.sec); const es = std.time.epoch.EpochSeconds{ .secs = ts }; const day = es.getEpochDay(); const yd = day.calculateYearDay(); const md = yd.calculateMonthDay(); const ds = es.getDaySeconds(); return std.fmt.bufPrint(buf, "{d:0>4}-{d:0>2}-{d:0>2}T{d:0>2}:{d:0>2}:{d:0>2}Z", .{ yd.year, @as(u32, @intFromEnum(md.month)) + 1, @as(u32, md.day_index) + 1, ds.getHoursIntoDay(), ds.getMinutesIntoHour(), ds.getSecondsIntoMinute(), }) catch "1970-01-01T00:00:00Z"; }