3 * $Id: tv.h,v 1.7 2004/04/08 01:36:13 mdw Exp $
5 * Manipulation of timeval structures
7 * (c) 1998 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of the mLib utilities library.
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.
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.
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,
37 /*----- Header files ------------------------------------------------------*/
41 /*----- A macro to make reading easier ------------------------------------*/
43 #define MILLION 1000000
45 /*----- Functions provided ------------------------------------------------*/
49 * Arguments: @struct timeval *dst@ = destination block
50 * @const struct timeval *a, *b@ = source blocks
54 * Use: Adds two timevals.
57 extern void tv_add(struct timeval */*dst*/,
58 const struct timeval */*a*/,
59 const struct timeval */*b*/);
61 #define TV_ADD(dst, a, b) TV_ADDL(dst, a, (b)->tv_sec, (b)->tv_usec)
63 /* --- @tv_addl@ --- *
65 * Arguments: @struct timeval *dst@ = destination block
66 * @const struct timeval *a@ = source blocks
67 * @time_t sec@, @unsigned long usec@ = time to add
71 * Use: Adds a literal time in seconds and microseconds.
74 extern void tv_addl(struct timeval */*dst*/,
75 const struct timeval */*a*/,
76 time_t /*sec*/, unsigned long /*usec*/);
78 #define TV_ADDL(dst, a, sec, usec) do { \
79 (dst)->tv_sec = (a)->tv_sec + (sec); \
80 (dst)->tv_usec = (a)->tv_usec + (usec); \
81 if ((dst)->tv_usec >= MILLION) { \
82 (dst)->tv_usec -= MILLION; \
89 * Arguments: @struct timeval *dst@ = destination block
90 * @const struct timeval *a, *b@ = source blocks
94 * Use: Subtracts two timevals.
97 extern void tv_sub(struct timeval */*dst*/,
98 const struct timeval */*a*/,
99 const struct timeval */*b*/);
101 #define TV_SUB(dst, a, b) TV_SUBL(dst, a, (b)->tv_sec, (b)->tv_usec)
103 /* --- @tv_subl@ --- *
105 * Arguments: @struct timeval *dst@ = destination block
106 * @const struct timeval *a@ = source blocks
107 * @time_t sec@, @unsigned long usec@ = time to subtract
111 * Use: Subtracts a literal time in seconds and microseconds.
114 extern void tv_subl(struct timeval */*dst*/,
115 const struct timeval */*a*/,
116 time_t /*sec*/, unsigned long /*usec*/);
118 #define TV_SUBL(dst, a, sec, usec) do { \
119 (dst)->tv_sec = (a)->tv_sec - (sec); \
120 if ((a)->tv_usec >= (usec)) \
121 (dst)->tv_usec = (a)->tv_usec - (usec); \
123 (dst)->tv_usec = (a)->tv_usec + MILLION - (usec); \
128 /* --- @tv_cmp@ --- *
130 * Arguments: @const struct timeval *a, *b@ = source blocks
132 * Returns: Less than, equal to, or greater than zero.
134 * Use: Compares two timevals.
137 extern int tv_cmp(const struct timeval */*a*/,
138 const struct timeval */*b*/);
140 #define TV_CMP(a, op, b) ((a)->tv_sec == (b)->tv_sec ? \
141 (a)->tv_usec op (b)->tv_usec : \
142 (a)->tv_sec op (b)->tv_sec)
144 /*----- That's all, folks -------------------------------------------------*/