chiark / gitweb /
Script to transform CVS sources into buildable source tree.
[mLib] / tv.c
1 /* -*-c-*-
2  *
3  * $Id: tv.c,v 1.2 1999/05/05 18:50:31 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 Software
26  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27  */
28
29 /*----- Revision history --------------------------------------------------* 
30  *
31  * $Log: tv.c,v $
32  * Revision 1.2  1999/05/05 18:50:31  mdw
33  * Change licensing conditions to LGPL.
34  *
35  * Revision 1.1  1998/11/25 23:30:01  mdw
36  * New file.
37  *
38  */
39
40 /*----- Header files ------------------------------------------------------*/
41
42 #include <sys/time.h>
43 #include "tv.h"
44
45 /*----- Main code ---------------------------------------------------------*/
46
47 /* --- @tv_add@ --- *
48  *
49  * Arguments:   @struct timeval *dst@ = destination block
50  *              @const struct timeval *a, *b@ = source blocks
51  *
52  * Returns:     ---
53  *
54  * Use:         Adds two timevals.
55  */
56
57 void tv_add(struct timeval *dst,
58             const struct timeval *a, const struct timeval *b)
59 {
60   dst->tv_sec = a->tv_sec + b->tv_sec;
61   dst->tv_usec = a->tv_usec + b->tv_usec;
62   if (dst->tv_usec >= 1000000) {
63     dst->tv_usec -= 1000000;
64     dst->tv_sec++;
65   }
66 }
67
68 /* --- @tv_sub@ --- *
69  *
70  * Arguments:   @struct timeval *dst@ = destination block
71  *              @const struct timeval *a, *b@ = source blocks
72  *
73  * Returns:     ---
74  *
75  * Use:         Subtracts two timevals.
76  */
77
78 void tv_sub(struct timeval *dst,
79             const struct timeval *a, const struct timeval *b)
80 {
81   dst->tv_sec = a->tv_sec - b->tv_sec;
82   if (a->tv_usec >= b->tv_usec)
83     dst->tv_usec = a->tv_usec - b->tv_usec;
84   else {
85     dst->tv_usec = a->tv_usec + 1000000 - b->tv_usec;
86     dst->tv_sec--;
87   }
88 }
89
90 /* --- @tv_cmp@ --- *
91  *
92  * Arguments:   @const struct timeval *a, *b@ = source blocks
93  *
94  * Returns:     Less than, equal to, or greater than zero.
95  *
96  * Use:         Compares two timevals.
97  */
98
99 int tv_cmp(const struct timeval *a, const struct timeval *b)
100 {
101   if (a->tv_sec > b->tv_sec)
102     return (1);
103   else if (a->tv_sec < b->tv_sec)
104     return (-1);
105   else if (a->tv_usec > b->tv_usec)
106     return (1);
107   else if (a->tv_usec < b->tv_usec)
108     return (-1);
109   else
110     return (0);
111 }
112
113 /*----- That's all, folks -------------------------------------------------*/