xref: /freebsd/contrib/libfido2/openbsd-compat/time.h (revision 3332f1b444d4a73238e9f59cca27bfc95fe936bd)
1 /*
2  * Public domain
3  * sys/time.h compatibility shim
4  */
5 
6 #if defined(_MSC_VER) && (_MSC_VER >= 1900)
7 #include <../ucrt/time.h>
8 #elif defined(_MSC_VER) && (_MSC_VER < 1900)
9 #include <../include/time.h>
10 #else
11 #include <time.h>
12 #endif
13 
14 #ifndef _COMPAT_TIME_H
15 #define _COMPAT_TIME_H
16 
17 #ifndef CLOCK_MONOTONIC
18 #define CLOCK_MONOTONIC CLOCK_REALTIME
19 #endif
20 
21 #ifndef CLOCK_REALTIME
22 #define CLOCK_REALTIME 0
23 #endif
24 
25 #ifndef HAVE_CLOCK_GETTIME
26 typedef int clockid_t;
27 int clock_gettime(clockid_t, struct timespec *);
28 #endif
29 
30 #ifdef HAVE_TIMESPECSUB
31 #include <sys/time.h>
32 #endif
33 
34 #ifndef HAVE_TIMESPECSUB
35 #define	timespecadd(tsp, usp, vsp)					\
36 	do {								\
37 		(vsp)->tv_sec = (tsp)->tv_sec + (usp)->tv_sec;		\
38 		(vsp)->tv_nsec = (tsp)->tv_nsec + (usp)->tv_nsec;	\
39 		if ((vsp)->tv_nsec >= 1000000000L) {			\
40 			(vsp)->tv_sec++;				\
41 			(vsp)->tv_nsec -= 1000000000L;			\
42 		}							\
43 	} while (0)
44 
45 #define timespecsub(tsp, usp, vsp)					\
46 	do {								\
47 		(vsp)->tv_sec = (tsp)->tv_sec - (usp)->tv_sec;		\
48 		(vsp)->tv_nsec = (tsp)->tv_nsec - (usp)->tv_nsec;	\
49 		if ((vsp)->tv_nsec < 0) {				\
50 			(vsp)->tv_sec--;				\
51 			(vsp)->tv_nsec += 1000000000L;			\
52 		}							\
53 	} while (0)
54 
55 #define	timespeccmp(tsp, usp, cmp)					\
56 	(((tsp)->tv_sec == (usp)->tv_sec) ?				\
57 	    ((tsp)->tv_nsec cmp (usp)->tv_nsec) :			\
58 	    ((tsp)->tv_sec cmp (usp)->tv_sec))
59 #endif
60 
61 #endif /* _COMPAT_TIME_H */
62