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