chiark / gitweb /
Track changes to selbuf interface.
[mLib] / tv.h
1 /* -*-c-*-
2  *
3  * $Id: tv.h,v 1.6 1999/12/10 23:42:04 mdw Exp $
4  *
5  * Manipulation of timeval structures
6  *
7  * (c) 1998 Straylight/Edgeware
8  */
9
10 /*----- Licensing notice --------------------------------------------------* 
11  *
12  * This file is part of the mLib utilities library.
13  *
14  * mLib is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU Library General Public License as
16  * published by the Free Software Foundation; either version 2 of the
17  * License, or (at your option) any later version.
18  * 
19  * mLib is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU Library General Public License for more details.
23  * 
24  * You should have received a copy of the GNU Library General Public
25  * License along with mLib; if not, write to the Free
26  * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
27  * MA 02111-1307, USA.
28  */
29
30 /*----- Revision history --------------------------------------------------* 
31  *
32  * $Log: tv.h,v $
33  * Revision 1.6  1999/12/10 23:42:04  mdw
34  * Change header file guard names.
35  *
36  * Revision 1.5  1999/05/21 22:13:12  mdw
37  * Moved most of the code into exported macros.  The main functions now
38  * implemented using the macros.
39  *
40  * Revision 1.4  1999/05/17 20:37:25  mdw
41  * New function `tv_addl' to add a literal to a time value.
42  *
43  * Revision 1.3  1999/05/06 19:51:36  mdw
44  * Reformatted the LGPL notice a little bit.
45  *
46  * Revision 1.2  1999/05/05 18:50:31  mdw
47  * Change licensing conditions to LGPL.
48  *
49  * Revision 1.1  1998/11/25 23:30:01  mdw
50  * New file.
51  *
52  */
53
54 #ifndef MLIB_TV_H
55 #define MLIB_TV_H
56
57 #ifdef __cplusplus
58   extern "C" {
59 #endif
60
61 /*----- Header files ------------------------------------------------------*/
62
63 #include <sys/time.h>
64
65 /*----- A macro to make reading easier ------------------------------------*/
66
67 #define MILLION 1000000
68
69 /*----- Functions provided ------------------------------------------------*/
70
71 /* --- @tv_add@ --- *
72  *
73  * Arguments:   @struct timeval *dst@ = destination block
74  *              @const struct timeval *a, *b@ = source blocks
75  *
76  * Returns:     ---
77  *
78  * Use:         Adds two timevals.
79  */
80
81 extern void tv_add(struct timeval */*dst*/,
82                    const struct timeval */*a*/,
83                    const struct timeval */*b*/);
84
85 #define TV_ADD(dst, a, b) TV_ADDL(dst, a, (b)->tv_sec, (b)->tv_usec)
86
87 /* --- @tv_addl@ --- *
88  *
89  * Arguments:   @struct timeval *dst@ = destination block
90  *              @const struct timeval *a@ = source blocks
91  *              @time_t sec@, @unsigned long usec@ = time to add
92  *
93  * Returns:     ---
94  *
95  * Use:         Adds a literal time in seconds and microseconds.
96  */
97
98 extern void tv_addl(struct timeval */*dst*/,
99                     const struct timeval */*a*/,
100                     time_t /*sec*/, unsigned long /*usec*/);
101
102 #define TV_ADDL(dst, a, sec, usec) do {                                 \
103   (dst)->tv_sec = (a)->tv_sec + (sec);                                  \
104   (dst)->tv_usec = (a)->tv_usec + (usec);                               \
105   if ((dst)->tv_usec >= MILLION) {                                      \
106     (dst)->tv_usec -= MILLION;                                          \
107     (dst)->tv_sec++;                                                    \
108   }                                                                     \
109 } while (0)
110
111 /* --- @tv_sub@ --- *
112  *
113  * Arguments:   @struct timeval *dst@ = destination block
114  *              @const struct timeval *a, *b@ = source blocks
115  *
116  * Returns:     ---
117  *
118  * Use:         Subtracts two timevals.
119  */
120
121 extern void tv_sub(struct timeval */*dst*/,
122                    const struct timeval */*a*/,
123                    const struct timeval */*b*/);
124
125 #define TV_SUB(dst, a, b) TV_SUBL(dst, a, (b)->tv_sec, (b)->tv_usec)
126
127 /* --- @tv_subl@ --- *
128  *
129  * Arguments:   @struct timeval *dst@ = destination block
130  *              @const struct timeval *a@ = source blocks
131  *              @time_t sec@, @unsigned long usec@ = time to subtract
132  *
133  * Returns:     ---
134  *
135  * Use:         Subtracts a literal time in seconds and microseconds.
136  */
137
138 extern void tv_subl(struct timeval */*dst*/,
139                     const struct timeval */*a*/,
140                     time_t /*sec*/, unsigned long /*usec*/);
141
142 #define TV_SUBL(dst, a, sec, usec) do {                                 \
143   (dst)->tv_sec = (a)->tv_sec - (sec);                                  \
144   if ((a)->tv_usec >= (usec))                                           \
145     (dst)->tv_usec = (a)->tv_usec - (usec);                             \
146   else {                                                                \
147     (dst)->tv_usec = (a)->tv_usec + MILLION - (usec);                   \
148     (dst)->tv_sec--;                                                    \
149   }                                                                     \
150 } while (0)
151
152 /* --- @tv_cmp@ --- *
153  *
154  * Arguments:   @const struct timeval *a, *b@ = source blocks
155  *
156  * Returns:     Less than, equal to, or greater than zero.
157  *
158  * Use:         Compares two timevals.
159  */
160
161 extern int tv_cmp(const struct timeval */*a*/,
162                   const struct timeval */*b*/);
163
164 #define TV_CMP(a, op, b) ((a)->tv_sec == (b)->tv_sec ?                  \
165                             (a)->tv_usec op (b)->tv_usec :              \
166                             (a)->tv_sec op (b)->tv_sec)
167
168 /*----- That's all, folks -------------------------------------------------*/
169
170 #ifdef __cplusplus
171   }
172 #endif
173
174 #endif