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