Function now [src]

Queries the system for the current moment of time as an Instant. This is not guaranteed to be monotonic or steadily increasing, but for most implementations it is. Returns error.Unsupported when a suitable clock is not detected.

Prototype

pub fn now() error{Unsupported}!Instant

Possible Errors

Unsupported

Source

pub fn now() error{Unsupported}!Instant { const clock_id = switch (builtin.os.tag) { .windows => { // QPC on windows doesn't fail on >= XP/2000 and includes time suspended. return .{ .timestamp = windows.QueryPerformanceCounter() }; }, .wasi => { var ns: std.os.wasi.timestamp_t = undefined; const rc = std.os.wasi.clock_time_get(.MONOTONIC, 1, &ns); if (rc != .SUCCESS) return error.Unsupported; return .{ .timestamp = ns }; }, .uefi => { var value: std.os.uefi.Time = undefined; const status = std.os.uefi.system_table.runtime_services.getTime(&value, null); if (status != .success) return error.Unsupported; return .{ .timestamp = value.toEpoch() }; }, // On darwin, use UPTIME_RAW instead of MONOTONIC as it ticks while // suspended. .macos, .ios, .tvos, .watchos, .visionos => posix.CLOCK.UPTIME_RAW, // On freebsd derivatives, use MONOTONIC_FAST as currently there's // no precision tradeoff. .freebsd, .dragonfly => posix.CLOCK.MONOTONIC_FAST, // On linux, use BOOTTIME instead of MONOTONIC as it ticks while // suspended. .linux => posix.CLOCK.BOOTTIME, // On other posix systems, MONOTONIC is generally the fastest and // ticks while suspended. else => posix.CLOCK.MONOTONIC, }; const ts = posix.clock_gettime(clock_id) catch return error.Unsupported; return .{ .timestamp = ts }; }