2012-06-22 21:26:37 +00:00
|
|
|
/* Subtract two struct timespec values.
|
|
|
|
|
2021-01-01 09:13:56 +00:00
|
|
|
Copyright (C) 2011-2021 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;
|
|
|
|
|
|
|
|
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;
|
2019-11-06 06:59:00 +00:00
|
|
|
time_t bs1;
|
|
|
|
if (!INT_ADD_WRAPV (bs, 1, &bs1))
|
|
|
|
bs = bs1;
|
2015-11-08 07:36:45 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-11-06 06:59:00 +00:00
|
|
|
if (INT_SUBTRACT_WRAPV (rs, bs, &rs))
|
2012-06-22 21:26:37 +00:00
|
|
|
{
|
2019-11-06 06:59:00 +00:00
|
|
|
if (0 < bs)
|
2012-06-22 21:26:37 +00:00
|
|
|
{
|
|
|
|
low_overflow:
|
2019-11-06 06:59:00 +00:00
|
|
|
rs = TYPE_MINIMUM (time_t);
|
2012-06-22 21:26:37 +00:00
|
|
|
rns = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-11-06 06:59:00 +00:00
|
|
|
rs = TYPE_MAXIMUM (time_t);
|
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
|
|
|
}
|