1#define _WASI_EMULATED_PROCESS_CLOCKS
 2#include <time.h>
 3#include <sys/times.h>
 4#include <wasi/api.h>
 5#include <common/time.h>
 6
 7_Static_assert(
 8    CLOCKS_PER_SEC == NSEC_PER_SEC,
 9    "This implementation assumes that `clock` is in nanoseconds"
10);
11
12// `clock` is a weak symbol so that application code can override it.
13// We want to use the function in libc, so use the libc-internal name.
14clock_t __clock(void);
15
16clock_t times(struct tms *buffer) {
17    __wasi_timestamp_t user = __clock();
18    *buffer = (struct tms){
19        .tms_utime = user,
20        // WASI doesn't provide a way to spawn a new process, so always 0.
21        .tms_cutime = 0
22    };
23
24    __wasi_timestamp_t realtime = 0;
25    (void)__wasi_clock_time_get(__WASI_CLOCKID_MONOTONIC, 0, &realtime);
26    return realtime;
27}