Blame lib/timespec.h

Packit Service c30d13
/* timespec -- System time interface
Packit Service c30d13
Packit Service c30d13
   Copyright (C) 2000, 2002, 2004-2005, 2007, 2009-2018 Free Software
Packit Service c30d13
   Foundation, Inc.
Packit Service c30d13
Packit Service c30d13
   This program is free software: you can redistribute it and/or modify
Packit Service c30d13
   it under the terms of the GNU General Public License as published by
Packit Service c30d13
   the Free Software Foundation; either version 3 of the License, or
Packit Service c30d13
   (at your option) any later version.
Packit Service c30d13
Packit Service c30d13
   This program is distributed in the hope that it will be useful,
Packit Service c30d13
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service c30d13
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit Service c30d13
   GNU General Public License for more details.
Packit Service c30d13
Packit Service c30d13
   You should have received a copy of the GNU General Public License
Packit Service c30d13
   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
Packit Service c30d13
Packit Service c30d13
#if ! defined TIMESPEC_H
Packit Service c30d13
# define TIMESPEC_H
Packit Service c30d13
Packit Service c30d13
# include <time.h>
Packit Service c30d13
Packit Service c30d13
#ifndef _GL_INLINE_HEADER_BEGIN
Packit Service c30d13
 #error "Please include config.h first."
Packit Service c30d13
#endif
Packit Service c30d13
_GL_INLINE_HEADER_BEGIN
Packit Service c30d13
#ifndef _GL_TIMESPEC_INLINE
Packit Service c30d13
# define _GL_TIMESPEC_INLINE _GL_INLINE
Packit Service c30d13
#endif
Packit Service c30d13
Packit Service c30d13
#ifdef __cplusplus
Packit Service c30d13
extern "C" {
Packit Service c30d13
#endif
Packit Service c30d13
Packit Service c30d13
#include "verify.h"
Packit Service c30d13
Packit Service c30d13
/* Resolution of timespec timestamps (in units per second), and log
Packit Service c30d13
   base 10 of the resolution.  */
Packit Service c30d13
Packit Service c30d13
enum { TIMESPEC_RESOLUTION = 1000000000 };
Packit Service c30d13
enum { LOG10_TIMESPEC_RESOLUTION = 9 };
Packit Service c30d13
Packit Service c30d13
/* Return a timespec with seconds S and nanoseconds NS.  */
Packit Service c30d13
Packit Service c30d13
_GL_TIMESPEC_INLINE struct timespec
Packit Service c30d13
make_timespec (time_t s, long int ns)
Packit Service c30d13
{
Packit Service c30d13
  struct timespec r;
Packit Service c30d13
  r.tv_sec = s;
Packit Service c30d13
  r.tv_nsec = ns;
Packit Service c30d13
  return r;
Packit Service c30d13
}
Packit Service c30d13
Packit Service c30d13
/* Return negative, zero, positive if A < B, A == B, A > B, respectively.
Packit Service c30d13
Packit Service c30d13
   For each timestamp T, this code assumes that either:
Packit Service c30d13
Packit Service c30d13
     * T.tv_nsec is in the range 0..999999999; or
Packit Service c30d13
     * T.tv_sec corresponds to a valid leap second on a host that supports
Packit Service c30d13
       leap seconds, and T.tv_nsec is in the range 1000000000..1999999999; or
Packit Service c30d13
     * T.tv_sec is the minimum time_t value and T.tv_nsec is -1; or
Packit Service c30d13
       T.tv_sec is the maximum time_t value and T.tv_nsec is 2000000000.
Packit Service c30d13
       This allows for special struct timespec values that are less or
Packit Service c30d13
       greater than all possible valid timestamps.
Packit Service c30d13
Packit Service c30d13
   In all these cases, it is safe to subtract two tv_nsec values and
Packit Service c30d13
   convert the result to integer without worrying about overflow on
Packit Service c30d13
   any platform of interest to the GNU project, since all such
Packit Service c30d13
   platforms have 32-bit int or wider.
Packit Service c30d13
Packit Service c30d13
   Replacing "a.tv_nsec - b.tv_nsec" with something like
Packit Service c30d13
   "a.tv_nsec < b.tv_nsec ? -1 : a.tv_nsec > b.tv_nsec" would cause
Packit Service c30d13
   this function to work in some cases where the above assumption is
Packit Service c30d13
   violated, but not in all cases (e.g., a.tv_sec==1, a.tv_nsec==-2,
Packit Service c30d13
   b.tv_sec==0, b.tv_nsec==999999999) and is arguably not worth the
Packit Service c30d13
   extra instructions.  Using a subtraction has the advantage of
Packit Service c30d13
   detecting some invalid cases on platforms that detect integer
Packit Service c30d13
   overflow.  */
