chiark / gitweb /
More generated manual pages.
[mLib] / tv.h
1 /* -*-c-*-
2  *
3  * $Id: tv.h,v 1.5 1999/05/21 22:13:12 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.5  1999/05/21 22:13:12  mdw
34  * Moved most of the code into exported macros.  The main functions now
35  * implemented using the macros.
36  *
37  * Revision 1.4  1999/05/17 20:37:25  mdw
38  * New function `tv_addl' to add a literal to a time value.
39  *
40  * Revision 1.3  1999/05/06 19:51:36  mdw
41  * Reformatted the LGPL notice a little bit.
42  *
43  * Revision 1.2  1999/05/05 18:50:31  mdw
44  * Change licensing conditions to LGPL.
45  *
46  * Revision 1.1  1998/11/25 23:30:01  mdw
47  * New file.
48  *
49  */
50
51 #ifndef TV_H
52 #define TV_H
53
54 #ifdef __cplusplus
55   extern "C" {
56 #endif
57
58 /*----- Header files ------------------------------------------------------*/
59
60 #include <sys/time.h>
61
62 /*----- A macro to make reading easier ------------------------------------*/
63
64 #define MILLION 1000000
65
66 /*----- Functions provided ------------------------------------------------*/
67
68 /* --- @tv_add@ --- *
69  *
70  * Arguments:   @struct timeval *dst@ = destination block
71  *              @const struct timeval *a, *b@ = source blocks
72  *
73  * Returns:     ---
74  *
75  * Use:         Adds two timevals.
76  */
77
78 extern void tv_add(struct timeval */*dst*/,
79                    const struct timeval */*a*/,
80                    const struct timeval */*b*/);
81
82 #define TV_ADD(dst, a, b) TV_ADDL(dst, a, (b)->tv_sec, (b)->tv_usec)
83
84 /* --- @tv_addl@ --- *
85  *
86  * Arguments:   @struct timeval *dst@ = destination block
87  *              @const struct timeval *a@ = source blocks
88  *              @time_t sec@, @unsigned long usec@ = time to add
89  *
90  * Returns:     ---
91  *
92  * Use:         Adds a literal time in seconds and microseconds.
93  */
94
95 extern void tv_addl(struct timeval */*dst*/,
96                     const struct timeval */*a*/,
97                     time_t /*sec*/, unsigned long /*usec*/);
98
99 #define TV_ADDL(dst, a, sec, usec) do {                                 \
100   (dst)->tv_sec = (a)->tv_sec + (sec);                                  \
101   (dst)->tv_usec = (a)->tv_usec + (usec);                               \
102   if ((dst)->tv_usec >= MILLION) {                                      \
103     (dst)->tv_usec -= MILLION;                                          \
104     (dst)->tv_sec++;                                                    \
105   }                                                                     \
106 } while (0)
107
108 /* --- @tv_sub@ --- *
109  *
110  * Arguments:   @struct timeval *dst@ = destination block
111  *              @const struct timeval *a, *b@ = source blocks
112  *
113  * Returns:     ---
114  *
115  * Use:         Subtracts two timevals.
116  */
117
118 extern void tv_sub(struct timeval */*dst*/,
119                    const struct timeval */*a*/,
120                    const struct timeval */*b*/);
121
122 #define TV_SUB(dst, a, b) TV_SUBL(dst, a, (b)->tv_sec, (b)->tv_usec)
123
124 /* --- @tv_subl@ --- *
125  *
126  * Arguments:   @struct timeval *dst@ = destination block
127  *              @const struct timeval *a@ = source blocks
128  *              @time_t sec@, @unsigned long usec@ = time to subtract
129  *
130  * Returns:     ---
131  *
132  * Use:         Subtracts a literal time in seconds and microseconds.
133  */
134
135 extern void tv_subl(struct timeval */*dst*/,
136                     const struct timeval */*a*/,
137                     time_t /*sec*/, unsigned long /*usec*/);
138
139 #define TV_SUBL(dst, a, sec, usec) do {                                 \
140   (dst)->tv_sec = (a)->tv_sec - (sec);                                  \
141   if ((a)->tv_usec >= (usec))                                           \
142     (dst)->tv_usec = (a)->tv_usec - (usec);                             \
143   else {                                                                \
144     (dst)->tv_usec = (a)->tv_usec + MILLION - (usec);                   \
145     (dst)->tv_sec--;                                                    \
146   }                                                                     \
147 } while (0)
148
149 /* --- @tv_cmp@ --- *
150  *
151  * Arguments:   @const struct timeval *a, *b@ = source blocks
152  *
153  * Returns:     Less than, equal to, or greater than zero.
154  *
155  * Use:         Compares two timevals.
156  */
157
158 extern int tv_cmp(const struct timeval */*a*/,
159                   const struct timeval */*b*/);
160
161 #define TV_CMP(a, op, b) ((a)->tv_sec == (b)->tv_sec ?                  \
162                             (a)->tv_usec op (b)->tv_usec :              \
163                             (a)->tv_sec op (b)->tv_sec)
164
165 /*----- That's all, folks -------------------------------------------------*/
166
167 #ifdef __cplusplus
168   }
169 #endif
170
171 #endif