2012-06-22 21:26:37 +00:00
|
|
|
/* Subtract two struct timespec values.
|
|
|
|
|
2019-01-01 00:59:58 +00:00
|
|
|
Copyright (C) 2011-2019 Free Software Foundation, Inc.
|
2012-06-22 21:26:37 +00:00
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
2017-09-13 09:07:03 +00:00
|
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>. */
|
2012-06-22 21:26:37 +00:00
|
|
|
|
|
|
|
/* Written by Paul Eggert. */
|
|
|
|
|
|
|
|
/* Return the difference between two timespec values A and B. On
|
2013-09-25 03:44:34 +00:00
|
|
|
overflow, return an extremal value. This assumes 0 <= tv_nsec <
|
Update from Gnulib
This incorporates:
2018-09-10 timespec: fix resolution confusion
2018-09-09 mktime: simplify in prep for glibc merge
2018-09-07 intprops: minor clarification of code
2018-09-06 stddef: Override max_align_t on NetBSD 8.0/x86
2018-09-06 fcntl: Fix F_DUPFD_CLOEXEC behaviour on Haiku
2018-09-06 strtoll, strtoull: Rely on limits-h module
2018-09-06 limits-h: Provide numerical limits macros
2018-09-06 fcntl: Don't access nonexistent optional argument
2018-09-02 mktime: fix unlikely race+overflow bug
2018-08-31 mktime, timegm: simplify glibc time64_t
2018-08-31 mktime, timegm: simplify merge to glibc
* build-aux/config.guess, build-aux/config.sub:
* lib/dtotimespec.c, lib/fcntl.c, lib/intprops.h:
* lib/limits.in.h, lib/mktime-internal.h, lib/mktime.c:
* lib/stat-time.h, lib/strtol.c, lib/timegm.c:
* lib/timespec-add.c, lib/timespec-sub.c, lib/timespec.h:
* lib/utimens.c, m4/limits-h.m4, m4/stddef_h.m4:
Copy from Gnulib.
2018-09-11 04:16:03 +00:00
|
|
|
TIMESPEC_HZ. */
|
2012-06-22 21:26:37 +00:00
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
#include "timespec.h"
|
|
|
|
|
|
|
|
#include "intprops.h"
|
|
|
|
|
|
|
|
struct timespec
|
|
|
|
timespec_sub (struct timespec a, struct timespec b)
|
|
|
|
{
|
|
|
|
time_t rs = a.tv_sec;
|
|
|
|
time_t bs = b.tv_sec;
|
|
|
|
int ns = a.tv_nsec - b.tv_nsec;
|
|
|
|
int rns = ns;
|
2015-11-08 07:36:45 +00:00
|
|
|
time_t tmin = TYPE_MINIMUM (time_t);
|
|
|
|
time_t tmax = TYPE_MAXIMUM (time_t);
|
2012-06-22 21:26:37 +00:00
|
|
|
|
|
|
|
if (ns < 0)
|
|
|
|
{
|
Update from Gnulib
This incorporates:
2018-09-10 timespec: fix resolution confusion
2018-09-09 mktime: simplify in prep for glibc merge
2018-09-07 intprops: minor clarification of code
2018-09-06 stddef: Override max_align_t on NetBSD 8.0/x86
2018-09-06 fcntl: Fix F_DUPFD_CLOEXEC behaviour on Haiku
2018-09-06 strtoll, strtoull: Rely on limits-h module
2018-09-06 limits-h: Provide numerical limits macros
2018-09-06 fcntl: Don't access nonexistent optional argument
2018-09-02 mktime: fix unlikely race+overflow bug
2018-08-31 mktime, timegm: simplify glibc time64_t
2018-08-31 mktime, timegm: simplify merge to glibc
* build-aux/config.guess, build-aux/config.sub:
* lib/dtotimespec.c, lib/fcntl.c, lib/intprops.h:
* lib/limits.in.h, lib/mktime-internal.h, lib/mktime.c:
* lib/stat-time.h, lib/strtol.c, lib/timegm.c:
* lib/timespec-add.c, lib/timespec-sub.c, lib/timespec.h:
* lib/utimens.c, m4/limits-h.m4, m4/stddef_h.m4:
Copy from Gnulib.
2018-09-11 04:16:03 +00:00
|
|
|
rns = ns + TIMESPEC_HZ;
|
2015-11-08 07:36:45 +00:00
|
|
|
if (bs < tmax)
|
|
|
|
bs++;
|
|
|
|
else if (- TYPE_SIGNED (time_t) < rs)
|
2012-06-22 21:26:37 +00:00
|
|
|
rs--;
|
2015-11-08 07:36:45 +00:00
|
|
|
else
|
|
|
|
goto low_overflow;
|
2012-06-22 21:26:37 +00:00
|
|
|
}
|
|
|
|
|
2015-11-08 07:36:45 +00:00
|
|
|
/* INT_SUBTRACT_WRAPV is not appropriate since time_t might be unsigned.
|
|
|
|
In theory time_t might be narrower than int, so plain
|
|
|
|
INT_SUBTRACT_OVERFLOW does not suffice. */
|
|
|
|
if (! INT_SUBTRACT_OVERFLOW (rs, bs) && tmin <= rs - bs && rs - bs <= tmax)
|
|
|
|
rs -= bs;
|
|
|
|
else
|
2012-06-22 21:26:37 +00:00
|
|
|
{
|
|
|
|
if (rs < 0)
|
|
|
|
{
|
|
|
|
low_overflow:
|
2015-11-08 07:36:45 +00:00
|
|
|
rs = tmin;
|
2012-06-22 21:26:37 +00:00
|
|
|
rns = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-11-08 07:36:45 +00:00
|
|
|
rs = tmax;
|
Update from Gnulib
This incorporates:
2018-09-10 timespec: fix resolution confusion
2018-09-09 mktime: simplify in prep for glibc merge
2018-09-07 intprops: minor clarification of code
2018-09-06 stddef: Override max_align_t on NetBSD 8.0/x86
2018-09-06 fcntl: Fix F_DUPFD_CLOEXEC behaviour on Haiku
2018-09-06 strtoll, strtoull: Rely on limits-h module
2018-09-06 limits-h: Provide numerical limits macros
2018-09-06 fcntl: Don't access nonexistent optional argument
2018-09-02 mktime: fix unlikely race+overflow bug
2018-08-31 mktime, timegm: simplify glibc time64_t
2018-08-31 mktime, timegm: simplify merge to glibc
* build-aux/config.guess, build-aux/config.sub:
* lib/dtotimespec.c, lib/fcntl.c, lib/intprops.h:
* lib/limits.in.h, lib/mktime-internal.h, lib/mktime.c:
* lib/stat-time.h, lib/strtol.c, lib/timegm.c:
* lib/timespec-add.c, lib/timespec-sub.c, lib/timespec.h:
* lib/utimens.c, m4/limits-h.m4, m4/stddef_h.m4:
Copy from Gnulib.
2018-09-11 04:16:03 +00:00
|
|
|
rns = TIMESPEC_HZ - 1;
|
2012-06-22 21:26:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-19 21:40:08 +00:00
|
|
|
return make_timespec (rs, rns);
|
2012-06-22 21:26:37 +00:00
|
|
|
}
|