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