2012-06-22 21:26:37 +00:00
|
|
|
/* Add two struct timespec values.
|
|
|
|
|
2024-01-02 01:47:10 +00:00
|
|
|
Copyright (C) 2011-2024 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
|
2022-02-23 19:11:52 +00:00
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
2012-06-22 21:26:37 +00:00
|
|
|
(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 sum of two timespec values A and B. On overflow, return
|
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
|
|
|
an extremal value. This assumes 0 <= tv_nsec < TIMESPEC_HZ. */
|
2012-06-22 21:26:37 +00:00
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
#include "timespec.h"
|
|
|
|
|
2023-05-17 22:07:38 +00:00
|
|
|
#include <stdckdint.h>
|
2012-06-22 21:26:37 +00:00
|
|
|
#include "intprops.h"
|
|
|
|
|
|
|
|
struct timespec
|
|
|
|
timespec_add (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;
|
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
|
|
|
int nsd = ns - TIMESPEC_HZ;
|
2012-06-22 21:26:37 +00:00
|
|
|
int rns = ns;
|
|
|
|
|
|
|
|
if (0 <= nsd)
|
|
|
|
{
|
|
|
|
rns = nsd;
|
2019-11-06 06:59:00 +00:00
|
|
|
time_t bs1;
|
2023-05-17 22:07:38 +00:00
|
|
|
if (!ckd_add (&bs1, bs, 1))
|
2019-11-06 06:59:00 +00:00
|
|
|
bs = bs1;
|
2015-11-08 07:36:45 +00:00
|
|
|
else if (rs < 0)
|
2012-06-22 21:26:37 +00:00
|
|
|
rs++;
|
2015-11-08 07:36:45 +00:00
|
|
|
else
|
|
|
|
goto high_overflow;
|
2012-06-22 21:26:37 +00:00
|
|
|
}
|
|
|
|
|
2023-05-17 22:07:38 +00:00
|
|
|
if (ckd_add (&rs, rs, bs))
|
2012-06-22 21:26:37 +00:00
|
|
|
{
|
2019-11-06 06:59:00 +00:00
|
|
|
if (bs < 0)
|
2012-06-22 21:26:37 +00:00
|
|
|
{
|
2019-11-06 06:59:00 +00:00
|
|
|
rs = TYPE_MINIMUM (time_t);
|
2012-06-22 21:26:37 +00:00
|
|
|
rns = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
high_overflow:
|
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
|
|
|
}
|