xref: /netbsd-src/external/gpl3/binutils/dist/libiberty/timeval-utils.c (revision cb63e24e8d6aae7ddac1859a9015f48b1d8bd90e)
1883529b6Schristos /* Basic struct timeval utilities.
2*cb63e24eSchristos    Copyright (C) 2011-2024 Free Software Foundation, Inc.
3883529b6Schristos 
4883529b6Schristos This file is part of the libiberty library.
5883529b6Schristos Libiberty is free software; you can redistribute it and/or
6883529b6Schristos modify it under the terms of the GNU Library General Public
7883529b6Schristos License as published by the Free Software Foundation; either
8883529b6Schristos version 2 of the License, or (at your option) any later version.
9883529b6Schristos 
10883529b6Schristos Libiberty is distributed in the hope that it will be useful,
11883529b6Schristos but WITHOUT ANY WARRANTY; without even the implied warranty of
12883529b6Schristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13883529b6Schristos Library General Public License for more details.
14883529b6Schristos 
15883529b6Schristos You should have received a copy of the GNU Library General Public
16883529b6Schristos License along with libiberty; see the file COPYING.LIB.  If not,
17883529b6Schristos write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
18883529b6Schristos Boston, MA 02110-1301, USA.  */
19883529b6Schristos 
20883529b6Schristos #include "config.h"
21883529b6Schristos 
22883529b6Schristos /* On some systems (such as WindISS), you must include <sys/types.h>
23883529b6Schristos    to get the definition of "time_t" before you include <time.h>.  */
24883529b6Schristos #include <sys/types.h>
25883529b6Schristos 
26883529b6Schristos #ifdef TIME_WITH_SYS_TIME
27883529b6Schristos # include <sys/time.h>
28883529b6Schristos # include <time.h>
29883529b6Schristos #else
30883529b6Schristos # if HAVE_SYS_TIME_H
31883529b6Schristos #  include <sys/time.h>
32883529b6Schristos # else
33883529b6Schristos #  ifdef HAVE_TIME_H
34883529b6Schristos #   include <time.h>
35883529b6Schristos #  endif
36883529b6Schristos # endif
37883529b6Schristos #endif
38883529b6Schristos 
39883529b6Schristos #include "timeval-utils.h"
40883529b6Schristos 
41883529b6Schristos /*
42883529b6Schristos 
43883529b6Schristos @deftypefn Extension void timeval_add (struct timeval *@var{a}, @
44883529b6Schristos   struct timeval *@var{b}, struct timeval *@var{result})
45883529b6Schristos 
46883529b6Schristos Adds @var{a} to @var{b} and stores the result in @var{result}.
47883529b6Schristos 
48883529b6Schristos @end deftypefn
49883529b6Schristos 
50883529b6Schristos */
51883529b6Schristos 
52883529b6Schristos void
timeval_add(struct timeval * result,const struct timeval * a,const struct timeval * b)53883529b6Schristos timeval_add (struct timeval *result,
54883529b6Schristos 	     const struct timeval *a, const struct timeval *b)
55883529b6Schristos {
56883529b6Schristos   result->tv_sec = a->tv_sec + b->tv_sec;
57883529b6Schristos   result->tv_usec = a->tv_usec + b->tv_usec;
58883529b6Schristos   if (result->tv_usec >= 1000000)
59883529b6Schristos     {
60883529b6Schristos       ++result->tv_sec;
61883529b6Schristos       result->tv_usec -= 1000000;
62883529b6Schristos     }
63883529b6Schristos }
64883529b6Schristos 
65883529b6Schristos /*
66883529b6Schristos 
67883529b6Schristos @deftypefn Extension void timeval_sub (struct timeval *@var{a}, @
68883529b6Schristos   struct timeval *@var{b}, struct timeval *@var{result})
69883529b6Schristos 
70883529b6Schristos Subtracts @var{b} from @var{a} and stores the result in @var{result}.
71883529b6Schristos 
72883529b6Schristos @end deftypefn
73883529b6Schristos 
74883529b6Schristos */
75883529b6Schristos 
76883529b6Schristos void
timeval_sub(struct timeval * result,const struct timeval * a,const struct timeval * b)77883529b6Schristos timeval_sub (struct timeval *result,
78883529b6Schristos 	     const struct timeval *a, const struct timeval *b)
79883529b6Schristos {
80883529b6Schristos   result->tv_sec = a->tv_sec - b->tv_sec;
81883529b6Schristos   result->tv_usec = a->tv_usec - b->tv_usec;
82883529b6Schristos   if (result->tv_usec < 0)
83883529b6Schristos     {
84883529b6Schristos       --result->tv_sec;
85883529b6Schristos       result->tv_usec += 1000000;
86883529b6Schristos     }
87883529b6Schristos }
88