authorgravatar for shritesh@shritesh.comShritesh Bhattarai <shritesh@shritesh.com> 2019-04-30 20:43:43-05:00
committergravatar for shritesh@shritesh.comShritesh Bhattarai <shritesh@shritesh.com> 2019-04-30 20:43:43-05:00
logd395ed2d403aa0ff57c493052e3675de23292424
tree3d5fb6d6a3de329464e56a5face49e785e76e807
parent0ce05fa621ba95ba3ce98f99cb14fec549409069

wasi: implement timestamp


2 files changed, 17 insertions(+), 4 deletions(-)

std/os/time.zig+13
...@@ -7,6 +7,7 @@ const testing = std.testing;...@@ -7,6 +7,7 @@ const testing = std.testing;
7const windows = std.os.windows;7const windows = std.os.windows;
8const linux = std.os.linux;8const linux = std.os.linux;
9const darwin = std.os.darwin;9const darwin = std.os.darwin;
10const wasi = std.os.wasi;
10const posix = std.os.posix;11const posix = std.os.posix;
1112
12pub const epoch = @import("epoch.zig");13pub const epoch = @import("epoch.zig");
...@@ -64,9 +65,21 @@ pub const milliTimestamp = switch (builtin.os) {...@@ -64,9 +65,21 @@ pub const milliTimestamp = switch (builtin.os) {
64 Os.windows => milliTimestampWindows,65 Os.windows => milliTimestampWindows,
65 Os.linux, Os.freebsd, Os.netbsd => milliTimestampPosix,66 Os.linux, Os.freebsd, Os.netbsd => milliTimestampPosix,
66 Os.macosx, Os.ios => milliTimestampDarwin,67 Os.macosx, Os.ios => milliTimestampDarwin,
68 Os.wasi => milliTimestampWasi,
67 else => @compileError("Unsupported OS"),69 else => @compileError("Unsupported OS"),
68};70};
6971
72fn milliTimestampWasi() u64 {
73 var ns: wasi.timestamp_t = undefined;
74
75 // TODO: Verify that precision is ignored
76 const err = wasi.clock_time_get(wasi.CLOCK_REALTIME, 1, &ns);
77 debug.assert(err == wasi.ESUCCESS);
78
79 const ns_per_ms = 1000;
80 return @divFloor(ns, ns_per_ms);
81}
82
70fn milliTimestampWindows() u64 {83fn milliTimestampWindows() u64 {
71 //FileTime has a granularity of 100 nanoseconds84 //FileTime has a granularity of 100 nanoseconds
72 // and uses the NTFS/Windows epoch85 // and uses the NTFS/Windows epoch
std/os/wasi/core.zig+4-4
...@@ -10,10 +10,10 @@ pub const ciovec_t = extern struct {...@@ -10,10 +10,10 @@ pub const ciovec_t = extern struct {
10 buf_len: usize,10 buf_len: usize,
11};11};
1212
13pub const CLOCK_REALTIME = 0;13pub const CLOCK_REALTIME: clockid_t = 0;
14pub const CLOCK_MONOTONIC = 1;14pub const CLOCK_MONOTONIC: clockid_t = 1;
15pub const CLOCK_PROCESS_CPUTIME_ID = 2;15pub const CLOCK_PROCESS_CPUTIME_ID: clockid_t = 2;
16pub const CLOCK_THREAD_CPUTIME_ID = 3;16pub const CLOCK_THREAD_CPUTIME_ID: clockid_t = 3;
1717
18pub const SIGABRT: signal_t = 6;18pub const SIGABRT: signal_t = 6;
1919