105caefcfSchristos /* Basic struct timeval utilities.
2*f22f0ef4Schristos Copyright (C) 2011-2022 Free Software Foundation, Inc.
305caefcfSchristos
405caefcfSchristos This file is part of the libiberty library.
505caefcfSchristos Libiberty is free software; you can redistribute it and/or
605caefcfSchristos modify it under the terms of the GNU Library General Public
705caefcfSchristos License as published by the Free Software Foundation; either
805caefcfSchristos version 2 of the License, or (at your option) any later version.
905caefcfSchristos
1005caefcfSchristos Libiberty is distributed in the hope that it will be useful,
1105caefcfSchristos but WITHOUT ANY WARRANTY; without even the implied warranty of
1205caefcfSchristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1305caefcfSchristos Library General Public License for more details.
1405caefcfSchristos
1505caefcfSchristos You should have received a copy of the GNU Library General Public
1605caefcfSchristos License along with libiberty; see the file COPYING.LIB. If not,
1705caefcfSchristos write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
1805caefcfSchristos Boston, MA 02110-1301, USA. */
1905caefcfSchristos
2005caefcfSchristos #include "config.h"
2105caefcfSchristos
2205caefcfSchristos /* On some systems (such as WindISS), you must include <sys/types.h>
2305caefcfSchristos to get the definition of "time_t" before you include <time.h>. */
2405caefcfSchristos #include <sys/types.h>
2505caefcfSchristos
2605caefcfSchristos #ifdef TIME_WITH_SYS_TIME
2705caefcfSchristos # include <sys/time.h>
2805caefcfSchristos # include <time.h>
2905caefcfSchristos #else
3005caefcfSchristos # if HAVE_SYS_TIME_H
3105caefcfSchristos # include <sys/time.h>
3205caefcfSchristos # else
3305caefcfSchristos # ifdef HAVE_TIME_H
3405caefcfSchristos # include <time.h>
3505caefcfSchristos # endif
3605caefcfSchristos # endif
3705caefcfSchristos #endif
3805caefcfSchristos
3905caefcfSchristos #include "timeval-utils.h"
4005caefcfSchristos
4105caefcfSchristos /*
4205caefcfSchristos
4305caefcfSchristos @deftypefn Extension void timeval_add (struct timeval *@var{a}, @
4405caefcfSchristos struct timeval *@var{b}, struct timeval *@var{result})
4505caefcfSchristos
4605caefcfSchristos Adds @var{a} to @var{b} and stores the result in @var{result}.
4705caefcfSchristos
4805caefcfSchristos @end deftypefn
4905caefcfSchristos
5005caefcfSchristos */
5105caefcfSchristos
5205caefcfSchristos void
timeval_add(struct timeval * result,const struct timeval * a,const struct timeval * b)5305caefcfSchristos timeval_add (struct timeval *result,
5405caefcfSchristos const struct timeval *a, const struct timeval *b)
5505caefcfSchristos {
5605caefcfSchristos result->tv_sec = a->tv_sec + b->tv_sec;
5705caefcfSchristos result->tv_usec = a->tv_usec + b->tv_usec;
5805caefcfSchristos if (result->tv_usec >= 1000000)
5905caefcfSchristos {
6005caefcfSchristos ++result->tv_sec;
6105caefcfSchristos result->tv_usec -= 1000000;
6205caefcfSchristos }
6305caefcfSchristos }
6405caefcfSchristos
6505caefcfSchristos /*
6605caefcfSchristos
6705caefcfSchristos @deftypefn Extension void timeval_sub (struct timeval *@var{a}, @
6805caefcfSchristos struct timeval *@var{b}, struct timeval *@var{result})
6905caefcfSchristos
7005caefcfSchristos Subtracts @var{b} from @var{a} and stores the result in @var{result}.
7105caefcfSchristos
7205caefcfSchristos @end deftypefn
7305caefcfSchristos
7405caefcfSchristos */
7505caefcfSchristos
7605caefcfSchristos void
timeval_sub(struct timeval * result,const struct timeval * a,const struct timeval * b)7705caefcfSchristos timeval_sub (struct timeval *result,
7805caefcfSchristos const struct timeval *a, const struct timeval *b)
7905caefcfSchristos {
8005caefcfSchristos result->tv_sec = a->tv_sec - b->tv_sec;
8105caefcfSchristos result->tv_usec = a->tv_usec - b->tv_usec;
8205caefcfSchristos if (result->tv_usec < 0)
8305caefcfSchristos {
8405caefcfSchristos --result->tv_sec;
8505caefcfSchristos result->tv_usec += 1000000;
8605caefcfSchristos }
8705caefcfSchristos }
88