chiark / gitweb /
Release version 2.1.1.
[mLib] / tv.c
1 /* -*-c-*-
2  *
3  * $Id: tv.c,v 1.6 2004/04/08 01:36:13 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 /*----- Header files ------------------------------------------------------*/
31
32 #include <sys/time.h>
33 #include "tv.h"
34
35 /*----- Main code ---------------------------------------------------------*/
36
37 /* --- @tv_add@ --- *
38  *
39  * Arguments:   @struct timeval *dst@ = destination block
40  *              @const struct timeval *a, *b@ = source blocks
41  *
42  * Returns:     ---
43  *
44  * Use:         Adds two timevals.
45  */
46
47 void tv_add(struct timeval *dst,
48             const struct timeval *a, const struct timeval *b)
49 {
50   TV_ADD(dst, a, b);
51 }
52
53 /* --- @tv_addl@ --- *
54  *
55  * Arguments:   @struct timeval *dst@ = destination block
56  *              @const struct timeval *a@ = source blocks
57  *              @time_t sec@, @unsigned long usec@ = time to add
58  *
59  * Returns:     ---
60  *
61  * Use:         Adds a literal time in seconds and microseconds.
62  */
63
64 void tv_addl(struct timeval *dst, const struct timeval *a,
65              time_t sec, unsigned long usec)
66 {
67   TV_ADDL(dst, a, sec, usec);
68 }
69
70 /* --- @tv_sub@ --- *
71  *
72  * Arguments:   @struct timeval *dst@ = destination block
73  *              @const struct timeval *a, *b@ = source blocks
74  *
75  * Returns:     ---
76  *
77  * Use:         Subtracts two timevals.
78  */
79
80 void tv_sub(struct timeval *dst,
81             const struct timeval *a, const struct timeval *b)
82 {
83   TV_SUB(dst, a, b);
84 }
85
86 /* --- @tv_subl@ --- *
87  *
88  * Arguments:   @struct timeval *dst@ = destination block
89  *              @const struct timeval *a@ = source blocks
90  *              @time_t sec@, @unsigned long usec@ = time to subtract
91  *
92  * Returns:     ---
93  *
94  * Use:         Subtracts a literal time in seconds and microseconds.
95  */
96
97 void tv_subl(struct timeval *dst, const struct timeval *a,
98                     time_t sec, unsigned long usec)
99 {
100   TV_SUBL(dst, a, sec, usec);
101 }
102
103 /* --- @tv_cmp@ --- *
104  *
105  * Arguments:   @const struct timeval *a, *b@ = source blocks
106  *
107  * Returns:     Less than, equal to, or greater than zero.
108  *
109  * Use:         Compares two timevals.
110  */
111
112 int tv_cmp(const struct timeval *a, const struct timeval *b)
113 {
114   /* --- This is more awkward than the case the macro deals with --- */
115
116   if (a->tv_sec > b->tv_sec)
117     return (1);
118   else if (a->tv_sec < b->tv_sec)
119     return (-1);
120   else if (a->tv_usec > b->tv_usec)
121     return (1);
122   else if (a->tv_usec < b->tv_usec)
123     return (-1);
124   else
125     return (0);
126 }
127
128 /*----- That's all, folks -------------------------------------------------*/