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