Commit d395ed2d40

Shritesh Bhattarai <shritesh@shritesh.com>
2019-05-01 03:43:43
wasi: implement timestamp
1 parent 0ce05fa
Changed files (2)
std
std/os/wasi/core.zig
@@ -10,10 +10,10 @@ pub const ciovec_t = extern struct {
     buf_len: usize,
 };
 
-pub const CLOCK_REALTIME = 0;
-pub const CLOCK_MONOTONIC = 1;
-pub const CLOCK_PROCESS_CPUTIME_ID = 2;
-pub const CLOCK_THREAD_CPUTIME_ID = 3;
+pub const CLOCK_REALTIME: clockid_t = 0;
+pub const CLOCK_MONOTONIC: clockid_t = 1;
+pub const CLOCK_PROCESS_CPUTIME_ID: clockid_t = 2;
+pub const CLOCK_THREAD_CPUTIME_ID: clockid_t = 3;
 
 pub const SIGABRT: signal_t = 6;
 
std/os/time.zig
@@ -7,6 +7,7 @@ const testing = std.testing;
 const windows = std.os.windows;
 const linux = std.os.linux;
 const darwin = std.os.darwin;
+const wasi = std.os.wasi;
 const posix = std.os.posix;
 
 pub const epoch = @import("epoch.zig");
@@ -64,9 +65,21 @@ pub const milliTimestamp = switch (builtin.os) {
     Os.windows => milliTimestampWindows,
     Os.linux, Os.freebsd, Os.netbsd => milliTimestampPosix,
     Os.macosx, Os.ios => milliTimestampDarwin,
+    Os.wasi => milliTimestampWasi,
     else => @compileError("Unsupported OS"),
 };
 
+fn milliTimestampWasi() u64 {
+    var ns: wasi.timestamp_t = undefined;
+
+    // TODO: Verify that precision is ignored
+    const err = wasi.clock_time_get(wasi.CLOCK_REALTIME, 1, &ns);
+    debug.assert(err == wasi.ESUCCESS);
+
+    const ns_per_ms = 1000;
+    return @divFloor(ns, ns_per_ms);
+}
+
 fn milliTimestampWindows() u64 {
     //FileTime has a granularity of 100 nanoseconds
     //  and uses the NTFS/Windows epoch