Update copyright dates with scripts/update-copyrights.
[glibc.git] / manual / examples / timeval_subtract.c
blob8bf8c4c432fb86812bd55f7985da5a37898ecd0c
1 /* struct timeval subtraction.
2 Copyright (C) 1991-2015 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, if not, see <http://www.gnu.org/licenses/>.
18 /* Subtract the `struct timeval' values X and Y,
19 storing the result in RESULT.
20 Return 1 if the difference is negative, otherwise 0. */
22 int
23 timeval_subtract (result, x, y)
24 struct timeval *result, *x, *y;
26 /* Perform the carry for the later subtraction by updating @var{y}. */
27 if (x->tv_usec < y->tv_usec) {
28 int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1;
29 y->tv_usec -= 1000000 * nsec;
30 y->tv_sec += nsec;
32 if (x->tv_usec - y->tv_usec > 1000000) {
33 int nsec = (x->tv_usec - y->tv_usec) / 1000000;
34 y->tv_usec += 1000000 * nsec;
35 y->tv_sec -= nsec;
38 /* Compute the time remaining to wait.
39 @code{tv_usec} is certainly positive. */
40 result->tv_sec = x->tv_sec - y->tv_sec;
41 result->tv_usec = x->tv_usec - y->tv_usec;
43 /* Return 1 if result is negative. */
44 return x->tv_sec < y->tv_sec;