3 * Manipulation of timeval structures
5 * (c) 1998 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the mLib utilities library.
12 * mLib is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * mLib is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with mLib; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
35 /*----- Header files ------------------------------------------------------*/
39 /*----- A macro to make reading easier ------------------------------------*/
41 #define MILLION 1000000
43 /*----- Functions provided ------------------------------------------------*/
47 * Arguments: @struct timeval *dst@ = destination block
48 * @const struct timeval *a, *b@ = source blocks
52 * Use: Adds two timevals.
55 extern void tv_add(struct timeval */
*dst*/
,
56 const struct timeval */
*a*/
,
57 const struct timeval */
*b*/
);
59 #define TV_ADD(dst, a, b) TV_ADDL(dst, a, (b)->tv_sec, (b)->tv_usec)
61 /* --- @tv_addl@ --- *
63 * Arguments: @struct timeval *dst@ = destination block
64 * @const struct timeval *a@ = source blocks
65 * @time_t sec@, @unsigned long usec@ = time to add
69 * Use: Adds a literal time in seconds and microseconds.
72 extern void tv_addl(struct timeval */
*dst*/
,
73 const struct timeval */
*a*/
,
74 time_t /*sec*/, unsigned long /*usec*/);
76 #define TV_ADDL(dst, a, sec, usec) do { \
77 (dst)->tv_sec = (a)->tv_sec + (sec); \
78 (dst)->tv_usec = (a)->tv_usec + (usec); \
79 if ((dst)->tv_usec >= MILLION) { \
80 (dst)->tv_usec -= MILLION; \
87 * Arguments: @struct timeval *dst@ = destination block
88 * @const struct timeval *a, *b@ = source blocks
92 * Use: Subtracts two timevals.
95 extern void tv_sub(struct timeval */
*dst*/
,
96 const struct timeval */
*a*/
,
97 const struct timeval */
*b*/
);
99 #define TV_SUB(dst, a, b) TV_SUBL(dst, a, (b)->tv_sec, (b)->tv_usec)
101 /* --- @tv_subl@ --- *
103 * Arguments: @struct timeval *dst@ = destination block
104 * @const struct timeval *a@ = source blocks
105 * @time_t sec@, @unsigned long usec@ = time to subtract
109 * Use: Subtracts a literal time in seconds and microseconds.
112 extern void tv_subl(struct timeval */
*dst*/
,
113 const struct timeval */
*a*/
,
114 time_t /*sec*/, unsigned long /*usec*/);
116 #define TV_SUBL(dst, a, sec, usec) do { \
117 (dst)->tv_sec = (a)->tv_sec - (sec); \
118 if ((a)->tv_usec >= (usec)) \
119 (dst)->tv_usec = (a)->tv_usec - (usec); \
121 (dst)->tv_usec = (a)->tv_usec + MILLION - (usec); \
126 /* --- @tv_cmp@ --- *
128 * Arguments: @const struct timeval *a, *b@ = source blocks
130 * Returns: Less than, equal to, or greater than zero.
132 * Use: Compares two timevals.
135 extern int tv_cmp(const struct timeval */
*a*/
,
136 const struct timeval */
*b*/
);
138 #define TV_CMP(a, op, b) ((a)->tv_sec == (b)->tv_sec ? \
139 (a)->tv_usec op (b)->tv_usec : \
140 (a)->tv_sec op (b)->tv_sec)
142 /*----- That's all, folks -------------------------------------------------*/