Skip to content

Commit e51b64f

Browse files
committed
Port time() and times()
1 parent 39e65a4 commit e51b64f

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

libc-bottom-half/clocks/times.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ clock_t times(struct tms *buffer) {
2121
.tms_cutime = 0
2222
};
2323

24+
#ifdef __wasilibc_use_wasip2
25+
monotonic_clock_instant_t realtime = monotonic_clock_now();
26+
#else
2427
__wasi_timestamp_t realtime = 0;
2528
(void)__wasi_clock_time_get(__WASI_CLOCKID_MONOTONIC, 0, &realtime);
29+
#endif
2630
return realtime;
2731
}

libc-bottom-half/cloudlibc/src/libc/time/time.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,12 @@
88
#include <time.h>
99

1010
time_t time(time_t *tloc) {
11+
#ifdef __wasilibc_use_wasip2
1112
__wasi_timestamp_t ts = 0;
1213
(void)__wasi_clock_time_get(__WASI_CLOCKID_REALTIME, NSEC_PER_SEC, &ts);
14+
#else
15+
monotonic_clock_instant_t ts = monotonic_clock_now();
16+
#endif
1317
if (tloc != NULL)
1418
*tloc = ts / NSEC_PER_SEC;
1519
return ts / NSEC_PER_SEC;

test/src/misc/time_and_times.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//! add-flags.py(CFLAGS): -D_WASI_EMULATED_PROCESS_CLOCKS
2+
//! add-flags.py(LDFLAGS): -lwasi-emulated-process-clocks
3+
#include <stdlib.h>
4+
#include <stdio.h>
5+
#include <time.h>
6+
#include <string.h>
7+
#include <errno.h>
8+
#include <sys/times.h>
9+
#include "test.h"
10+
11+
#define TEST(c, ...) ((c) ? 1 : (t_error(#c" failed: " __VA_ARGS__),0))
12+
13+
int main(void)
14+
{
15+
time_t t;
16+
struct tms buffer;
17+
18+
clock_t result = times(&buffer);
19+
TEST(result > 0);
20+
TEST(buffer.tms_utime > 0);
21+
TEST(buffer.tms_cutime == 0); // always 0 under WASI
22+
23+
time_t t1 = time(&t);
24+
TEST(t==t1);
25+
TEST(t > 0);
26+
27+
return t_status;
28+
}

0 commit comments

Comments
 (0)