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