1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
//! 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, @backingInt(md.month)) + 1,
@as(u32, md.day_index) + 1,
ds.getHoursIntoDay(),
ds.getMinutesIntoHour(),
ds.getSecondsIntoMinute(),
}) catch "1970-01-01T00:00:00Z";
}