DoxigAlpha

now

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.

Type definitions in this namespace

Types

#
Instant
An Instant represents a timestamp with respect to the currently
Timer
A monotonic, high performance timer.

Get a calendar timestamp, in seconds, relative to UTC 1970-01-01.

Functions

#
timestamp
Get a calendar timestamp, in seconds, relative to UTC 1970-01-01.
milliTimestamp
Get a calendar timestamp, in milliseconds, relative to UTC 1970-01-01.
microTimestamp
Get a calendar timestamp, in microseconds, relative to UTC 1970-01-01.
nanoTimestamp
Get a calendar timestamp, in nanoseconds, relative to UTC 1970-01-01.

= 1000

Values

#
ns_per_us
= 1000
ns_per_ms
= 1000 * ns_per_us
ns_per_s
= 1000 * ns_per_ms
ns_per_min
= 60 * ns_per_s
ns_per_hour
= 60 * ns_per_min
ns_per_day
= 24 * ns_per_hour
ns_per_week
= 7 * ns_per_day
us_per_ms
= 1000
us_per_s
= 1000 * us_per_ms
us_per_min
= 60 * us_per_s
us_per_hour
= 60 * us_per_min
us_per_day
= 24 * us_per_hour
us_per_week
= 7 * us_per_day
ms_per_s
= 1000
ms_per_min
= 60 * ms_per_s
ms_per_hour
= 60 * ms_per_min
ms_per_day
= 24 * ms_per_hour
ms_per_week
= 7 * ms_per_day
s_per_hour
= s_per_min * 60
s_per_day
= s_per_hour * 24
s_per_week
= s_per_day * 7

Source

Implementation

#
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 => {
            const value, _ = std.os.uefi.system_table.runtime_services.getTime() catch 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 };
}