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