Packit Service c30d13
Packit Service c30d13
_GL_TIMESPEC_INLINE int _GL_ATTRIBUTE_PURE
Packit Service c30d13
timespec_cmp (struct timespec a, struct timespec b)
Packit Service c30d13
{
Packit Service c30d13
  if (a.tv_sec < b.tv_sec)
Packit Service c30d13
    return -1;
Packit Service c30d13
  if (a.tv_sec > b.tv_sec)
Packit Service c30d13
    return 1;
Packit Service c30d13
Packit Service c30d13
  /* Pacify gcc -Wstrict-overflow (bleeding-edge circa 2017-10-02).  See:
Packit Service c30d13
     https://lists.gnu.org/r/bug-gnulib/2017-10/msg00006.html  */
Packit Service c30d13
  assume (-1 <= a.tv_nsec && a.tv_nsec <= 2 * TIMESPEC_RESOLUTION);
Packit Service c30d13
  assume (-1 <= b.tv_nsec && b.tv_nsec <= 2 * TIMESPEC_RESOLUTION);
Packit Service c30d13
Packit Service c30d13
  return a.tv_nsec - b.tv_nsec;
Packit Service c30d13
}
Packit Service c30d13
Packit Service c30d13
/* Return -1, 0, 1, depending on the sign of A.  A.tv_nsec must be
Packit Service c30d13
   nonnegative.  */
Packit Service c30d13
_GL_TIMESPEC_INLINE int _GL_ATTRIBUTE_PURE
Packit Service c30d13
timespec_sign (struct timespec a)
Packit Service c30d13
{
Packit Service c30d13
  return a.tv_sec < 0 ? -1 : a.tv_sec || a.tv_nsec;
Packit Service c30d13
}
Packit Service c30d13
Packit Service c30d13
struct timespec timespec_add (struct timespec, struct timespec)
Packit Service c30d13
  _GL_ATTRIBUTE_CONST;
Packit Service c30d13
struct timespec timespec_sub (struct timespec, struct timespec)
Packit Service c30d13
  _GL_ATTRIBUTE_CONST;
Packit Service c30d13
struct timespec dtotimespec (double)
Packit Service c30d13
  _GL_ATTRIBUTE_CONST;
Packit Service c30d13
Packit Service c30d13
/* Return an approximation to A, of type 'double'.  */
Packit Service c30d13
_GL_TIMESPEC_INLINE double
Packit Service c30d13
timespectod (struct timespec a)
Packit Service c30d13
{
Packit Service c30d13
  return a.tv_sec + a.tv_nsec / 1e9;
Packit Service c30d13
}
Packit Service c30d13
Packit Service c30d13
void gettime (struct timespec *);
Packit Service c30d13
int settime (struct timespec const *);
Packit Service c30d13
Packit Service c30d13
#ifdef __cplusplus
Packit Service c30d13
}
Packit Service c30d13
#endif
Packit Service c30d13
Packit Service c30d13
_GL_INLINE_HEADER_END
Packit Service c30d13
Packit Service c30d13
#endif