1*e4b17023SJohn Marino /* Basic struct timeval utilities.
2*e4b17023SJohn Marino Copyright (C) 2011 Free Software Foundation, Inc.
3*e4b17023SJohn Marino
4*e4b17023SJohn Marino This file is part of the libiberty library.
5*e4b17023SJohn Marino Libiberty is free software; you can redistribute it and/or
6*e4b17023SJohn Marino modify it under the terms of the GNU Library General Public
7*e4b17023SJohn Marino License as published by the Free Software Foundation; either
8*e4b17023SJohn Marino version 2 of the License, or (at your option) any later version.
9*e4b17023SJohn Marino
10*e4b17023SJohn Marino Libiberty is distributed in the hope that it will be useful,
11*e4b17023SJohn Marino but WITHOUT ANY WARRANTY; without even the implied warranty of
12*e4b17023SJohn Marino MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13*e4b17023SJohn Marino Library General Public License for more details.
14*e4b17023SJohn Marino
15*e4b17023SJohn Marino You should have received a copy of the GNU Library General Public
16*e4b17023SJohn Marino License along with libiberty; see the file COPYING.LIB. If not,
17*e4b17023SJohn Marino write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
18*e4b17023SJohn Marino Boston, MA 02110-1301, USA. */
19*e4b17023SJohn Marino
20*e4b17023SJohn Marino #include "config.h"
21*e4b17023SJohn Marino
22*e4b17023SJohn Marino /* On some systems (such as WindISS), you must include <sys/types.h>
23*e4b17023SJohn Marino to get the definition of "time_t" before you include <time.h>. */
24*e4b17023SJohn Marino #include <sys/types.h>
25*e4b17023SJohn Marino
26*e4b17023SJohn Marino #ifdef TIME_WITH_SYS_TIME
27*e4b17023SJohn Marino # include <sys/time.h>
28*e4b17023SJohn Marino # include <time.h>
29*e4b17023SJohn Marino #else
30*e4b17023SJohn Marino # if HAVE_SYS_TIME_H
31*e4b17023SJohn Marino # include <sys/time.h>
32*e4b17023SJohn Marino # else
33*e4b17023SJohn Marino # ifdef HAVE_TIME_H
34*e4b17023SJohn Marino # include <time.h>
35*e4b17023SJohn Marino # endif
36*e4b17023SJohn Marino # endif
37*e4b17023SJohn Marino #endif
38*e4b17023SJohn Marino
39*e4b17023SJohn Marino #include "timeval-utils.h"
40*e4b17023SJohn Marino
41*e4b17023SJohn Marino /*
42*e4b17023SJohn Marino
43*e4b17023SJohn Marino @deftypefn Extension void timeval_add (struct timeval *@var{a}, @
44*e4b17023SJohn Marino struct timeval *@var{b}, struct timeval *@var{result})
45*e4b17023SJohn Marino
46*e4b17023SJohn Marino Adds @var{a} to @var{b} and stores the result in @var{result}.
47*e4b17023SJohn Marino
48*e4b17023SJohn Marino @end deftypefn
49*e4b17023SJohn Marino
50*e4b17023SJohn Marino */
51*e4b17023SJohn Marino
52*e4b17023SJohn Marino void
timeval_add(struct timeval * result,const struct timeval * a,const struct timeval * b)53*e4b17023SJohn Marino timeval_add (struct timeval *result,
54*e4b17023SJohn Marino const struct timeval *a, const struct timeval *b)
55*e4b17023SJohn Marino {
56*e4b17023SJohn Marino result->tv_sec = a->tv_sec + b->tv_sec;
57*e4b17023SJohn Marino result->tv_usec = a->tv_usec + b->tv_usec;
58*e4b17023SJohn Marino if (result->tv_usec >= 1000000)
59*e4b17023SJohn Marino {
60*e4b17023SJohn Marino ++result->tv_sec;
61*e4b17023SJohn Marino result->tv_usec -= 1000000;
62*e4b17023SJohn Marino }
63*e4b17023SJohn Marino }
64*e4b17023SJohn Marino
65*e4b17023SJohn Marino /*
66*e4b17023SJohn Marino
67*e4b17023SJohn Marino @deftypefn Extension void timeval_sub (struct timeval *@var{a}, @
68*e4b17023SJohn Marino struct timeval *@var{b}, struct timeval *@var{result})
69*e4b17023SJohn Marino
70*e4b17023SJohn Marino Subtracts @var{b} from @var{a} and stores the result in @var{result}.
71*e4b17023SJohn Marino
72*e4b17023SJohn Marino @end deftypefn
73*e4b17023SJohn Marino
74*e4b17023SJohn Marino */
75*e4b17023SJohn Marino
76*e4b17023SJohn Marino void
timeval_sub(struct timeval * result,const struct timeval * a,const struct timeval * b)77*e4b17023SJohn Marino timeval_sub (struct timeval *result,
78*e4b17023SJohn Marino const struct timeval *a, const struct timeval *b)
79*e4b17023SJohn Marino {
80*e4b17023SJohn Marino result->tv_sec = a->tv_sec - b->tv_sec;
81*e4b17023SJohn Marino result->tv_usec = a->tv_usec - b->tv_usec;
82*e4b17023SJohn Marino if (result->tv_usec < 0)
83*e4b17023SJohn Marino {
84*e4b17023SJohn Marino --result->tv_sec;
85*e4b17023SJohn Marino result->tv_usec += 1000000;
86*e4b17023SJohn Marino }
87*e4b17023SJohn Marino }
88