116dce513Schristos /* Basic struct timeval utilities.
2*e992f068Schristos Copyright (C) 2011-2022 Free Software Foundation, Inc.
316dce513Schristos
416dce513Schristos This file is part of the libiberty library.
516dce513Schristos Libiberty is free software; you can redistribute it and/or
616dce513Schristos modify it under the terms of the GNU Library General Public
716dce513Schristos License as published by the Free Software Foundation; either
816dce513Schristos version 2 of the License, or (at your option) any later version.
916dce513Schristos
1016dce513Schristos Libiberty is distributed in the hope that it will be useful,
1116dce513Schristos but WITHOUT ANY WARRANTY; without even the implied warranty of
1216dce513Schristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1316dce513Schristos Library General Public License for more details.
1416dce513Schristos
1516dce513Schristos You should have received a copy of the GNU Library General Public
1616dce513Schristos License along with libiberty; see the file COPYING.LIB. If not,
1716dce513Schristos write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
1816dce513Schristos Boston, MA 02110-1301, USA. */
1916dce513Schristos
2016dce513Schristos #include "config.h"
2116dce513Schristos
2216dce513Schristos /* On some systems (such as WindISS), you must include <sys/types.h>
2316dce513Schristos to get the definition of "time_t" before you include <time.h>. */
2416dce513Schristos #include <sys/types.h>
2516dce513Schristos
2616dce513Schristos #ifdef TIME_WITH_SYS_TIME
2716dce513Schristos # include <sys/time.h>
2816dce513Schristos # include <time.h>
2916dce513Schristos #else
3016dce513Schristos # if HAVE_SYS_TIME_H
3116dce513Schristos # include <sys/time.h>
3216dce513Schristos # else
3316dce513Schristos # ifdef HAVE_TIME_H
3416dce513Schristos # include <time.h>
3516dce513Schristos # endif
3616dce513Schristos # endif
3716dce513Schristos #endif
3816dce513Schristos
3916dce513Schristos #include "timeval-utils.h"
4016dce513Schristos
4116dce513Schristos /*
4216dce513Schristos
4316dce513Schristos @deftypefn Extension void timeval_add (struct timeval *@var{a}, @
4416dce513Schristos struct timeval *@var{b}, struct timeval *@var{result})
4516dce513Schristos
4616dce513Schristos Adds @var{a} to @var{b} and stores the result in @var{result}.
4716dce513Schristos
4816dce513Schristos @end deftypefn
4916dce513Schristos
5016dce513Schristos */
5116dce513Schristos
5216dce513Schristos void
timeval_add(struct timeval * result,const struct timeval * a,const struct timeval * b)5316dce513Schristos timeval_add (struct timeval *result,
5416dce513Schristos const struct timeval *a, const struct timeval *b)
5516dce513Schristos {
5616dce513Schristos result->tv_sec = a->tv_sec + b->tv_sec;
5716dce513Schristos result->tv_usec = a->tv_usec + b->tv_usec;
5816dce513Schristos if (result->tv_usec >= 1000000)
5916dce513Schristos {
6016dce513Schristos ++result->tv_sec;
6116dce513Schristos result->tv_usec -= 1000000;
6216dce513Schristos }
6316dce513Schristos }
6416dce513Schristos
6516dce513Schristos /*
6616dce513Schristos
6716dce513Schristos @deftypefn Extension void timeval_sub (struct timeval *@var{a}, @
6816dce513Schristos struct timeval *@var{b}, struct timeval *@var{result})
6916dce513Schristos
7016dce513Schristos Subtracts @var{b} from @var{a} and stores the result in @var{result}.
7116dce513Schristos
7216dce513Schristos @end deftypefn
7316dce513Schristos
7416dce513Schristos */
7516dce513Schristos
7616dce513Schristos void
timeval_sub(struct timeval * result,const struct timeval * a,const struct timeval * b)7716dce513Schristos timeval_sub (struct timeval *result,
7816dce513Schristos const struct timeval *a, const struct timeval *b)
7916dce513Schristos {
8016dce513Schristos result->tv_sec = a->tv_sec - b->tv_sec;
8116dce513Schristos result->tv_usec = a->tv_usec - b->tv_usec;
8216dce513Schristos if (result->tv_usec < 0)
8316dce513Schristos {
8416dce513Schristos --result->tv_sec;
8516dce513Schristos result->tv_usec += 1000000;
8616dce513Schristos }
8716dce513Schristos }
88