1 #pragma once
2 
3 #ifdef __cplusplus
4 extern "C" {
5 #endif
6 
7 #include <features.h>
8 
9 #include <sys/select.h>
10 
11 int gettimeofday(struct timeval* __restrict, void* __restrict);
12 
13 #if defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) || defined(_BSD_SOURCE)
14 
15 #define ITIMER_REAL 0
16 #define ITIMER_VIRTUAL 1
17 #define ITIMER_PROF 2
18 
19 struct itimerval {
20     struct timeval it_interval;
21     struct timeval it_value;
22 };
23 
24 int getitimer(int, struct itimerval*);
25 int setitimer(int, const struct itimerval* __restrict, struct itimerval* __restrict);
26 int utimes(const char*, const struct timeval[2]);
27 
28 #endif
29 
30 #if defined(_GNU_SOURCE) || defined(_BSD_SOURCE)
31 struct timezone {
32     int tz_minuteswest;
33     int tz_dsttime;
34 };
35 int futimes(int, const struct timeval[2]);
36 int futimesat(int, const char*, const struct timeval[2]);
37 int lutimes(const char*, const struct timeval[2]);
38 int settimeofday(const struct timeval*, const struct timezone*);
39 int adjtime(const struct timeval*, struct timeval*);
40 #define timerisset(t) ((t)->tv_sec || (t)->tv_usec)
41 #define timerclear(t) ((t)->tv_sec = (t)->tv_usec = 0)
42 #define timercmp(s, t, op) \
43     ((s)->tv_sec == (t)->tv_sec ? (s)->tv_usec op(t)->tv_usec : (s)->tv_sec op(t)->tv_sec)
44 #define timeradd(s, t, a)                                             \
45     (void)((a)->tv_sec = (s)->tv_sec + (t)->tv_sec,                   \
46            ((a)->tv_usec = (s)->tv_usec + (t)->tv_usec) >= 1000000 && \
47                ((a)->tv_usec -= 1000000, (a)->tv_sec++))
48 #define timersub(s, t, a)                                      \
49     (void)((a)->tv_sec = (s)->tv_sec - (t)->tv_sec,            \
50            ((a)->tv_usec = (s)->tv_usec - (t)->tv_usec) < 0 && \
51                ((a)->tv_usec += 1000000, (a)->tv_sec--))
52 #endif
53 
54 #if defined(_GNU_SOURCE)
55 #define TIMEVAL_TO_TIMESPEC(tv, ts) \
56     ((ts)->tv_sec = (tv)->tv_sec, (ts)->tv_nsec = (tv)->tv_usec * 1000, (void)0)
57 #define TIMESPEC_TO_TIMEVAL(tv, ts) \
58     ((tv)->tv_sec = (ts)->tv_sec, (tv)->tv_usec = (ts)->tv_nsec / 1000, (void)0)
59 #endif
60 
61 #ifdef __cplusplus
62 }
63 #endif
64