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