1 #include "threads_impl.h"
2 
3 #include <errno.h>
4 #include <stdint.h>
5 #include <time.h>
6 
7 #include <zircon/syscalls.h>
8 
9 #include "libc.h"
10 
__clock_gettime(clockid_t clk,struct timespec * ts)11 int __clock_gettime(clockid_t clk, struct timespec* ts) {
12     uint32_t zx_clock;
13     switch (clk) {
14     case CLOCK_MONOTONIC:
15     case CLOCK_MONOTONIC_RAW:
16         zx_clock = ZX_CLOCK_MONOTONIC;
17         break;
18     case CLOCK_REALTIME:
19         zx_clock = ZX_CLOCK_UTC;
20         break;
21     case CLOCK_THREAD_CPUTIME_ID:
22         zx_clock = ZX_CLOCK_THREAD;
23         break;
24     default:
25         errno = EINVAL;
26         return -1;
27     }
28     zx_time_t now;
29     zx_status_t status = _zx_clock_get_new(zx_clock, &now);
30     if (status != ZX_OK) {
31         __builtin_trap();
32     }
33     ts->tv_sec = now / ZX_SEC(1);
34     ts->tv_nsec = now % ZX_SEC(1);
35     return 0;
36 }
37 
38 weak_alias(__clock_gettime, clock_gettime);
39