__time_to_tm: initialize tm_zone and tm_gmtoff
[musl] / src / time / clock_gettime.c
1 #include <time.h>
2 #include <errno.h>
3 #include <stdint.h>
4 #include "syscall.h"
5 #include "libc.h"
6
7 static int sc_clock_gettime(clockid_t clk, struct timespec *ts)
8 {
9         int r = __syscall(SYS_clock_gettime, clk, ts);
10         if (!r) return r;
11         if (r == -ENOSYS) {
12                 if (clk == CLOCK_REALTIME) {
13                         __syscall(SYS_gettimeofday, clk, ts, 0);
14                         ts->tv_nsec = (int)ts->tv_nsec * 1000;
15                         return 0;
16                 }
17                 r = -EINVAL;
18         }
19         errno = -r;
20         return -1;
21 }
22
23 weak_alias(sc_clock_gettime, __vdso_clock_gettime);
24
25 int (*__cgt)(clockid_t, struct timespec *) = __vdso_clock_gettime;
26
27 int __clock_gettime(clockid_t clk, struct timespec *ts)
28 {
29         /* Conditional is to make this work prior to dynamic linking */
30         return __cgt ? __cgt(clk, ts) : sc_clock_gettime(clk, ts);
31 }
32
33 weak_alias(__clock_gettime, clock_gettime);