1 /* @(#)time.h	1.20 13/10/01 Copyright 1996-2013 J. Schilling */
2 /*
3  *	Generic header for users of time(), gettimeofday() ...
4  *
5  *	It includes definitions for time_t, struct timeval, ...
6  *
7  *	Copyright (c) 1996-2013 J. Schilling
8  */
9 /*
10  * The contents of this file are subject to the terms of the
11  * Common Development and Distribution License, Version 1.0 only
12  * (the "License").  You may not use this file except in compliance
13  * with the License.
14  *
15  * See the file CDDL.Schily.txt in this distribution for details.
16  * A copy of the CDDL is also available via the Internet at
17  * http://www.opensource.org/licenses/cddl1.txt
18  *
19  * When distributing Covered Code, include this CDDL HEADER in each
20  * file and include the License file CDDL.Schily.txt from this distribution.
21  */
22 
23 #ifndef	_SCHILY_TIME_H
24 #define	_SCHILY_TIME_H
25 
26 #ifndef	_SCHILY_MCONFIG_H
27 #include <schily/mconfig.h>
28 #endif
29 
30 #ifndef	_SCHILY_TYPES_H
31 #include <schily/types.h>	/* Needed for time_t		*/
32 #endif
33 
34 #ifdef	TIME_WITH_SYS_TIME
35 #	ifndef	_INCL_SYS_TIME_H
36 #	include <sys/time.h>
37 #	define	_INCL_SYS_TIME_H
38 #	endif
39 #	ifndef	_INCL_TIME_H
40 #	include <time.h>
41 #	define	_INCL_TIME_H
42 #	endif
43 #else
44 #ifdef	HAVE_SYS_TIME_H
45 #	ifndef	_INCL_SYS_TIME_H
46 #	include <sys/time.h>
47 #	define	_INCL_SYS_TIME_H
48 #	endif
49 #else
50 #	ifndef	_INCL_TIME_H
51 #	include <time.h>
52 #	define	_INCL_TIME_H
53 #	endif
54 #endif
55 #endif
56 
57 #ifdef	__cplusplus
58 extern "C" {
59 #endif
60 
61 #ifdef	timerclear
62 /*
63  * With MSVC timerclear / struct timeval present in case that
64  * winsock2.h has been included before.
65  */
66 #undef	HAVE_STRUCT_TIMEVAL
67 #define	HAVE_STRUCT_TIMEVAL	1
68 #endif
69 
70 #ifndef	HAVE_STRUCT_TIMEVAL
71 
72 struct timeval {
73 	long	tv_sec;
74 	long	tv_usec;
75 };
76 #endif
77 
78 #ifndef	HAVE_STRUCT_TIMEZONE
79 
80 struct timezone {
81 	int	tz_minuteswest;
82 	int	tz_dsttime;
83 };
84 #endif
85 
86 #ifndef	HAVE_STRUCT_TIMESPEC
87 
88 struct timespec {
89 	time_t	tv_sec;
90 	long	tv_nsec;
91 };
92 #endif
93 
94 
95 #undef	timerclear
96 #define	timerclear(tvp)		(tvp)->tv_sec = (tvp)->tv_usec = 0
97 
98 #undef	timerfix
99 #define	timerfix1(tvp)		while ((tvp)->tv_usec < 0) {		\
100 					(tvp)->tv_sec--;		\
101 					(tvp)->tv_usec += 1000000;	\
102 				}
103 
104 #define	timerfix2(tvp)		while ((tvp)->tv_usec > 1000000) {	\
105 					(tvp)->tv_sec++;		\
106 					(tvp)->tv_usec -= 1000000;	\
107 				}
108 
109 #define	timerfix(tvp)		do { timerfix1(tvp); timerfix2(tvp); } while (0)
110 
111 /*
112  * timersub() and timeradd() are defined on FreeBSD with a different
113  * interface (3 parameters).
114  */
115 #undef	timersub
116 #define	timersub(tvp1, tvp2)	do {					\
117 					(tvp1)->tv_sec -= (tvp2)->tv_sec; \
118 					(tvp1)->tv_usec -= (tvp2)->tv_usec; \
119 					timerfix1(tvp1); timerfix2(tvp1); \
120 				} while (0)
121 
122 #undef	timeradd
123 #define	timeradd(tvp1, tvp2)	do {					\
124 					(tvp1)->tv_sec += (tvp2)->tv_sec; \
125 					(tvp1)->tv_usec += (tvp2)->tv_usec; \
126 					timerfix1(tvp1); timerfix2(tvp1); \
127 				} while (0)
128 
129 
130 #undef	timespecclear
131 #define	timespecclear(tsp)	(tsp)->tv_sec = (tsp)->tv_nsec = 0
132 
133 #undef	timespecfix
134 #define	timespecfix1(tsp)	while ((tsp)->tv_nsec < 0) {		\
135 					(tsp)->tv_sec--;		\
136 					(tsp)->tv_nsec += 1000000000;	\
137 				}
138 
139 #define	timespecfix2(tsp)	while ((tsp)->tv_nsec > 1000000000) {	\
140 					(tsp)->tv_sec++;		\
141 					(tsp)->tv_nsec -= 1000000000;	\
142 				}
143 
144 #define	timespecfix(tsp)	do { timespecfix1(tsp); timespecfix2(tsp); } while (0)
145 
146 #undef	timespecsub
147 #define	timespecsub(tsp1, tsp2)	do {					\
148 					(tsp1)->tv_sec -= (tsp2)->tv_sec; \
149 					(tsp1)->tv_nsec -= (tsp2)->tv_nsec; \
150 					timespecfix1(tsp1); timespecfix2(tsp1); \
151 				} while (0)
152 
153 #undef	timespecadd
154 #define	timespecadd(tsp1, tsp2)	do {					\
155 					(tsp1)->tv_sec += (tsp2)->tv_sec; \
156 					(tsp1)->tv_nsec += (tsp2)->tv_nsec; \
157 					timespecfix1(tsp1); timespecfix2(tsp1); \
158 				} while (0)
159 
160 #ifdef	__cplusplus
161 }
162 #endif
163 
164 #endif	/* _SCHILY_TIME_H */
165