Blame gnulib-tests/timespec-sub.c

Packit Service fdd496
/* Subtract two struct timespec values.
Packit Service fdd496
Packit Service fdd496
   Copyright (C) 2011-2017 Free Software Foundation, Inc.
Packit Service fdd496
Packit Service fdd496
   This program is free software: you can redistribute it and/or modify
Packit Service fdd496
   it under the terms of the GNU General Public License as published by
Packit Service fdd496
   the Free Software Foundation; either version 3 of the License, or
Packit Service fdd496
   (at your option) any later version.
Packit Service fdd496
Packit Service fdd496
   This program is distributed in the hope that it will be useful,
Packit Service fdd496
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service fdd496
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit Service fdd496
   GNU General Public License for more details.
Packit Service fdd496
Packit Service fdd496
   You should have received a copy of the GNU General Public License
Packit Service fdd496
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
Packit Service fdd496
Packit Service fdd496
/* Written by Paul Eggert.  */
Packit Service fdd496
Packit Service fdd496
/* Return the difference between two timespec values A and B.  On
Packit Service fdd496
   overflow, return an extremal value.  This assumes 0 <= tv_nsec <
Packit Service fdd496
   TIMESPEC_RESOLUTION.  */
Packit Service fdd496
Packit Service fdd496
#include <config.h>
Packit Service fdd496
#include "timespec.h"
Packit Service fdd496
Packit Service fdd496
#include "intprops.h"
Packit Service fdd496
Packit Service fdd496
struct timespec
Packit Service fdd496
timespec_sub (struct timespec a, struct timespec b)
Packit Service fdd496
{
Packit Service fdd496
  time_t rs = a.tv_sec;
Packit Service fdd496
  time_t bs = b.tv_sec;
Packit Service fdd496
  int ns = a.tv_nsec - b.tv_nsec;
Packit Service fdd496
  int rns = ns;
Packit Service fdd496
  time_t tmin = TYPE_MINIMUM (time_t);
Packit Service fdd496
  time_t tmax = TYPE_MAXIMUM (time_t);
Packit Service fdd496
Packit Service fdd496
  if (ns < 0)
Packit Service fdd496
    {
Packit Service fdd496
      rns = ns + TIMESPEC_RESOLUTION;
Packit Service fdd496
      if (bs < tmax)
Packit Service fdd496
        bs++;
Packit Service fdd496
      else if (- TYPE_SIGNED (time_t) < rs)
Packit Service fdd496
        rs--;
Packit Service fdd496
      else
Packit Service fdd496
        goto low_overflow;
Packit Service fdd496
    }
Packit Service fdd496
Packit Service fdd496
  /* INT_SUBTRACT_WRAPV is not appropriate since time_t might be unsigned.
Packit Service fdd496
     In theory time_t might be narrower than int, so plain
Packit Service fdd496
     INT_SUBTRACT_OVERFLOW does not suffice.  */
Packit Service fdd496
  if (! INT_SUBTRACT_OVERFLOW (rs, bs) && tmin <= rs - bs && rs - bs <= tmax)
Packit Service fdd496
    rs -= bs;
Packit Service fdd496
  else
Packit Service fdd496
    {
Packit Service fdd496
      if (rs < 0)
Packit Service fdd496
        {
Packit Service fdd496
        low_overflow:
Packit Service fdd496
          rs = tmin;
Packit Service fdd496
          rns = 0;
Packit Service fdd496
        }
Packit Service fdd496
      else
Packit Service fdd496
        {
Packit Service fdd496
          rs = tmax;
Packit Service fdd496
          rns = TIMESPEC_RESOLUTION - 1;
Packit Service fdd496
        }
Packit Service fdd496
    }
Packit Service fdd496
Packit Service fdd496
  return make_timespec (rs, rns);
Packit Service fdd496
}