nanoTimestamp
Get a calendar timestamp, in nanoseconds, relative to UTC 1970-01-01.
Precision of timing depends on the hardware and operating system.
On Windows this has a maximum granularity of 100 nanoseconds.
The return value is signed because it is possible to have a date that is
before the epoch.
See posix.clock_gettime for a POSIX timestamp.
Type definitions in this namespace
Types
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_min
- = 60
- 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 nanoTimestamp() i128 {
switch (builtin.os.tag) {
.windows => {
// RtlGetSystemTimePrecise() has a granularity of 100 nanoseconds and uses the NTFS/Windows epoch,
// which is 1601-01-01.
const epoch_adj = epoch.windows * (ns_per_s / 100);
return @as(i128, windows.ntdll.RtlGetSystemTimePrecise() + epoch_adj) * 100;
},
.wasi => {
var ns: std.os.wasi.timestamp_t = undefined;
const err = std.os.wasi.clock_time_get(.REALTIME, 1, &ns);
assert(err == .SUCCESS);
return ns;
},
.uefi => {
const value, _ = std.os.uefi.system_table.runtime_services.getTime() catch return 0;
return value.toEpoch();
},
else => {
const ts = posix.clock_gettime(.REALTIME) catch |err| switch (err) {
error.UnsupportedClock, error.Unexpected => return 0, // "Precision of timing depends on hardware and OS".
};
return (@as(i128, ts.sec) * ns_per_s) + ts.nsec;
},
}
}